当前位置:   article > 正文

使用自带方法或web3js或ethersjs三种方式实现转账、查余额等原生币_ethers.js usdt转账

ethers.js usdt转账

metamask获取的etherum,使用etherum自带方法实现,etherum参数都是16进制。web3js参数为10进制。

获取gasPrice

metamask:结果返回16进制

async getGasPrice() {
    let ret;
    await ethereum.request({"method": "eth_gasPrice"}).then(res => {
      ret = res;
    }).catch(err => {
      errorHandlerOfMetaMaskRequest(err)
    });
    return ret + "";
  }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

web3js:结果返回10进制bignumber,Number(num)默认十进制

  async getGasPrice() {
    let ret;
    await web3.eth.getGasPrice().then(res => {
      ret = Number(res);
    }).catch(err => {
      errorHandlerOfMetaMaskRequest(err)
    });
    return ret + "";
  }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

ethersjs:结果返回16进制bignumber,Number(num)默认十进制

  async getGasPrice() {
    let ret;
    let provider = new ethers.providers.Web3Provider(ethereum);
    await provider.getGasPrice().then(res => {
      ret = Number(res);
    }).catch(err => {
      errorHandlerOfMetaMaskRequest(err)
    });
    return ret + "";
  }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

获取gasLimit

metamask:结果返回16进制

async estimateGas(param) {
    let ret;
    const myContract = this.getContract(param.abi, param.address);
    await ethereum.request({
      "method":"eth_estimateGas",
      "params":[{
        "form":param.from,
        "to":param.address,
        "value": toHex(param.amount * Math.pow(10,18)),
        "input": myContract.methods[param.funcName]().encodeABI()}]
    }).then(res => {
      //16进制结果
      console.log(res)
      ret = res;
    }).catch(err => {
      errorHandlerOfMetaMaskRequest(err)
    });
    return ret;
  }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

web3js:10进制bignumber,转为10进制

  async estimateGas(param) {
    let ret;
    const myContract = this.getContract(param.abi, param.address);
    await web3.eth.estimateGas({
      form:param.from,
      to:param.address,
      value: param.amount * Math.pow(10,18),
      data: myContract.methods[param.funcName]().encodeABI()
    }).then(res => {
      ret = Number(res);
    }).catch(err => {
      errorHandlerOfMetaMaskRequest(err)
    });
    return ret + "";
  }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

ethersjs:16进制bignumber

  async estimateGas(param) {
    let ret;
    let provider = new ethers.providers.Web3Provider(ethereum);
    const myContract = this.getContract(param.abi, param.address);
    await provider.estimateGas({
      form:param.from,
      to:param.address,
      value: param.amount * Math.pow(10,18),
      data: myContract.methods[param.funcName]().encodeABI()
    }).then(res => {
      ret = Number(res);
    }).catch(err => {
      errorHandlerOfMetaMaskRequest(err)
    });
    return ret + "";
  }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

ABI编码合约方法

//web3js
myContract.methods[param.funcName]().encodeABI()
//ethersjs
myContract.functions[param.funcName]().encode()
  • 1
  • 2
  • 3
  • 4

转账

async sendTransaction(param) {
    let price = await this.getGasPrice();//16进制
    let gas = await this.estimateGas(param);
    const myContract = this.getContract(param.abi, param.address);
    if (!myContract) return;
    return new Promise((resolve, reject) => {
      //metamask参数都是16进制,web3js方法10进制
      ethereum.request({
        method: "eth_sendTransaction",
        params: [{
          from: param.from, 
          to: param.address, 
          value: toHex(param.amount * Math.pow(10,18)), 
          gasPrice: price, 
          gas: gas, 
          chainId: store.state.metaMask?.chainID,
          data: myContract.methods[param.funcName]().encodeABI()
        }]
      })
        .then((res) => {
          console.log(res)
          resolve(res)
        })
        .catch((error) => { console.error(error); reject(error) });
    })
  }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

web3js

async sendTransaction(param) {
	const myContract = this.getContract(param.abi, param.address);
    if (!myContract) return;
    return new Promise((resolve, reject) => {
  	web3.eth.sendTransaction({
          from: param.from,
          to: param.address, // Required except during contract publications.
          value: param.amount * Math.pow(10, 18),
          chainId: store.state.metaMask?.chainID,
          data: myContract.methods[param.funcName]().encodeABI()
        })
        .then((res) => {
          console.log(res)
          resolve(res)
        })
        .catch((error) => { console.error(error); reject(error) });
    })
}    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

ethersjs实现原生币转账提现等

sendTransactionUseEthers(param) {
    const ethprovider = new ethers.providers.Web3Provider(ethereum);
    const signer = ethprovider.getSigner();
    const contract = new ethers.Contract(param.address, param.abi, signer)
    return new Promise((resolve, reject) => {
      try {
        const func = async () => {
          let value = param.amount?ethers.utils.parseEther(param.amount):null;
          let tx = await contract[param.funcName](value?{ value: value }:null);
          let receipt = await tx.wait();
          resolve(receipt)
        }
        func();
      } catch (error) {
        console.log(error)
        reject(error)
      }
    })
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

余额

metamask

async getBalance(account) {
    let balance;
    await ethereum.request({ method: 'eth_getBalance', params: [account, "latest"] }).then(res => {
      balance = Number(res)/Math.pow(10,18)
    })
    return balance;
  }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

web3js

async getBalance(account) {
    let balance;
    await web3.eth.getBalance(account).then(res => {
      balance = Number(res)/Math.pow(10,18)
    })
    return balance;
  }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

ethersjs

async getBalance(account) {
    let balance;
    const ethprovider = new ethers.providers.Web3Provider(ethereum);
    const signer = ethprovider.getSigner();
    await ethprovider.getBalance(account).then(res => {
      balance = Number(res)/Math.pow(10,18)
    })
    return balance;
  }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/445144
推荐阅读
相关标签
  

闽ICP备14008679号