chain_graphql/src/chain/DistributorReactor.ts
CounterFire2023 ded16ac57b project init
2023-06-25 11:11:49 +08:00

68 lines
2.1 KiB
TypeScript

import { Contract } from 'web3-eth-contract'
import Web3 from 'web3'
import { Account } from 'web3-core'
const abi = require('abis/NftDistributor.json').abi
export class DistributorReactor {
private web3: Web3
private contract: Contract
private account: Account
constructor({ web3, address }: { web3: Web3; address: string }) {
this.web3 = web3
this.account = this.web3.eth.accounts.wallet[0]
this.contract = new this.web3.eth.Contract(abi, address, { from: this.account.address })
}
/**
* 发布NFT列表
*/
async publishAirdropList({
address,
to,
nftList,
encodeABI = false,
}: {
address?: string
to: string
nftList: string[]
encodeABI?: boolean
}) {
const contract = address ? new this.web3.eth.Contract(abi, address, { from: this.account.address }) : this.contract
if (encodeABI) {
return contract.methods.addNFTData(to, nftList).encodeABI()
}
let gas = await contract.methods.addNFTData(to, nftList).estimateGas({ from: this.account.address })
let res = await contract.methods.addNFTData(to, nftList).send({ gas: (gas * 1.5) | 0 })
return res
}
/**
* mint nft to user
*/
async mintNft({
address,
to,
count,
encodeABI = false,
}: {
address?: string
to: string
count: number
encodeABI?: boolean
}) {
const contract = address ? new this.web3.eth.Contract(abi, address, { from: this.account.address }) : this.contract
const countStr = count + ''
if (encodeABI) {
return contract.methods.mintToUser(to, countStr).encodeABI()
}
let gas = await contract.methods.mintToUser(to, countStr).estimateGas({ from: this.account.address })
let res = await contract.methods.mintToUser(to, countStr).send({ gas: (gas * 1.5) | 0 })
return res
}
/**
* 查询用户可mint的数量
*/
async getMintableCount({ address, user }: { address?: string; user: string }) {
const contract = address ? new this.web3.eth.Contract(abi, address, { from: this.account.address }) : this.contract
return await contract.methods.getMintableCount(user).call()
}
}