Erc-20

與代幣合約互動

  • January 28, 2022

我正在嘗試創建一個簡單的合約來與之互動,但是當我嘗試檢索餘額時似乎總是得到 0 並且我不明白我做錯了什麼。我有硬幣,它應該返回正確的餘額。

堅固性:

// SPDX-License-Identifier: GPL-3.0-only
pragma solidity ^0.8.11;

import "IERC20.sol";

contract TestContract {

IERC20 testToken = IERC20(my_token_address_here);

function GetUserTokenBalance(address name) public view returns(uint256){ 
  name = msg.sender;
  return testToken.balanceOf(name);
}

}

謝謝!

我設法做到了,似乎我在做一些錯誤的聯想。請參閱下面的正確程式碼:

// SPDX-License-Identifier: GPL-3.0-only
pragma solidity ^0.8.11;

import "IERC20.sol";

contract TestContract {

IERC20 token;

constructor() {
   token = IERC20(my_token_here);
}

function GetUserTokenBalance(address name) public view returns(uint256){ 
  return token.balanceOf(name);
}

}

引用自:https://ethereum.stackexchange.com/questions/120159