I am creating a contract to help migrate old ERC20 tokens to an updated ERC20 tokens.
I was running a test for the migrate method i've created
function migrateTokens() public payable {
uint256 totalBalance = _deprecatedToken.balanceOf(_msgSender());
require(totalBalance != 0, "Address not holding deprecated tokens");
_deprecatedToken.transferFrom(
_msgSender(),
address(owner()),
totalBalance
);
_token.transferFrom(address(owner()), _msgSender(), totalBalance);
}
the first line
uint256 totalBalance = _deprecatedToken.balanceOf(_msgSender());
is always returning 0.
But if i check the balance from outside the contract it is returning the correct number, but the issue resides in calling the .balanceOf method inside another contract
Extra info:
This is how i'am fullfilling the _deprecatedToken and _token variables
function updateToken(address updatedToken) public onlyOwner {
_deprecatedToken = _token;
_token = IERC20(updatedToken);
}
I am missing a core concept in this, please can i get some help here ?
Can you show the script calling the function to see if maybe there is an await
missing, or if a parameter might be the cause?
Those are the metadata to complete the test
describe('TokenExchange', () => {
let token: Token;
let tokenExchange: TokenExchange;
let trustedForwarder: MinimalForwarder;
let owner: SignerWithAddress;
let bot: SignerWithAddress;
let user: SignerWithAddress;
const tokenSupply = 1000;
async function updateToken() {
const updatedToken = await deploy(1000, trustedForwarder.address);
await (await tokenExchange.connect(owner).updateToken(updatedToken.address)).wait();
return updatedToken;
}
async function migrateToken() {
await (await token.connect(owner).transfer(user.address, 10)).wait();
const updatedToken = await updateToken();
const userBalance = (await token.balanceOf(user.address)).toNumber();
await (await token.connect(user).approve(tokenExchange.address, userBalance)).wait();
await (await updatedToken.connect(owner).approve(tokenExchange.address, userBalance)).wait();
await (await tokenExchange.connect(user).migrateTokens()).wait();
}
beforeEach(async () => {
[owner, bot, user] = await ethers.getSigners();
trustedForwarder = await deployTrustedForwarder();
token = await deploy(tokenSupply, trustedForwarder.address);
tokenExchange = await deployExchange(token.address, trustedForwarder.address);
await (await tokenExchange.connect(owner).grantRole(tokenExchange.BOT_ROLE(), bot.address)).wait();
});
This is the test scenario that fails:
it('should migrate all address deprecated tokens to the updated tokens', async () => {
const updatedToken = await updateToken();
await migrateToken();
expect(await token.balanceOf(user.address))
.to
.be
.equal(0)
expect(await updatedToken.balanceOf(user.address))
.to
.be
.equal(10)
})
frangio
November 28, 2022, 4:21pm
4
There is no reason why this would behave differently when called from inside another contract versus when called off-chain. There must be another problem. Make sure you're using the same token addresses.