NEWTON
Asked
3 months ago
13
views
0
How can I use the meta-class of contract (starknetjs) for interaction?
Newton
asked
3 months ago
0
Accepted answer
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
3 months ago
How can I read contract memory with starknetjs?
How can I write contract memory with starknetjs?
How can I interact with my contract on Starknet using starknetjs?
How can I connect to deployed StarkNet contract using starknetjs?
How do I create account using starknetjs?
How do I create AX (ArgentX) contract using starknetjs?
How do I get started with starknetjs?
How do I check that my Solidity compiler version is >= 0.8.0 ?
StarkNet Faucet: How can I get a Goerli-2 eth faucet tokens (StarkNet)?
Has anyone had `get_caller_address` problems in devnet ?
How do I connect my DAPP to Starknet-devnet using starknetjs?
How to protect my contract from reentrancy in Cairo Language / StarkNet?
CairoLang error: `GatewayError: {'contract_address': ['Expected hex string, got:
StarknetErrorCode.DEPRECATED_TRANSACTION while deploying StarkNet contract