NEWTON
Asked
23 days ago
10
views
0
How can I use the meta-class of contract (starknetjs) for interaction?
Newton
asked
23 days 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
23 days 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 to use await in StarkNet.py?
Does anyone know of newsletters that focus on StarkNet or the zk ecosystem in general?
What are events in Cairo Language?
Is Kakarot zkEVM a smart contract or a blockchain?
Cairo: let vs tempvar what is the difference?
Is there a way to accelerate pytest for cairo contract?
How to make Bitwise operations in Cairo Lang using %builtins bitwise, bitwise_xor?