Skip to content

Commit

Permalink
Merge pull request #200 from badurinantun/update-guides
Browse files Browse the repository at this point in the history
Update guides
  • Loading branch information
badurinantun authored Jun 7, 2022
2 parents cd61356 + 416da13 commit b2ba2f8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 34 deletions.
28 changes: 21 additions & 7 deletions www/guides/account.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
```
42 changes: 15 additions & 27 deletions www/guides/erc20.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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...`);
Expand All @@ -38,42 +40,28 @@ 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()
);
```

## 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
Expand All @@ -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()
);
```

0 comments on commit b2ba2f8

Please sign in to comment.