Last active
November 4, 2024 13:11
-
-
Save g00fy-/82e38f7c930fea5ec197c6f1aa6d78b7 to your computer and use it in GitHub Desktop.
tevm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { createMemoryClient, walletActions } from 'tevm' | |
| import { HttpTransportConfig, fallback, http, parseUnits } from 'viem'; | |
| import { mainnet } from 'viem/chains' | |
| const options: HttpTransportConfig = { | |
| timeout: 5000, | |
| async onFetchRequest(request: Request) { | |
| console.log(`Fetching ${request.url.toString()}`, await request.clone().text()) | |
| }, | |
| async onFetchResponse(response) { | |
| const text = response.ok ? await response.clone().text() : undefined; | |
| console.log(`Received response ${response.url}: ${response.status}`, text) | |
| }, | |
| batch: true | |
| } | |
| const forkClient = fallback([ | |
| // http('https://eth.llamarpc.com', options), | |
| http('https://cloudflare-eth.com', options), | |
| // http('https://mainnet.gateway.tenderly.co', options), | |
| // http('https://ethereum.blockpi.network/v1/rpc/public', options), | |
| // http('https://ethereum-rpc.publicnode.com', options), | |
| ], { rank: false, retryCount: 3, retryDelay: 500 }) | |
| const client = createMemoryClient({ | |
| // storageCache: {}, | |
| fork: { | |
| transport: forkClient({}), | |
| blockTag: 21005694n, | |
| }, | |
| }).extend(walletActions) | |
| async function main() { | |
| const alice = '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e'; | |
| const bob = '0xaf5191b0de278c7286d6c7cc6ab6bb8a73ba2cd6' | |
| await client.impersonateAccount({ | |
| address: alice | |
| }); | |
| const oldBalance = await client.getBalance({ | |
| address: alice, | |
| }) | |
| console.log(`Balance ${oldBalance}`) | |
| console.log(`Setting balance`) | |
| await client.setBalance({ address: alice, value: 10n ** 18n }) | |
| console.log(`Sending transaction`) | |
| await client.sendTransaction({ | |
| account: alice, | |
| value: parseUnits("0.0001", 18), | |
| to: '0xaf5191b0de278c7286d6c7cc6ab6bb8a73ba2cd6' | |
| }) | |
| const newBalance = await client.getBalance({ | |
| address: alice | |
| }); | |
| console.log(`New balance ${newBalance}`) | |
| } | |
| await main(); | |
| type Extendable<value extends object> = value & { extend: <props extends object >(extendFn: (value: value) => props) => Extendable<Prettify<value & props>> } | |
| function createExtendable<schema extends object>(value: schema) { | |
| function extend<value extends schema, props extends object>(value: value, props: props): Extendable<value & props> { | |
| const result = { | |
| ...value, | |
| ...props, | |
| } | |
| return { | |
| ...result, | |
| extend: (extendFn) => extend(result, extendFn(result)) | |
| }; | |
| } | |
| return extend(value, {}); | |
| } | |
| type Prettify<T> = { | |
| [K in keyof T]: T[K]; | |
| } & {}; | |
| const chain = createExtendable({ | |
| chainKey: 'string' | |
| }).extend(() => ({ | |
| dupa: 2 | |
| })).extend(() => ({ | |
| object: 3 | |
| })) | |
| // using DEAL | |
| import { test, expect } from 'vitest'; | |
| import { createMemoryClient } from 'tevm' | |
| import { ERC20 } from 'tevm/contract' | |
| import { tevmActions } from 'tevm/decorators' | |
| const client = createMemoryClient({ | |
| // loggingLevel: 'debug', | |
| name: 'tevm', | |
| account: alice, | |
| miningConfig: { | |
| type: 'auto' | |
| }, | |
| fork: { | |
| transport: fallback([ | |
| http('https://cloudflare-eth.com'), | |
| http('https://eth.llamarpc.com'), | |
| http('https://rpc.ankr.com/eth'), | |
| http('https://ethereum.publicnode.com'), | |
| ], { | |
| retryCount: 3, | |
| retryDelay: 1000, | |
| })({}), | |
| } | |
| }) | |
| .extend(publicActions) | |
| .extend(client => ({ | |
| deal: (params: { | |
| /** The address of the ERC20 token to deal */ | |
| erc20?: Address | |
| /** The owner of the dealt tokens */ | |
| account: Address | |
| /** The amount of tokens to deal */ | |
| amount: bigint | |
| }) => tevmActions()(client.tevm).deal(params) | |
| })).extend(erc20Actions) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment