tronprotocol / tronweb

Javascript API Library for interacting with the TRON Network
MIT License
413 stars 271 forks source link

use tronweb error in vue3 #474

Closed kk20222202 closed 6 months ago

kk20222202 commented 7 months ago

when want to call contract to get balance,just error.

` import TronWeb from 'tronweb';

const USDTAbi = [{ "inputs": [{ "indexed": true, "name": "owner", "type": "address" }, { "indexed": true, "name": "spender", "type": "address" }, { "name": "value", "type": "uint256" }], "name": "Approval", "type": "Event" }, { "inputs": [{ "indexed": true, "name": "from", "type": "address" }, { "indexed": true, "name": "to", "type": "address" }, { "name": "value", "type": "uint256" }], "name": "Transfer", "type": "Event" }, { "outputs": [{ "type": "uint256" }], "constant": true, "inputs": [{ "name": "owner", "type": "address" }, { "name": "spender", "type": "address" }], "name": "allowance", "stateMutability": "View", "type": "Function" }, { "outputs": [{ "type": "bool" }], "inputs": [{ "name": "spender", "type": "address" }, { "name": "value", "type": "uint256" }], "name": "approve", "stateMutability": "Nonpayable", "type": "Function" }, { "outputs": [{ "type": "uint256" }], "constant": true, "inputs": [{ "name": "account", "type": "address" }], "name": "balanceOf", "stateMutability": "View", "type": "Function" }, { "outputs": [{ "type": "uint8" }], "constant": true, "name": "decimals", "stateMutability": "View", "type": "Function" }, { "outputs": [{ "type": "bool" }], "inputs": [{ "name": "spender", "type": "address" }, { "name": "subtractedValue", "type": "uint256" }], "name": "decreaseAllowance", "stateMutability": "Nonpayable", "type": "Function" }, { "outputs": [{ "type": "bool" }], "inputs": [{ "name": "spender", "type": "address" }, { "name": "addedValue", "type": "uint256" }], "name": "increaseAllowance", "stateMutability": "Nonpayable", "type": "Function" }, { "outputs": [{ "type": "string" }], "constant": true, "name": "name", "stateMutability": "View", "type": "Function" }, { "outputs": [{ "type": "string" }], "constant": true, "name": "symbol", "stateMutability": "View", "type": "Function" }, { "outputs": [{ "type": "uint256" }], "constant": true, "name": "totalSupply", "stateMutability": "View", "type": "Function" }, { "outputs": [{ "type": "bool" }], "inputs": [{ "name": "recipient", "type": "address" }, { "name": "amount", "type": "uint256" }], "name": "transfer", "stateMutability": "Nonpayable", "type": "Function" }, { "outputs": [{ "type": "bool" }], "inputs": [{ "name": "sender", "type": "address" }, { "name": "recipient", "type": "address" }, { "name": "amount", "type": "uint256" }], "name": "transferFrom", "stateMutability": "Nonpayable", "type": "Function" }];

let tronWeb=new TronWeb({ fullHost: 'https://api.trongrid.io' });

let instance = await tronWeb.contract(USDTAbi, 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t'); let balance = await instance.methods.balanceOf('user address').call(); `

tronweb.js?v=e92b7a12:28293 Uncaught (in promise) TypeError: (0 , pt.createHash) is not a function at ft (tronweb.js?v=e92b7a12:28293:36) at vt (tronweb.js?v=e92b7a12:28299:21) at Uc (tronweb.js?v=e92b7a12:33959:20) at Module.Pc (tronweb.js?v=e92b7a12:33883:59) at Object.toHex (tronweb.js?v=e92b7a12:38200:109) at Hc (tronweb.js?v=e92b7a12:33988:29) at tronweb.js?v=e92b7a12:34044:28 at Array.forEach () at e4 (tronweb.js?v=e92b7a12:34040:29) at qc (tronweb.js?v=e92b7a12:34061:18)

图片

kk20222202 commented 7 months ago

const pt = require_crypto(); var ft = function(e3) { return (0, pt.createHash)("sha256").update(e3).digest(); }

图片

start940315 commented 7 months ago

It seems like there are some problem requiring crypto module. You can try add crypto-browserify in your webpack config and node_modules.