Skip to content

Commit 655e14f

Browse files
authored
Feature/Transfer balance script (#1)
* bootstrap script added * transfer balance script added to examples and updated package-lock files * readme file updated * address PR comments
1 parent 219c4c2 commit 655e14f

File tree

12 files changed

+112
-35
lines changed

12 files changed

+112
-35
lines changed

examples/custom-rpc/package-lock.json

Lines changed: 14 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/ethers-provider-metamask/package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
CHAIN_ID=
22
PRIVATE_KEY=
33
RPC_URL=
4-
TOKEN_ADDRESS=
4+
TOKEN_ADDRESS=
5+
TRANSFER_VALUE_ETH=
6+
TRANSFER_WALLET_ADDRESS=

examples/ethers-provider-nodejs/README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ This repository demonstrates how to interact with the Silent Data [Rollup] using
77
- Connect to the Silent Data [Rollup] network
88
- Retrieve token details (name, symbol, decimals)
99
- Transfer tokens to a randomly generated wallet
10-
- Check token balances
10+
- Check wallet balances
11+
- Transfer wallet balances
1112

1213
## Prerequisites
1314

@@ -34,10 +35,12 @@ This repository demonstrates how to interact with the Silent Data [Rollup] using
3435
3. Create a `.env` file in the root directory and add your environment variables:
3536

3637
```plaintext
37-
RPC_URL=<your_rpc_url>
3838
CHAIN_ID=<your_chain_id>
3939
PRIVATE_KEY=<your_private_key>
40+
RPC_URL=<your_rpc_url>
4041
TOKEN_ADDRESS=<your_token_address>
42+
TRANSFER_VALUE_ETH=<transfer_to_value>
43+
TRANSFER_WALLET_ADDRESS=<transfer_to_private_key>
4144
```
4245

4346
## Usage
@@ -53,3 +56,9 @@ or
5356
```bash
5457
npm run get-balance
5558
```
59+
60+
or
61+
62+
```bash
63+
npm run transfer-balance
64+
```

examples/ethers-provider-nodejs/package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/ethers-provider-nodejs/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"name": "example-ethers-provider-nodejs",
33
"scripts": {
44
"contract-interaction": "ts-node src/contract-interaction.ts",
5-
"get-balance": "ts-node src/get-balance.ts"
5+
"get-balance": "ts-node src/get-balance.ts",
6+
"transfer-balance": "ts-node src/transfer-balance.ts"
67
},
78
"dependencies": {
89
"@appliedblockchain/silentdatarollup-core": "file:../../packages/core",

examples/ethers-provider-nodejs/src/contract-interaction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,4 @@ const main = async () => {
8484
console.log('✅ My token balance:', formatEther(balanceAfter), symbol)
8585
}
8686

87-
main()
87+
main().catch(console.error)

examples/ethers-provider-nodejs/src/get-balance.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ REQUIRED_ENV_VARS.forEach((envVar) => {
1010
}
1111
})
1212

13-
const RPC_URL = process.env.RPC_URL as string
1413
const CHAIN_ID = process.env.CHAIN_ID as string
1514
const PRIVATE_KEY = process.env.PRIVATE_KEY as string
15+
const RPC_URL = process.env.RPC_URL as string
1616

1717
const provider = new SilentDataRollupProvider({
1818
rpcUrl: RPC_URL,
@@ -22,8 +22,9 @@ const provider = new SilentDataRollupProvider({
2222

2323
const main = async () => {
2424
const wallet = new Wallet(PRIVATE_KEY)
25-
const balance = await provider.getBalance(wallet.address)
26-
console.log('Balance:', formatEther(balance))
25+
const walletAddress = await wallet.getAddress()
26+
const balance = await provider.getBalance(walletAddress)
27+
console.log(`Balance of "${walletAddress}" is ${formatEther(balance)} ETH`)
2728
}
2829

29-
main()
30+
main().catch(console.error)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import 'dotenv/config'
2+
import { SilentDataRollupProvider } from '@appliedblockchain/silentdatarollup-ethers-provider'
3+
import { formatEther, parseEther, Wallet } from 'ethers'
4+
5+
const REQUIRED_ENV_VARS = [
6+
'CHAIN_ID',
7+
'PRIVATE_KEY',
8+
'RPC_URL',
9+
'TRANSFER_VALUE_ETH',
10+
'TRANSFER_WALLET_ADDRESS',
11+
] as const
12+
13+
REQUIRED_ENV_VARS.forEach((envVar) => {
14+
if (!process.env[envVar]) {
15+
throw new Error(`${envVar} environment variable is required`)
16+
}
17+
})
18+
19+
const CHAIN_ID = process.env.CHAIN_ID as string
20+
const PRIVATE_KEY = process.env.PRIVATE_KEY as string
21+
const RPC_URL = process.env.RPC_URL as string
22+
const TRANSFER_VALUE_ETH = parseEther(process.env.TRANSFER_VALUE_ETH as string)
23+
const TRANSFER_WALLET_ADDRESS = process.env.TRANSFER_WALLET_ADDRESS as string
24+
25+
const provider = new SilentDataRollupProvider({
26+
rpcUrl: RPC_URL,
27+
chainId: Number(CHAIN_ID),
28+
privateKey: PRIVATE_KEY,
29+
})
30+
31+
const main = async () => {
32+
const fromWalletAddress = await provider.signer.getAddress()
33+
34+
const balanceBeforeFrom = await provider.getBalance(fromWalletAddress)
35+
console.log(
36+
`Balance of "${fromWalletAddress}" is ${formatEther(balanceBeforeFrom)} ETH`,
37+
)
38+
39+
const balanceBeforeTo = await provider.getBalance(TRANSFER_WALLET_ADDRESS)
40+
console.log(
41+
`Balance of "${TRANSFER_WALLET_ADDRESS}" is ${formatEther(balanceBeforeTo)} ETH\n`,
42+
)
43+
44+
console.log(
45+
`Sending ${formatEther(TRANSFER_VALUE_ETH)} ETH from "${fromWalletAddress}" to "${TRANSFER_WALLET_ADDRESS}"`,
46+
)
47+
const tx = await provider.signer.sendTransaction({
48+
to: TRANSFER_WALLET_ADDRESS,
49+
value: TRANSFER_VALUE_ETH,
50+
})
51+
await provider.waitForTransaction(tx.hash)
52+
console.log(`Transaction hash: ${tx.hash}\n`)
53+
54+
const balanceAfterFrom = await provider.getBalance(fromWalletAddress)
55+
console.log(
56+
`Balance of "${fromWalletAddress}" is ${formatEther(balanceAfterFrom)} ETH`,
57+
)
58+
59+
const balanceAfterTo = await provider.getBalance(TRANSFER_WALLET_ADDRESS)
60+
console.log(
61+
`Balance of "${TRANSFER_WALLET_ADDRESS}" is ${formatEther(balanceAfterTo)} ETH`,
62+
)
63+
}
64+
65+
main().catch(console.error)

examples/hardhat-plugin-fireblocks/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)