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

Martinh/cctp tutorial #71

Open
wants to merge 41 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
9cb2ec3
added snippet and main page structure
martin0995 Sep 5, 2024
0792f66
added helpers file
martin0995 Sep 6, 2024
2f2ae26
added main snippets for cctp
martin0995 Sep 11, 2024
79faffe
finished automatic cctp transfers
martin0995 Sep 11, 2024
bac224b
updated amount from snippet
martin0995 Sep 12, 2024
ec1c8ac
updated amount from snippet
martin0995 Sep 12, 2024
2ee2434
created snippet for partial transfers
martin0995 Sep 12, 2024
88e8913
updated page title
martin0995 Sep 12, 2024
6b7f5ec
grammarly check and partial transfer content
martin0995 Sep 12, 2024
f6ce866
updated page description
martin0995 Sep 12, 2024
45634bd
added resources section
martin0995 Sep 12, 2024
09be304
updated snippet names accordingly
martin0995 Sep 18, 2024
96b21a6
updated snippet comments with right steps
martin0995 Sep 18, 2024
36edd16
aligned snippet steps with tutorial
martin0995 Sep 18, 2024
741e9b0
updated page title
martin0995 Sep 18, 2024
8abf25a
updated introduction
martin0995 Sep 18, 2024
a0ffde4
updated core concepts section based on feedback
martin0995 Sep 18, 2024
6d1c6f9
updated supported chains subsection
martin0995 Sep 18, 2024
99f98d8
Update tutorials/messaging/cctp.md
martin0995 Sep 18, 2024
f0994ff
Merge branch 'martinh/cctp-tutorial' of https://github.com/wormhole-f…
martin0995 Sep 18, 2024
7687c3f
Update tutorials/messaging/cctp.md
martin0995 Sep 18, 2024
208facd
updated snippet names with correct order
martin0995 Sep 18, 2024
446d7d5
colon consistency
martin0995 Sep 18, 2024
92190b5
Merge branch 'martinh/cctp-tutorial' of https://github.com/wormhole-f…
martin0995 Sep 18, 2024
a0c11ea
Update tutorials/messaging/cctp.md
martin0995 Sep 18, 2024
03b1f1c
Update tutorials/messaging/cctp.md
martin0995 Sep 18, 2024
ee469d8
Update tutorials/messaging/cctp.md
martin0995 Sep 18, 2024
32eea86
Update tutorials/messaging/cctp.md
martin0995 Sep 18, 2024
5ff9020
Update tutorials/messaging/cctp.md
martin0995 Sep 18, 2024
c7a2a4d
Update tutorials/messaging/cctp.md
martin0995 Sep 18, 2024
e2858d4
Update tutorials/messaging/cctp.md
martin0995 Sep 18, 2024
7109dcb
Update tutorials/messaging/cctp.md
martin0995 Sep 18, 2024
acc04fd
Update tutorials/messaging/cctp.md
martin0995 Sep 18, 2024
c1299af
Update tutorials/messaging/cctp.md
martin0995 Sep 18, 2024
64aff36
Update tutorials/messaging/cctp.md
martin0995 Sep 18, 2024
cf146fd
Merge branch 'martinh/cctp-tutorial' of https://github.com/wormhole-f…
martin0995 Sep 18, 2024
6101207
updated steps for consistency
martin0995 Sep 18, 2024
e8ba3aa
updated manual transfer introduction as well as subtitles
martin0995 Sep 18, 2024
cfb5ea7
updated code based on feedback
martin0995 Sep 18, 2024
54edaeb
updated titles and subtitles organization
martin0995 Sep 18, 2024
4c07511
grammarly check
martin0995 Sep 18, 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
58 changes: 58 additions & 0 deletions .snippets/code/tutorials/messaging/cctp/cctp-sdk-1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import {
ChainAddress,
ChainContext,
Network,
Signer,
Wormhole,
Chain,
} from '@wormhole-foundation/sdk';
import evm from '@wormhole-foundation/sdk/evm';
import solana from '@wormhole-foundation/sdk/solana';
import { config } from 'dotenv';
config();

export interface SignerStuff<N extends Network, C extends Chain> {
chain: ChainContext<N, C>;
signer: Signer<N, C>;
address: ChainAddress<C>;
}

// Function to fetch environment variables (like your private key)
function getEnv(key: string): string {
const val = process.env[key];
if (!val) throw new Error(`Missing environment variable: ${key}`);
return val;
}

// Signer setup function for different blockchain platforms
export async function getSigner<N extends Network, C extends Chain>(
chain: ChainContext<N, C>
): Promise<{
chain: ChainContext<N, C>;
signer: Signer<N, C>;
address: ChainAddress<C>;
}> {
let signer: Signer;
const platform = chain.platform.utils()._platform;

switch (platform) {
case 'Solana':
signer = await (
await solana()
).getSigner(await chain.getRpc(), getEnv('SOL_PRIVATE_KEY'));
break;
case 'Evm':
signer = await (
await evm()
).getSigner(await chain.getRpc(), getEnv('ETH_PRIVATE_KEY'));
break;
default:
throw new Error('Unsupported platform: ' + platform);
}

return {
chain,
signer: signer as Signer<N, C>,
address: Wormhole.chainAddress(chain.chain, signer.address()),
};
}
55 changes: 55 additions & 0 deletions .snippets/code/tutorials/messaging/cctp/cctp-sdk-2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { wormhole } from '@wormhole-foundation/sdk';
import evm from '@wormhole-foundation/sdk/evm';
import solana from '@wormhole-foundation/sdk/solana';
import * as dotenv from 'dotenv';
import { getSigner } from './helpers/helpers';

// Load environment variables
dotenv.config();

(async function () {
const wh = await wormhole('Testnet', [evm, solana]);

// Set up source and destination chains
const sendChain = wh.getChain('Avalanche');
const rcvChain = wh.getChain('Solana');

// Configure the signers
const source = await getSigner(sendChain);
const destination = await getSigner(rcvChain);

// Define the transfer amount (in the smallest unit, so 0.1 USDC = 100,000 units assuming 6 decimals)
const amt = 100_000n;

const automatic = false;

// Create the Circle transfer object
const xfer = await wh.circleTransfer(
amt,
source.address,
destination.address,
automatic
);

console.log('Circle Transfer object created:', xfer);

// Initiate the transfer on the source chain (Avalanche)
console.log('Starting Transfer');
const srcTxids = await xfer.initiateTransfer(source.signer);
console.log(`Started Transfer: `, srcTxids);

// Wait for Circle Attestation (VAA)
const timeout = 60 * 1000; // Timeout in milliseconds (60 seconds)
console.log('Waiting for Attestation');
const attestIds = await xfer.fetchAttestation(timeout);
console.log(`Got Attestation: `, attestIds);

// Complete the transfer on the destination chain (Solana)
console.log('Completing Transfer');
const dstTxids = await xfer.completeTransfer(destination.signer);
console.log(`Completed Transfer: `, dstTxids);

console.log('Circle Transfer status: ', xfer);

process.exit(0);
})();
43 changes: 43 additions & 0 deletions .snippets/code/tutorials/messaging/cctp/cctp-sdk-3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { wormhole } from '@wormhole-foundation/sdk';
import evm from '@wormhole-foundation/sdk/evm';
import solana from '@wormhole-foundation/sdk/solana';
import * as dotenv from 'dotenv';
import { getSigner } from '../helpers/helpers';

// Load environment variables
dotenv.config();

(async function () {
// Initialize the Wormhole object for the Testnet environment and add supported chains (evm and solana)
const wh = await wormhole('Testnet', [evm, solana]);

// Set up source and destination chains
const sendChain = wh.getChain('Avalanche');
const rcvChain = wh.getChain('Solana');

// Configure the signers
const source = await getSigner(sendChain);
const destination = await getSigner(rcvChain);

// Define the transfer amount (in the smallest unit, so 0.1 USDC = 100,000 units assuming 6 decimals)
const amt = 100_000n;

const automatic = true;

// Create the Circle transfer object (USDC-only)
const xfer = await wh.circleTransfer(
amt,
source.address,
destination.address,
automatic
);

console.log('Circle Transfer object created:', xfer);

// Initiate the transfer on the source chain (Avalanche)
console.log('Starting Transfer');
const srcTxids = await xfer.initiateTransfer(source.signer);
console.log(`Started Transfer: `, srcTxids);

process.exit(0);
})();
38 changes: 38 additions & 0 deletions .snippets/code/tutorials/messaging/cctp/cctp-sdk-4.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { CircleTransfer, wormhole } from '@wormhole-foundation/sdk';
import evm from '@wormhole-foundation/sdk/evm';
import solana from '@wormhole-foundation/sdk/solana';
import * as dotenv from 'dotenv';
import { getSigner } from '../helpers/helpers';

// Load environment variables
dotenv.config();

(async function () {
// Initialize the Wormhole object for the Testnet environment and add supported chains (evm and solana)
const wh = await wormhole('Testnet', [evm, solana]);

// Grab chain Contexts -- these hold a reference to a cached rpc client
const rcvChain = wh.getChain('Solana');

// Get signer from local key
const destination = await getSigner(rcvChain);

const timeout = 60 * 1000; // Timeout in milliseconds (60 seconds)

// Rebuild the transfer from the source txid
const xfer = await CircleTransfer.from(
wh,
{
chain: 'Avalanche',
txid: '0x6b6d5f101a32aa6d2f7bf0bf14d72bfbf76a640e1b2fdbbeeac5b82069cda4dd',
},
timeout
);

const dstTxIds = await xfer.completeTransfer(destination.signer);
console.log('Completed transfer: ', dstTxIds);

console.log('Circle Transfer status: ', xfer);

process.exit(0);
})();
1 change: 1 addition & 0 deletions tutorials/messaging/.pages
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ title: Messaging
nav:
- index.md
- 'Create Cross-Chain Contracts': 'cross-chain-contracts.md'
- 'Transfer USDC via Wormhole SDK': 'cctp.md'
Loading
Loading