diff --git a/www/guides/account.md b/www/guides/account.md index e670edab9..fb55c2afe 100644 --- a/www/guides/account.md +++ b/www/guides/account.md @@ -14,7 +14,15 @@ Install the latest version of starknet with `npm install starknet@next` ```javascript import fs from "fs"; -import * as starknet from "starknet"; +import fs from "fs"; +import { + Account, + Contract, + defaultProvider, + ec, + json, + number, +} from "starknet"; ``` ## Generate random key pair. @@ -44,17 +52,23 @@ const accountResponse = await defaultProvider.deployContract({ Wait for the deployment transaction to be accepted and assign the address of the deployed account to the Account object. -Use your new account object to sign transactions, messages or verify signatures! - ```javascript await defaultProvider.waitForTransaction(accountResponse.transaction_hash); const accountContract = new Contract( compiledArgentAccount.abi, accountResponse.address ); -const { transaction_hash: initializeTxHash } = await accountContract.initialize( - starkKeyPub, - "0" +const initializeResponse = await accountContract.initialize(starkKeyPub, "0"); + +await defaultProvider.waitForTransaction(initializeResponse.transaction_hash); +``` + +Once account contract is initialized [Account](../docs/API/account.md) instance can be created. Use your new account instance to sign transactions, messages or verify signatures! + +```js +const account = new Account( + defaultProvider, + accountResponse.address, + starkKeyPair ); -await defaultProvider.waitForTransaction(initializeTxHash); ``` diff --git a/www/guides/erc20.md b/www/guides/erc20.md index 6421ebab6..5878e79b1 100644 --- a/www/guides/erc20.md +++ b/www/guides/erc20.md @@ -4,7 +4,7 @@ sidebar_position: 3 # Deploy an ERC20 Contract -Dpeploy the contract and wait for deployment transaction to be accepted on StarkNet +Deploy the contract and wait for deployment transaction to be accepted on StarkNet ```javascript const compiledErc20 = json.parse( @@ -26,9 +26,11 @@ const erc20 = new Contract(compiledErc20.abi, erc20Address); ## Mint tokens to an account address +Make sure you created the `Account` instance following the [Creating an Account](./account.md) guide. + ```javascript const { transaction_hash: mintTxHash } = await erc20.mint( - accountContract.address, + account.address, "1000" ); console.log(`Waiting for Tx to be Accepted on Starknet - Minting...`); @@ -38,11 +40,11 @@ await defaultProvider.waitForTransaction(mintTxHash); ## Check balance after mint ```javascript -console.log(`Calling StarkNet for accountContract balance...`); -const balanceBeforeTransfer = await erc20.balance_of(accountContract.address); +console.log(`Calling StarkNet for account balance...`); +const balanceBeforeTransfer = await erc20.balance_of(account.address); console.log( - `accountContract Address ${accountContract.address} has a balance of:`, + `account Address ${account.address} has a balance of:`, number.toBN(balanceBeforeTransfer.res, 16).toString() ); ``` @@ -50,30 +52,16 @@ console.log( ## Transfer tokens ```javascript -// Get the nonce of the account and prepare transfer tx -console.log(`Calling StarkNet for accountContract nonce...`); -const nonce = (await accountContract.call("get_nonce")).nonce.toString(); -const calls = [ +// Execute tx transfer of 10 tokens +console.log(`Invoke Tx - Transfer 10 tokens back to erc20 contract...`); +const { transaction_hash: transferTxHash } = await account.execute( { contractAddress: erc20Address, entrypoint: "transfer", calldata: [erc20Address, "10"], }, -]; -const msgHash = hash.hashMulticall(accountContract.address, calls, nonce, "0"); - -const { callArray, calldata } = transformCallsToMulticallArrays(calls); - -// sign tx to transfer 10 tokens -const signature = ec.sign(starkKeyPair, msgHash); - -// Execute tx transfer of 10 tokens -console.log(`Invoke Tx - Transfer 10 tokens back to erc20 contract...`); -const { transaction_hash: transferTxHash } = await accountContract.__execute__( - callArray, - calldata, - nonce, - signature + undefined, + { maxFee: "0" } ); // Wait for the invoke transaction to be accepted on StarkNet @@ -85,11 +73,11 @@ await defaultProvider.waitForTransaction(transferTxHash); ```javascript // Check balance after transfer - should be 990 -console.log(`Calling StarkNet for accountContract balance...`); -const balanceAfterTransfer = await erc20.balance_of(accountContract.address); +console.log(`Calling StarkNet for account balance...`); +const balanceAfterTransfer = await erc20.balance_of(account.address); console.log( - `accountContract Address ${accountContract.address} has a balance of:`, + `account Address ${account.address} has a balance of:`, number.toBN(balanceAfterTransfer.res, 16).toString() ); ```