2022-10-10 09:37:20 +00:00
|
|
|
import { toast } from 'react-hot-toast'
|
|
|
|
|
|
|
|
import { isValidAddress } from './isValidAddress'
|
|
|
|
|
|
|
|
export interface AirdropAllocation {
|
|
|
|
address: string
|
2023-06-08 07:08:47 +00:00
|
|
|
amount?: string
|
|
|
|
tokenId?: string
|
2022-10-10 09:37:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const isValidAccountsFile = (file: AirdropAllocation[]) => {
|
|
|
|
let sumOfAmounts = 0
|
|
|
|
file.forEach((allocation) => {
|
|
|
|
sumOfAmounts += Number(allocation.amount)
|
|
|
|
})
|
|
|
|
if (sumOfAmounts > 10000) {
|
2023-01-12 07:18:10 +00:00
|
|
|
toast.error(`Accounts file must cover less than 10000 tokens`)
|
2022-10-10 09:37:20 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
const checks = file.map((account) => {
|
|
|
|
// Check if address is valid bech32 address
|
2023-01-12 07:18:10 +00:00
|
|
|
if (account.address.trim().startsWith('stars')) {
|
|
|
|
if (!isValidAddress(account.address.trim())) {
|
|
|
|
return { address: false }
|
|
|
|
}
|
2022-10-10 09:37:20 +00:00
|
|
|
}
|
|
|
|
// Check if address start with stars
|
2023-01-12 07:18:10 +00:00
|
|
|
if (!account.address.trim().startsWith('stars') && !account.address.trim().endsWith('.stars')) {
|
2022-10-10 09:37:20 +00:00
|
|
|
return { address: false }
|
|
|
|
}
|
2023-06-08 07:08:47 +00:00
|
|
|
|
|
|
|
if (!account.amount && !account.tokenId) {
|
|
|
|
return { amount: false, tokenId: false }
|
|
|
|
}
|
|
|
|
|
2022-10-10 09:37:20 +00:00
|
|
|
// Check if amount is valid
|
2023-06-08 07:08:47 +00:00
|
|
|
if (account.amount && (!Number.isInteger(Number(account.amount)) || !(Number(account.amount) > 0))) {
|
2022-10-10 09:37:20 +00:00
|
|
|
return { amount: false }
|
|
|
|
}
|
2023-06-08 07:08:47 +00:00
|
|
|
// Check if tokenId is valid
|
|
|
|
if (account.tokenId && (!Number.isInteger(Number(account.tokenId)) || !(Number(account.tokenId) > 0))) {
|
|
|
|
return { tokenId: false }
|
|
|
|
}
|
2022-10-10 09:37:20 +00:00
|
|
|
return null
|
|
|
|
})
|
|
|
|
|
2023-01-12 07:18:10 +00:00
|
|
|
const isStargazeAddresses = file.every(
|
|
|
|
(account) => account.address.trim().startsWith('stars') || account.address.trim().endsWith('.stars'),
|
|
|
|
)
|
2022-10-10 09:37:20 +00:00
|
|
|
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
|
|
|
|
}
|
2023-06-08 07:08:47 +00:00
|
|
|
|
|
|
|
if (checks.filter((check) => check?.amount === false && check.tokenId === false).length > 0) {
|
|
|
|
toast.error('No amount or token ID found in the file. Please check the header.')
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-10-10 09:37:20 +00:00
|
|
|
if (checks.filter((check) => check?.amount === false).length > 0) {
|
|
|
|
toast.error('Invalid amount in file. Amount must be a positive integer.')
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-06-08 07:08:47 +00:00
|
|
|
if (checks.filter((check) => check?.tokenId === false).length > 0) {
|
|
|
|
toast.error('Invalid token ID in file. Token ID must be a positive integer.')
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-10-10 09:37:20 +00:00
|
|
|
// if (duplicateCheck.length > 0) {
|
|
|
|
// toast.error('The file contains duplicate addresses.')
|
|
|
|
// return false
|
|
|
|
// }
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|