合约调用与黑名单地址查询

  • 安装链上js调用环境

    • npm install tronweb

    • npm install web3

      mounted(){
      const web3 = new Web3("https://ethereum-mainnet.s.chainbase.online/v1/2NdSmCEFvi9eONl5rTP5tH8nxMD");
        console.log(web3);
        let walletAddress ='0x9c8f9f2dbe1f68b0604156df9e316043258d5b01' //钱包地址
        const minABI = [
          // isBlackListed
          {
            constant: true,
            inputs: [{ name: '', type: 'address' }],
            name: 'isBlacklisted',
            outputs: [{ name: '', type: 'bool'}],
            type: 'function',
          },
        ]
        console.log( this.addressdetaildata.address);
        const tokenAddress = '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48'//合约固定
        console.log(tokenAddress);
        const contract = new web3.eth.Contract(minABI, tokenAddress);
        console.log(contract);
        this.callContract(contract,walletAddress)}
      method:{
           callContract(contract,walletAddress) {
            const result =  contract.methods.isBlacklisted(walletAddress).call();
          console.log(result);
          result.then(res=>{
            console.log(res);
            this.resultvalue=res
          })
      }
      }

      Promise i<pending> i[[Prototype]]: Promise[[PromiseState]]:"fulfilled'[[PromiseResult]]: true     Promise使用.then转

  • tron和eth方法

  • 
    import Web3 from "web3";
    import { Buffer } from "buffer-es6";
    export default{ 
        tronwebfn(address){
            const TronWeb = require('tronweb')
    const tron_grid_api_key = '0f4c2d98-a688-4956-8fc3-2e0d3b9b914d'//接后端
    const tronWeb = new TronWeb({
        fullHost: 'https://api.trongrid.io',
        headers: { "TRON-PRO-API-KEY": tron_grid_api_key},
        privateKey: ''
    })
    let walletAddress = address;
     //abi为变量,可配置
    const minABI = [
        {
          "constant":true,
          "inputs":[{"name":"","type":"address"}],
          "name":"isBlackListed",
          "outputs":[{"name":"","type":"bool"}],
          "payable":false,
          "stateMutability":"view",
          "type":"function"
        }
      ]
    const tokenAddress = 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t';
    
    async function getBlackListed() {
      tronWeb.setAddress(tokenAddress);
      global.Buffer = Buffer;
      let contract = await tronWeb.contract(minABI, tokenAddress);
      const result = await contract.isBlackListed(walletAddress).call();
    //   console.log(`USDT block: ${result}`);
      return result
    }
    
    return getBlackListed();
          },
          ethorbscwebfn(address){
             const web3 = new Web3("https://ethereum-mainnet.s.chainbase.online/v1/2NdSmCEFvi9eONl5rTP5tH8nxMD");
        console.log(web3);
        let walletAddress =address
        //abi为变量,可配置
        const minABI = [
          {
            constant: true,
            inputs: [{ name: '', type: 'address' }],
            name: 'isBlacklisted',
            outputs: [{ name: '', type: 'bool'}],
            type: 'function',
          },
        ]
        const tokenAddress = '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48'//合约固定
        const contract = new web3.eth.Contract(minABI, tokenAddress);
        async function getBlackListed() {
            const result =await contract.methods.isBlacklisted(walletAddress).call();
            // console.log(`USDT block: ${result}`);
            return result
          }
    
    return getBlackListed();
          },
      }

    报错解决Uncaught (in promise) ReferenceError: Buffer is not defined
    at s.value [as sha3] (Tron_pb.js:14:1)
    at new e (Tron_pb.js:14:1)
    at eval (Tron_pb.js:14:1)
    at Array.forEach (<anonymous>)
    at e.value (Tron_pb.js:14:1)
    at new e (Tron_pb.js:14:1)
    at s.value (Tron_pb.js:14:1)
    at getBlackListed (main.js:90:1)
    at eval (main.js:95:1)
    at ./src/main.js (app.js:1138:1)

    下载插件npm install buffer-es6

    import { Buffer } from "buffer-es6";

    然后在代码写入  global.Buffer = Buffer;

  • 10
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值