-
安装链上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;
合约调用与黑名单地址查询
于 2024-01-16 17:54:52 首次发布