Skip to content
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

Add tests for SDK #54

Merged
merged 24 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion .github/workflows/build-lint-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,19 @@ jobs:
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: 20
node-version: 22
cache: pnpm

- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1

- name: Install dependencies
run: pnpm install

- name: create test env file
run: |
touch packages/sdk/.env
gsteenkamp89 marked this conversation as resolved.
Show resolved Hide resolved
echo VITE_ANVIL_ALCHEMY_KEY=${{ secrets.VITE_ANVIL_ALCHEMY_KEY }} >> packages/sdk/.env

- name: Run CI
run: pnpm run ci
1 change: 1 addition & 0 deletions packages/sdk/.example.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
VITE_ANVIL_ALCHEMY_KEY="<key>"
gsteenkamp89 marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 4 additions & 2 deletions packages/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist",
"type-check": "tsc",
"check-exports": "attw --pack . --ignore-rules=cjs-resolves-to-esm",
"test": "vitest run --reporter=verbose",
"test": "vitest run --config ./vitest.config.mts",
"ci": "pnpm run build && pnpm run check-exports pnpm npm run lint && pnpm run test",
"typedoc": "typedoc --out docs src/index.ts"
},
Expand All @@ -48,6 +48,7 @@
"@total-typescript/ts-reset": "^0.6.0",
"@types/node": "^20",
"eslint": "^8.57.0",
"msw": "^2.4.9",
"prettier": "^3.2.5",
"tsup": "^8.0.2",
"typedoc": "^0.26.7",
Expand All @@ -63,6 +64,7 @@
"viem": "^2.20.1"
},
"dependencies": {
"@across-protocol/constants": "^3.1.15"
"@across-protocol/constants": "^3.1.15",
"prool": "^0.0.16"
gsteenkamp89 marked this conversation as resolved.
Show resolved Hide resolved
}
}
17 changes: 17 additions & 0 deletions packages/sdk/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
Account,
Address,
Chain,
GetEventArgs,
Hash,
Hex,
PublicClient,
Expand All @@ -10,6 +11,8 @@ import {
} from "viem";
import { STATUS } from "../constants";
import { AcrossChain } from "../utils/getSupportedChains";
import { spokePoolAbi } from "../abis/SpokePool";
import { NoNullValuesOfObject } from "../utils/typeUtils";

export type Status = keyof typeof STATUS;

Expand Down Expand Up @@ -78,3 +81,17 @@ export type Deposit = {
};

export type ChainInfoMap = Map<number, AcrossChain>;

type MaybeFilledV3RelayEvent = GetEventArgs<
typeof spokePoolAbi,
"FilledV3Relay",
{ IndexedOnly: false }
>;

type MaybeDepositV3Event = GetEventArgs<
typeof spokePoolAbi,
"V3FundsDeposited",
{ IndexedOnly: false }
>;
export type FilledV3RelayEvent = NoNullValuesOfObject<MaybeFilledV3RelayEvent>;
export type V3FundsDepositedEvent = NoNullValuesOfObject<MaybeDepositV3Event>;
2 changes: 1 addition & 1 deletion packages/sdk/src/utils/configurePublicClients.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function configurePublicClients(
const customTransport = transports?.[chain.id];
const transport =
customTransport ??
(rpcUrl?.startsWith("wss") ? webSocket(rpcUrl) : http(rpcUrl));
(rpcUrl?.startsWith("ws") ? webSocket(rpcUrl) : http(rpcUrl));
return [
chain.id,
createPublicClient({
Expand Down
4 changes: 4 additions & 0 deletions packages/sdk/src/utils/typeUtils.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
export type MakeOptional<T extends object, K extends keyof T> = Omit<T, K> &
Partial<Pick<T, K>>;

export type NoNullValuesOfObject<T extends object> = {
[Property in keyof T]-?: NonNullable<T[Property]>;
};
8 changes: 0 additions & 8 deletions packages/sdk/test/actions/getAvailableRoutes.test.ts

This file was deleted.

283 changes: 283 additions & 0 deletions packages/sdk/test/common/anvil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,283 @@
import { arbitrum, base, type Chain, mainnet, optimism } from "viem/chains";
import {
BLOCK_NUMBER_ARBITRUM,
BLOCK_NUMBER_BASE,
BLOCK_NUMBER_MAINNET,
BLOCK_NUMBER_OPTIMISM,
FORK_URL_ARBITRUM,
FORK_URL_BASE,
FORK_URL_MAINNET,
FORK_URL_OPTIMISM,
pool,
PRIVATE_KEY,
} from "./constants";
import {
type Account,
type Client,
createPublicClient,
createTestClient,
createWalletClient,
http,
publicActions,
type PublicActions,
type TestActions,
type TestRpcSchema,
type Transport,
type WalletActions,
walletActions,
} from "viem";
import { privateKeyToAccount } from "viem/accounts";
import { type Compute } from "./utils";
import { createServer } from "prool";
import { anvil } from "prool/instances";
import {
publicActionsL2,
publicActionsL1,
type PublicActionsL2,
type PublicActionsL1,
} from "viem/op-stack";

export function getRpcUrls({ port }: { port: number }) {
return {
port,
rpcUrls: {
default: {
// append the test worker id to the rpc urls.
http: [`http://localhost:${port}/${pool}`],
webSocket: [`ws://localhost:${port}/${pool}`],
},
public: {
http: [`http://localhost:${port}/${pool}`],
webSocket: [`ws://localhost:${port}/${pool}`],
},
},
} as const;
}

type Fork = { blockNumber: bigint; url: string; opStack?: boolean };

export type AnvilChain = Compute<
Chain & {
fork: Fork;
port: number;
}
>;

const optimismFork = {
blockNumber: BLOCK_NUMBER_OPTIMISM,
url: FORK_URL_OPTIMISM,
opStack: true,
} as const satisfies Fork;

const baseFork = {
blockNumber: BLOCK_NUMBER_BASE,
url: FORK_URL_BASE,
opStack: true,
} as const satisfies Fork;

const mainnetFork = {
blockNumber: BLOCK_NUMBER_MAINNET,
url: FORK_URL_MAINNET,
} as const satisfies Fork;

const arbitrumFork = {
blockNumber: BLOCK_NUMBER_ARBITRUM,
url: FORK_URL_ARBITRUM,
} as const satisfies Fork;

// PORT: 8545
const optimismAnvil = {
...optimism,
...getRpcUrls({ port: 8545 }),
fork: optimismFork,
} as const satisfies AnvilChain;

// PORT: 8546
const baseAnvil = {
...base,
...getRpcUrls({ port: 8546 }),
fork: baseFork,
} as const satisfies AnvilChain;

// PORT: 8547
const mainnetAnvil = {
...mainnet,
...getRpcUrls({ port: 8547 }),
fork: mainnetFork,
} as const satisfies AnvilChain;

// PORT: 8548
const arbitrumAnvil = {
...arbitrum,
...getRpcUrls({ port: 8548 }),
fork: arbitrumFork,
} as const satisfies AnvilChain;

const account = privateKeyToAccount(PRIVATE_KEY);

// TEST (CHAIN) CLIENTS

export const chainClientMainnet: ChainClient = createTestClient({
account,
chain: mainnetAnvil,
mode: "anvil",
transport: http(),
})
.extend(forkMethods)
.extend(publicActions)
.extend(walletActions);

export const chainClientArbitrum: ChainClient = createTestClient({
account,
chain: arbitrumAnvil,
mode: "anvil",
transport: http(),
})
.extend(forkMethods)
.extend(publicActions)
.extend(walletActions);

// export const chainClientOptimism: OpStackChainClient = createTestClient({
// account,
// chain: optimismAnvil,
// mode: "anvil",
// transport: http(),
// })
// .extend(forkMethods)
// .extend(publicActionsL1())
// .extend(publicActionsL2());

// export const chainClientBase: OpStackChainClient = createTestClient({
// account,
// chain: baseAnvil,
// mode: "anvil",
// transport: http(),
// })
// .extend(forkMethods)
// .extend(publicActionsL1())
// .extend(publicActionsL2());

// PUBLIC CLIENTS
// export const publicClientOptimism = createPublicClient({
// chain: optimismAnvil,
// transport: http(),
// });

// export const publicClientBase = createPublicClient({
// chain: baseAnvil,
// transport: http(),
// });
gsteenkamp89 marked this conversation as resolved.
Show resolved Hide resolved
gsteenkamp89 marked this conversation as resolved.
Show resolved Hide resolved

export const publicClientMainnet = createPublicClient({
chain: mainnetAnvil,
transport: http(),
});

export const publicClientArbitrum = createPublicClient({
chain: arbitrumAnvil,
transport: http(),
});

// WALLET CLIENTS
// export const testWalletOptimism = createWalletClient({
// account,
// chain: optimismAnvil,
// transport: http(),
// });

// export const testWalletBase = createWalletClient({
// account,
// chain: baseAnvil,
// transport: http(),
// });

export const testWalletMainnet = createWalletClient({
account,
chain: mainnetAnvil,
transport: http(),
});

export const testWalletArbitrum = createWalletClient({
account,
chain: arbitrumAnvil,
transport: http(),
});

// export type OpStackChainClient = Client<
// Transport,
// AnvilChain,
// Account,
// TestRpcSchema<"anvil">,
// TestActions & ForkMethods & PublicActionsL1 & PublicActionsL2
// >;

export type ChainClient = Client<
Transport,
AnvilChain,
Account,
TestRpcSchema<"anvil">,
TestActions & ForkMethods & PublicActions & WalletActions
>;

type ForkMethods = ReturnType<typeof forkMethods>;

function forkMethods(
client: Client<
Transport,
AnvilChain,
Account,
TestRpcSchema<"anvil">,
TestActions
>,
) {
return {
async restart() {
return await fetch(`${client.chain.rpcUrls.default.http[0]}/restart`);
},
async stop() {
return await fetch(`${client.chain.rpcUrls.default.http[0]}/stop`);
},
async healthcheck() {
return await fetch(`${client.chain.rpcUrls.default.http[0]}/healthcheck`);
},
async start() {
const server = createServer({
instance: anvil({
chainId: client.chain.id,
forkUrl: client.chain.fork.url,
forkBlockNumber: client.chain.fork.blockNumber,
blockTime: 1,
}),
port: client.chain.port,
});

await server.start();

const res = await this.healthcheck();
console.log(res);
return server;
},
/** Resets fork attached to chain at starting block number. */
resetFork() {
return client.reset({
jsonRpcUrl: client.chain.fork.url,
blockNumber: client.chain.fork.blockNumber,
});
},
};
}

export const chains = {
arbitrumAnvil,
mainnetAnvil,
};

// export const opStackChainClients: Record<string, OpStackChainClient> = {
// chainClientOptimism,
// chainClientBase,
// };

export const chainClients: Record<string, ChainClient> = {
chainClientMainnet,
chainClientArbitrum,
};
Loading