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

Add Wormhole SDK Page #9

Merged
merged 76 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
76 commits
Select commit Hold shift + click to select a range
9a56529
initial setup
themacexpert Aug 2, 2024
b0a3717
grammarly overhaul
themacexpert Aug 2, 2024
e87feec
prettify all code snippets
themacexpert Aug 2, 2024
a0f1336
in progress
themacexpert Aug 2, 2024
0697e78
additions
themacexpert Aug 2, 2024
fb1e5a6
revisions
themacexpert Aug 3, 2024
12f9b02
Update build/build-multichain-applications/wormhole-sdk.md
themacexpert Aug 6, 2024
7127c26
Update build/build-multichain-applications/wormhole-sdk.md
themacexpert Aug 6, 2024
ae8d03d
Update build/build-multichain-applications/wormhole-sdk.md
themacexpert Aug 6, 2024
ac2e106
Update build/build-multichain-applications/wormhole-sdk.md
themacexpert Aug 6, 2024
d2cc237
Update build/build-multichain-applications/wormhole-sdk.md
themacexpert Aug 6, 2024
28acee0
Update build/build-multichain-applications/wormhole-sdk.md
themacexpert Aug 6, 2024
78ae923
Update build/build-multichain-applications/wormhole-sdk.md
themacexpert Aug 6, 2024
759d1e1
Update build/build-multichain-applications/wormhole-sdk.md
themacexpert Aug 6, 2024
127533e
change warning to admonition
themacexpert Aug 6, 2024
7faabcf
rev
themacexpert Aug 6, 2024
2aa20eb
rev'
themacexpert Aug 6, 2024
e1a8c30
revisions
themacexpert Aug 6, 2024
06d7ab9
Update build/build-multichain-applications/wormhole-sdk.md
themacexpert Aug 8, 2024
c8b61f1
Update build/build-multichain-applications/wormhole-sdk.md
themacexpert Aug 8, 2024
1aab200
Update build/build-multichain-applications/wormhole-sdk.md
themacexpert Aug 8, 2024
9c85726
Update build/build-multichain-applications/wormhole-sdk.md
themacexpert Aug 8, 2024
3e02832
import eco
themacexpert Aug 8, 2024
89a419a
run prettier
themacexpert Aug 8, 2024
e016e9b
Merge branch 'themacexpert/buildMulti' of https://github.com/wormhole…
themacexpert Aug 8, 2024
fefa23b
rev
themacexpert Aug 9, 2024
991f5fa
Update build/build-multichain-applications/wormhole-sdk.md
themacexpert Aug 9, 2024
172c19b
Update build/build-multichain-applications/wormhole-sdk.md
themacexpert Aug 9, 2024
ba6a051
Update build/build-multichain-applications/wormhole-sdk.md
themacexpert Aug 9, 2024
fb2d561
Update build/build-multichain-applications/wormhole-sdk.md
themacexpert Aug 9, 2024
c046980
Update .snippets/code/build/build-multichain-applications/wormhole-sd…
themacexpert Aug 9, 2024
49cd0d2
Update build/build-multichain-applications/wormhole-sdk.md
themacexpert Aug 9, 2024
844f2f7
Update build/build-multichain-applications/wormhole-sdk.md
themacexpert Aug 9, 2024
20eb5e3
Update build/build-multichain-applications/wormhole-sdk.md
themacexpert Aug 9, 2024
3b030b7
Update build/build-multichain-applications/wormhole-sdk.md
themacexpert Aug 9, 2024
0236cc5
rev
themacexpert Aug 9, 2024
8a34a4f
Merge branch 'themacexpert/buildMulti' of https://github.com/wormhole…
themacexpert Aug 9, 2024
dc2f5ee
rev
themacexpert Aug 9, 2024
17ddf5b
rev
themacexpert Aug 9, 2024
12f3ed2
rev
themacexpert Aug 9, 2024
13452e0
rev
themacexpert Aug 9, 2024
eadd665
rev
themacexpert Aug 9, 2024
bb1eb4d
rev
themacexpert Aug 9, 2024
ac5b8ab
rev
themacexpert Aug 9, 2024
be232ca
rev
themacexpert Aug 9, 2024
99c76dc
Update build/build-multichain-applications/wormhole-sdk.md
themacexpert Aug 9, 2024
baf4b3a
Update build/build-multichain-applications/wormhole-sdk.md
themacexpert Aug 9, 2024
38247e0
rev
themacexpert Aug 9, 2024
ea35466
Merge branch 'themacexpert/buildMulti' of https://github.com/wormhole…
themacexpert Aug 9, 2024
22460ba
rev
themacexpert Aug 9, 2024
c1578d7
rev
themacexpert Aug 9, 2024
72735bc
rev
themacexpert Aug 9, 2024
f0c3b0d
rev
themacexpert Aug 10, 2024
203d23d
rev
themacexpert Aug 10, 2024
ca4f42d
rev
themacexpert Aug 10, 2024
07aa1cd
Update .snippets/code/build/build-apps/wormhole-sdk/cctp.ts
themacexpert Aug 13, 2024
ec1fe0b
comment capitalization
themacexpert Aug 13, 2024
d7834b2
rev
themacexpert Aug 13, 2024
3b132dd
Update .snippets/code/build/build-apps/wormhole-sdk/cctp.ts
themacexpert Aug 13, 2024
0d99dce
Update build/build-apps/wormhole-sdk.md
themacexpert Aug 13, 2024
cba9d1a
rev
themacexpert Aug 13, 2024
7d59712
Update build/build-apps/wormhole-sdk.md
themacexpert Aug 13, 2024
9e95244
rev
themacexpert Aug 13, 2024
787ed06
rev
themacexpert Aug 13, 2024
ecc5aae
revise
themacexpert Aug 13, 2024
4939bf2
revise
themacexpert Aug 13, 2024
58d2178
rev
themacexpert Aug 13, 2024
dc972ee
rev
themacexpert Aug 13, 2024
66551bb
revise
themacexpert Aug 13, 2024
bc93e07
revise
themacexpert Aug 13, 2024
63fab26
revise
themacexpert Aug 13, 2024
7feb80e
revise
themacexpert Aug 13, 2024
f8c9bbc
Update build/build-apps/wormhole-sdk.md
themacexpert Aug 19, 2024
bbac961
Update build/build-apps/wormhole-sdk.md
themacexpert Aug 19, 2024
a1a2ab7
Update build/build-apps/wormhole-sdk.md
themacexpert Aug 19, 2024
d423942
delete unneeded snippet
themacexpert Aug 19, 2024
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
19 changes: 19 additions & 0 deletions .snippets/code/build/build-apps/wormhole-sdk/addresses.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// It's possible to convert a string address to its Native address
const ethAddr: NativeAddress<'Evm'> = toNative('Ethereum', '0xbeef...');

// A common type in the SDK is the `ChainAddress` which provides
// the additional context of the `Chain` this address is relevant for
const senderAddress: ChainAddress = Wormhole.chainAddress(
'Ethereum',
'0xbeef...'
);
const receiverAddress: ChainAddress = Wormhole.chainAddress(
'Solana',
'Sol1111...'
);

// Convert the ChainAddress back to its canonical string address format
const strAddress = Wormhole.canonicalAddress(senderAddress); // => '0xbeef...'

// Or if the ethAddr above is for an emitter and you need the UniversalAddress
const emitterAddr = ethAddr.toUniversalAddress().toString();
127 changes: 127 additions & 0 deletions .snippets/code/build/build-apps/wormhole-sdk/cctp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import {
Chain,
CircleTransfer,
Network,
Signer,
TransactionId,
TransferState,
Wormhole,
amount,
wormhole,
} from '@wormhole-foundation/sdk';
import evm from '@wormhole-foundation/sdk/evm';
import solana from '@wormhole-foundation/sdk/solana';
import { SignerStuff, getSigner, waitForRelay } from './helpers/index.js';

/*
Notes:
Only a subset of chains are supported by Circle for CCTP, see core/base/src/constants/circle.ts for currently supported chains

AutoRelayer takes a 0.1 USDC fee when transferring to any chain beside Goerli, which is 1 USDC
*/
//

(async function () {
// Init the Wormhole object, passing in the config for which network
// to use (e.g. Mainnet/Testnet) and what Platforms to support
const wh = await wormhole('Testnet', [evm, solana]);

// Grab chain Contexts
const sendChain = wh.getChain('Avalanche');
const rcvChain = wh.getChain('Solana');

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

// 6 decimals for USDC (except for BSC, so check decimals before using this)
const amt = amount.units(amount.parse('0.2', 6));

// Choose whether or not to have the attestation delivered for you
const automatic = false;

// If the transfer is requested to be automatic, you can also request that
// during redemption, the receiver gets some amount of native gas transferred to them
// so that they may pay for subsequent transactions
// The amount specified here is denominated in the token being transferred (USDC here)
const nativeGas = automatic ? amount.units(amount.parse('0.0', 6)) : 0n;

await cctpTransfer(wh, source, destination, {
amount: amt,
automatic,
nativeGas,
});

})();

async function cctpTransfer<N extends Network>(
wh: Wormhole<N>,
src: SignerStuff<N, any>,
dst: SignerStuff<N, any>,
req: {
amount: bigint;
automatic: boolean;
nativeGas?: bigint;
}
) {

const xfer = await wh.circleTransfer(
// Amount as bigint (base units)
req.amount,
// Sender chain/address
src.address,
// Receiver chain/address
dst.address,
// Automatic delivery boolean
req.automatic,
// Payload to be sent with the transfer
undefined,
// If automatic, native gas can be requested to be sent to the receiver
req.nativeGas
);

// Note, if the transfer is requested to be Automatic, a fee for performing the relay
// will be present in the quote. The fee comes out of the amount requested to be sent.
// If the user wants to receive 1.0 on the destination, the amount to send should be 1.0 + fee.
// The same applies for native gas dropoff
const quote = await CircleTransfer.quoteTransfer(
src.chain,
dst.chain,
xfer.transfer
);
console.log('Quote', quote);

console.log('Starting Transfer');
const srcTxids = await xfer.initiateTransfer(src.signer);
console.log(`Started Transfer: `, srcTxids);

if (req.automatic) {
const relayStatus = await waitForRelay(srcTxids[srcTxids.length - 1]!);
console.log(`Finished relay: `, relayStatus);
return;
}

console.log('Waiting for Attestation');
const attestIds = await xfer.fetchAttestation(60_000);
console.log(`Got Attestation: `, attestIds);

console.log('Completing Transfer');
const dstTxids = await xfer.completeTransfer(dst.signer);
console.log(`Completed Transfer: `, dstTxids);
}

export async function completeTransfer(
wh: Wormhole<Network>,
txid: TransactionId,
signer: Signer
): Promise<void> {

const xfer = await CircleTransfer.from(wh, txid);

const attestIds = await xfer.fetchAttestation(60 * 60 * 1000);
console.log('Got attestation: ', attestIds);

const dstTxIds = await xfer.completeTransfer(signer);
console.log('Completed transfer: ', dstTxIds);
}
10 changes: 10 additions & 0 deletions .snippets/code/build/build-apps/wormhole-sdk/config-override.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const wh = await wormhole('Testnet', [solana], {
chains: {
Solana: {
contracts: {
coreBridge: '11111111111111111111111111111',
},
rpc: 'https://api.devnet.solana.com',
},
},
});
15 changes: 15 additions & 0 deletions .snippets/code/build/build-apps/wormhole-sdk/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { wormhole } from '@wormhole-foundation/sdk';
import solana from '@wormhole-foundation/sdk/solana';
(async function () {
const wh = await wormhole('Testnet', [solana], {
chains: {
Solana: {
contracts: {
coreBridge: '11111111111111111111111111111',
},
rpc: 'https://api.devnet.solana.com',
},
},
});
console.log(wh.config.chains.Solana);
})();
210 changes: 210 additions & 0 deletions .snippets/code/build/build-apps/wormhole-sdk/cosmos.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
import {
Chain,
GatewayTransfer,
GatewayTransferDetails,
Network,
TokenId,
Wormhole,
amount,
wormhole,
} from '@wormhole-foundation/sdk';

// Import the platform specific packages

import cosmwasm from '@wormhole-foundation/sdk/cosmwasm';
import evm from '@wormhole-foundation/sdk/evm';
import solana from '@wormhole-foundation/sdk/solana';
import { SignerStuff, getSigner } from './helpers/index.js';

// We're going to transfer into, around, and out of the Cosmos ecosystem
// First on Avalanche, transparently through gateway and over IBC to Cosmoshub
// Then over IBC, transparently through gateway and over IBC to Osmosis
// Finally out of Osmosis, transparently through gateway, out to Avalanche

// eg:
// Avalanche[avax] => {Gateway ->}Osmosis[gateway/wrapped avax]
// Osmosis[gateway/wrapped avax] -> {Gateway ->} Cosmoshub[gateway/wrapped avax]
// Cosmoshub[gateway/wrapped avax] -> {Gateway} => Avalanch[avax]

// Key:
// => : Regular contract call
// -> : IBC Transfer
// {*}: Transparently handled by Gateway

(async function () {
// Init Wormhole object, passing config for which network
// to use (e.g. Mainnet/Testnet) and what Platforms to support
const wh = await wormhole('Mainnet', [evm, solana, cosmwasm]);
// Pick up where you left off by updating the txids as you go
let fakeIt = false;

// Grab chain Contexts for each leg of our journey
const external = wh.getChain('Solana');
const cosmos1 = wh.getChain('Dymension');
const cosmos2 = wh.getChain('Injective');

// Get signer from local key but anything that implements
// Signer interface (e.g. wrapper around web wallet) should work
const leg1 = await getSigner(external);
const leg2 = await getSigner(cosmos1);
const leg3 = await getSigner(cosmos2);

// We'll use the native token on the source chain
const token: TokenId = Wormhole.tokenId(external.chain, 'native');
const amt = amount.units(
amount.parse('0.001', external.config.nativeTokenDecimals)
);

// Transfer native token from source chain, through gateway, to a cosmos chain
let route1 = fakeIt
? await GatewayTransfer.from(
wh,
{
chain: external.chain,
txid: '5y2BnJ1Nwqe4m6KTSrry5Ni88xqVrqo4jdbuNwAPDuXEonQRVLbALf7abViwucKKr8U8cDfJtDmqnuRAAC6i6wtb',
},
600_000
)
: await transferIntoCosmos(wh, token, amt, leg1, leg2);
console.log('Route 1 (External => Cosmos)', route1);

// Lookup the Gateway representation of the wrappd token
const { denom } = route1.ibcTransfers![0]!.data;
const cosmosTokenAddress = Wormhole.parseAddress('Wormchain', denom);

// Transfer Gateway factory tokens over IBC through gateway to another Cosmos chain
let route2 = fakeIt
? await GatewayTransfer.from(
wh,
{
chain: cosmos1.chain,
txid: '3014CABA727C8A1BFCBD282095C771ACBAB3B13CC595B702ABFD3A4502315FBD',
},
600_000
)
: await transferBetweenCosmos(
wh,
{ chain: cosmos1.chain, address: cosmosTokenAddress },
1000n,
leg2,
leg3
);
console.log('Route 2 (Cosmos -> Cosmos): ', route2);

// Transfer Gateway factory token through gateway back to source chain
let route3 = fakeIt
? await GatewayTransfer.from(
wh,
{
chain: cosmos2.chain,
txid: 'BEDD0CE2FEA8FF5DF81FCA5142E72745E154F87D496CDA147FC4D5D46A7C7D81',
},
600_000
)
: await transferOutOfCosmos(
wh,
{ chain: cosmos2.chain, address: cosmosTokenAddress },
1000n,
leg3,
leg1
);
console.log('Route 3 (Cosmos => External): ', route3);
})();

async function transferIntoCosmos(
wh: Wormhole<Network>,
token: TokenId,
amount: bigint,
src: SignerStuff<Network, Chain>,
dst: SignerStuff<Network, Chain>
): Promise<GatewayTransfer<Network>> {
console.log(
`Beginning transfer into Cosmos from ${
src.chain.chain
}:${src.address.address.toString()} to ${
dst.chain.chain
}:${dst.address.address.toString()}`
);

const xfer = await GatewayTransfer.from(wh, {
token: token,
amount: amount,
from: src.address,
to: dst.address,
} as GatewayTransferDetails);
console.log('Created GatewayTransfer: ', xfer.transfer);

const srcTxIds = await xfer.initiateTransfer(src.signer);
console.log('Started transfer on source chain', srcTxIds);

const attests = await xfer.fetchAttestation(600_000);
console.log('Got Attestations', attests);
return xfer;
}

async function transferBetweenCosmos<N extends Network>(
wh: Wormhole<N>,
token: TokenId,
amount: bigint,
src: SignerStuff<N, Chain>,
dst: SignerStuff<N, Chain>
): Promise<GatewayTransfer<N>> {
console.log(
`Beginning transfer within cosmos from ${
src.chain.chain
}:${src.address.address.toString()} to ${
dst.chain.chain
}:${dst.address.address.toString()}`
);

const xfer = await GatewayTransfer.from(wh, {
token: token,
amount: amount,
from: src.address,
to: dst.address,
} as GatewayTransferDetails);
console.log('Created GatewayTransfer: ', xfer.transfer);

const srcTxIds = await xfer.initiateTransfer(src.signer);
console.log('Started transfer on source chain', srcTxIds);

const attests = await xfer.fetchAttestation(60_000);
console.log('Got attests: ', attests);

return xfer;
}

async function transferOutOfCosmos<N extends Network>(
wh: Wormhole<N>,
token: TokenId,
amount: bigint,
src: SignerStuff<N, Chain>,
dst: SignerStuff<N, Chain>
): Promise<GatewayTransfer<N>> {
console.log(
`Beginning transfer out of cosmos from ${
src.chain.chain
}:${src.address.address.toString()} to ${
dst.chain.chain
}:${dst.address.address.toString()}`
);

const xfer = await GatewayTransfer.from(wh, {
token: token,
amount: amount,
from: src.address,
to: dst.address,
} as GatewayTransferDetails);
console.log('Created GatewayTransfer: ', xfer.transfer);
const srcTxIds = await xfer.initiateTransfer(src.signer);
console.log('Started transfer on source chain', srcTxIds);

const attests = await xfer.fetchAttestation(600_000);
console.log('Got attests', attests);

// Since we're leaving cosmos, this is required to complete the transfer
const dstTxIds = await xfer.completeTransfer(dst.signer);
console.log('Completed transfer on destination chain', dstTxIds);

return xfer;
}
Loading
Loading