--- sidebar_position: 4 --- # Interact with contract TronWeb provides several methods to interact with contract. First, you need to instantiate a contract. Here we use USDT contract in nile test net and assume the address is `USDT_ADDRESS`. Now we try to interact with it. ## call Use call to execute a pure or view smart contract method. These methods do not modify the blockchain, do not cost anything to execute and are also not broadcasted to the network. Assume we want to know the balance of `ACCOUNT_ADDRESS`, you can do it as follow: ```js let abi = [{"outputs":[{"type":"uint256"}],"constant":true,"inputs":[{"name":"who","type":"address"}],"name":"balanceOf","stateMutability":"View","type":"Function"},{"outputs":[{"type":"bool"}],"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","stateMutability":"Nonpayable","type":"Function"}]; let contract = await tronWeb.contract(abi, 'USDT_ADDRESS'); let result = await contract.balanceOf('ACCOUNT_ADDRESS').call(); console.log(result.toString(10)); // result is big number, using toString method to convert it to string. ``` ## send Use send to execute a non-pure or modify smart contract method on a given smart contract that modify or change values on the blockchain. These methods consume resources(bandwidth and energy) to perform as the changes need to be broadcasted out to the network. Now we want to transfer some USDT(TRC20 token) to `ACCOUNT_ADDRESS`, we can use `transfer` method: ```js let abi = [...]; let contract = await tronWeb.contract(abi, 'USDT_ADDRESS'); let txID = await contract.transfer('ACCOUNT_ADDRESS', 100).send(); // now you can visit web page https://nile.tronscan.org/#/transaction/${txID} to view the transaction detail. // or using code below: let result = await tronWeb.trx.getTransaction(txID); ``` > Not only can we use call & send, but we also can use triggerConstantContract & triggerSmartContract. ## triggerConstantContract Trigger the read-only function of the contract ( they are the contract function which decorated by the pure and view modifiers), the query result is a non-solidified state. Now we use another way to query the balance of `ACCOUNT_ADDRESS`: ```js const functionSelector = 'balanceOf(address)'; const parameter = [{ type: 'address', value: 'ACCOUNT_ADDRESS' }] const result = await tronWeb.transactionBuilder.triggerConstantContract('USDT_ADDRESS', functionSelector, {}, parameter); // result is as below. We can see the constant_result returns the correct balance value of ACCOUNT_ADDRESS { "result": { "result": true }, "energy_used": 935, "constant_result": [ "0000000000000000000000000000000000000000000000000000000000000064" ], "transaction": { "ret": [ {} ], "visible": false, "txID": "a1c6d9b7d3c6cf8485fa1fff023a377123ab0d5285b4bc410ac1f017572fc5c9", "raw_data": { "contract": [ { "parameter": { "value": { "data": "70a08231000000000000000000000000526f3626eaccc3f5fadd8f5f51fd9c49ce53b090", "owner_address": "4175f09e51f8ecb695a0be1701581ec9493b164495", "contract_address": "41eca9bc828a3005b9a3b909f2cc5c2a54794de05f" }, "type_url": "type.googleapis.com/protocol.TriggerSmartContract" }, "type": "TriggerSmartContract" } ], "ref_block_bytes": "39c0", "ref_block_hash": "20db92bea2aa0929", "expiration": 1677062004000, "timestamp": 1677061945989 }, "raw_data_hex": "0a0239c0220820db92bea2aa092940a0babdc5e7305a8e01081f1289010a31747970652e676f6f676c65617069732e636f6d2f70726f746f636f6c2e54726967676572536d617274436f6e747261637412540a154175f09e51f8ecb695a0be1701581ec9493b164495121541eca9bc828a3005b9a3b909f2cc5c2a54794de05f222470a08231000000000000000000000000526f3626eaccc3f5fadd8f5f51fd9c49ce53b0907085f5b9c5e730" } } ``` ## triggerSmartContract It returns TransactionExtention, which contains the unsigned Transaction. Now we use triggerSmartContract to rewrite `transfer`: ```js const functionSelector = 'transfer(address,uint256)'; const parameter = [{type:'address',value:'ACCOUNT_ADDRESS'},{type:'uint256',value:100}] const tx = await tronWeb.transactionBuilder.triggerSmartContract('USDT_ADDRESS', functionSelector, {}, parameter); const signedTx = await tronWeb.trx.sign(tx.transaction); const result = await tronWeb.trx.sendRawTransaction(signedTx); // the result looks like below: { "result": true, "txid": "bea6cff8d62d62209f87810396a9a26802b93f566cb08f925ec91a071002060f", "transaction": { "visible": false, "txID": "bea6cff8d62d62209f87810396a9a26802b93f566cb08f925ec91a071002060f", "raw_data": { "contract": [ { "parameter": { "value": { "data": "a9059cbb000000000000000000000000526f3626eaccc3f5fadd8f5f51fd9c49ce53b0900000000000000000000000000000000000000000000000000000000000000064", "owner_address": "4175f09e51f8ecb695a0be1701581ec9493b164495", "contract_address": "41eca9bc828a3005b9a3b909f2cc5c2a54794de05f" }, "type_url": "type.googleapis.com/protocol.TriggerSmartContract" }, "type": "TriggerSmartContract" } ], "ref_block_bytes": "3bc7", "ref_block_hash": "a5b57140c2f487fa", "expiration": 1677063573000, "fee_limit": 150000000, "timestamp": 1677063515485 }, "raw_data_hex": "0a023bc72208a5b57140c2f487fa40889c9dc6e7305aae01081f12a9010a31747970652e676f6f676c65617069732e636f6d2f70726f746f636f6c2e54726967676572536d617274436f6e747261637412740a154175f09e51f8ecb695a0be1701581ec9493b164495121541eca9bc828a3005b9a3b909f2cc5c2a54794de05f2244a9059cbb000000000000000000000000526f3626eaccc3f5fadd8f5f51fd9c49ce53b090000000000000000000000000000000000000000000000000000000000000006470ddda99c6e730900180a3c347", "signature": [ "c1dddbc3812ad0b93d245b304506f6df54c0ff8e56167a52211e560ca1bb8aea45329c9ed2f3a0e959412cf1099f3042f5f716677bb196df1d02e0bf27d5dec800" ] } } ``` We now checkout the balance of `ACCOUNT_ADDRESS`, it would be 200. ## estimateEnergy If you want to know the cost to trigger a smart contract, you can use this method. It takes the same arguments as `triggerSmartContract` except the feeLimit parameter, but the result could not be the same as the real cost. See the example below: ```js const functionSelector = 'transfer(address,uint256)'; const parameter = [{type:'address',value:'ACCOUNT_ADDRESS'},{type:'uint256',value:100}] const result = await tronWeb.transactionBuilder.estimateEnergy('USDT_ADDRESS', functionSelector, {}, parameter); // the result looks like below: { "result": { "result": true }, "energy_required": 16482 } ```