-
Notifications
You must be signed in to change notification settings - Fork 26
Create Token Attestation guide for Token Bridge #473
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
Open
dawnkelly09
wants to merge
4
commits into
main
Choose a base branch
from
dawn/attest-token
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
126 changes: 126 additions & 0 deletions
126
.snippets/code/products/token-bridge/guides/attest-tokens/attest.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import { | ||
wormhole, | ||
Wormhole, | ||
TokenId, | ||
TokenAddress, | ||
} from '@wormhole-foundation/sdk'; | ||
import { signSendWait, toNative } from '@wormhole-foundation/sdk-connect'; | ||
import evm from '@wormhole-foundation/sdk/evm'; | ||
import solana from '@wormhole-foundation/sdk/solana'; | ||
import { getSigner } from './helper'; | ||
|
||
async function attestToken() { | ||
// Initialize wormhole instance, define the network, platforms, and chains | ||
const wh = await wormhole('Testnet', [evm, solana]); | ||
const sourceChain = wh.getChain('Moonbeam'); | ||
const destinationChain = wh.getChain('Solana'); | ||
|
||
// Define the token to check for a wrapped version | ||
const tokenId: TokenId = Wormhole.tokenId( | ||
sourceChain.chain, | ||
'INSERT_TOKEN_CONTRACT_ADDRESS' | ||
); | ||
// Check if the token is registered with destinationChain token bridge contract | ||
// Registered = returns the wrapped token ID | ||
// Not registered = runs the attestation flow to register the token | ||
let wrappedToken: TokenId; | ||
try { | ||
wrappedToken = await wh.getWrappedAsset(destinationChain.chain, tokenId); | ||
console.log( | ||
'✅ Token already registered on destination:', | ||
wrappedToken.address | ||
); | ||
} catch (e) { | ||
console.log( | ||
'⚠️ Token is NOT registered on destination. Running attestation flow...' | ||
); | ||
// Attestation flow code | ||
// Retrieve the token bridge context for the source chain | ||
// This is where you will send the transaction to attest the token | ||
const tb = await sourceChain.getTokenBridge(); | ||
// Get the signer for the source chain | ||
const sourceSigner = await getSigner(sourceChain); | ||
// Define the token to attest and a payer address | ||
const token: TokenAddress<typeof sourceChain.chain> = toNative( | ||
sourceChain.chain, | ||
tokenId.address.toString() | ||
); | ||
const payer = toNative(sourceChain.chain, sourceSigner.signer.address()); | ||
// Call the `createAttestation` method to create a new attestation | ||
// and sign and send the transaction | ||
for await (const tx of tb.createAttestation(token, payer)) { | ||
const txids = await signSendWait( | ||
sourceChain, | ||
tb.createAttestation(token), | ||
sourceSigner.signer | ||
); | ||
console.log('✅ Attestation transaction sent:', txids); | ||
// Parse the transaction to get Wormhole message ID | ||
const messages = await sourceChain.parseTransaction(txids[0].txid); | ||
console.log('✅ Attestation messages:', messages); | ||
// Set a timeout for fetching the VAA, this can take several minutes | ||
// depending on the source chain network and finality | ||
const timeout = 25 * 60 * 1000; | ||
// Fetch the VAA for the attestation message | ||
const vaa = await wh.getVaa( | ||
messages[0]!, | ||
'TokenBridge:AttestMeta', | ||
timeout | ||
); | ||
if (!vaa) throw new Error('❌ VAA not found before timeout.'); | ||
// Get the token bridge context for the destination chain | ||
// and submit the attestation VAA | ||
const destTb = await destinationChain.getTokenBridge(); | ||
// Get the signer for the destination chain | ||
const destinationSigner = await getSigner(destinationChain); | ||
const payer = toNative( | ||
destinationChain.chain, | ||
destinationSigner.signer.address() | ||
); | ||
const destTxids = await signSendWait( | ||
destinationChain, | ||
destTb.submitAttestation(vaa, payer), | ||
destinationSigner.signer | ||
); | ||
console.log('✅ Attestation submitted on destination:', destTxids); | ||
} | ||
// Poll for the wrapped token to appear on the destination chain | ||
const maxAttempts = 50; // ~5 minutes with 6s interval | ||
const interval = 6000; | ||
let attempt = 0; | ||
let registered = false; | ||
|
||
while (attempt < maxAttempts && !registered) { | ||
attempt++; | ||
try { | ||
const wrapped = await wh.getWrappedAsset( | ||
destinationChain.chain, | ||
tokenId | ||
); | ||
console.log( | ||
`✅ Wrapped token is now available on ${destinationChain.chain}:`, | ||
wrapped.address | ||
); | ||
registered = true; | ||
} catch { | ||
console.log( | ||
`⏳ Waiting for wrapped token to register on ${destinationChain.chain}...` | ||
); | ||
await new Promise((res) => setTimeout(res, interval)); | ||
} | ||
} | ||
if (!registered) { | ||
throw new Error( | ||
`❌ Token attestation did not complete in time on ${destinationChain.chain}` | ||
); | ||
} | ||
console.log( | ||
`🚀 Token attestation complete! Token registered with ${destinationChain.chain}.` | ||
); | ||
} | ||
} | ||
|
||
attestToken().catch((e) => { | ||
console.error('❌ Error in attestToken', e); | ||
process.exit(1); | ||
}); |
59 changes: 59 additions & 0 deletions
59
.snippets/code/products/token-bridge/guides/attest-tokens/helper.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { | ||
Chain, | ||
ChainAddress, | ||
ChainContext, | ||
Wormhole, | ||
Network, | ||
Signer, | ||
} from '@wormhole-foundation/sdk'; | ||
import type { SignAndSendSigner } from '@wormhole-foundation/sdk'; | ||
import evm from '@wormhole-foundation/sdk/evm'; | ||
import solana from '@wormhole-foundation/sdk/solana'; | ||
import sui from '@wormhole-foundation/sdk/sui'; | ||
|
||
/** | ||
* Returns a signer for the given chain using locally scoped credentials. | ||
* The required values (EVM_PRIVATE_KEY, SOL_PRIVATE_KEY, SUI_MNEMONIC) must | ||
* be loaded securely beforehand, for example via a keystore, secrets | ||
* manager, or environment variables (not recommended). | ||
*/ | ||
export async function getSigner<N extends Network, C extends Chain>( | ||
chain: ChainContext<N, C> | ||
): Promise<{ | ||
chain: ChainContext<N, C>; | ||
signer: SignAndSendSigner<N, C>; | ||
address: ChainAddress<C>; | ||
}> { | ||
let signer: Signer<any, any>; | ||
const platform = chain.platform.utils()._platform; | ||
|
||
// Customize the signer by adding or removing platforms as needed | ||
// Be sure to import the necessary packages for the platforms you want to support | ||
switch (platform) { | ||
case 'Evm': | ||
signer = await ( | ||
await evm() | ||
).getSigner(await chain.getRpc(), EVM_PRIVATE_KEY!); | ||
break; | ||
case 'Solana': | ||
signer = await ( | ||
await solana() | ||
).getSigner(await chain.getRpc(), SOL_PRIVATE_KEY!); | ||
break; | ||
case 'Sui': | ||
signer = await ( | ||
await sui() | ||
).getSigner(await chain.getRpc(), SUI_MNEMONIC!); | ||
break; | ||
default: | ||
throw new Error(`Unsupported platform: ${platform}`); | ||
} | ||
|
||
const typedSigner = signer as SignAndSendSigner<N, C>; | ||
|
||
return { | ||
chain, | ||
signer: typedSigner, | ||
address: Wormhole.chainAddress(chain.chain, signer.address()), | ||
}; | ||
} |
10 changes: 10 additions & 0 deletions
10
.snippets/code/products/token-bridge/guides/attest-tokens/terminal01.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<div id="termynal" data-termynal> | ||
<span data-ty="input"><span class="file-path"></span>npx tsx attest.ts</span> | ||
<span data-ty>✅ Token already registered on destination: SolanaAddress { | ||
type: 'Native', | ||
address: PublicKey [PublicKey(2qjSAGrpT2eTb673KuGAR5s6AJfQ1X5Sg177Qzuqt7yB)] { | ||
_bn: BN: 1b578bb9b7a04a1aab3b5b64b550d8fc4f73ab343c9cf8532d2976b77ec4a8ca | ||
} | ||
}</span> | ||
<span data-ty="input"><span class="file-path"></span></span> | ||
</div> |
5 changes: 5 additions & 0 deletions
5
.snippets/code/products/token-bridge/guides/attest-tokens/terminal02.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<div id="termynal" data-termynal> | ||
<span data-ty="input"><span class="file-path"></span>npx tsx attest.ts</span> | ||
<span data-ty>⚠️ Token is NOT registered on destination. Running attestation flow...</span> | ||
<span data-ty="input"><span class="file-path"></span></span> | ||
</div> |
24 changes: 24 additions & 0 deletions
24
.snippets/code/products/token-bridge/guides/attest-tokens/terminal03.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<div id="termynal" data-termynal> | ||
<span data-ty="input"><span class="file-path"></span>npx tsx attest.ts</span> | ||
<span data-ty>⚠️ Token is NOT registered on destination. Running attestation | ||
flow...</span> | ||
<span data-ty>✅ Attestation transaction sent: [ { chain: 'Moonbeam', txid: | ||
'0xbaf7429e1099cac6f39ef7e3c30e38776cfb5b6be837dcd8793374c8ee491799' } | ||
]</span> | ||
<span data-ty>✅ Attestation messages: [ { chain: 'Moonbeam', emitter: UniversalAddress { | ||
address: [Uint8Array] }, sequence: 1507n } ]</span> | ||
<span data-ty>Retrying Wormholescan:GetVaaBytes, attempt 0/750</span> | ||
<span data-ty>Retrying Wormholescan:GetVaaBytes, attempt 1/750</span> | ||
<span data-ty>.....</span> | ||
<span data-ty>Retrying Wormholescan:GetVaaBytes, attempt 10/750</span> | ||
<span data-ty>📨 Submitting attestation VAA to Solana...</span> | ||
<span data-ty>✅ Attestation submitted on destination: [ { chain: 'Solana', txid: | ||
'3R4oF5P85jK3wKgkRs5jmE8BBLoM4wo2hWSgXXL6kA8efbj2Vj9vfuFSb53xALqYZuv3FnXDwJNuJfiKKDwpDH1r' | ||
} ]</span> | ||
<span data-ty>✅ Wrapped token is now available on Solana: SolanaAddress { type: | ||
'Native', address: PublicKey | ||
[PublicKey(2qjSAGrpT2eTb673KuGAR5s6AJfQ1X5Sg177Qzuqt7yB)] { _bn: BN: | ||
1b578bb9b7a04a1aab3b5b64b550d8fc4f73ab343c9cf8532d2976b77ec4a8ca } }</span> | ||
<span data-ty>🚀 Token attestation complete!</span> | ||
<span data-ty="input"><span class="file-path"></span></span> | ||
</div> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.