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

use UnsignedTransaction type #10

Merged
merged 5 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
13 changes: 6 additions & 7 deletions wormhole-connect/src/hooks/useIsTransferLimited.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,12 @@ const useIsTransferLimited = (): IsTransferLimitedResult => {
transferNotional > chain.notionalLimit
? 'EXCEEDS_MAX_NOTIONAL'
: transferNotional >
chain.bigTransactionSize * REMAINING_NOTIONAL_TOLERANCE
? 'EXCEEDS_LARGE_TRANSFER_LIMIT'
: transferNotional >
chain.remainingAvailableNotional *
REMAINING_NOTIONAL_TOLERANCE
? 'EXCEEDS_REMAINING_NOTIONAL'
: undefined;
chain.bigTransactionSize * REMAINING_NOTIONAL_TOLERANCE
? 'EXCEEDS_LARGE_TRANSFER_LIMIT'
: transferNotional >
chain.remainingAvailableNotional * REMAINING_NOTIONAL_TOLERANCE
? 'EXCEEDS_REMAINING_NOTIONAL'
: undefined;
return {
isLimited: !!isLimitedReason,
reason: isLimitedReason,
Expand Down
2 changes: 1 addition & 1 deletion wormhole-connect/src/hooks/useTrackTransfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { sleep } from 'utils';
const TRACK_TIMEOUT = 120 * 1000;

// TODO: document this hook, especially since it sets and depends on the receipt state
const useTrackTransfer = () => {
const useTrackTransfer = (): void => {
const dispatch = useDispatch();

const routeContext = useContext(RouteContext);
Expand Down
18 changes: 1 addition & 17 deletions wormhole-connect/src/routes/operator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,23 +272,7 @@ export class Operator {
supported[token.key] = token;
}
} catch (e) {
// Fall back to less efficient method
for (const token of config.tokensArr) {
const { key } = token;
const alreadySupported = supported[key];
if (!alreadySupported) {
const isSupported = await r.isSupportedDestToken(
token,
sourceToken,
sourceChain,
destChain,
);

if (isSupported) {
supported[key] = config.tokens[key];
}
}
}
console.error(e);
}
}
return Object.values(supported);
Expand Down
5 changes: 1 addition & 4 deletions wormhole-connect/src/routes/sdkv2/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@
NATIVE_GAS_DROPOFF_SUPPORTED = false;
AUTOMATIC_DEPOSIT = false;

constructor(
readonly rc: routes.RouteConstructor,
routeType: Route,
) {
constructor(readonly rc: routes.RouteConstructor, routeType: Route) {
super();
this.TYPE = routeType;
}
Expand Down Expand Up @@ -335,7 +332,7 @@
const srcChain = (await this.getV2ChainContext(fromChainV1)).context;
const dstChain = (await this.getV2ChainContext(toChainV1)).context;

const [_route, quote] = await this.getQuote(

Check warning on line 335 in wormhole-connect/src/routes/sdkv2/route.ts

View workflow job for this annotation

GitHub Actions / lint

'_route' is assigned a value but never used
amountIn.toString(),
srcTokenV2,
dstTokenV2,
Expand Down
44 changes: 1 addition & 43 deletions wormhole-connect/src/routes/sdkv2/signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import { ChainId, ChainName } from 'sdklegacy';
import config, { getWormholeContextV2 } from 'config';
import { signAndSendTransaction, TransferWallet } from 'utils/wallet';

import { SignRequest } from 'utils/wallet/types';

// Utility class that bridges between legacy Connect signer interface and SDKv2 signer interface
export class SDKv2Signer<N extends Network, C extends Chain>
implements SignAndSendSigner<N, C>
Expand Down Expand Up @@ -54,11 +52,9 @@ export class SDKv2Signer<N extends Network, C extends Chain>
const txHashes: TxHash[] = [];

for (const tx of txs) {
const request: SignRequest = this.createSignRequest(tx);

const txId = await signAndSendTransaction(
this._chainNameV1,
request,
tx,
TransferWallet.SENDING,
this._options,
);
Expand All @@ -68,44 +64,6 @@ export class SDKv2Signer<N extends Network, C extends Chain>
return txHashes;
}

// This takes an SDKv2 UnsignedTransaction and prepares it for use by xlabs-wallet-adapter
private createSignRequest(tx: UnsignedTransaction<N, C>): SignRequest {
const platform = chainToPlatform(tx.chain);

if (platform === 'Evm') {
return {
platform,
transaction: tx.transaction,
};
} else if (platform === 'Solana') {
return {
platform,
transaction: tx.transaction.transaction,
signers: tx.transaction.signers,
};
} else if (platform === 'Cosmwasm') {
//debugger;
return {
platform,
transaction: tx,
};
} else if (platform === 'Sui') {
return {
platform,
transaction: tx.transaction,
};
} else if (platform === 'Aptos') {
return {
platform,
transaction: tx.transaction,
};
} else {
throw new Error(
`createSignRequest is unimplemented for platform ${platform}`,
);
}
}

chain() {
return this._chainContextV2.chain;
}
Expand Down
10 changes: 5 additions & 5 deletions wormhole-connect/src/sdklegacy/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,27 +86,27 @@ export type AnyContracts = any;
export interface ParsedMessage {
sendTx: string;
sender: string;
amount: BigInt;
amount: bigint;
payloadID: number;
recipient: string;
toChain: ChainName;
fromChain: ChainName;
tokenAddress: string;
tokenChain: ChainName;
tokenId: TokenId;
sequence?: BigInt;
sequence?: bigint;
emitterAddress?: string;
block: number;
gasFee?: BigInt;
gasFee?: bigint;
payload?: string;
fromAddress?: string;
}

export interface ParsedRelayerPayload {
relayerPayloadId: number;
to: string;
relayerFee: BigInt;
toNativeTokenAmount: BigInt;
relayerFee: bigint;
toNativeTokenAmount: bigint;
}

export type ParsedRelayerMessage = ParsedMessage & ParsedRelayerPayload;
Expand Down
8 changes: 4 additions & 4 deletions wormhole-connect/src/sdklegacy/wormhole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ export class WormholeContext extends MultiProvider<Domain> {
this.conf.env === 'mainnet'
? MAINNET_CHAINS
: this.conf.env === 'devnet'
? DEVNET_CHAINS
: TESTNET_CHAINS;
? DEVNET_CHAINS
: TESTNET_CHAINS;
const chainConfig = (chains as any)[n];
if (!chainConfig) throw new Error(`invalid network name ${n}`);
// register domain
Expand Down Expand Up @@ -188,8 +188,8 @@ export class WormholeContext extends MultiProvider<Domain> {
return env === 'mainnet'
? MAINNET_CONFIG
: env === 'devnet'
? DEVNET_CONFIG
: TESTNET_CONFIG;
? DEVNET_CONFIG
: TESTNET_CONFIG;
}

// BEGIN stubbed methods for SDKV2 migration
Expand Down
9 changes: 7 additions & 2 deletions wormhole-connect/src/utils/wallet/aptos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@ import { AptosWallet } from '@xlabs-libs/wallet-aggregator-aptos';

import { Types } from 'aptos';

import { Network } from '@wormhole-foundation/sdk';
import {
AptosUnsignedTransaction,
AptosChains,
} from '@wormhole-foundation/sdk-aptos';

import config from 'config';
import { SignRequestAptos } from './types';

const aptosWallets = {
aptos: new AptosWallet(new AptosWalletAdapter()),
Expand All @@ -41,7 +46,7 @@ export function fetchOptions() {
}

export async function signAndSendTransaction(
request: SignRequestAptos,
request: AptosUnsignedTransaction<Network, AptosChains>,
wallet: Wallet | undefined,
) {
// The wallets do not handle Uint8Array serialization
Expand Down
47 changes: 21 additions & 26 deletions wormhole-connect/src/utils/wallet/cosmos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@ import {
import config from 'config';

import { ChainName, Context, ChainResourceMap } from 'sdklegacy';
import { SignRequestCosmos } from './types';
import { Network } from '@wormhole-foundation/sdk';
import {
CosmwasmUnsignedTransaction,
CosmwasmChains,
} from '@wormhole-foundation/sdk-cosmwasm';

const getCosmosWalletsEndpointsMap = () => {
const prepareMap = (map: ChainResourceMap) =>
Object.keys(map).reduce(
(acc, k) => {
const conf = config.chains[k as ChainName];
if (conf?.chainId && conf?.context === Context.COSMOS) {
acc[conf.chainId] = map[k as ChainName]!;
}
return acc;
},
{} as Record<string, string>,
);
Object.keys(map).reduce((acc, k) => {
const conf = config.chains[k as ChainName];
if (conf?.chainId && conf?.context === Context.COSMOS) {
acc[conf.chainId] = map[k as ChainName]!;
}
return acc;
}, {} as Record<string, string>);

const rpcs = prepareMap(config.rpcs);
const rests = prepareMap(config.rest);
Expand All @@ -32,26 +33,20 @@ const buildCosmosEvmWallets = () => {
const { rests, rpcs } = getCosmosWalletsEndpointsMap();
const wallets: CosmosEvmWallet[] = getEvmWallets(rpcs, rests);

return wallets.reduce(
(acc, w: CosmosEvmWallet) => {
acc[w.getName()] = w;
return acc;
},
{} as Record<string, Wallet>,
);
return wallets.reduce((acc, w: CosmosEvmWallet) => {
acc[w.getName()] = w;
return acc;
}, {} as Record<string, Wallet>);
};

const buildCosmosWallets = () => {
const { rests, rpcs } = getCosmosWalletsEndpointsMap();
const wallets: CosmosWallet[] = getWallets(rpcs, rests);

return wallets.reduce(
(acc, w: CosmosWallet) => {
acc[w.getName()] = w;
return acc;
},
{} as Record<string, Wallet>,
);
return wallets.reduce((acc, w: CosmosWallet) => {
acc[w.getName()] = w;
return acc;
}, {} as Record<string, Wallet>);
};

export const wallets = {
Expand All @@ -60,7 +55,7 @@ export const wallets = {
};

export async function signAndSendTransaction(
request: SignRequestCosmos,
request: CosmwasmUnsignedTransaction<Network, CosmwasmChains>,
wallet: Wallet | undefined,
) {
const cosmosWallet = wallet as CosmosWallet;
Expand Down
8 changes: 6 additions & 2 deletions wormhole-connect/src/utils/wallet/evm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import {
WalletConnectWallet,
} from '@kev1n-peters/wallet-aggregator-evm';

import { SignRequestEvm } from 'utils/wallet/types';
import {
EvmUnsignedTransaction,
EvmChains,
} from '@wormhole-foundation/sdk-evm';
import { Network } from '@wormhole-foundation/sdk';

import config from 'config';

Expand Down Expand Up @@ -44,7 +48,7 @@ export async function switchChain(w: Wallet, chainId: number | string) {
}

export async function signAndSendTransaction(
request: SignRequestEvm,
request: EvmUnsignedTransaction<Network, EvmChains>,
w: Wallet,
chainName: string,
options: any, // TODO ?!?!!?!?
Expand Down
58 changes: 39 additions & 19 deletions wormhole-connect/src/utils/wallet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,21 @@ import { Dispatch } from 'redux';
import { useEffect } from 'react';
import { useDispatch, useSelector } from 'react-redux';

import { SignRequest } from './types';
import { Network, Chain, UnsignedTransaction } from '@wormhole-foundation/sdk';

import {
EvmUnsignedTransaction,
EvmChains,
} from '@wormhole-foundation/sdk-evm';
import {
SuiUnsignedTransaction,
SuiChains,
} from '@wormhole-foundation/sdk-sui';
import {
AptosUnsignedTransaction,
AptosChains,
} from '@wormhole-foundation/sdk-aptos';
import { SolanaUnsignedTransaction } from '@wormhole-foundation/sdk-solana';

export enum TransferWallet {
SENDING = 'sending',
Expand Down Expand Up @@ -204,7 +218,7 @@ export const watchAsset = async (asset: AssetInfo, type: TransferWallet) => {

export const signAndSendTransaction = async (
chain: ChainName,
request: SignRequest,
request: UnsignedTransaction<Network, Chain>,
walletType: TransferWallet,
options: any = {},
): Promise<string> => {
Expand All @@ -215,30 +229,36 @@ export const signAndSendTransaction = async (
throw new Error('wallet is undefined');
}

if (chainConfig.context === Context.ETH && request.platform === 'Evm') {
if (chainConfig.context === Context.ETH) {
const { signAndSendTransaction } = await import('utils/wallet/evm');
const tx = await signAndSendTransaction(request, wallet, chain, options);
const tx = await signAndSendTransaction(
request as EvmUnsignedTransaction<Network, EvmChains>,
wallet,
chain,
options,
);
return tx;
} else if (
chainConfig.context === Context.SOLANA &&
request.platform === 'Solana'
) {
} else if (chainConfig.context === Context.SOLANA) {
const { signAndSendTransaction } = await import('utils/wallet/solana');
const tx = await signAndSendTransaction(request, wallet, options);
const tx = await signAndSendTransaction(
request as SolanaUnsignedTransaction<Network>,
wallet,
options,
);
return tx.id;
} else if (
chainConfig.context === Context.SUI &&
request.platform === 'Sui'
) {
} else if (chainConfig.context === Context.SUI) {
const { signAndSendTransaction } = await import('utils/wallet/sui');
const tx = await signAndSendTransaction(request, wallet);
const tx = await signAndSendTransaction(
request as SuiUnsignedTransaction<Network, SuiChains>,
wallet,
);
return tx.id;
} else if (
chainConfig.context === Context.APTOS &&
request.platform === 'Aptos'
) {
} else if (chainConfig.context === Context.APTOS) {
const { signAndSendTransaction } = await import('utils/wallet/aptos');
const tx = await signAndSendTransaction(request, wallet);
const tx = await signAndSendTransaction(
request as AptosUnsignedTransaction<Network, AptosChains>,
wallet,
);
return tx.id;
} else {
throw new Error('unimplemented');
Expand Down
Loading
Loading