Update .csv content validation

This commit is contained in:
Serkan Reis 2023-06-08 10:08:47 +03:00
parent 893b5b89c3
commit 9742ec6d04

View File

@ -4,13 +4,15 @@ import { isValidAddress } from './isValidAddress'
export interface AirdropAllocation {
address: string
amount: string
amount?: string
tokenId?: string
}
export const isValidAccountsFile = (file: AirdropAllocation[]) => {
let sumOfAmounts = 0
file.forEach((allocation) => {
sumOfAmounts += Number(allocation.amount)
console.log('sum: ', sumOfAmounts)
})
if (sumOfAmounts > 10000) {
toast.error(`Accounts file must cover less than 10000 tokens`)
@ -28,10 +30,19 @@ export const isValidAccountsFile = (file: AirdropAllocation[]) => {
if (!account.address.trim().startsWith('stars') && !account.address.trim().endsWith('.stars')) {
return { address: false }
}
if (!account.amount && !account.tokenId) {
return { amount: false, tokenId: false }
}
// Check if amount is valid
if (!Number.isInteger(Number(account.amount)) || !(Number(account.amount) > 0)) {
if (account.amount && (!Number.isInteger(Number(account.amount)) || !(Number(account.amount) > 0))) {
return { amount: false }
}
// Check if tokenId is valid
if (account.tokenId && (!Number.isInteger(Number(account.tokenId)) || !(Number(account.tokenId) > 0))) {
return { tokenId: false }
}
return null
})
@ -47,11 +58,22 @@ export const isValidAccountsFile = (file: AirdropAllocation[]) => {
toast.error('Invalid address in file')
return false
}
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
}
if (checks.filter((check) => check?.amount === false).length > 0) {
toast.error('Invalid amount in file. Amount must be a positive integer.')
return false
}
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
}
// if (duplicateCheck.length > 0) {
// toast.error('The file contains duplicate addresses.')
// return false