重构:重构 ERC721Reactor.ts 和 RequestTask.ts 的属性和方法。
- 在 RequestTask.ts 中添加新的 `newRecord` 属性,默认值为; - 通过删除不必要的参数和合并方法,重构 `ERC721Reactor.ts`; - 在 `ERC721Reactor.ts` 的 `transfer` 和 `mint` 方法中增加 gas 限制。
This commit is contained in:
parent
c98a1b0b2a
commit
0a6b7b69ff
@ -9,7 +9,7 @@ export const ERC721_INTERFACE_ID = '0x80ac58cd'
|
|||||||
export const ERC721_METADATA_INTERFACE_ID = '0x5b5e139f'
|
export const ERC721_METADATA_INTERFACE_ID = '0x5b5e139f'
|
||||||
export const ERC721_ENUMERABLE_INTERFACE_ID = '0x780e9d63'
|
export const ERC721_ENUMERABLE_INTERFACE_ID = '0x780e9d63'
|
||||||
|
|
||||||
const ablNft = require('abis/BEBadge.json').abi
|
const abiNft = require('abis/BEBadge.json').abi
|
||||||
|
|
||||||
export class ERC721Reactor {
|
export class ERC721Reactor {
|
||||||
private web3: Web3
|
private web3: Web3
|
||||||
@ -19,7 +19,7 @@ export class ERC721Reactor {
|
|||||||
constructor({ web3, address }: { web3: Web3; address: string }) {
|
constructor({ web3, address }: { web3: Web3; address: string }) {
|
||||||
this.web3 = web3
|
this.web3 = web3
|
||||||
this.account = this.web3.eth.accounts.wallet[0]
|
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
|
index: number
|
||||||
}): Promise<string> => {
|
}): Promise<string> => {
|
||||||
const contract = address
|
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
|
: this.contract
|
||||||
return new Promise<string>((resolve, reject) => {
|
return new Promise<string>((resolve, reject) => {
|
||||||
contract.methods.tokenOfOwnerByIndex(selectedAddress, index).call((error: Error, result: string) => {
|
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> => {
|
getBalance = async ({ address, selectedAddress }: { address?: string; selectedAddress: string }): Promise<number> => {
|
||||||
const contract = address
|
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
|
: this.contract
|
||||||
return new Promise<number>((resolve, reject) => {
|
return new Promise<number>((resolve, reject) => {
|
||||||
contract.methods.balanceOf(selectedAddress).call((error: Error, result: number) => {
|
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> => {
|
getTokenURI = async ({ address, tokenId }: { address?: string; tokenId: string }): Promise<string> => {
|
||||||
const contract = address
|
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
|
: this.contract
|
||||||
const supportsMetadata = await this.contractSupportsMetadataInterface(address)
|
const supportsMetadata = await this.contractSupportsMetadataInterface(address)
|
||||||
if (!supportsMetadata) {
|
if (!supportsMetadata) {
|
||||||
@ -135,7 +135,7 @@ export class ERC721Reactor {
|
|||||||
*/
|
*/
|
||||||
getAssetName = async (address?: string): Promise<string> => {
|
getAssetName = async (address?: string): Promise<string> => {
|
||||||
const contract = address
|
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
|
: this.contract
|
||||||
return new Promise<string>((resolve, reject) => {
|
return new Promise<string>((resolve, reject) => {
|
||||||
contract.methods.name().call((error: Error, result: string) => {
|
contract.methods.name().call((error: Error, result: string) => {
|
||||||
@ -157,7 +157,7 @@ export class ERC721Reactor {
|
|||||||
*/
|
*/
|
||||||
getAssetSymbol = async (address?: string): Promise<string> => {
|
getAssetSymbol = async (address?: string): Promise<string> => {
|
||||||
const contract = address
|
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
|
: this.contract
|
||||||
return new Promise<string>((resolve, reject) => {
|
return new Promise<string>((resolve, reject) => {
|
||||||
contract.methods.symbol().call((error: Error, result: string) => {
|
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> {
|
async getOwnerOf({ address, tokenId }: { address?: string; tokenId: string }): Promise<string> {
|
||||||
const contract = address
|
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
|
: this.contract
|
||||||
return new Promise<string>((resolve, reject) => {
|
return new Promise<string>((resolve, reject) => {
|
||||||
contract.methods.ownerOf(tokenId).call((error: Error, result: string) => {
|
contract.methods.ownerOf(tokenId).call((error: Error, result: string) => {
|
||||||
@ -209,7 +209,7 @@ export class ERC721Reactor {
|
|||||||
interfaceId: string
|
interfaceId: string
|
||||||
}): Promise<boolean> => {
|
}): Promise<boolean> => {
|
||||||
const contract = address
|
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
|
: this.contract
|
||||||
return new Promise<boolean>((resolve, reject) => {
|
return new Promise<boolean>((resolve, reject) => {
|
||||||
contract.methods.supportsInterface(interfaceId).call((error: Error, result: boolean) => {
|
contract.methods.supportsInterface(interfaceId).call((error: Error, result: boolean) => {
|
||||||
@ -309,7 +309,7 @@ export class ERC721Reactor {
|
|||||||
gas?: number
|
gas?: number
|
||||||
}) {
|
}) {
|
||||||
const contract = address
|
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
|
: this.contract
|
||||||
return contract.methods.safeTransferFrom(from, to, tokenId).send({
|
return contract.methods.safeTransferFrom(from, to, tokenId).send({
|
||||||
from,
|
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
|
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
|
: this.contract
|
||||||
let gas = await contract.methods.mint(to, tokenId, configId).estimateGas({ gas: 1000000 })
|
let gas = await contract.methods.mint(to, tokenId).estimateGas({ gas: 1000000 })
|
||||||
return contract.methods.mint(to, tokenId, configId).send({ gas: (gas * 1.1) | 0 })
|
return contract.methods.mint(to, tokenId).send({ gas: (gas * 1.1) | 0 })
|
||||||
}
|
}
|
||||||
|
|
||||||
async batchMint({
|
async batchMint({
|
||||||
@ -330,44 +330,22 @@ export class ERC721Reactor {
|
|||||||
address,
|
address,
|
||||||
to,
|
to,
|
||||||
tokenIds,
|
tokenIds,
|
||||||
configIds,
|
|
||||||
}: {
|
}: {
|
||||||
account?: string
|
account?: string
|
||||||
address?: string
|
address?: string
|
||||||
to: string
|
to: string
|
||||||
tokenIds: string[]
|
tokenIds: string[]
|
||||||
configIds: string[]
|
|
||||||
}) {
|
}) {
|
||||||
const contract = address
|
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
|
: this.contract
|
||||||
// let gas = await contract.methods.batchMint(to, tokenIds, configIds).estimateGas({ gas: 1000000 })
|
// let gas = await contract.methods.batchMint(to, tokenIds, configIds).estimateGas({ gas: 1000000 })
|
||||||
return contract.methods.batchMint(to, tokenIds, configIds).send({ gas: 1000000 })
|
return contract.methods.batchMint(to, tokenIds).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 })
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async getPastEvents({ address, fromBlock }: { address?: string; fromBlock: number }) {
|
async getPastEvents({ address, fromBlock }: { address?: string; fromBlock: number }) {
|
||||||
const contract = address
|
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
|
: this.contract
|
||||||
return contract.getPastEvents('BatchMint', {
|
return contract.getPastEvents('BatchMint', {
|
||||||
fromBlock,
|
fromBlock,
|
||||||
|
@ -96,7 +96,7 @@ export class RequestTaskClass extends BaseModule {
|
|||||||
result = await new BlockChain().erc721Reactor.transfer(self.reqData)
|
result = await new BlockChain().erc721Reactor.transfer(self.reqData)
|
||||||
break
|
break
|
||||||
case TaskType.MINT_PRESALE_BOX:
|
case TaskType.MINT_PRESALE_BOX:
|
||||||
result = await new BlockChain().erc721Reactor.mintOne(self.reqData)
|
result = await new BlockChain().erc721Reactor.mint(self.reqData)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
logger.info(result)
|
logger.info(result)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user