0

so i'v 2 smart contracts token.sol

contract Token is IERC20, ReentrancyGuard {

    string private Name;
    string private Sympol;
    uint private Decimal;
    uint private TotalSupply;

    address public owner;
    mapping (address user  => uint balance ) BalanceOf;
    mapping (address tokenOwner  => mapping (address spender  => uint amount )) Allowed;
    mapping (address burner  => uint amount ) public burnedTokens;

    
    mapping (address burner  => uint  number) RewardCounter;


    Stake private stake;
    mapping(address => uint256 dateOfstackingEndsInUnixTimesStamp) public stakingPeriod;

    
    constructor(string memory _name, string memory _sympol, uint _decimal, uint _totalSupply, address _stake) {
        Name = _name;
        Sympol = _sympol;
        Decimal = _decimal;
        TotalSupply = _totalSupply;
        BalanceOf[msg.sender] = _totalSupply;
        owner = msg.sender;
        stake = Stake(_stake);
    }
        // the rest of the code ...
}

and i'v stake.sol

contract Stake {
    Token private token;
    mapping (address user => uint amount) stakeOf;
    
    constructor(address _token) {
        token = Token(_token);
    }
    //the rest of the code

this is how i deploy theme in token.js

let Token, token, Stake, stake
    let accounts = []
    let owner, user1, user2, user3, user4, burner, staker
    

    beforeEach( async()=>{
        // setup accounts
        accounts = await ethers.getSigners()
        owner = accounts[0]
        user1 = accounts[1]
        user2 = accounts[2]
        user3 = accounts[3]
        user4 = accounts[4]
        burner = accounts[5]
        staker = accounts[6]
        // load Contracts 
        Token = await ethers.getContractFactory('Token')
        Stake = await ethers.getContractFactory('Stake')
        // deploy Contract
        try {
            stake = await Stake.deploy( token.address)
            console.log(`stake address:    ${stake.address}`);
        } catch (error) {
            console.error('Error deploying Stake contract:', error);
        }
        token = await Token.deploy(
            'YasCoin', 'YCN', 18, YasCoin(1000),stake.address
        )
        
    })

2 Answers 2

1

i fix it was a circular dependency problem because in both of the constructor of the smart contracts they depeand on each other address before they even deployed, so i deploy both of theme without the addresses and i add 2 function they hold the address of the other contract after both are deployed like this

//token.sol
function setStakeContract(address _stake) public {
        require(owner == msg.sender, "Only the owner can set the Stake contract");
        stake = Stake(_stake);
    }
//stake.sol
 function setTokenContract(address _token) public {
        token = Token(_token);
    }
//then i add this to my test file after deployment statments
            await token.setStakeContract(stake.target);
            await stake.setTokenContract(token.target);
0

stake.address won't work, it's actually stake.target. Let me know if the error persists.

4
  • i appreciate your response , using your suggestion i did this solidity try { stake = await Stake.deploy( token.target(), YasCoin(1000)) console.log(`stake address: ${stake.address}`); } catch (error) { console.error('Error deploying Stake contract:', error); } token = await Token.deploy( 'YasCoin', 'YCN', 18, YasCoin(1000),stake.target() ) now and i've got this error TypeError: Cannot read properties of undefined (reading 'target')
    – Yassine
    Oct 14, 2023 at 18:54
  • After deploying the contract, add this line too. stake = await stake.waitForDeployment. This will make sure your contract is deployed before accessing the address. And if it still doesn't works, then try to console the stake instance itself to know whether or not the contract is deployed. Oct 15, 2023 at 5:15
  • thank you @Zartaj Afser i solve it it was a circular dependency problem
    – Yassine
    Oct 16, 2023 at 9:34
  • Cool, happy coding. Oct 16, 2023 at 9:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.