How to write caller address of a function in testing? - testing

I have a function call like this:
await multisig.connect(acc5).verify(from,to,value,_signature)
i want to check that caller address of the verify() is equal to the from, how can i write this in ether.js??
I am expecting acc5 is equal to from, how to check this in testing??

Each ethers account is an object that contains property address. So you can validate acc5.address against the from value.
const isTheSame = acc5.address == from;

Related

Create a transaction with data using etherjs

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).

Vue - is it possible to pass in a data property into a method to be modified?

I have a component that displays balances for 4 separate tokens. I'd like to have a function like this:
async updateTokenBalance(token, balance) {
balance = await getTokenBalance(token);
balance = balance.value.uiAmount;
}
which I can call like so:
updateTokenBalance(this.a_acc, this.a_balance);
updateTokenBalance(this.b_acc, this.b_balance);
updateTokenBalance(this.c_acc, this.c_balance);
updateTokenBalance(this.d_acc, this.d_balance);
unfortunately this doesn't seem to work. The only way I've gotten it to work so far is by having 4 separate functions:
async updateATokenBalance() {
let balance = await getTokenBalance(this.a_acc);
this.a_balance = balance.value.uiAmount;
}
// the other 3 analogous
in particular, it's the second line of the function where I try to assign the balance to a data property of this.a_balance that breaks if this.a_balance is passed in as arg.
Is there a way to make it work?
If your data come from an API, you should iterate through the incoming object list and run the function for each item with the analogous arguments.

How to get balanceOf of tron smart contract

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');

In a Postman pre-request-script, how can I read the actual value of a header that uses a variable

I have a variable called token with a specific value myTokenValue
I try to make a call that includes that variable in a header, tokenHeader:{{token}}
I also have a pre-request-script that needs to change the request based on the value of the token header, but if I try to read the value pm.request.headers.get('tokenHeader') I get the literal value {{token}} instead of the interpolated myTokenValue
How do I get this value without having to look at the variable directly?
You can use the following function to replace any Postman variables in a string with their resolved values:
var resolveVariables = s => s.replace(/\{\{([^}]+)\}\}/g,
(match, capture) => pm.variables.get(capture));
In your example:
var token = resolveVariables(pm.request.headers.get('tokenHeader'));
Basically I was missing a function to interpolate a string, injecting variables from the environment
There are some workarounds:
write your own function, as in this comment by pomeh
function interpolate (value) {
return value.replace(/{{([^}]+)}}/g, function (match, $1) {
return pm.variables.get($1);
});
}
use Postman's own replaceSubstitutions, as in this comment by codenirvana
function interpolate (value) {
const {Property} = require('postman-collection');
let resolved = Property.replaceSubstitutions(value, pm.variables.toObject());
}
Either of these can be used as
const tokenHeader = interpolate(pm.request.headers.get('tokenHeader'));
but the second one is also null safe.

In truffle test using js, how to set the parameters after the `contract `?

Such as WhitelistedCrowdsale.test.js in openzeppelin-solidity:
contract('WhitelistedCrowdsale', function ([_, wallet, authorized, unauthorized, anotherAuthorized]) { ... } in line 12.
why the parametes of function(...) is _, wallet, authorized, unauthorized, anotherAuthorized? Can they be other things? why?
Thanks!
Truffle injects the list of accounts available from the node you’re connected to. From the Truffle docs:
The contract() function provides a list of accounts made available by your Ethereum client which you can use to write tests.
To use these accounts, you write your test case like this:
contract(‘MyContract’, function(accounts) {
it(‘test1’, function() {
const account = accounts[0];
// do something with account
}
});
accounts is just an array. The code from OpenZeppelin you posted is expecting there to be at least 5 accounts available in the node (the same array of accounts are available via web3.eth.getAccounts()). They are just decomposing the array into specific variable names. _ is accounts[0], wallet is accounts[1], etc. You can name them whatever you want.