I am using tronlink chrome extension and trying to call balanceOf method of a smart contract. I am very new to smart contract. Unable to find any solution. Please check my code:
let contractDetail = await window.tronWeb.trx.getContract('TG7DLMkJPYeG4QTZ8Qfgk9Mu7ePM5SQpbN');
let contract = await window.tronWeb.contract(contractDetail.abi.entrys, 'TG7DLMkJPYeG4QTZ8Qfgk9Mu7ePM5SQpbN');
balance = contract.balanceOf.call('TNkJRejobNuZhV2LiwfGQ7wPNiLtcbDueS');
console.log(balance)
//Error: Uncaught TypeError: Cannot read property 'call' of undefined
balanceOf requires one argument.
'balanceOf(address)'
Instead of
contract.balanceOf.call('TNkJRejobNuZhV2LiwfGQ7wPNiLtcbDueS');
You should pass in the address into balanceOf
contract.balanceOf('TNkJRejobNuZhV2LiwfGQ7wPNiLtcbDueS').call();
Use tronWeb.trx.getBalance(CONTRACT_ADDRESS);
Your code should look like
balance = await tronWeb.trx.getBalance('TNkJRejobNuZhV2LiwfGQ7wPNiLtcbDueS');
Related
I'm studying the Solidity, and I want to understand how to interact with a smartcotnract and etherjs.
I have a simple function like this:
function buyNumber(uint256 _number) public payable {
if(msg.value < 0.1 ether){
revert("more eth!");
}
...todoStuff
}
And I have the test
const tx = {
from: owner.address,
to: myContract.address,
value: ethers.utils.parseEther('0.1'),
data: '0x4b729aff0000000000000000000000000000000000000000000000000000000000000001'
}
let sendTx = await owner.sendTransaction(tx);
console.log(sendTx)
Now, the transaction works because I get 0x4b729aff0000000000000000000000000000000000000000000000000000000000000001 the function signature and parameters with
let iface = new ethers.utils.Interface(ABI)
iface.encodeFunctionData("buyNumber", [ 1 ])
0x4b729aff0000000000000000000000000000000000000000000000000000000000000001
Can I get the same result in easy way? How can I call a function in my contract with params? I could put the msg.value as parameter, but I prefer to use less parameters
You can use the ethers Contract helper class, which allows invoking its methods in a more developer-friedly way, based on the provided contract ABI JSON.
It does the same thing in the background as your script - encodes the function call to the data value.
const contract = new ethers.Contract(contractAddress, abiJson, signerInstance);
// the Solidity function accepts 1 param - a number
// the last param is the `overrides` object - see docs below
await contract.buyNumber(1, {
value: ethers.utils.parseEther('0.1')
});
You can also use the overrides object to specify non-default transaction params. Such as its value (the default value is 0).
how can I interact with interfaces through hardhat.
For example in brownie you can just call function from an interface like this:
def main():
weth = interface.IWeth("weth_address")
tx = weth.deposit({"from": account, "value": "value"})
and that's it.
Is there a way to do the same in hardhat? I've been siting on this problem for couple of hours and for the life of me I can't figure out how to do this.
If it's not possible how do I get weth through solidity.
Yes, you can do it with ethers.getContractAt.
import { ethers } from "hardhat";
...
// Assume `ISetup` is an interface of the contract.
const setup = await ethers.getContractAt("ISetup", contractAddress);
In hardhat, you can achieve it using the attachment.
Go to hardhat console: npx hardhat console
Get the contract factory: factory = await ethers.getContractFactory('YourContractName')
Attach to address weth = await factory.attach('weth_address')
Do your function call e.g:
signer = await ethers.getSigner();
await signer.sendTransaction({ to: tokenAddress, value: ethers.utils.parseEther("5.0") });
How can I send tron to a smart contract function in tronlink web.
I am trying the below method, but not working.
var contract = await tronweb.contract().at('{{env('CONTRACT_ADDRESS')}}')
await contract.Invest({{session('member_id')}}).address.send(amount);
it's calling the smart contract function but not sending tron.
callValue - Amount of SUN transferred to the contract with this transaction.
(1TRX = 1,000,000 SUN)
await contract.Invest({{session('member_id')}}).send({callValue: amount})
You can check tronweb documentation for send () parameters.
when I deploy using Truffle, the contract constructor gets called right? and in there I can set
owner = msg.sender
Now on the client side, using web3 in javascript when I do something like:
window.ethereum.enable().then((accounts)=>{
contractInstance = new web3.eth.Contract(abi, "0x2c2D9E87eCFbCb9758df8cf063C71d3C9DBE5304", {from : accounts[1]});
console.log("contractInstance", contractInstance);
});
does this also call the constructor ? or is this a different situation
No, it does not. The constructor is called when you deploy (migrate) your contract to the blockchain.
Im try to Get Product details with GetMatchingProductSample.php
request param look correctly:
$request->setSellerId(MERCHANT_ID);
$request->setMarketplaceId(my ID);
$request->setASINList(my ASIN);
when i try to execute i receve always this error:
Fatal error: Call to a member function _toQueryParameterArray() on a non-object
I've look a method but i dont see any error
When i type request-> i have only these methods:
setASINlist
setSellerId
setMWSAuthToken
Finally i found a way to do a right call.
Obviously they lacked the parameters and if someone can serve this is the correct call:
$asins = array('B06Y16RL4W', 'B071DQ128D');
$request = new
MarketplaceWebServiceProducts_Model_GetMatchingProductRequest();
$asin_list = new MarketplaceWebServiceProducts_Model_ASINListType();
$request->setSellerId(MERCHANT_ID);
$request->setMarketplaceId(MARKETPLACE_ID);
$asin_list->setASIN($asins);
$request->setASINList($asin_list);