NEWTON
Asked
23 days ago
8
views
0
How can I write contract memory with starknetjs?
Newton
asked
23 days ago
0
Accepted answer
To increase the balance, you need in addition a connected and funded Account. You have to use the invoke function: contract.invoke("function_name",[params])
After the invoke function, you have to wait for the incorporation of the modification of Balance in the network, with await provider.waitForTransaction(transaction_hash)
Here an example to increase and check the balance :
//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.call("get_balance"); console.log("Initial balance =", bal1.res.toString()); const resu = await myTestContract.invoke("increase_balance", [10, 30]); await provider.waitForTransaction(resu.transaction_hash); const bal2 = await myTestContract.call("get_balance"); console.log("Initial balance =", bal2.res.toString());
If you have to invoke a function of a contract that need the proof that you have the private key of the account, you have to invoke this function with account.execute.We will see this case more in detail in ERC20 scripts, but in summary, you uses this command with the following parameters :
const executeHash = await account.execute( { contractAddress: myContractAddress, entrypoint: 'transfer', calldata: stark.compileCalldata({ recipient: receiverAddress, amount: ['10'] }) } ); await provider.waitForTransaction(executeHash.transaction_hash);
Newton
answered
23 days ago
How can I read contract memory with starknetjs?
How can I write contact memory with Account.execute in 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 get started with starknetjs?
How do I create account using starknetjs?
How can I use meta-class of contract (starknetjs) for interaction?
Is there a way to cache context + StarkNet state?
Hey, is there anyway to debug Cairo contracts in hardhat environment? Something like hardhat console in solidity
caigo error: undefined: contracts.CounterCompiled
How to divide felt in Cairo Lang using unsigned_div_rem?
How do you optimize gas in Cairo with Uint256/felt?
Is Kakarot a Starkware project?
What’s the difference between Account Abstraction (AA) and Externally Owned Accounts (EOA)?