链请求增加chain参数, 用于向不同链发送请求

This commit is contained in:
zhl 2023-05-31 15:25:40 +08:00
parent 23bc433368
commit 16698de399
2 changed files with 59 additions and 7 deletions

View File

@ -3,6 +3,8 @@ import Web3 from 'web3'
import { Contract } from 'web3-eth-contract'
import { Account } from 'web3-core'
import { AllChains } from './allchain'
import { HttpRetryProvider } from './HttpRetryProvider'
const abiFt = require('abis/ERC20.json').abi
export class ERC20Reactor {
@ -114,6 +116,7 @@ export class ERC20Reactor {
to,
amount,
account,
chain,
gas,
encodeABI = false,
estimate = false,
@ -122,13 +125,22 @@ export class ERC20Reactor {
from?: string
to: string
account?: string
chain?: number
amount: number | string
gas?: number
encodeABI?: boolean
estimate?: boolean
}) {
from = from || account || this.account.address
const contract = new this.web3.eth.Contract(abiFt, address, { from: account || this.account.address })
let w3 = this.web3
if (chain) {
let chainData = AllChains.find(o => o.id === chain)
if (chainData) {
const provider = new HttpRetryProvider(chainData.rpc.split('|'))
w3 = new Web3(provider)
}
}
const contract = new w3.eth.Contract(abiFt, address, { from: account || this.account.address })
const amountBN = Web3.utils.toBN(Web3.utils.toWei(amount + ''))
if (encodeABI) {
return contract.methods.transferFrom(from, to, amountBN).encodeABI()
@ -146,16 +158,26 @@ export class ERC20Reactor {
to,
amount,
account,
chain,
encodeABI = false,
}: {
account?: string
address?: string
to: string
amount: string
chain?: number
encodeABI: boolean
}) {
let w3 = this.web3
if (chain) {
let chainData = AllChains.find(o => o.id === chain)
if (chainData) {
const provider = new HttpRetryProvider(chainData.rpc.split('|'))
w3 = new Web3(provider)
}
}
const contract = address
? new this.web3.eth.Contract(abiFt, address, { from: account || this.account.address })
? new w3.eth.Contract(abiFt, address, { from: account || this.account.address })
: this.contract
let amountBN = Web3.utils.toBN(Web3.utils.toWei(amount + ''))
if (encodeABI) {

View File

@ -3,6 +3,8 @@ import { getFormattedIpfsUrl } from 'utils/wallet.util'
import Web3 from 'web3'
import { Contract } from 'web3-eth-contract'
import { Account } from 'web3-core'
import { AllChains } from './allchain'
import { HttpRetryProvider } from './HttpRetryProvider'
export const ERC721 = 'ERC721'
export const ERC721_INTERFACE_ID = '0x80ac58cd'
@ -299,6 +301,7 @@ export class ERC721Reactor {
to,
tokenId,
account,
chain,
gas,
encodeABI = false,
}: {
@ -307,11 +310,20 @@ export class ERC721Reactor {
to: string
tokenId: string
account?: string
chain?: number
gas?: number
encodeABI?: boolean
}) {
let w3 = this.web3
if (chain) {
let chainData = AllChains.find(o => o.id === chain)
if (chainData) {
const provider = new HttpRetryProvider(chainData.rpc.split('|'))
w3 = new Web3(provider)
}
}
const contract = address
? new this.web3.eth.Contract(abiNft, address, { from: account || this.account.address })
? new w3.eth.Contract(abiNft, address, { from: account || this.account.address })
: this.contract
if (encodeABI) {
return contract.methods.safeTransferFrom(from, to, tokenId).encodeABI()
@ -326,16 +338,24 @@ export class ERC721Reactor {
address,
to,
tokenId,
chain,
encodeABI = false,
}: {
address?: string
to: string
tokenId: string
chain?: number
encodeABI?: boolean
}) {
const contract = address
? new this.web3.eth.Contract(abiNft, address, { from: this.account.address })
: this.contract
let w3 = this.web3
if (chain) {
let chainData = AllChains.find(o => o.id === chain)
if (chainData) {
const provider = new HttpRetryProvider(chainData.rpc.split('|'))
w3 = new Web3(provider)
}
}
const contract = address ? new w3.eth.Contract(abiNft, address, { from: this.account.address }) : this.contract
if (encodeABI) {
return contract.methods.mint(to, tokenId).encodeABI()
}
@ -348,16 +368,26 @@ export class ERC721Reactor {
address,
to,
tokenIds,
chain,
encodeABI = false,
}: {
account?: string
address?: string
to: string
tokenIds: string[]
chain?: number
encodeABI?: boolean
}) {
let w3 = this.web3
if (chain) {
let chainData = AllChains.find(o => o.id === chain)
if (chainData) {
const provider = new HttpRetryProvider(chainData.rpc.split('|'))
w3 = new Web3(provider)
}
}
const contract = address
? new this.web3.eth.Contract(abiNft, address, { from: account || this.account.address })
? new w3.eth.Contract(abiNft, address, { from: account || this.account.address })
: this.contract
// let gas = await contract.methods.batchMint(to, tokenIds, configIds).estimateGas({ gas: 1000000 })
if (encodeABI) {