重构:重构 ERC721Reactor.ts 和 RequestTask.ts 的属性和方法。

- 在 RequestTask.ts 中添加新的 `newRecord` 属性,默认值为;
- 通过删除不必要的参数和合并方法,重构 `ERC721Reactor.ts`;
- 在 `ERC721Reactor.ts` 的 `transfer` 和 `mint` 方法中增加 gas 限制。
This commit is contained in:
zhl 2023-04-06 19:49:08 +08:00
parent c98a1b0b2a
commit 0a6b7b69ff
2 changed files with 18 additions and 40 deletions

View File

@ -9,7 +9,7 @@ export const ERC721_INTERFACE_ID = '0x80ac58cd'
export const ERC721_METADATA_INTERFACE_ID = '0x5b5e139f'
export const ERC721_ENUMERABLE_INTERFACE_ID = '0x780e9d63'
const ablNft = require('abis/BEBadge.json').abi
const abiNft = require('abis/BEBadge.json').abi
export class ERC721Reactor {
private web3: Web3
@ -19,7 +19,7 @@ export class ERC721Reactor {
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(ablNft, address, { from: this.account.address })
this.contract = new this.web3.eth.Contract(abiNft, address, { from: this.account.address })
}
/**
@ -70,7 +70,7 @@ export class ERC721Reactor {
index: number
}): Promise<string> => {
const contract = address
? new this.web3.eth.Contract(ablNft, address, { from: this.account.address })
? new this.web3.eth.Contract(abiNft, address, { from: this.account.address })
: this.contract
return new Promise<string>((resolve, reject) => {
contract.methods.tokenOfOwnerByIndex(selectedAddress, index).call((error: Error, result: string) => {
@ -86,7 +86,7 @@ export class ERC721Reactor {
getBalance = async ({ address, selectedAddress }: { address?: string; selectedAddress: string }): Promise<number> => {
const contract = address
? new this.web3.eth.Contract(ablNft, address, { from: this.account.address })
? new this.web3.eth.Contract(abiNft, address, { from: this.account.address })
: this.contract
return new Promise<number>((resolve, reject) => {
contract.methods.balanceOf(selectedAddress).call((error: Error, result: number) => {
@ -109,7 +109,7 @@ export class ERC721Reactor {
*/
getTokenURI = async ({ address, tokenId }: { address?: string; tokenId: string }): Promise<string> => {
const contract = address
? new this.web3.eth.Contract(ablNft, address, { from: this.account.address })
? new this.web3.eth.Contract(abiNft, address, { from: this.account.address })
: this.contract
const supportsMetadata = await this.contractSupportsMetadataInterface(address)
if (!supportsMetadata) {
@ -135,7 +135,7 @@ export class ERC721Reactor {
*/
getAssetName = async (address?: string): Promise<string> => {
const contract = address
? new this.web3.eth.Contract(ablNft, address, { from: this.account.address })
? new this.web3.eth.Contract(abiNft, address, { from: this.account.address })
: this.contract
return new Promise<string>((resolve, reject) => {
contract.methods.name().call((error: Error, result: string) => {
@ -157,7 +157,7 @@ export class ERC721Reactor {
*/
getAssetSymbol = async (address?: string): Promise<string> => {
const contract = address
? new this.web3.eth.Contract(ablNft, address, { from: this.account.address })
? new this.web3.eth.Contract(abiNft, address, { from: this.account.address })
: this.contract
return new Promise<string>((resolve, reject) => {
contract.methods.symbol().call((error: Error, result: string) => {
@ -180,7 +180,7 @@ export class ERC721Reactor {
*/
async getOwnerOf({ address, tokenId }: { address?: string; tokenId: string }): Promise<string> {
const contract = address
? new this.web3.eth.Contract(ablNft, address, { from: this.account.address })
? new this.web3.eth.Contract(abiNft, address, { from: this.account.address })
: this.contract
return new Promise<string>((resolve, reject) => {
contract.methods.ownerOf(tokenId).call((error: Error, result: string) => {
@ -209,7 +209,7 @@ export class ERC721Reactor {
interfaceId: string
}): Promise<boolean> => {
const contract = address
? new this.web3.eth.Contract(ablNft, address, { from: this.account.address })
? new this.web3.eth.Contract(abiNft, address, { from: this.account.address })
: this.contract
return new Promise<boolean>((resolve, reject) => {
contract.methods.supportsInterface(interfaceId).call((error: Error, result: boolean) => {
@ -309,7 +309,7 @@ export class ERC721Reactor {
gas?: number
}) {
const contract = address
? new this.web3.eth.Contract(ablNft, address, { from: account || this.account.address })
? new this.web3.eth.Contract(abiNft, address, { from: account || this.account.address })
: this.contract
return contract.methods.safeTransferFrom(from, to, tokenId).send({
from,
@ -317,12 +317,12 @@ export class ERC721Reactor {
})
}
async mint({ address, to, tokenId, configId }: { address?: string; to: string; tokenId: string; configId: string }) {
async mint({ address, to, tokenId }: { address?: string; to: string; tokenId: string }) {
const contract = address
? new this.web3.eth.Contract(ablNft, address, { from: this.account.address })
? new this.web3.eth.Contract(abiNft, address, { from: this.account.address })
: this.contract
let gas = await contract.methods.mint(to, tokenId, configId).estimateGas({ gas: 1000000 })
return contract.methods.mint(to, tokenId, configId).send({ gas: (gas * 1.1) | 0 })
let gas = await contract.methods.mint(to, tokenId).estimateGas({ gas: 1000000 })
return contract.methods.mint(to, tokenId).send({ gas: (gas * 1.1) | 0 })
}
async batchMint({
@ -330,44 +330,22 @@ export class ERC721Reactor {
address,
to,
tokenIds,
configIds,
}: {
account?: string
address?: string
to: string
tokenIds: string[]
configIds: string[]
}) {
const contract = address
? new this.web3.eth.Contract(ablNft, address, { from: account || this.account.address })
? new this.web3.eth.Contract(abiNft, address, { from: account || this.account.address })
: this.contract
// let gas = await contract.methods.batchMint(to, tokenIds, configIds).estimateGas({ gas: 1000000 })
return contract.methods.batchMint(to, tokenIds, configIds).send({ gas: 1000000 })
}
async mintOne({
account,
address,
to,
tokenId,
nftType,
}: {
account?: string
address?: string
to: string
tokenId: string
nftType: string
}) {
const contract = address
? new this.web3.eth.Contract(ablNft, address, { from: account || this.account.address })
: this.contract
let gas = await contract.methods.mintBox(to, tokenId, nftType).estimateGas({ gas: 1000000 })
return contract.methods.mintBox(to, tokenId, nftType).send({ gas: 1000000 })
return contract.methods.batchMint(to, tokenIds).send({ gas: 1000000 })
}
async getPastEvents({ address, fromBlock }: { address?: string; fromBlock: number }) {
const contract = address
? new this.web3.eth.Contract(ablNft, address, { from: this.account.address })
? new this.web3.eth.Contract(abiNft, address, { from: this.account.address })
: this.contract
return contract.getPastEvents('BatchMint', {
fromBlock,

View File

@ -96,7 +96,7 @@ export class RequestTaskClass extends BaseModule {
result = await new BlockChain().erc721Reactor.transfer(self.reqData)
break
case TaskType.MINT_PRESALE_BOX:
result = await new BlockChain().erc721Reactor.mintOne(self.reqData)
result = await new BlockChain().erc721Reactor.mint(self.reqData)
break
}
logger.info(result)