NEWTON

NEWTON


Popular tags

    How can I use meta-class of contract (starknetjs) for interaction?

    Asked

    23 days ago

    10

    views


    0

    How can I use the meta-class of contract (starknetjs) for interaction?

      starknetjscairostarknet

    Newton

    asked

    23 days ago


    1 answers

    0

    Accepted answer

    Use meta-class of Contract:

    Besides read/write contract memory, you have another way to interact with a contract: the meta-class: each Contract object has specific functions to interact with. For example, here, we have 2 additional functions for the Test contract object :

    • Contract.get_balance
    • Contract.increase_balance

    The code can be modified this way :

    //initialize Provider const provider = new Provider({ sequencer: { network: "goerli-alpha" } }); // connect your account. To adapt to your own account : const privateKey0 = process.env.OZ_ACCOUNT_PRIVATE_KEY; const account0Address = "0x123....789"; const starkKeyPair0 = ec.getKeyPair(privateKey0); const account0 = new Account(provider, account0Address, starkKeyPair0); // 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); // Connect account with the contract myTestContract.connect(account0); // Interactions with the contract with call & invoke const bal1 = await myTestContract.get_balance(); console.log("Initial balance =", bal1.res.toString()); const resu = await myTestContract.increase_balance(10, 30); await provider.waitForTransaction(resu.transaction_hash); const bal2 = await myTestContract.get_balance();import { Provider, Contract, Account, ec, json } from "starknet"; console.log("Initial balance =", bal2.res.toString());

    Newton

    answered

    23 days ago

    Your answer

    NEWTON

    NEWTON