Skip to content

Commit

Permalink
SDKv2 migration: Route adapter (wormhole-foundation#2090)
Browse files Browse the repository at this point in the history
* sdkv2 route unit tests wip

* fix mistake

* ugh

* injective

* version lock deps to fix SDK clashes

* move mappings

* circular dep?

* ugh

* moar tests

* provide better rpc for solana

* compare isSupportedChain

* add exception for cosmos token bridge check

* supported src, dest token

* disable RPC-heavy tests for now

* clean up logs

* get rid of unused methods

* working quotes sorta

* example of local sdk working (in case i break it)

* script for using local copy of sdk

* repro

* script for installing local copy of sdk

* it works \o/

* remove unnecessary overrides

* remove old copy

* move route mapping to its own file

* moar route mapping

* fix mapping

* muahahaa

* revert part of 541607b

* fix mistake

* bamp

* test

* test2

* move localStorage call

* move wormhole deps to top level package

* sigh

* package lock

* i can do this all day mof

* move RPC-hungry tests to idle file for now

* rename fn

* remove redundant check
  • Loading branch information
artursapek committed May 24, 2024
1 parent e3c3655 commit aefabd3
Show file tree
Hide file tree
Showing 16 changed files with 18,903 additions and 9,872 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ jobs:
- run: npm ci
- run: npm run build -w sdk
- run: npm test -w wormhole-connect
env:
REACT_APP_SOLANA_RPC: ${{ vars.REACT_APP_SOLANA_RPC }}

check-sdn-list:
runs-on: ubuntu-latest
Expand Down
27,580 changes: 17,870 additions & 9,710 deletions package-lock.json

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,27 @@
},
"lint-staged": {
"**/*": "prettier --workspaces --if-present --write --ignore-unknown"
},
"overrides": {
"@injectivelabs/sdk-ts@1.10.72": {
"@injectivelabs/token-metadata": "1.10.42"
},
"@ledgerhq/hw-transport@6.27.1": {
"@ledgerhq/errors": "6.10.2",
"@ledgerhq/logs": "6.12.0",
"@ledgerhq/devices": "6.27.1"
},
"@ledgerhq/hw-transport-webhhid@6.27.1": {
"@ledgerhq/errors": "6.10.2",
"@ledgerhq/logs": "6.12.0",
"@ledgerhq/devices": "6.27.1"
},
"@wormhole-foundation/sdk-definitions-ntt": {
"@wormhole-foundation/sdk-definitions": "^0.7.0-beta.1",
"@wormhole-foundation/sdk-base": "^0.7.0-beta.1"
},
"@wormhole-foundation/wormhole-connect": {
"aptos": "1.5.0"
}
}
}
20 changes: 20 additions & 0 deletions sdk/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"env": {
"node": true
},
"root": true,
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint"],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"prettier"
],
"rules": {
"comma-dangle": ["error", "always-multiline"],
"semi": ["error", "always"],
"@typescript-eslint/explicit-module-boundary-types": ["error"],
"@typescript-eslint/no-non-null-assertion": ["error"],
"@typescript-eslint/no-explicit-any": ["error", { "ignoreRestArgs": true }]
}
}
2 changes: 1 addition & 1 deletion sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"eslint": "^8.31.0",
"eslint-config-prettier": "^8.6.0",
"ethers": "^5.7.2",
"typedoc": "^0.24.8",
"typedoc": "^0.25.0",
"typescript": "^5.3.3"
},
"scripts": {
Expand Down
15 changes: 11 additions & 4 deletions wormhole-connect/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,23 @@
"@cosmjs/tendermint-rpc": "^0.31.3",
"@emotion/react": "^11.10.5",
"@emotion/styled": "^11.10.5",
"@injectivelabs/sdk-ts": "1.10.72",
"@injectivelabs/token-metadata": "1.10.42",
"@ledgerhq/devices": "6.27.1",
"@ledgerhq/errors": "6.10.2",
"@ledgerhq/hw-transport": "6.27.1",
"@ledgerhq/hw-transport-webhid": "6.27.1",
"@ledgerhq/logs": "6.12.0",
"@manahippo/aptos-wallet-adapter": "^1.0.8",
"@mui/icons-material": "^5.11.0",
"@mui/material": "^5.11.4",
"@mysten/sui.js": "^0.32.2",
"@reduxjs/toolkit": "^1.9.1",
"@solana/wallet-adapter-wallets": "^0.19.25",
"@solana/web3.js": "^1.73.0",
"@wormhole-foundation/sdk": "^0.5.3-beta.6",
"@wormhole-foundation/sdk-definitions": "^0.6.5",
"@wormhole-foundation/sdk-definitions-ntt": "^0.0.1-beta.4",
"@wormhole-foundation/sdk": "^0.7.0-beta.3",
"@wormhole-foundation/sdk-definitions": "^0.7.0-beta.3",
"@wormhole-foundation/sdk-definitions-ntt": "^0.0.1-beta.5",
"@xlabs-libs/wallet-aggregator-aptos": "^0.0.1-alpha.14",
"@xlabs-libs/wallet-aggregator-core": "^0.0.1-alpha.18",
"@xlabs-libs/wallet-aggregator-cosmos": "^0.0.1-alpha.14",
Expand Down Expand Up @@ -66,7 +73,7 @@
"lint:ci": "scripts/lint_ci.sh",
"prettier": "prettier --write ./src",
"analyze": "NODE_ENV=production NODE_OPTIONS=--max-old-space-size=6144 vite-bundle-visualizer",
"test": "vitest run",
"test": "vitest run tests/ci",
"test:dev": "vitest",
"checksdn": "npx tsx scripts/ofac/checkSdnListForUpdates.ts",
"preview": "vite preview"
Expand Down
57 changes: 43 additions & 14 deletions wormhole-connect/src/config/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
Network as NetworkConnect,
TokenConfig as TokenConfigV1,
TokensConfig as TokensConfigV1,
ChainsConfig as ChainsConfigV1,
} from 'config/types';

import * as v2 from '@wormhole-foundation/sdk';
Expand All @@ -12,17 +11,9 @@ import * as v2 from '@wormhole-foundation/sdk';
// This is only meant to be used while we transition to SDKv2
export class SDKConverter {
wh: v1.WormholeContext;
chains: ChainsConfigV1;
tokens: TokensConfigV1;

constructor(
wh: v1.WormholeContext,
chains: ChainsConfigV1,
tokens: TokensConfigV1,
) {

constructor(wh: v1.WormholeContext) {
this.wh = wh;
this.chains = chains;
this.tokens = tokens;
}

// Chain conversion
Expand All @@ -36,6 +27,9 @@ export class SDKConverter {
}

toChainV2(chain: v1.ChainName | v1.ChainId): v2.Chain {
if (typeof chain !== 'number' && typeof chain !== 'string') {
throw new Error(JSON.stringify(chain));
}
return v2.toChain(this.wh.toChainId(chain));
}

Expand Down Expand Up @@ -81,18 +75,29 @@ export class SDKConverter {
}
}

tokenIdV2<C extends v2.Chain>(
chain: v1.ChainName | v1.ChainId,
address: string,
): v2.TokenId<C> {
const chainv2 = this.toChainV2(chain) as C;
return v2.Wormhole.tokenId(chainv2, address);
}

isTokenConfigV1(v: v1.TokenId | TokenConfigV1): v is TokenConfigV1 {
return 'key' in v;
}

// Attempts to find the Connect TokenConfig, which is comomnly used in Connect code base,
// given a v2.TokenId
findTokenConfigV1(tokenId: v2.TokenId): TokenConfigV1 | undefined {
findTokenConfigV1(
tokenId: v2.TokenId,
tokenConfigs: TokenConfigV1[],
): TokenConfigV1 | undefined {
const isNative = tokenId.address === 'native';
const chain = this.toChainNameV1(tokenId.chain);

for (const key in this.tokens) {
const token = this.tokens[key];
for (const key in tokenConfigs) {
const token = tokenConfigs[key];
if (token.nativeChain === chain) {
if (isNative && token.tokenId === undefined) {
// Connect's TokenConfig lacks a tokenId field when it's the native gas token
Expand All @@ -106,4 +111,28 @@ export class SDKConverter {
}
}
}

getTokenIdV2ForKey<C extends v2.Chain>(
key: string,
chain: v1.ChainName | v1.ChainId,
tokenConfigs: TokensConfigV1,
): v2.TokenId<C> | undefined {
const tokenConfig = tokenConfigs[key];
if (!tokenConfig) return undefined;

const chainName = this.wh.toChainName(chain);

if (tokenConfig.nativeChain === chainName) {
if (tokenConfig.tokenId) {
return this.tokenIdV2(chainName, tokenConfig.tokenId.address);
}
} else {
if (tokenConfig.foreignAssets && tokenConfig.foreignAssets[chainName]) {
return this.tokenIdV2(
chainName,
tokenConfig.foreignAssets[chainName]!.address,
);
}
}
}
}
2 changes: 1 addition & 1 deletion wormhole-connect/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export function buildConfig(
validateDefaults(customConfig.bridgeDefaults, networkData.chains, tokens);
}

const sdkConverter = new SDKConverter(wh, networkData.chains, tokens);
const sdkConverter = new SDKConverter(wh);

return {
wh,
Expand Down
61 changes: 61 additions & 0 deletions wormhole-connect/src/routes/mappings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { Route } from 'config/types';

import { routes } from '@wormhole-foundation/sdk';
import { RouteAbstract } from './abstracts/routeAbstract';

// Legacy routes
import { BridgeRoute } from './bridge';
import { RelayRoute } from './relay';
import { CCTPRelayRoute } from './cctpRelay';
import { CCTPManualRoute } from './cctpManual';
import { TBTCRoute } from './tbtc';
import { ETHBridge } from './porticoBridge/ethBridge';
import { wstETHBridge } from './porticoBridge/wstETHBridge';
import { CosmosGatewayRoute } from './cosmosGateway';
import { NttManual, NttRelay } from './ntt';

export interface RouteImpls {
v1: RouteAbstract;
v2?: routes.RouteConstructor;
}

export function getRouteImpls(route: Route): RouteImpls {
return {
[Route.Bridge]: {
v1: new BridgeRoute(),
v2: routes.TokenBridgeRoute,
},
[Route.Relay]: {
v1: new RelayRoute(),
v2: routes.AutomaticTokenBridgeRoute,
},
[Route.CCTPManual]: {
v1: new CCTPManualRoute(),
v2: routes.CCTPRoute,
},
[Route.CCTPRelay]: {
v1: new CCTPRelayRoute(),
v2: routes.AutomaticCCTPRoute,
},
[Route.CosmosGateway]: {
v1: new CosmosGatewayRoute(),
},
[Route.TBTC]: {
v1: new TBTCRoute(),
},
[Route.ETHBridge]: {
v1: new ETHBridge(),
v2: routes.AutomaticPorticoRoute,
},
[Route.wstETHBridge]: {
v1: new wstETHBridge(),
v2: routes.AutomaticPorticoRoute,
},
[Route.NttManual]: {
v1: new NttManual(),
},
[Route.NttRelay]: {
v1: new NttRelay(),
},
}[route];
}
Loading

0 comments on commit aefabd3

Please sign in to comment.