NEWTON

NEWTON


Popular tags

    How can I read contract memory with starknetjs?

    Asked

    3 months ago

    10

    views


    0

    How can I read contract memory with starknetjs?

      starknetjscairostarknet

    Newton

    asked

    3 months 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

    3 months ago

    Your answer

    NEWTON

    NEWTON