Skip to content

Commit

Permalink
solana signing working, getting rid of SendResult
Browse files Browse the repository at this point in the history
  • Loading branch information
artursapek committed Jul 1, 2024
1 parent 4d005e5 commit 0714341
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 51 deletions.
27 changes: 22 additions & 5 deletions wormhole-connect/src/config/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,29 @@ export class SDKConverter {
}
}

toTokenIdV2(token: v1.TokenId | TokenConfigV1): v2.TokenId {
toTokenIdV2(
token: v1.TokenId | TokenConfigV1,
chain?: v1.ChainName,
): v2.TokenId {
if (this.isTokenConfigV1(token)) {
return v2.Wormhole.tokenId(
this.toChainV2(token.nativeChain),
token.tokenId?.address ?? 'native',
);
if (chain && chain != token.nativeChain) {
// Getting foreign address
let foreignAsset = token.foreignAssets?.[chain];

Check failure on line 74 in wormhole-connect/src/config/converter.ts

View workflow job for this annotation

GitHub Actions / lint

'foreignAsset' is never reassigned. Use 'const' instead
if (foreignAsset) {
return v2.Wormhole.tokenId(
this.toChainV2(chain),
foreignAsset.address,
);
} else {
throw new Error('no foreign asset');
}
} else {
// Getting native address
return v2.Wormhole.tokenId(
this.toChainV2(token.nativeChain),
token.tokenId?.address ?? 'native',
);
}
} else {
return v2.Wormhole.tokenId(this.toChainV2(token.chain), token.address);
}
Expand Down
9 changes: 6 additions & 3 deletions wormhole-connect/src/routes/sdkv2/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ export class SDKv2Route extends RouteAbstract {
async send(
sourceToken: TokenConfig,
amount: string,
fromChainV1: ChainName | ChainId,
fromChainV1: ChainName,
senderAddress: string,
toChainV1: ChainName | ChainId,
recipientAddress: string,
Expand All @@ -406,15 +406,18 @@ export class SDKv2Route extends RouteAbstract {
const fromChainV2 = await this.getV2ChainContext(fromChainV1);
const toChainV2 = await this.getV2ChainContext(toChainV1);

const sourceTokenV2 = config.sdkConverter.toTokenIdV2(sourceToken);
const sourceTokenV2 = config.sdkConverter.toTokenIdV2(
sourceToken,
fromChainV1,
);

const destTokenV2 = config.sdkConverter.getTokenIdV2ForKey(
destToken,
toChainV1,
config.tokens,
);

console.log(sourceTokenV2, destTokenV2);
console.log(sourceToken, sourceTokenV2, destTokenV2);

if (!destTokenV2) throw new Error(`Couldn't find destToken`);

Expand Down
3 changes: 3 additions & 0 deletions wormhole-connect/src/routes/sdkv2/signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export class SDKv2Signer<N extends Network, C extends Chain>
switch (platform) {
case 'Evm':
// TODO switch multi-provider to ethers 6
// and remove this ethers5-to-6 conversion
let serialized = ethers6.Transaction.from({

Check failure on line 81 in wormhole-connect/src/routes/sdkv2/signer.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected lexical declaration in case block

Check failure on line 81 in wormhole-connect/src/routes/sdkv2/signer.ts

View workflow job for this annotation

GitHub Actions / lint

'serialized' is never reassigned. Use 'const' instead
to: tx.transaction.to,
data: tx.transaction.data,
Expand All @@ -90,6 +91,8 @@ export class SDKv2Signer<N extends Network, C extends Chain>
data: tx5.data,
};
return unsignedTx as SendResult;
case 'Solana':
return tx.transaction.transaction;
default:
console.warn(`toSendResult is unimplemented for platform ${platform}`);
return tx as SendResult;
Expand Down
12 changes: 5 additions & 7 deletions wormhole-connect/src/utils/wallet/evm.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Wallet } from '@xlabs-libs/wallet-aggregator-core';
import { SendResult } from 'sdklegacy';
import { TransactionRequest } from '@ethersproject/abstract-provider';
import { Deferrable } from '@ethersproject/properties';
import {
EVMWallet,
InjectedWallet,
WalletConnectWallet,
} from '@kev1n-peters/wallet-aggregator-evm';

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

import config from 'config';

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

export async function signAndSendTransaction(
transaction: SendResult,
request: SignRequestEvm,
w: Wallet,
chainName: string,
options: any, // TODO ?!?!!?!?
Expand All @@ -53,9 +53,7 @@ export async function signAndSendTransaction(
const signer = config.wh.getSigner(chainName);
if (!signer) throw new Error('No signer found for chain' + chainName);

const tx = await signer.sendTransaction(
transaction as Deferrable<TransactionRequest>,
);
const tx = await signer.sendTransaction(request.transaction);
let result = await tx.wait();

// TODO move all this to ethers 6
Expand Down
48 changes: 25 additions & 23 deletions wormhole-connect/src/utils/wallet/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import {
ChainId,
ChainName,
Context,
SendResult,
ChainConfig,
} from 'sdklegacy';
import { ChainId, ChainName, Context, ChainConfig } from 'sdklegacy';
import {
NotSupported,
Wallet,
Expand All @@ -25,6 +19,8 @@ import { Dispatch } from 'redux';
import { useEffect } from 'react';
import { useDispatch, useSelector } from 'react-redux';

import { SignRequest } from './types';

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

export const signAndSendTransaction = async (
chain: ChainName,
transaction: SendResult,
request: SignRequest,
walletType: TransferWallet,
options: any = {},
): Promise<string> => {
Expand All @@ -219,45 +215,51 @@ export const signAndSendTransaction = async (
throw new Error('wallet is undefined');
}

if (chainConfig.context === Context.ETH && request.platform === 'Evm') {
const { signAndSendTransaction } = await import('utils/wallet/evm');
const tx = await signAndSendTransaction(request, wallet, chain, options);
return tx;
} else if (
chainConfig.context === Context.SOLANA &&
request.platform === 'Solana'
) {
const { signAndSendTransaction } = await import('utils/wallet/solana');
const tx = await signAndSendTransaction(request, wallet, options);
return tx.id;
} else {
throw new Error('unimplemented');
}

/*
switch (chainConfig.context) {
case Context.ETH: {
const { signAndSendTransaction } = await import('utils/wallet/evm');
const tx = await signAndSendTransaction(
transaction,
wallet,
chain,
options,
);
return tx;
}
case Context.SOLANA: {
const { signAndSendTransaction } = await import('utils/wallet/solana');
const tx = await signAndSendTransaction(transaction, wallet, options);
return tx.id;
}
case Context.SUI: {
const { signAndSendTransaction } = await import('utils/wallet/sui');
const tx = await signAndSendTransaction(transaction, wallet);
const tx = await signAndSendTransaction(request, wallet);
return tx.id;
}
case Context.APTOS: {
const { signAndSendTransaction } = await import('utils/wallet/aptos');
const tx = await signAndSendTransaction(transaction, wallet);
const tx = await signAndSendTransaction(request, wallet);
return tx.id;
}
case Context.SEI: {
const { signAndSendTransaction } = await import('utils/wallet/sei');
const tx = await signAndSendTransaction(transaction, wallet);
const tx = await signAndSendTransaction(request, wallet);
return tx.id;
}
case Context.COSMOS: {
const { signAndSendTransaction } = await import('utils/wallet/cosmos');
const tx = await signAndSendTransaction(transaction, wallet);
const tx = await signAndSendTransaction(request, wallet);
return tx.id;
}
default:
throw new Error(`Invalid context ${chainConfig.context}`);
}
*/
};

export const postVaa = async (
Expand Down
25 changes: 12 additions & 13 deletions wormhole-connect/src/utils/wallet/solana.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { SendResult } from 'sdklegacy';
import { WalletAdapterNetwork as SolanaNetwork } from '@solana/wallet-adapter-base';

import { Wallet } from '@xlabs-libs/wallet-aggregator-core';
Expand All @@ -12,12 +11,9 @@ import {
WalletConnectWalletAdapter,
} from '@solana/wallet-adapter-wallets';

import {
clusterApiUrl,
ConfirmOptions,
Connection,
Transaction,
} from '@solana/web3.js';
import { clusterApiUrl, ConfirmOptions, Connection } from '@solana/web3.js';

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

import {
SolanaWallet,
Expand All @@ -34,10 +30,13 @@ export function fetchOptions() {
const connection = new Connection(config.rpcs.solana || clusterApiUrl(tag));

return {
...getSolanaStandardWallets(connection).reduce((acc, w) => {
acc[getWalletName(w)] = w;
return acc;
}, {} as Record<string, Wallet>),
...getSolanaStandardWallets(connection).reduce(
(acc, w) => {
acc[getWalletName(w)] = w;
return acc;
},
{} as Record<string, Wallet>,
),
bitget: new SolanaWallet(new BitgetWalletAdapter(), connection),
clover: new SolanaWallet(new CloverWalletAdapter(), connection),
coin98: new SolanaWallet(new Coin98WalletAdapter(), connection),
Expand All @@ -63,7 +62,7 @@ export function fetchOptions() {
}

export async function signAndSendTransaction(
transaction: SendResult,
request: SignRequestSolana,
wallet: Wallet | undefined,
options?: ConfirmOptions,
) {
Expand All @@ -72,7 +71,7 @@ export async function signAndSendTransaction(
}

return await (wallet as SolanaWallet).signAndSendTransaction({
transaction: transaction as Transaction,
transaction: request.transaction,
options,
});
}
17 changes: 17 additions & 0 deletions wormhole-connect/src/utils/wallet/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Transaction as SolanaTransaction } from '@solana/web3.js';

import { TransactionRequest as EvmTransactionRequest } from '@ethersproject/abstract-provider';
import { Deferrable } from '@ethersproject/properties';

// These types use sdkv2's Platform type for platform
export interface SignRequestEvm {
platform: 'Evm';
transaction: Deferrable<EvmTransactionRequest>;
}

export interface SignRequestSolana {
platform: 'Solana';
transaction: SolanaTransaction;
}

export type SignRequest = SignRequestEvm | SignRequestSolana;

0 comments on commit 0714341

Please sign in to comment.