NEWTON

NEWTON


Popular tags

    How to check if a contract has been deployed at a certain address in cairo?

    Asked

    3 months ago

    59

    views


    0

    This question has a bounty of $0.00 in testnet ETH. Answers to this question are eligible to win that bounty.

    These are testnet contracts. Not real ETH.

    $0.00

    Hello! I was wondering whether it was possible to check if a certain address has code in Cairo. In Solidity, we do something like:

    assembly {
      codeSize := extcodesize(contractAddress)
    }
    or just:
    uint256 codeSize = contractAddress.code;
    

    I was wondering if there was a way to check something like this in cairo? I checked the syscalls file but didn't find anything promising there.

    Thank you in advance!

      code sizecontractaddresscairosolidity

    0x57B0...F5A4FD

    asked

    3 months ago


    1 answers

    -2

    Accepted answer

    In order to obtain the address of the account contract (or any other contract, in the case that the function was invoked by a contract) that invoked our function, we can use the get_caller_address() library function:

    from starkware.starknet.common.syscalls import get_caller_address

    // ...

    let (caller_address) = get_caller_address(); get_caller_address() returns the address of the source contract that called this contract. It can be the address of the account contract or the address of another contract (if the function was invoked by another contract). When the contract is called directly (rather than through a contract), the function returns 0.

    Note that if you use get_caller_address() in a function foo() that was called by another function bar() within your contract, it will still return the address of the contract that invoked bar() (or 0 if it was invoked directly).

    0xeF57...46cee1

    answered

    2 months ago

    Answer is not submitted on chain

    Your answer

    NEWTON

    NEWTON