Implement whitelist-flex related utils
This commit is contained in:
parent
856ae4e53f
commit
e7e66380e1
31
utils/csvToFlexList.ts
Normal file
31
utils/csvToFlexList.ts
Normal file
@ -0,0 +1,31 @@
|
||||
import type { WhitelistFlexMember } from 'components/WhitelistFlexUpload'
|
||||
|
||||
export const csvToFlexList = (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 whitelist-flex file.')
|
||||
}
|
||||
if (headers[0] !== 'address' || headers[1] !== 'mint_count') {
|
||||
throw new Error('Invalid whitelist-flex file. Headers must be "address" and "mint_count".')
|
||||
}
|
||||
|
||||
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 WhitelistFlexMember[]
|
||||
}
|
52
utils/isValidFlexListFile.ts
Normal file
52
utils/isValidFlexListFile.ts
Normal file
@ -0,0 +1,52 @@
|
||||
import type { WhitelistFlexMember } from 'components/WhitelistFlexUpload'
|
||||
import { toast } from 'react-hot-toast'
|
||||
|
||||
import { isValidAddress } from './isValidAddress'
|
||||
|
||||
export const isValidFlexListFile = (file: WhitelistFlexMember[]) => {
|
||||
let sumOfAmounts = 0
|
||||
file.forEach((allocation) => {
|
||||
sumOfAmounts += Number(allocation.mint_count)
|
||||
})
|
||||
if (sumOfAmounts > 10000) {
|
||||
toast.error(`Total mint count should be less than 10000 tokens (current count: ${sumOfAmounts}))`)
|
||||
return false
|
||||
}
|
||||
|
||||
const checks = file.map((account) => {
|
||||
// Check if address is valid bech32 address
|
||||
if (account.address.trim().startsWith('stars')) {
|
||||
if (!isValidAddress(account.address.trim())) {
|
||||
return { address: false }
|
||||
}
|
||||
}
|
||||
// Check if address start with stars
|
||||
if (!account.address.trim().startsWith('stars') && !account.address.trim().endsWith('.stars')) {
|
||||
return { address: false }
|
||||
}
|
||||
// Check if amount is valid
|
||||
if (!Number.isInteger(Number(account.mint_count)) || !(Number(account.mint_count) > 0)) {
|
||||
return { mint_count: false }
|
||||
}
|
||||
return null
|
||||
})
|
||||
|
||||
const isStargazeAddresses = file.every(
|
||||
(account) => account.address.trim().startsWith('stars') || account.address.trim().endsWith('.stars'),
|
||||
)
|
||||
if (!isStargazeAddresses) {
|
||||
toast.error('All accounts must be on the Stargaze 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?.mint_count === false).length > 0) {
|
||||
toast.error('Invalid mint count in file. Mint count must be a positive integer.')
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
Loading…
Reference in New Issue
Block a user