Skip to content

Commit 7fff1b8

Browse files
authored
add getRecipientAddress to RelayerAbstract (#8)
1 parent a1cd01f commit 7fff1b8

File tree

8 files changed

+92
-107
lines changed

8 files changed

+92
-107
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
node_modules/
2+
.env

packages/aptos/src/context.ts

+13-17
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515
hexToUint8Array,
1616
parseTokenTransferPayload,
1717
TokenBridgeAbstract,
18-
MAINNET_CHAINS,
1918
} from '@wormhole-foundation/connect-sdk';
2019
import { AptosContracts } from './contracts';
2120
import {
@@ -61,22 +60,19 @@ export class AptosContext extends TokenBridgeAbstract<Types.EntryFunctionPayload
6160
const destContext = this.wormhole.getContext(recipientChain);
6261
const recipientChainId = this.wormhole.toChainId(recipientChain);
6362

64-
let recipientAccount = recipientAddress;
65-
// get token account for solana
66-
if (recipientChainId === MAINNET_CHAINS.solana) {
67-
let tokenId = token;
68-
if (token === NATIVE) {
69-
tokenId = {
70-
address: APTOS_COIN,
71-
chain: 'aptos',
72-
};
73-
}
74-
recipientAccount = await this.wormhole.getSolanaRecipientAddress(
75-
recipientChain,
76-
tokenId as TokenId,
77-
recipientAddress,
78-
);
79-
}
63+
const tokenId: TokenId =
64+
token === NATIVE
65+
? {
66+
address: APTOS_COIN,
67+
chain: 'aptos',
68+
}
69+
: token;
70+
71+
const recipientAccount = await destContext.getRecipientAddress(
72+
tokenId,
73+
recipientAddress,
74+
);
75+
8076
const formattedRecipientAccount = arrayify(
8177
destContext.formatAddress(recipientAccount),
8278
);

packages/evm/src/context.ts

+12-18
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ import {
2727
parseVaa,
2828
RelayerAbstract,
2929
createNonce,
30-
MAINNET_CHAINS,
31-
Network,
3230
} from '@wormhole-foundation/connect-sdk';
3331
import { EvmContracts } from './contracts';
3432

@@ -190,22 +188,18 @@ export class EvmContext extends RelayerAbstract<ethers.ContractReceipt> {
190188
const amountBN = ethers.BigNumber.from(amount);
191189
const bridge = this.contracts.mustGetBridge(sendingChain);
192190

193-
let recipientAccount = recipientAddress;
194-
// get token account for solana
195-
if (recipientChainId === MAINNET_CHAINS.solana) {
196-
let tokenId = token;
197-
if (token === NATIVE) {
198-
tokenId = {
199-
address: await bridge.WETH(),
200-
chain: sendingChainName,
201-
};
202-
}
203-
recipientAccount = await this.wormhole.getSolanaRecipientAddress(
204-
recipientChain,
205-
tokenId as TokenId,
206-
recipientAddress,
207-
);
208-
}
191+
const tokenID: TokenId =
192+
token === NATIVE
193+
? {
194+
address: await bridge.WETH(),
195+
chain: sendingChainName,
196+
}
197+
: token;
198+
199+
const recipientAccount = await destContext.getRecipientAddress(
200+
tokenID,
201+
recipientAddress,
202+
);
209203

210204
if (token === NATIVE) {
211205
// sending native ETH

packages/sdk-core/src/abstracts/tokenBridge.ts

+15
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,21 @@ export abstract class TokenBridgeAbstract<TransactionResult> {
8888
*/
8989
abstract parseAddress(address: any): string;
9090

91+
/**
92+
* Gets the recipient address on the recieving chain.
93+
* Note: this is a NoOp for chains other than Solana with its ATA
94+
*
95+
* @param tokenId The token identifier (native chain/address)
96+
* @param recipientAddress The address of the receiver
97+
* @returns The relayer fee
98+
*/
99+
async getRecipientAddress(
100+
tokenId: TokenId,
101+
recipientAddress: string,
102+
): Promise<string> {
103+
return recipientAddress;
104+
}
105+
91106
/**
92107
* Format a token address to 32-bytes universal address, which can be utilized by the Wormhole contracts
93108
*

packages/sdk-core/src/wormhole.ts

+10-28
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ import { SeiAbstract } from './abstracts/contexts/sei';
4949
* 'ethereum', // sending chain
5050
* '0x789...', // sender address
5151
* 'moonbeam', // destination chain
52-
* '0x789..., // recipient address on destination chain
52+
* '0x789...', // recipient address on destination chain
5353
* )
5454
*/
5555
export class Wormhole extends MultiProvider<Domain> {
@@ -159,31 +159,9 @@ export class Wormhole extends MultiProvider<Domain> {
159159
return context;
160160
}
161161

162-
async getSolanaRecipientAddress(
163-
recipientChain: ChainName | ChainId,
164-
tokenId: TokenId,
165-
recipientAddress: string,
166-
) {
167-
const recipientChainId = this.toChainId(recipientChain);
168-
if (recipientChainId === MAINNET_CHAINS.solana) {
169-
let solanaContext: SolanaAbstract;
170-
try {
171-
solanaContext = this.getContext(MAINNET_CHAINS.solana) as any;
172-
} catch (_) {
173-
throw new Error(
174-
'You attempted to send a transfer to Solana, but the Solana context is not registered. You must import SolanaContext from @wormhole-foundation/connect-sdk-solana and pass it in to the Wormhole class constructor',
175-
);
176-
}
177-
const account = await solanaContext.getAssociatedTokenAddress(
178-
tokenId as TokenId,
179-
recipientAddress,
180-
);
181-
return account.toString();
182-
}
183-
}
184-
185162
/**
186-
* Fetches the address for a token representation on any chain (These are the Wormhole token addresses, not necessarily the cannonical version of that token)
163+
* Fetches the address for a token representation on any chain
164+
* These are the Wormhole token addresses, not necessarily the cannonical version of that token
187165
*
188166
* @param tokenId The Token ID (chain/address)
189167
* @param chain The chain name or id
@@ -198,7 +176,8 @@ export class Wormhole extends MultiProvider<Domain> {
198176
}
199177

200178
/**
201-
* Fetches the address for a token representation on any chain (These are the Wormhole token addresses, not necessarily the cannonical version of that token)
179+
* Fetches the address for a token representation on any chain
180+
* These are the Wormhole token addresses, not necessarily the cannonical version of that token
202181
*
203182
* @param tokenId The Token ID (chain/address)
204183
* @param chain The chain name or id
@@ -296,7 +275,9 @@ export class Wormhole extends MultiProvider<Domain> {
296275
seiContext = this.getContext(MAINNET_CHAINS.solana) as any;
297276
} catch (_) {
298277
throw new Error(
299-
'You attempted to send a transfer to Sei, but the Sei context is not registered. You must import SeiContext from @wormhole-foundation/connect-sdk-sei and pass it in to the Wormhole class constructor',
278+
'You attempted to send a transfer to Sei, but the Sei context is not registered. ' +
279+
'You must import SeiContext from @wormhole-foundation/connect-sdk-sei and pass it ' +
280+
'in to the Wormhole class constructor',
300281
);
301282
}
302283
const { payload: seiPayload, receiver } =
@@ -431,7 +412,8 @@ export class Wormhole extends MultiProvider<Domain> {
431412
}
432413

433414
/**
434-
* Gets a VAA from the API or Guardian RPC, finality must be met before the VAA will be available. See {@link ChainConfig.finalityThreshold | finalityThreshold} on {@link MAINNET_CONFIG | the config}
415+
* Gets a VAA from the API or Guardian RPC, finality must be met before the VAA will be available.
416+
* See {@link ChainConfig.finalityThreshold | finalityThreshold} on {@link MAINNET_CONFIG | the config}
435417
*
436418
* @param msg The MessageIdentifier used to fetch the VAA
437419
* @returns The ParsedVAA if available

packages/sei/src/context.ts

+4-10
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ import {
4141
parseTokenTransferPayload,
4242
parseVaa,
4343
NATIVE,
44-
Network,
4544
SeiAbstract,
4645
} from '@wormhole-foundation/connect-sdk';
4746
import { SeiContracts } from './contracts';
@@ -173,15 +172,10 @@ export class SeiContext
173172
const destContext = this.wormhole.getContext(recipientChain);
174173
const targetChain = this.wormhole.toChainId(recipientChain);
175174

176-
let recipientAccount = recipientAddress;
177-
// get token account for solana
178-
if (targetChain === MAINNET_CHAINS.solana) {
179-
recipientAccount = await this.wormhole.getSolanaRecipientAddress(
180-
recipientChain,
181-
token as TokenId,
182-
recipientAddress,
183-
);
184-
}
175+
const recipientAccount = await destContext.getRecipientAddress(
176+
token,
177+
recipientAddress,
178+
);
185179

186180
const targetAddress = Buffer.from(
187181
destContext.formatAddress(recipientAccount),

packages/solana/src/context.ts

+11
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,17 @@ export class SolanaContext
182182
);
183183
}
184184

185+
async getRecipientAddress(
186+
tokenId: TokenId,
187+
recipientAddress: string,
188+
): Promise<string> {
189+
const account = await this.getAssociatedTokenAddress(
190+
tokenId,
191+
recipientAddress,
192+
);
193+
return account.toString();
194+
}
195+
185196
/**
186197
* Gets the Associate Token Account
187198
*

packages/sui/src/context.ts

+26-34
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ import {
2222
parseTokenTransferPayload,
2323
Wormhole,
2424
RelayerAbstract,
25-
MAINNET_CHAINS,
26-
Network,
2725
} from '@wormhole-foundation/connect-sdk';
2826

2927
import { SuiContracts } from './contracts';
@@ -87,22 +85,19 @@ export class SuiContext extends RelayerAbstract<TransactionBlock> {
8785
const relayerFeeBigInt = relayerFee ? BigInt(relayerFee) : undefined;
8886
const amountBigInt = BigNumber.from(amount).toBigInt();
8987

90-
let recipientAccount = recipientAddress;
91-
// get token account for solana
92-
if (recipientChainId === MAINNET_CHAINS.solana) {
93-
let tokenId = token;
94-
if (token === NATIVE) {
95-
tokenId = {
96-
address: SUI_TYPE_ARG,
97-
chain: 'sui',
98-
};
99-
}
100-
recipientAccount = await this.wormhole.getSolanaRecipientAddress(
101-
recipientChain,
102-
tokenId as TokenId,
103-
recipientAddress,
104-
);
105-
}
88+
const tokenId: TokenId =
89+
token === NATIVE
90+
? {
91+
address: SUI_TYPE_ARG,
92+
chain: 'sui',
93+
}
94+
: token;
95+
96+
const recipientAccount = await destContext.getRecipientAddress(
97+
tokenId,
98+
recipientAddress,
99+
);
100+
106101
const formattedRecipientAccount = arrayify(
107102
destContext.formatAddress(recipientAccount),
108103
);
@@ -408,22 +403,19 @@ export class SuiContext extends RelayerAbstract<TransactionBlock> {
408403
const amountBigInt = BigNumber.from(amount).toBigInt();
409404
const toNativeTokenBigInt = BigNumber.from(toNativeToken).toBigInt();
410405

411-
let recipientAccount = recipientAddress;
412-
// get token account for solana
413-
if (recipientChainId === MAINNET_CHAINS.solana) {
414-
let tokenId = token;
415-
if (token === NATIVE) {
416-
tokenId = {
417-
address: SUI_TYPE_ARG,
418-
chain: 'sui',
419-
};
420-
}
421-
recipientAccount = await this.wormhole.getSolanaRecipientAddress(
422-
recipientChain,
423-
tokenId as TokenId,
424-
recipientAddress,
425-
);
426-
}
406+
const tokenId: TokenId =
407+
token === NATIVE
408+
? {
409+
address: SUI_TYPE_ARG,
410+
chain: 'sui',
411+
}
412+
: token;
413+
414+
const recipientAccount = await destContext.getRecipientAddress(
415+
tokenId,
416+
recipientAddress,
417+
);
418+
427419
const formattedRecipientAccount = `0x${Buffer.from(
428420
arrayify(destContext.formatAddress(recipientAccount)),
429421
).toString('hex')}`;

0 commit comments

Comments
 (0)