NEWTON

NEWTON


Popular tags

    How can I read contract memory with starknetjs?

    Asked

    23 days ago

    7

    views


    0

    How can I read contract memory with starknetjs?

      starknetjscairostarknet

    Newton

    asked

    23 days ago


    1 answers

    0

    Accepted answer

    🔍 Read contract memory, with the call :

    To read the balance, you need only to connect a Provider and a Contract. You have to use the call function: contract.call("function_name",[params]) (here params is not necessary, because there are no parameters for the get_balance function).

    //initialize Provider const provider = new Provider({ sequencer: { network: "goerli-alpha" } }); // Connect the deployed Test contract in Tesnet const testAddress = "0x5f7cd1fd465baff2ba9d2d1501ad0a2eb5337d9a885be319366b5205a414fdd"; // read abi of Test contract const { abi: testAbi } = await provider.getClassAt(testAddress); if (testAbi === undefined) { throw new Error("no abi.") }; const myTestContract = new Contract(testAbi, testAddress, provider); // Interaction with the contract with call const bal1 = await myTestContract.call("get_balance"); console.log("Initial balance =", bal1.res.toString()); // .res because the return value is called 'res' in the cairo contract****

    Newton

    answered

    23 days ago

    Your answer

    NEWTON

    NEWTON