-
Notifications
You must be signed in to change notification settings - Fork 5
feat: replace code examples with tests #51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
d513b65
feat: replace code examples with tests
sarahschwartz 29820b9
chore: add job permission
sarahschwartz 06baf16
Merge branch 'main' into sarah/import-code-examples
sarahschwartz d3a7119
docs: update rpc doc
sarahschwartz b3b48e9
fix: env format
sarahschwartz ac482af
fix: CI setup
sarahschwartz fc49ca7
fix: CI test setup
sarahschwartz 8d638a4
fix: CI
sarahschwartz a912a91
fix: try fix
sarahschwartz 4b155ae
fix: docs tests
sarahschwartz b84676e
fix: tests
sarahschwartz cb960b9
docs: cleanup
sarahschwartz 6df2fae
Merge branch 'main' into sarah/import-code-examples
dutterbutter dced269
docs: update llm and contributing docs
sarahschwartz dd4ef6b
fix: lint
sarahschwartz 2a0588f
chore: split up tests
sarahschwartz 838d359
feat: split up tests
sarahschwartz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| // ANCHOR: error-import | ||
| import { isZKsyncError } from '../../../src/core/types/errors'; | ||
| // ANCHOR_END: error-import | ||
|
|
||
| import { type ErrorEnvelope as Envelope, Resource, ErrorType } from '../../../src/core/types/errors'; | ||
| import { Account, createPublicClient, createWalletClient, http, parseEther } from 'viem'; | ||
| import { privateKeyToAccount } from 'viem/accounts'; | ||
| import { createViemClient, createViemSdk, type ViemSdk } from '../../../src/adapters/viem'; | ||
| import { beforeAll, describe, it } from 'bun:test'; | ||
| import { l1Chain, l2Chain } from '../viem/chains'; | ||
| import { ETH_ADDRESS } from '../../../src/core'; | ||
| import type { Exact } from "./types"; | ||
|
|
||
| // ANCHOR: envelope-type | ||
| export interface ErrorEnvelope { | ||
| /** Resource surface that raised the error. */ | ||
| resource: Resource; | ||
| /** SDK operation, e.g. 'withdrawals.finalize' */ | ||
| operation: string; | ||
| /** Broad category */ | ||
| type: ErrorType; | ||
| /** Human-readable, stable message for developers. */ | ||
| message: string; | ||
|
|
||
| /** Optional detail that adapters may enrich (reverts, extra context) */ | ||
| context?: Record<string, unknown>; | ||
|
|
||
| /** If the error is a contract revert, adapters add decoded info here. */ | ||
| revert?: { | ||
| /** 4-byte selector as 0x…8 hex */ | ||
| selector: `0x${string}`; | ||
| /** Decoded error name when available (e.g. 'InvalidProof') */ | ||
| name?: string; | ||
| /** Decoded args (ethers/viem output), when available */ | ||
| args?: unknown[]; | ||
| /** Optional adapter-known labels */ | ||
| contract?: string; | ||
| /** Optional adapter-known function name */ | ||
| fn?: string; | ||
| }; | ||
|
|
||
| /** Original thrown error */ | ||
| cause?: unknown; | ||
| } | ||
| // ANCHOR_END: envelope-type | ||
|
|
||
| describe('checks rpc docs examples', () => { | ||
|
|
||
| let sdk: ViemSdk; | ||
| let account: Account; | ||
| let params: any; | ||
|
|
||
| beforeAll(() => { | ||
| account = privateKeyToAccount(process.env.PRIVATE_KEY as `0x${string}`); | ||
|
|
||
| const l1 = createPublicClient({ transport: http(process.env.L1_RPC!) }); | ||
| const l2 = createPublicClient({ transport: http(process.env.L2_RPC!) }); | ||
| const l1Wallet = createWalletClient({ chain: l1Chain, account, transport: http(process.env.L1_RPC!) }); | ||
| const l2Wallet = createWalletClient({ chain: l2Chain, account, transport: http(process.env.L2_RPC!) }); | ||
|
|
||
| const client = createViemClient({ l1, l2, l1Wallet, l2Wallet }); | ||
| sdk = createViemSdk(client); | ||
|
|
||
| params = { | ||
| amount: parseEther('0.01'), | ||
| to: account.address, | ||
| token: ETH_ADDRESS, | ||
| } as const; | ||
| }) | ||
|
|
||
| // this test will always succeed | ||
| // but any errors will be highlighted | ||
| it('checks to see if the error types are updated', async () => { | ||
| const _envelopeType: Exact<ErrorEnvelope, Envelope> = true; | ||
| }); | ||
|
|
||
| it('shows how to handle errors', async () => { | ||
| // ANCHOR: zksync-error | ||
| try { | ||
| const handle = await sdk.deposits.create(params); | ||
| } catch (e) { | ||
| if (isZKsyncError(e)) { | ||
| const err = e; // type-narrowed | ||
| const { type, resource, operation, message, context, revert } = err.envelope; | ||
|
|
||
| switch (type) { | ||
| case 'VALIDATION': | ||
| case 'STATE': | ||
| // user/action fixable (bad input, not-ready, etc.) | ||
| break; | ||
| case 'EXECUTION': | ||
| case 'RPC': | ||
| // network/tx/provider issues | ||
| break; | ||
| } | ||
|
|
||
| console.error(JSON.stringify(err.toJSON())); // structured log | ||
| } else { | ||
| throw e; // non-SDK error | ||
| } | ||
| } | ||
| // ANCHOR_END: zksync-error | ||
| }) | ||
|
|
||
| it('handles a withdrawal error', async () => { | ||
| // ANCHOR: try-create | ||
| const res = await sdk.withdrawals.tryCreate(params); | ||
| if (!res.ok) { | ||
| if (isZKsyncError(res.error)) { | ||
| // res.error is a ZKsyncError | ||
| console.warn(res.error.envelope.message, res.error.envelope.operation); | ||
| } else { | ||
| throw new Error("Unkown error"); | ||
| } | ||
| } else { | ||
| console.log('l2TxHash', res.value.l2TxHash); | ||
| } | ||
| // ANCHOR_END: try-create | ||
|
|
||
| if(!res.ok) throw new Error("response not ok"); | ||
| const l2TxHash = res.value.l2TxHash; | ||
|
|
||
| // ANCHOR: revert-details | ||
| try { | ||
| await sdk.withdrawals.finalize(l2TxHash); | ||
| } catch (e) { | ||
| if (isZKsyncError(e) && e.envelope.revert) { | ||
| const { selector, name, args } = e.envelope.revert; | ||
| // e.g., name === 'InvalidProof' or 'TransferAmountExceedsBalance' | ||
| } | ||
| } | ||
| // ANCHOR_END: revert-details | ||
| }) | ||
|
|
||
| it('handles a deposit error', async () => { | ||
| // ANCHOR: envelope-error | ||
| const res = await sdk.deposits.tryCreate(params); | ||
| if (!res.ok) { | ||
| if (isZKsyncError(res.error)) console.error(res.error.envelope); // structured envelope | ||
| } | ||
| // ANCHOR_END: envelope-error | ||
| }) | ||
|
|
||
| }); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| import { beforeAll, describe, expect, it } from 'bun:test'; | ||
|
|
||
| import { createPublicClient, createWalletClient, http } from 'viem'; | ||
| import { privateKeyToAccount } from 'viem/accounts'; | ||
| import { createViemClient, type ViemClient } from '../../../src/adapters/viem'; | ||
| import { Address, Hex, type ZksRpc as ZksType } from '../../../src/core'; | ||
| import { GenesisContractDeployment, GenesisInput as GenesisType, GenesisStorageEntry, L2ToL1Log, ProofNormalized as ProofN, ReceiptWithL2ToL1 as RWithLog } from '../../../src/core/rpc/types'; | ||
|
|
||
| import { l1Chain, l2Chain } from '../viem/chains'; | ||
| import type { Exact } from "./types"; | ||
|
|
||
| // ANCHOR: zks-rpc | ||
| interface ZksRpc { | ||
| getBridgehubAddress(): Promise<Address>; | ||
| getL2ToL1LogProof(txHash: Hex, index: number): Promise<ProofNormalized>; | ||
| getReceiptWithL2ToL1(txHash: Hex): Promise<ReceiptWithL2ToL1 | null>; | ||
| getGenesis(): Promise<GenesisInput>; | ||
| } | ||
| // ANCHOR_END: zks-rpc | ||
|
|
||
| // ANCHOR: proof-receipt-type | ||
| type ProofNormalized = { | ||
| id: bigint; | ||
| batchNumber: bigint; | ||
| proof: Hex[]; | ||
| }; | ||
|
|
||
| type ReceiptWithL2ToL1 = { | ||
| transactionHash?: Hex; | ||
| status?: string | number; | ||
| blockNumber?: string | number; | ||
| logs?: Array<{ | ||
| address: Address; | ||
| topics: Hex[]; | ||
| data: Hex; | ||
| }>; | ||
| // ZKsync-specific field | ||
| l2ToL1Logs?: L2ToL1Log[]; | ||
| }; | ||
| // ANCHOR_END: proof-receipt-type | ||
|
|
||
| // ANCHOR: genesis-type | ||
| export type GenesisInput = { | ||
| initialContracts: GenesisContractDeployment[]; | ||
| additionalStorage: GenesisStorageEntry[]; | ||
| executionVersion: number; | ||
| genesisRoot: Hex; | ||
| }; | ||
| // ANCHOR_END: genesis-type | ||
|
|
||
| describe('checks rpc docs examples', () => { | ||
|
|
||
| let client: ViemClient; | ||
|
|
||
| beforeAll(() => { | ||
| const account = privateKeyToAccount(process.env.PRIVATE_KEY as `0x${string}`); | ||
|
|
||
| const l1 = createPublicClient({ transport: http(process.env.L1_RPC!) }); | ||
| const l2 = createPublicClient({ transport: http(process.env.L2_RPC!) }); | ||
| const l1Wallet = createWalletClient({ chain: l1Chain, account, transport: http(process.env.L1_RPC!) }); | ||
| const l2Wallet = createWalletClient({ chain: l2Chain, account, transport: http(process.env.L2_RPC!) }); | ||
|
|
||
| client = createViemClient({ l1, l2, l1Wallet, l2Wallet }); | ||
| }) | ||
|
|
||
| // this test will always succeed | ||
| // but any errors will be highlighted | ||
| it('checks to see if the zks rpc types are updated', async () => { | ||
| const _rpcType: Exact<ZksRpc, ZksType> = true; | ||
| const _proofType: Exact<ProofNormalized, ProofN> = true; | ||
| const _receiptType: Exact<ReceiptWithL2ToL1, RWithLog> = true; | ||
| const _genesisType: Exact<GenesisInput, GenesisType> = true; | ||
| }); | ||
|
|
||
| it('tries to get the bridehub address', async () => { | ||
| // ANCHOR: bridgehub-address | ||
| const addr = await client.zks.getBridgehubAddress(); | ||
| // ANCHOR_END: bridgehub-address | ||
| expect(addr).toContain("0x"); | ||
| }); | ||
|
|
||
| it('tries to get the genesis', async () => { | ||
| // ANCHOR: genesis-method | ||
| const genesis = await client.zks.getGenesis(); | ||
|
|
||
| for (const contract of genesis.initialContracts) { | ||
| console.log('Contract at', contract.address, 'with bytecode', contract.bytecode); | ||
| } | ||
|
|
||
| console.log('Execution version:', genesis.executionVersion); | ||
| console.log('Genesis root:', genesis.genesisRoot); | ||
| // ANCHOR_END: genesis-method | ||
| expect(genesis.initialContracts).toBeArray(); | ||
| }); | ||
|
|
||
| }); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| export type Exact<A, B> = | ||
| (<T>() => T extends A ? 1 : 2) extends | ||
| (<T>() => T extends B ? 1 : 2) | ||
| ? (<T>() => T extends B ? 1 : 2) extends | ||
| (<T>() => T extends A ? 1 : 2) | ||
| ? true | ||
| : false | ||
| : false; |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.