Skip to content

Commit f1e0ed2

Browse files
authored
Example updater script (#388)
1 parent 8fc0802 commit f1e0ed2

18 files changed

+608
-239
lines changed

README.md

+388-117
Large diffs are not rendered by default.

__tests__/tilt/helpers/helpers.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414
//
1515
// import { ETH_PRIVATE_KEY, SOLANA_PRIVATE_KEY, TERRA_PRIVATE_KEY } from "./consts";
1616
//
17-
// export interface TransferStuff {
17+
// export interface SignerStuff {
1818
// chain: ChainContext<Platform>;
1919
// signer: Signer;
2020
// address: ChainAddress;
2121
// }
2222
//
23-
// export async function getStuff(
23+
// export async function getSigner(
2424
// chain: ChainContext<Platform>,
25-
// ): Promise<TransferStuff> {
25+
// ): Promise<SignerStuff> {
2626
// let signer: Signer;
2727
// switch (chain.platform.platform) {
2828
// case "Solana":

__tests__/tilt/tokenbridge.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
//
2121
// import { expect, jest, describe, test } from '@jest/globals';
2222
//
23-
// import { TEST_ERC20, TEST_SOLANA_TOKEN, getStuff } from './helpers';
23+
// import { TEST_ERC20, TEST_SOLANA_TOKEN, getSigner } from './helpers';
2424
//
2525
//
2626
// jest.setTimeout(10 * 60 * 1000)
@@ -60,11 +60,11 @@
6060
// let dstBalanceToken: NativeAddress<Platform>;
6161
//
6262
// beforeAll(async () => {
63-
// const srcStuff = await getStuff(src);
63+
// const srcStuff = await getSigner(src);
6464
// srcSigner = srcStuff.signer
6565
// srcAcct = srcStuff.address
6666
//
67-
// const dstStuff = await getStuff(dst)
67+
// const dstStuff = await getSigner(dst)
6868
// dstSigner = dstStuff.signer
6969
// dstAcct = dstStuff.address
7070
//

examples/src/cctp.ts

+18-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
} from "@wormhole-foundation/sdk";
1010
import { evm } from "@wormhole-foundation/sdk/evm";
1111
import { solana } from "@wormhole-foundation/sdk/solana";
12-
import { TransferStuff, getStuff, waitForRelay } from "./helpers";
12+
import { SignerStuff, getSigner, waitForRelay } from "./helpers";
1313

1414
/*
1515
Notes:
@@ -29,8 +29,8 @@ AutoRelayer takes a 0.1usdc fee when xfering to any chain beside goerli, which i
2929

3030
// Get signer from local key but anything that implements
3131
// Signer interface (e.g. wrapper around web wallet) should work
32-
const source = await getStuff(sendChain);
33-
const destination = await getStuff(rcvChain);
32+
const source = await getSigner(sendChain);
33+
const destination = await getSigner(rcvChain);
3434

3535
// 6 decimals for USDC (except for bsc, so check decimals before using this)
3636
const amt = amount.units(amount.parse("0.2", 6));
@@ -66,20 +66,27 @@ AutoRelayer takes a 0.1usdc fee when xfering to any chain beside goerli, which i
6666

6767
async function cctpTransfer<N extends Network>(
6868
wh: Wormhole<N>,
69-
src: TransferStuff<N, Chain>,
70-
dst: TransferStuff<N, Chain>,
69+
src: SignerStuff<N, Chain>,
70+
dst: SignerStuff<N, Chain>,
7171
req: {
7272
amount: bigint;
7373
automatic: boolean;
7474
nativeGas?: bigint;
7575
},
7676
) {
77+
// EXAMPLE_CCTP_TRANSFER
7778
const xfer = await wh.circleTransfer(
79+
// amount as bigint (base units)
7880
req.amount,
81+
// sender chain/address
7982
src.address,
83+
// receiver chain/address
8084
dst.address,
85+
// automatic delivery boolean
8186
req.automatic,
87+
// payload to be sent with the transfer
8288
undefined,
89+
// If automatic, native gas can be requested to be sent to the receiver
8390
req.nativeGas,
8491
);
8592
console.log(xfer);
@@ -101,20 +108,24 @@ async function cctpTransfer<N extends Network>(
101108
return;
102109
}
103110

111+
// Note: Depending on chain finality, this timeout may need to be increased.
112+
// See https://developers.circle.com/stablecoin/docs/cctp-technical-reference#mainnet for more
104113
console.log("Waiting for Attestation");
105-
const attestIds = await xfer.fetchAttestation();
114+
const attestIds = await xfer.fetchAttestation(60_000);
106115
console.log(`Got Attestation: `, attestIds);
107116

108117
console.log("Completing Transfer");
109118
const dstTxids = await xfer.completeTransfer(dst.signer);
110119
console.log(`Completed Transfer: `, dstTxids);
120+
// EXAMPLE_CCTP_TRANSFER
111121
}
112122

113123
export async function completeTransfer(
114124
wh: Wormhole<Network>,
115125
txid: TransactionId,
116126
signer: Signer,
117127
): Promise<void> {
128+
// EXAMPLE_RECOVER_TRANSFER
118129
// Rebuild the transfer from the source txid
119130
const xfer = await CircleTransfer.from(wh, txid);
120131

@@ -123,4 +134,5 @@ export async function completeTransfer(
123134

124135
const dstTxIds = await xfer.completeTransfer(signer);
125136
console.log("Completed transfer: ", dstTxIds);
137+
// EXAMPLE_RECOVER_TRANSFER
126138
}

examples/src/config.ts

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Wormhole } from "@wormhole-foundation/sdk";
22
import { solana } from "@wormhole-foundation/sdk/solana";
33

44
(async function () {
5+
// EXAMPLE_CONFIG_OVERRIDE
56
// Pass a partial WormholeConfig object to override specific
67
// fields in the default config
78
const wh = new Wormhole("Testnet", [solana.Platform], {
@@ -14,6 +15,7 @@ import { solana } from "@wormhole-foundation/sdk/solana";
1415
},
1516
},
1617
});
18+
// EXAMPLE_CONFIG_OVERRIDE
1719

1820
console.log(wh.config.chains.Solana);
1921
})();

examples/src/cosmos.ts

+16-10
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { cosmwasm } from "@wormhole-foundation/sdk/cosmwasm";
1313
import { evm } from "@wormhole-foundation/sdk/evm";
1414
import { solana } from "@wormhole-foundation/sdk/solana";
1515

16-
import { TransferStuff, getStuff } from "./helpers";
16+
import { SignerStuff, getSigner } from "./helpers";
1717

1818
// We're going to transfer into, around, and out of the Cosmos ecosystem
1919
// First on Avalanche, transparently through gateway and over IBC to Cosmoshub
@@ -44,9 +44,9 @@ import { TransferStuff, getStuff } from "./helpers";
4444

4545
// Get signer from local key but anything that implements
4646
// Signer interface (e.g. wrapper around web wallet) should work
47-
const leg1 = await getStuff(external);
48-
const leg2 = await getStuff(cosmos1);
49-
const leg3 = await getStuff(cosmos2);
47+
const leg1 = await getSigner(external);
48+
const leg2 = await getSigner(cosmos1);
49+
const leg3 = await getSigner(cosmos2);
5050

5151
// we'll use the native token on the source chain
5252
const token: TokenId = Wormhole.tokenId(external.chain, "native");
@@ -112,9 +112,10 @@ async function transferIntoCosmos(
112112
wh: Wormhole<Network>,
113113
token: TokenId,
114114
amount: bigint,
115-
src: TransferStuff<Network, Chain>,
116-
dst: TransferStuff<Network, Chain>,
115+
src: SignerStuff<Network, Chain>,
116+
dst: SignerStuff<Network, Chain>,
117117
): Promise<GatewayTransfer<Network>> {
118+
// EXAMPLE_GATEWAY_INBOUND
118119
console.log(
119120
`Beginning transfer into Cosmos from ${src.chain.chain}:${src.address.address.toString()} to ${
120121
dst.chain.chain
@@ -134,6 +135,7 @@ async function transferIntoCosmos(
134135

135136
const attests = await xfer.fetchAttestation(600_000);
136137
console.log("Got Attestations", attests);
138+
// EXAMPLE_GATEWAY_INBOUND
137139

138140
return xfer;
139141
}
@@ -142,9 +144,10 @@ async function transferBetweenCosmos<N extends Network>(
142144
wh: Wormhole<N>,
143145
token: TokenId,
144146
amount: bigint,
145-
src: TransferStuff<N, Chain>,
146-
dst: TransferStuff<N, Chain>,
147+
src: SignerStuff<N, Chain>,
148+
dst: SignerStuff<N, Chain>,
147149
): Promise<GatewayTransfer<N>> {
150+
// EXAMPLE_GATEWAY_INTERCOSMOS
148151
console.log(
149152
`Beginning transfer within cosmos from ${
150153
src.chain.chain
@@ -164,6 +167,7 @@ async function transferBetweenCosmos<N extends Network>(
164167

165168
const attests = await xfer.fetchAttestation(60_000);
166169
console.log("Got attests: ", attests);
170+
// EXAMPLE_GATEWAY_INTERCOSMOS
167171

168172
return xfer;
169173
}
@@ -172,9 +176,10 @@ async function transferOutOfCosmos<N extends Network>(
172176
wh: Wormhole<N>,
173177
token: TokenId,
174178
amount: bigint,
175-
src: TransferStuff<N, Chain>,
176-
dst: TransferStuff<N, Chain>,
179+
src: SignerStuff<N, Chain>,
180+
dst: SignerStuff<N, Chain>,
177181
): Promise<GatewayTransfer<N>> {
182+
// EXAMPLE_GATEWAY_OUTBOUND
178183
console.log(
179184
`Beginning transfer out of cosmos from ${
180185
src.chain.chain
@@ -197,6 +202,7 @@ async function transferOutOfCosmos<N extends Network>(
197202
// Since we're leaving cosmos, this is required to complete the transfer
198203
const dstTxIds = await xfer.completeTransfer(dst.signer);
199204
console.log("Completed transfer on destination chain", dstTxIds);
205+
// EXAMPLE_GATEWAY_OUTBOUND
200206

201207
return xfer;
202208
}

examples/src/createWrapped.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { evm } from "@wormhole-foundation/sdk/evm";
55
import { solana } from "@wormhole-foundation/sdk/solana";
66

77
import { inspect } from "util";
8-
import { getStuff } from "./helpers";
8+
import { getSigner } from "./helpers";
99

1010
(async function () {
1111
const wh = new Wormhole("Testnet", [evm.Platform, solana.Platform, algorand.Platform]);
@@ -22,7 +22,7 @@ import { getStuff } from "./helpers";
2222

2323
// grab context and signer
2424
const origChain = wh.getChain(token.chain);
25-
const { signer: origSigner } = await getStuff(origChain);
25+
const { signer: origSigner } = await getSigner(origChain);
2626

2727
// Note: if the VAA is not produced before the attempt to retrieve it times out
2828
// you should set this value to the txid logged in the previous run
@@ -61,7 +61,7 @@ import { getStuff } from "./helpers";
6161
// destination chain
6262
const chain = "Algorand";
6363
const destChain = wh.getChain(chain);
64-
const { signer } = await getStuff(destChain);
64+
const { signer } = await getSigner(destChain);
6565

6666
// grab a ref to the token bridge
6767
const tb = await destChain.getTokenBridge();

examples/src/helpers/helpers.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ function getEnv(key: string): string {
3636
return val;
3737
}
3838

39-
export interface TransferStuff<N extends Network, C extends Chain> {
39+
export interface SignerStuff<N extends Network, C extends Chain> {
4040
chain: ChainContext<N, C>;
4141
signer: Signer<N, C>;
4242
address: ChainAddress<C>;
4343
}
4444

45-
export async function getStuff<N extends Network, C extends Chain>(
45+
export async function getSigner<N extends Network, C extends Chain>(
4646
chain: ChainContext<N, C>,
47-
): Promise<TransferStuff<N, C>> {
47+
): Promise<SignerStuff<N, C>> {
4848
let signer: Signer;
4949
const platform = chain.platform.utils()._platform;
5050
switch (platform) {

examples/src/index.ts

+25-14
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,66 @@
1-
import { CONFIG, Wormhole, amount, api, signSendWait } from "@wormhole-foundation/sdk";
2-
1+
// EXAMPLE_IMPORTS
2+
import { Wormhole } from "@wormhole-foundation/sdk";
33
import { algorand } from "@wormhole-foundation/sdk/algorand";
44
import { cosmwasm } from "@wormhole-foundation/sdk/cosmwasm";
55
import { evm } from "@wormhole-foundation/sdk/evm";
66
import { solana } from "@wormhole-foundation/sdk/solana";
77
import { sui } from "@wormhole-foundation/sdk/sui";
8+
// EXAMPLE_IMPORTS
89

9-
import { getStuff } from "./helpers";
10+
import { amount, signSendWait } from "@wormhole-foundation/sdk";
11+
import { getSigner } from "./helpers";
1012

1113
(async function () {
12-
// Setup
13-
const wh = new Wormhole("Mainnet", [
14+
// EXAMPLE_WORMHOLE_INIT
15+
const wh = new Wormhole("Testnet", [
1416
evm.Platform,
1517
solana.Platform,
1618
sui.Platform,
1719
algorand.Platform,
1820
cosmwasm.Platform,
1921
]);
22+
// EXAMPLE_WORMHOLE_INIT
23+
24+
// EXAMPLE_WORMHOLE_CHAIN
25+
// Grab a ChainContext object from our configured Wormhole instance
26+
const ctx = wh.getChain("Solana");
27+
// EXAMPLE_WORMHOLE_CHAIN
2028

21-
const snd = wh.getChain("Sui");
2229
const rcv = wh.getChain("Algorand");
2330

24-
const sender = await getStuff(snd);
25-
const receiver = await getStuff(rcv);
31+
const sender = await getSigner(ctx);
32+
const receiver = await getSigner(rcv);
2633

2734
// Get a Token Bridge contract client on the source
28-
const sndTb = await snd.getTokenBridge();
35+
const sndTb = await ctx.getTokenBridge();
2936

3037
// Create a transaction stream for transfers
3138
const transfer = sndTb.transfer(
3239
sender.address.address,
3340
receiver.address,
3441
"native",
35-
amount.units(amount.parse("0.1", snd.config.nativeTokenDecimals)),
42+
amount.units(amount.parse("0.1", ctx.config.nativeTokenDecimals)),
3643
);
3744

3845
// Sign and send the transaction
39-
const txids = await signSendWait(snd, transfer, sender.signer);
46+
const txids = await signSendWait(ctx, transfer, sender.signer);
4047
console.log("Sent: ", txids);
4148

4249
// Get the wormhole message id from the transaction
43-
const [whm] = await snd.parseTransaction(txids[txids.length - 1]!.txid);
50+
const [whm] = await ctx.parseTransaction(txids[txids.length - 1]!.txid);
4451
console.log("Wormhole Messages: ", whm);
4552

53+
// EXAMPLE_WORMHOLE_VAA
4654
// Get the VAA from the wormhole message id
47-
const vaa = await api.getVaaWithRetry(
48-
CONFIG["Testnet"].api,
55+
const vaa = await wh.getVaa(
56+
// Wormhole Message ID
4957
whm!,
58+
// Protocol:Payload name to use for decoding the VAA payload
5059
"TokenBridge:Transfer",
60+
// Timeout in milliseconds, depending on the chain and network, the VAA may take some time to be available
5161
60_000,
5262
);
63+
// EXAMPLE_WORMHOLE_VAA
5364

5465
// Now get the token bridge on the redeem side
5566
const rcvTb = await rcv.getTokenBridge();

0 commit comments

Comments
 (0)