Counters library

:1234: Code to reproduce


:computer: Environment

Hello I'm using the Counters library in my smart contract project! I have multiple contracts in my project but I'm running into a very weird issue when using the counters library and I do not know why this is happening and I can't find a way around it!!!
I'm declaring a variable as "CountersUpgradeable.Counter public ID" and this variable to be used and called from multiple contracts. The issue is when I use the "ID.increment()" functionality. The value of ID would live only inside this contract! so for example if I have contract A and Contract B. if I use "ID.increment()" in contract A it will become 1 if I use it again It will be 2. now if I go to contract B and and use "ID.increment()" ID now would only be 1. I do not understand why this is happening instead of ID becoming 3??? ID now has two values, the first is the value from contract A which is 2, and the second value is 1 which is from contract B! Thank you for talking the time to read my issue, I hope you have a solution or an idea of why this is happening!!

using hardhat

Did you create an instance in each contract or only one instance in Contract A which can also be called by Contract B? It sounds to me like there are two IDs.

thank you so much for your reply! so I declared ID in only one of the contracts as " CountersUpgradeable.Counter public ID". Then I used "using CountersUpgradeable for CountersUpgradeable.Counter;" in both contracts!

Please share the Solidity code. I agree with @maxareo it sounds like you deployed two different contracts. The variable would not be shared between them.

Thank you so much, Yes I'm deploying both contracts, but is there a way around this issue if I want to deploy multiple contracts and increment the same value?


You would need to have a central contract that exposes the value and a way to increment it (likely permissioned).

Please share code snippets as text instead of screenshots!

Thank you so much for your response, I'm incrementing the same value. I have "ID" declared In contract C then Contract A and contract B inherit Contract C. From A and B I Increment The ID value!

//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/utils/Counters.sol";

import "./C.sol";

contract A is C {
using Counters for Counters.Counter;

function incrementId() public {
    ID.increment();
}

}
//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/utils/Counters.sol";
import './C.sol';
contract B is C {
using Counters for Counters.Counter;

function incrementId() public {
    ID.increment();
}

}
//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/utils/Counters.sol";

contract C {
using Counters for Counters.Counter;
Counters.Counter public ID;
}

My js test file looks like

const { expect } = require("chai");
const { ethers } = require("hardhat");

describe("ABC", function () {
  it("Should increment IDS ", async function () {
    let A, a, B, b, C, c;


     A = await ethers.getContractFactory("A");
     a = await A.deploy();

     B = await ethers.getContractFactory("B");
     b = await B.deploy();

     C = await ethers.getContractFactory("C");
     c = await C.deploy();

    a.incrementId()
    a.incrementId()
    b.incrementId()

    console.log("contract A ID value: ", await a.ID())
    console.log("contract B ID value: ", await b.ID())
    console.log("contract C ID value: ", await c.ID())

  });
});

my results are:

  ABC
contract A ID value:  BigNumber { value: "2" }
contract B ID value:  BigNumber { value: "1" }
contract C ID value:  BigNumber { value: "0" }
    โœ“ Should increment IDS  (1097ms)