Multiple token airdrop to a single address (with shuffle)

This commit is contained in:
Serkan Reis
2022-10-10 12:37:20 +03:00
parent 0541c3f046
commit de18a319b8
4 changed files with 173 additions and 6 deletions
+31
View File
@@ -0,0 +1,31 @@
import type { AirdropAllocation } from './isValidAccountsFile'
export const csvToArray = (str: string, delimiter = ',') => {
let newline = '\n'
if (str.includes('\r')) newline = '\r'
if (str.includes('\r\n')) newline = '\r\n'
const headers = str.slice(0, str.indexOf(newline)).split(delimiter)
if (headers.length !== 2) {
throw new Error('Invalid accounts file')
}
if (headers[0] !== 'address' || headers[1] !== 'amount') {
throw new Error('Invalid accounts file')
}
const rows = str.slice(str.indexOf('\n') + 1).split(newline)
const arr = rows
.filter((row) => row !== '')
.map((row) => {
const values = row.split(delimiter)
const el = headers.reduce((object, header, index) => {
// @ts-expect-error assume object as Record<string, unknown>
object[header] = values[index]
return object
}, {})
return el
})
return arr as AirdropAllocation[]
}
+57
View File
@@ -0,0 +1,57 @@
import { toast } from 'react-hot-toast'
import { isValidAddress } from './isValidAddress'
export interface AirdropAllocation {
address: string
amount: string
}
export const isValidAccountsFile = (file: AirdropAllocation[]) => {
let sumOfAmounts = 0
file.forEach((allocation) => {
sumOfAmounts += Number(allocation.amount)
})
if (sumOfAmounts > 10000) {
toast.error(`Accounts file must have less than 10000 accounts`)
return false
}
const checks = file.map((account) => {
// Check if address is valid bech32 address
if (!isValidAddress(account.address)) {
return { address: false }
}
// Check if address start with stars
if (!account.address.startsWith('stars')) {
return { address: false }
}
// Check if amount is valid
if (!Number.isInteger(Number(account.amount)) || !(Number(account.amount) > 0)) {
return { amount: false }
}
return null
})
const isStargazeAddresses = file.every((account) => account.address.startsWith('stars'))
if (!isStargazeAddresses) {
toast.error('All accounts must be on the same network')
return false
}
if (checks.filter((check) => check?.address === false).length > 0) {
toast.error('Invalid address in file')
return false
}
if (checks.filter((check) => check?.amount === false).length > 0) {
toast.error('Invalid amount in file. Amount must be a positive integer.')
return false
}
// if (duplicateCheck.length > 0) {
// toast.error('The file contains duplicate addresses.')
// return false
// }
return true
}