From 0e89f3d1ebb82099a9692e460e08c75ad5ddeb75 Mon Sep 17 00:00:00 2001 From: Emre Bogazliyanlioglu Date: Tue, 19 Nov 2024 19:01:45 +0300 Subject: [PATCH] Mock route Signed-off-by: Emre Bogazliyanlioglu --- connect/src/routes/index.ts | 1 + connect/src/routes/mock/index.ts | 153 +++++++++++++++++++++++++++++++ 2 files changed, 154 insertions(+) create mode 100644 connect/src/routes/mock/index.ts diff --git a/connect/src/routes/index.ts b/connect/src/routes/index.ts index afd25408b..1f2336408 100644 --- a/connect/src/routes/index.ts +++ b/connect/src/routes/index.ts @@ -7,3 +7,4 @@ export * from "./common.js"; export * from "./tokenBridge/index.js"; export * from "./portico/index.js"; export * from "./cctp/index.js"; +export * from "./mock/index.js"; diff --git a/connect/src/routes/mock/index.ts b/connect/src/routes/mock/index.ts new file mode 100644 index 000000000..d67da6d20 --- /dev/null +++ b/connect/src/routes/mock/index.ts @@ -0,0 +1,153 @@ +import type { Chain, Network } from "@wormhole-foundation/sdk-base"; +import type { + ChainAddress, + ChainContext, + Signer, + TokenId, +} from "@wormhole-foundation/sdk-definitions"; +import { nativeTokenId } from "@wormhole-foundation/sdk-definitions"; +import type { StaticRouteMethods } from "../route.js"; +import { AutomaticRoute } from "../route.js"; +import type { + Quote, + Receipt, + TransferParams, + ValidatedTransferParams, + ValidationResult, +} from "../types.js"; +import type { RouteTransferRequest } from "../request.js"; +import { chains } from "@wormhole-foundation/sdk-base"; + +export namespace AutomaticMockRoute { + export type Options = { + // Expressed in percentage terms + // e.g. 1.0 = 100% + nativeGas: number; + }; + + export type NormalizedParams = {}; + + export interface ValidatedParams extends ValidatedTransferParams { + normalizedParams: NormalizedParams; + } +} + +type Op = AutomaticMockRoute.Options; +type Vp = AutomaticMockRoute.ValidatedParams; + +type Tp = TransferParams; +type Vr = ValidationResult; +type R = Receipt; +type Q = Quote; + +export class AutomaticMockRoute + extends AutomaticRoute + implements StaticRouteMethods +{ + static NATIVE_GAS_DROPOFF_SUPPORTED = true; + + static meta = { + name: "AutomaticMock", + }; + + static supportedNetworks(): Network[] { + return ["Mainnet", "Testnet"]; + } + + // get the list of chains this route supports + static supportedChains(network: Network): Chain[] { + return chains; + } + + // get the list of source tokens that are possible to send + static async supportedSourceTokens(fromChain: ChainContext): Promise { + return Promise.resolve([nativeTokenId(fromChain.chain)]); + } + + // get the list of destination tokens that may be received on the destination chain + static async supportedDestinationTokens( + sourceToken: TokenId, + fromChain: ChainContext, + toChain: ChainContext, + ): Promise { + return Promise.resolve([nativeTokenId(toChain.chain)]); + } + + static isProtocolSupported(chain: ChainContext): boolean { + return true; + } + + getDefaultOptions(): Op { + return { nativeGas: 0.0 }; + } + + async isAvailable(request: RouteTransferRequest): Promise { + return Promise.resolve(true); + } + + async validate(request: RouteTransferRequest, params: Tp): Promise { + const options = params.options ?? this.getDefaultOptions(); + + const updatedParams = { ...params, options }; + const validatedParams: Vp = { + ...updatedParams, + normalizedParams: { ...updatedParams }, + }; + + return Promise.resolve({ valid: true, params: validatedParams }); + } + + async quote(request: RouteTransferRequest, params: Vp): Promise { + return Promise.resolve({ + success: true, + sourceToken: { + token: request.source, + amount: { + amount: "1000000", + decimals: 8, + }, + }, + destinationToken: { + token: request.destination, + amount: { + amount: "1000000", + decimals: 8, + }, + }, + relayFee: { + token: request.destination, + amount: { + amount: "10000", + decimals: 8, + }, + }, + warnings: [], + eta: "120000", + }); + } + + async initiate( + request: RouteTransferRequest, + signer: Signer, + quote: Q, + to: ChainAddress, + ): Promise { + return { + from: request.fromChain.chain, + to: request.toChain.chain, + state: 1, + originTxs: [], + }; + } + + public override async *track(receipt: R, timeout?: number) { + const trackFunc = async function* () { + yield { + ...receipt, + state: 4, + }; + }; + + yield* trackFunc(); + } +}