Skip to content

Commit ce5460d

Browse files
committed
updates readme example
1 parent 6fb8a73 commit ce5460d

2 files changed

Lines changed: 152 additions & 4 deletions

File tree

README.md

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,32 +62,74 @@ This monorepo contains the following packages:
6262
### Simple ETH Transfer
6363

6464
```typescript
65-
import { Effect } from "effect";
65+
import { Console, Effect } from "effect";
66+
import { getRandomPrivateKey } from "@evm-effect/crypto/getRandomPrivateKey";
67+
import { getAddressFromPrivateKey, signTransaction } from "@evm-effect/crypto/transactions";
68+
import { Address, Bytes, Bytes32, U64, U256, Uint } from "@evm-effect/ethereum-types";
6669
import {
6770
Account, applyBody, BlockChain, BlockEnvironment,
6871
Fork, LegacyTransaction, State
6972
} from "@evm-effect/evm";
70-
import { Address, Bytes, U64, U256, Uint } from "@evm-effect/ethereum-types";
7173

7274
const program = Effect.gen(function* () {
7375
// Create empty blockchain
7476
const chainId = U64.constant(1n);
7577
const blockchain = BlockChain.empty(chainId);
7678

79+
// Generate accounts
80+
const alicePrivateKey = getRandomPrivateKey();
81+
const alice = getAddressFromPrivateKey(alicePrivateKey);
82+
const bob = new Address("0xbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
83+
7784
// Fund Alice with 10 ETH
78-
const alice = new Address("0xaaaa...");
7985
yield* State.setAccount(blockchain.state, alice, Account.make({
8086
nonce: Uint.constant(0n),
8187
balance: U256.constant(10n * 10n ** 18n),
8288
code: Bytes.empty(),
8389
}));
8490

85-
// Create and execute transaction...
91+
// Create transaction: Alice sends 1 ETH to Bob
92+
const unsignedTx = LegacyTransaction.make({
93+
nonce: U256.constant(0n),
94+
gasPrice: Uint.constant(10n * 10n ** 9n), // 10 gwei
95+
gas: Uint.constant(21_000n),
96+
to: bob,
97+
value: U256.constant(1n * 10n ** 18n), // 1 ETH
98+
data: Bytes.empty(),
99+
});
100+
101+
// Sign transaction
102+
const signature = signTransaction({ transaction: unsignedTx, privateKey: alicePrivateKey });
103+
const signedTx: LegacyTransaction = { ...unsignedTx, ...signature };
104+
105+
// Create block environment
106+
const blockEnv = BlockEnvironment.make({
107+
chainId,
108+
state: blockchain.state,
109+
blockGasLimit: Uint.constant(30_000_000n),
110+
blockHashes: [],
111+
coinbase: new Address("0xcccccccccccccccccccccccccccccccccccccccc"),
112+
number: Uint.constant(1n),
113+
baseFeePerGas: Uint.constant(10n * 10n ** 9n),
114+
time: U256.constant(BigInt(Math.floor(Date.now() / 1000))),
115+
prevRandao: Bytes32.zero(),
116+
difficulty: Uint.constant(0n),
117+
excessBlobGas: U64.constant(0n),
118+
parentBeaconBlockRoot: Bytes32.zero(),
119+
});
120+
121+
// Execute block with transaction
86122
const blockOutput = yield* applyBody(blockEnv, [signedTx], [], []);
123+
124+
yield* Console.log(`Gas used: ${blockOutput.blockGasUsed.value}`);
125+
yield* Console.log(`Bob balance: ${State.getAccount(blockchain.state, bob).balance.value / 10n ** 18n} ETH`);
87126
});
88127

89128
// Run with London fork
90129
Effect.runPromise(program.pipe(Effect.provide(Fork.london())));
130+
// Output:
131+
// Gas used: 21000
132+
// Bob balance: 1 ETH
91133
```
92134

93135
## Installation
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/**
2+
* README Example - Simple ETH Transfer
3+
*
4+
* This is the working example used in the README.md
5+
*/
6+
7+
import { getRandomPrivateKey } from "@evm-effect/crypto/getRandomPrivateKey";
8+
import {
9+
getAddressFromPrivateKey,
10+
signTransaction,
11+
} from "@evm-effect/crypto/transactions";
12+
import {
13+
Address,
14+
Bytes,
15+
Bytes32,
16+
U64,
17+
U256,
18+
Uint,
19+
} from "@evm-effect/ethereum-types";
20+
import {
21+
Account,
22+
applyBody,
23+
BlockChain,
24+
BlockEnvironment,
25+
Fork,
26+
LegacyTransaction,
27+
State,
28+
} from "@evm-effect/evm";
29+
import { Console, Effect } from "effect";
30+
31+
const program = Effect.gen(function* () {
32+
// Create empty blockchain
33+
const chainId = U64.constant(1n);
34+
const blockchain = BlockChain.empty(chainId);
35+
36+
// Generate accounts
37+
const alicePrivateKey = getRandomPrivateKey();
38+
const alice = getAddressFromPrivateKey(alicePrivateKey);
39+
const bob = new Address("0xbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
40+
const coinbase = new Address("0xcccccccccccccccccccccccccccccccccccccccc");
41+
42+
// Fund Alice with 10 ETH
43+
yield* State.setAccount(
44+
blockchain.state,
45+
alice,
46+
Account.make({
47+
nonce: Uint.constant(0n),
48+
balance: U256.constant(10n * 10n ** 18n),
49+
code: Bytes.empty(),
50+
}),
51+
);
52+
53+
// Create transaction: Alice sends 1 ETH to Bob
54+
const unsignedTx = LegacyTransaction.make({
55+
nonce: U256.constant(0n),
56+
gasPrice: Uint.constant(10n * 10n ** 9n), // 10 gwei
57+
gas: Uint.constant(21_000n),
58+
to: bob,
59+
value: U256.constant(1n * 10n ** 18n), // 1 ETH
60+
data: Bytes.empty(),
61+
});
62+
63+
// Sign transaction
64+
const signature = signTransaction({
65+
transaction: unsignedTx,
66+
privateKey: alicePrivateKey,
67+
});
68+
69+
const signedTx: LegacyTransaction = {
70+
...unsignedTx,
71+
v: signature.v,
72+
r: signature.r,
73+
s: signature.s,
74+
};
75+
76+
// Create block environment
77+
const blockEnv = BlockEnvironment.make({
78+
chainId,
79+
state: blockchain.state,
80+
blockGasLimit: Uint.constant(30_000_000n),
81+
blockHashes: [],
82+
coinbase,
83+
number: Uint.constant(1n),
84+
baseFeePerGas: Uint.constant(10n * 10n ** 9n),
85+
time: U256.constant(BigInt(Math.floor(Date.now() / 1000))),
86+
prevRandao: Bytes32.zero(),
87+
difficulty: Uint.constant(0n),
88+
excessBlobGas: U64.constant(0n),
89+
parentBeaconBlockRoot: Bytes32.zero(),
90+
});
91+
92+
// Execute block with transaction
93+
const blockOutput = yield* applyBody(blockEnv, [signedTx], [], []);
94+
95+
// Check results
96+
const aliceBalance = State.getAccount(blockchain.state, alice).balance;
97+
const bobBalance = State.getAccount(blockchain.state, bob).balance;
98+
99+
yield* Console.log("Transaction executed!");
100+
yield* Console.log(`Gas used: ${blockOutput.blockGasUsed.value}`);
101+
yield* Console.log(`Alice balance: ${aliceBalance.value / 10n ** 18n} ETH`);
102+
yield* Console.log(`Bob balance: ${bobBalance.value / 10n ** 18n} ETH`);
103+
});
104+
105+
// Run with London fork
106+
Effect.runPromise(program.pipe(Effect.provide(Fork.london())));

0 commit comments

Comments
 (0)