@@ -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" ;
6669import {
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
7274const 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
90129Effect .runPromise (program .pipe (Effect .provide (Fork .london ())));
130+ // Output:
131+ // Gas used: 21000
132+ // Bob balance: 1 ETH
91133```
92134
93135## Installation
0 commit comments