我正在尝试构建与前端的简单 tron 智能合约的接口,但我不能! 这是问题:
PS D:\trontest> node index.js
(node:13816) UnhandledPromiseRejectionWarning: TypeError: contract is not a function
at f (D:\trontest\index.js:45:26)
at processTicksAndRejections (internal/process/task_queues.js:93:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:13816) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:13816) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Index.js
const TronGrid = require("trongrid");
const TronWeb = require("tronweb");
const tronWeb = new TronWeb({
fullHost: 'https://api.shasta.trongrid.io'
});
const contractAddress = "TNSRwKB4YpLQTbmG9tST9zAqXXssho5hgZ";
async function f() {
let contract = await tronWeb.contract().at(contractAddress);
const result = await contract().f().call();
console.log(result);
}
f();
constract 有两个方法 f() 和 g() 返回字符串,但我不能调用它
package.json
{
"name": "trontest",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"trongrid": "^1.2.6",
"tronweb": "^3.2.6"
}
}
UPD1 进行一些更改
const TronGrid = require("trongrid");
const TronWeb = require("tronweb");
const tronWeb = new TronWeb({
fullHost: 'https://api.shasta.trongrid.io',
headers: { "TRON-PRO-API-KEY": '*****-e677-4f4c-a9cf-2156aa5e8453' },
privateKey: 'c4f27f7b052350703bcf*****6ecddfdd612b21c891'
});
async function f() {
let instance = await tronWeb.contract("TNSRwKB4YpLQTbmG9tST9zAqXXssho5hgZ");
let result = await instance().f().call();
console.log(result);
}
f();
D:\trontest>node index.js
(node:1360) UnhandledPromiseRejectionWarning: TypeError: e.forEach is not a function
at e.value (D:\trontest\node_modules\tronweb\dist\TronWeb.node.js:1:110856)
at new e (D:\trontest\node_modules\tronweb\dist\TronWeb.node.js:1:108814)
at s.value (D:\trontest\node_modules\tronweb\dist\TronWeb.node.js:1:147871)
at f (D:\trontest\index.js:44:34)
at Object.<anonymous> (D:\trontest\index.js:48:1)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
现在我看到问题是 UnhandledPromiseRejectionWarning: TypeError: e.forEach is not a function
答案 0 :(得分:0)
试试
实例 = 等待 tronWeb.contract().at(trc20ContractAddress)
答案 1 :(得分:0)
听起来很简单,但对我来说,这一天很艰难。我只是没有做)这是有效的代码:
const TronWeb = require("tronweb");
const tronWeb = new TronWeb({
fullHost: 'https://api.shasta.trongrid.io',
headers: { "TRON-PRO-API-KEY": '6ad9cb75-****-4f4c-a9cf-2156aa5e8453' },
privateKey: 'c4f27f7b0*********67d3714405141cdf61b6ecddfdd612b21c891'
});
async function f() {
let instance = await tronWeb.contract().at('TNSRwKB4YpLQTbmG9tST9zAqXXssho5hgZ');
let result = await instance['g']().call();
console.log(result);
}
f();
看看如何正确需要输入方法
Before:
let instance = await tronWeb.contract("TNSRwKB4YpLQTbmG9tST9zAqXXssho5hgZ");
let result = await instance().f().call(); - WRONG
After:
let instance = await tronWeb.contract().at('TNSRwKB4YpLQTbmG9tST9zAqXXssho5hgZ');
let result = await instance['f']().call(); - RIGHT!
Or:
let instance = await tronWeb.contract().at('TNSRwKB4YpLQTbmG9tST9zAqXXssho5hgZ');
let result = await instance.f().call(); - RIGHT!
' instead of "
,在没有 ['name of method']().call();
的情况下调用方法 instance
和 ()