1

The "At Address" button in the remix deploy tab is to interact with an already existing contract. So why is it necessary (to be able to interact with it) to:

  1. paste the code of the contract I want to interact with
  2. compile the same contract

Why can't I just interact an existing contract without having its source code?

0

1 Answer 1

1

You need to know the function signature or the function selector in order to encode the transaction data to be sent to that smart contract to interact with that specific function.

Remix needs to know the function it will call before hand to be able to encode the data for it. So it requires you to input the contract code or a contract interface to interact with an already deployed contract using the At Address button.

You don't really need to paste the actual smart contract code. Just paste the function definitions of that smart contract, using an interface or an empty smart contract. That will work just fine.

For example, if you want to interact with Remix a deployed contract like this:

contract BitFieldExample {

    // Bit Field

    // In this example, a maximum of 32 boolean values can be stored in this 32-bit integer.
    uint256 public checkList;

    // 'Checks' the item at index `itemIndex`, by turning on that bit.
    function check(uint8 itemIndex) public returns(uint256) {

        require(itemIndex < 32, "Index value should be less than 32.");

        // We've seen this before, in example 7) Turn on a specific bit
        checkList = checkList | (1 << itemIndex);

        return checkList;

    }

    // Checks the item at index `itemIndex`, by turning on that bit.
    function isComplete(uint8 itemIndex) public view returns(bool) {

        require(itemIndex < 32, "Index value should be less than 32.");

        // We've seen this before, in example 1) Write an algorithm to get the bit at ith position.
        bool complete = (checkList & (1 << itemIndex)) > 0;

        return complete;

    }

    function unCheck(uint8 itemIndex) public returns(uint256) {

        require(itemIndex < 32, "Index value should be less than 32.");

        // We've seen this before, in example 8) Turn off a specific bit
        uint256 reversedBits = ~(1 << itemIndex);

        checkList = checkList & reversedBits;

        return checkList;

    }

    function toggleCheck(uint8 itemIndex) public returns(uint256) {

        require(itemIndex < 32, "Index value should be less than 32.");
        // We've seen this before, in example 4) Write an algorithm to toggle a bit at ith position.
        checkList = checkList ^ (1 << itemIndex);

        return checkList;

    }

    // We've seen this before, in example 12) Count on bits
    function getCountOfChecked() public view returns(uint8) {
        uint256 number = checkList;
        uint8 _count = 0; // Start our count at 0
        while(number != 0) {
            if (number & 1 == 1) { // If the first bit is 1, then count it.
                _count++;
            }
            // We already processed the first bit, now let's right shift it to forget about it
            // and have the next bit as the first bit
            number = number >> 1;
        }
        return _count;
    }

}

You don't need to paste the whole code in Remix, you can simply paste an empty smart contract with the same functions that the contract has and point to the address where the actual contract is deployed:

contract BitFieldExampleCopy {

    function check(uint8 itemIndex) external  returns(uint256) {}

    function isComplete(uint8 itemIndex) external view returns(bool) {}

    function unCheck(uint8 itemIndex) external returns(uint256) {}

    function toggleCheck(uint8 itemIndex) external returns(uint256) {}

    function getCountOfChecked() external view returns(uint8) {}

}

Or paste an interface with the same functions that the contract has and point to the address where the actual contract is deployed:

interface BitFieldExampleInterface {

    function check(uint8 itemIndex) external  returns(uint256);

    function isComplete(uint8 itemIndex) external view returns(bool);

    function unCheck(uint8 itemIndex) external returns(uint256);

    function toggleCheck(uint8 itemIndex) external returns(uint256);

    function getCountOfChecked() external view returns(uint8);

}

Both approaches will work just fine and point to the same contract and Remix will be able to interact with it.

When trying to interact with an already deployed contract from Remix, try to see if that smart contract implements an interface(s) and copy/paste the interface code instead of the actual contract.

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