Solidity - Check balance of a user address using openzeppelin - blockchain

I'm using Truffle and upgradable Openzeppelin contracts. I have two contracts.
Token.sol:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.2;
import "#openzeppelin/contracts-upgradeable/token/ERC20/ERC20Upgradeable.sol";
import "#openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
contract MyToken is Initializable, ERC20Upgradeable {
/// #custom:oz-upgrades-unsafe-allow constructor
constructor() initializer {}
function initialize() initializer public {
__ERC20_init("MyToken", "MTK");
_mint(msg.sender, 10000000 * 10 ** decimals());
}
}
AnotherContract.sol:
pragma solidity ^0.8.2;
import "./IAnotherContract.sol";
contract AnotherContract is IAnotherContract {
function doSomethingIfBalanceIsEnough()
external
returns (string memory)
{
// ...
}
}
How do I check how many MTK tokens a user has? I need to check it in doSomethingIfBalanceIsEnough function.

The token contract implements a balanceOf() function that returns token balance of an address.
You can make an external call from AnotherContract to MyToken address, invoking its balanceOf() function, passing it the user's address.
pragma solidity ^0.8.2;
import "./IAnotherContract.sol";
interface IERC20 {
function balanceOf(address) external view returns (uint256);
}
contract AnotherContract is IAnotherContract {
function doSomethingIfBalanceIsEnough()
external
returns (string memory)
{
uint256 userBalance = IERC20(myTokenAddress).balanceOf(msg.sender);
if (userBalance > 0) {
// ...
}
}
}

Related

Retrieve mapping data from one contract to another contract in solidity

Here, I'm trying to get tempMap data from MainContract in Temp contract but I'm not able to get value form tempMap, as well as I'm using external visibility in MainContract but still didn’t get any response.
// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;
contract MainContract {
mapping(uint => string) public tempMap;
function addData(uint number,string memory name ) public {
tempMap[number] = name;
}
function get(uint number) external view returns(string memory){
return tempMap[number];
}
}
contract Temp{
MainContract main = new MainContract();
function getData(uint number) public view returns(string memory){
return main.get(number);
}
}
You have to pass the address of the deployed contract, so it knows where to make the call.
Your temp contract will look something like this.
contract Temp{
MainContract main;
constructor(MainContract _main) {
main = _main;
}
function getData(uint number) public view returns(string memory){
return main.get(number);
}
}

why this is not compiling

I'm having this problem:
StorageFactory.sol:13:27: ParserError: Expected ';' but got '('
simpleStorage store(_simpleStorageNumber);
^
and this is my code:
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;
import "./SimpleStorage.sol";
contract StorageFactory{
SimpleStorage[] public simpleStorageArray;
function createSimpleStorageContract() public {
SimpleStorage simpleStorage = new SimpleStorage();
simpleStorageArray.push(simpleStorage);
}
function sfStore(uint256 _simpleStorrageIndex, uint256 _simpleStorageNumber) public{
SimpleStorage simpleStorage = SimpleStorage(address(simpleStorageArray[_simpleStorageIndex]));
simpleStorage store(_simpleStorageNumber);
}
I dont know what exactly the SimpleStorage.sol contract you are importing is contain but if i understand it right then this might help -
//SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.5.0 <0.9.0;
import "./SimpleStorage.sol";
contract StorageFactory{
SimpleStorage[] public simpleStorageArray;
function createSimpleStorageContract() public
{
SimpleStorage simpleStorageContract = new SimpleStorage();
simpleStorageArray.push(simpleStorageContract);
}
function sfStore(uint256 _simpleStorrageIndex, uint256 _simpleStorageNumber) public
{
SimpleStorage simpleStorageContract = SimpleStorage(address(simpleStorageArray[_simpleStorageIndex]));
simpleStorageContract.store(_simpleStorageNumber);
}
}
If this is not helping, please show us the code for the simpleStorage contract.
Anyway the solution you trying to create will be very costy.
Here is a cotract that get your goal without using another contract and is much cheaper to run -
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract Answer
{
mapping(address => uint) public StorageMap; //collection of address and numbers
function InsertToStorage(address name, uint favoritenumber) public //insert a address and favorite number
{
StorageMap[name] = favoritenumber;
}
function GetFavoriteNumber(address name) public view returns(uint) //insert an address and return the favorite number of that address
{
return StorageMap[name];
}
}
Good Luck!

solidity: how can i call a payable function from another contract and enter the amount i want to pay? the other contract is not on the editor

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
interface IBank {
function addTax(uint256 moneyToAdd) external payable;
}
contract Counter {
uint public count;
address bankAddress;
function setbankAddress(address _bankAddress) public payable {
bankAddress = _bankAddress;
}
function addMon(uint moneyToAdd) public payable {
require(moneyToAdd <= msg.value, "Ether value sent is not
correct");
}
function pay(uint _Amount) public payable {
IBank(bankAddress).addTax{value:_Amount}(_Amount);
}
}
when I deploy it I first add the contract address that I want to interact with, it's working with all functions that do not require value

Figuring out a solidity Interface issue

I got an example of a solidity Interface.
1/ Any clue if this method is accurate as it's implementing the Interface within the inherited from Contract and not within the extension Contract.
2/ I tried implementing it, contractA functions run correctly, however contractB getCount function is not running correctly.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
interface InterfaceA {
function count() external view returns (uint256);
function increment() external;
}
contract contractA {
uint256 number = 0;
function count() external view returns (uint256) {
return number;
}
function increment() external {
number++ ;
}
}
// SPDX-License-Identifier: MIT
import './contractA.sol' ;
pragma solidity ^0.8.0;
contract contractB {
address addressA;
function setPointer(address _addressA) external {
addressA = _addressA;
}
function getCount() external view returns (uint256) {
InterfaceA b = InterfaceA(addressA);
b.count();
}
function addToIncrement() external {
InterfaceA b = InterfaceA(addressA);
b.increment();
}
}
I think the only issue is you are not returning anything from getCount. You added the return signature, you should had a warning when you compiled it.
function getCount() external view returns (uint256) {
InterfaceA b = InterfaceA(addressA);
return b.count();
}

How to Call contract inside another contarct in solidity version 0.5.2?

I'm using solidity version 0.5.2
pragma solidity ^0.5.2;
contract CampaignFactory{
address[] public deployedCampaigns;
function createCampaign(uint minimum) public{
address newCampaign = new Campaign(minimum,msg.sender); //Error
//here!!!
deployedCampaigns.push(newCampaign);
}
function getDeployedCampaigns() public view returns(address[] memory){
return deployedCampaigns;
}
}
I'm getting the error while assigning calling the Campaign contract inside CampaignFactory contract
TypeError: Type contract Campaign is not implicitly convertible to expected
type address.
address newCampaign = new Campaign(minimum,msg.sender);
I have another contract called Campaign which i want to access inside CampaignFactory.
contract Campaign{
//some variable declarations and some codes here......
and I have the constructor as below
constructor (uint minimum,address creator) public{
manager=creator;
minimumContribution=minimum;
}
You can just cast it:
address newCampaign = address(new Campaign(minimum,msg.sender));
Or better yet, stop using address and use the more specific type Campaign:
pragma solidity ^0.5.2;
contract CampaignFactory{
Campaign[] public deployedCampaigns;
function createCampaign(uint minimum) public {
Campaign newCampaign = new Campaign(minimum, msg.sender);
deployedCampaigns.push(newCampaign);
}
function getDeployedCampaigns() public view returns(Campaign[] memory) {
return deployedCampaigns;
}
}
To call an existing contract from another contract ,pass the contract address inside cast
pragma solidity ^0.5.1;
contract D {
uint x;
constructor (uint a) public {
x = a;
}
function getX() public view returns(uint a)
{
return x;
}
}
contract C {
//DAddress : is the exsiting contract instance address after deployment
function getValue(address DAddress) public view returns(uint a){
D d =D(DAddress);
a=d.getX();
}
}