Contract

tronWeb.contract
创建包装ABI的合约对象。 使您可以轻松地调用合约中的函数。

用法

tronWeb.contract()

参数
abi:可选择,合约提供的abi
address:可选择,合约地址,可以是HEX格式,也可以是Base58格式

返回值
Object

示例

//方式1:通过ABI和合约地址创建合约对象
let instance = await tronWeb.contract([{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"}],"TREwN2qRkME9TyQUz8dG6HfjEyKGMPHAS5")

//方式2:先创建一个空合约对象,然后通过at函数指定合约地址。如果链上有ABI,at函数会自动加载链上的abi,如果链上没有ABI,则需手动加载
let instance = await tronWeb.contract().at("TREwN2qRkME9TyQUz8dG6HfjEyKGMPHAS5");

如果合约的ABI不存在于链上,则方式2需要调用loadAbi函数手动加载ABI,示例如下:

//方式1:
let instance =await tronWeb.contract([{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"}],"TREwN2qRkME9TyQUz8dG6HfjEyKGMPHAS5")
let res = await instance.totalSupply().call({_isConstant:true})

//方式2:
let instance = await tronWeb.contract().at("TREwN2qRkME9TyQUz8dG6HfjEyKGMPHAS5");
instance.loadAbi([{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"}]);
let res = await instance.totalSupply().call({_isConstant:true})