'Change sender account is when sending coins?
I have deployed an (openzeppelin's) ERC20 smart contract using truffle on my Ganache development network.
When contract is deployed, the first address (on the development ganache network) receives the amount of tokens specified by me below (in this case 20):
// 2_deploy_contracts.js
var GLDToken = artifacts.require("GLDToken");
module.exports = function(deployer) {
deployer.deploy(GLDToken, 20);
}
As per Openzeppelin docs I have made a function to make transfers which works perfectly, see below the full code:
// app.js
var GLDToken = artifacts.require("GLDToken");
var contractAddress = '0x3cAd6d5f5acFcCBAaC516Df696c65C2AAA2D1F82';
var testWallet1 = '0x35a356879a4573dE4CeD9D36ff76bBbA98afA2dA';
var testWallet2 = '0x8AB027e8439169Aa68514299c8bDa29C8A98483a';
var testWallet3 = '0xA384974dd53A21d10E3671FE23aB45E0CA0CDDf8';
module.exports = function() {
async function sendTransfer(recipient, amount) {
let ins = await GLDToken.at(contractAddress);
let res = await ins.transfer(recipient, amount);
console.log(res.toString());
}
sendTransfer(testWallet2, 5);
}
However I'm unable to specify a sender address and it defaults to the first wallet on my Ganache network, this means I'm able to make transfers from testWallet1 to testWallet2 and testWallet1 to testWallet3 but not from testWallet2 to testWallet3.
The transfer function of the smart contract is the one below and as you can see uses _msgSender():
function transfer(address recipient, uint256 amount) public virtual override returns (bool) {
_transfer(_msgSender(), recipient, amount);
return true;
}
How can I change this?
Solution 1:[1]
As you are not using from in the config, the contract would take the default account to do things.
You may set the default account as you want
web3.eth.defaultAccount = testWallet2;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | kaiyee0 |
