From 79abc6332c6ae46383dee268b2dc8294746861ad Mon Sep 17 00:00:00 2001 From: spacemandev Date: Fri, 26 Aug 2022 09:51:55 -0500 Subject: [PATCH] added code to track spl token mint --- .../solana/programs/solana/src/account.rs | 1 + .../solana/programs/solana/src/context.rs | 2 +- .../chains/solana/programs/solana/src/lib.rs | 3 +- projects/xmint/handlers/evm.ts | 4 +- projects/xmint/handlers/solana.ts | 51 ++++++++++++------- projects/xmint/tests/evm-evm.bash | 3 ++ projects/xmint/tests/evm-solana.bash | 9 ++-- projects/xmint/tests/test.bash | 11 ++-- 8 files changed, 53 insertions(+), 31 deletions(-) diff --git a/projects/xmint/chains/solana/programs/solana/src/account.rs b/projects/xmint/chains/solana/programs/solana/src/account.rs index 34f980c..c8f9c56 100644 --- a/projects/xmint/chains/solana/programs/solana/src/account.rs +++ b/projects/xmint/chains/solana/programs/solana/src/account.rs @@ -4,6 +4,7 @@ use anchor_lang::prelude::*; pub struct Config { pub owner: Pubkey, pub nonce: u64, + pub mint: Pubkey } #[account] diff --git a/projects/xmint/chains/solana/programs/solana/src/context.rs b/projects/xmint/chains/solana/programs/solana/src/context.rs index 5f06722..4afcbc1 100644 --- a/projects/xmint/chains/solana/programs/solana/src/context.rs +++ b/projects/xmint/chains/solana/programs/solana/src/context.rs @@ -8,7 +8,7 @@ pub struct Initialize<'info>{ seeds=[b"config"], payer=owner, bump, - space=8+32+8+1 + space=8+32+8+32+1 )] pub config: Account<'info, Config>, #[account(mut)] diff --git a/projects/xmint/chains/solana/programs/solana/src/lib.rs b/projects/xmint/chains/solana/programs/solana/src/lib.rs index b73bc44..594aec8 100644 --- a/projects/xmint/chains/solana/programs/solana/src/lib.rs +++ b/projects/xmint/chains/solana/programs/solana/src/lib.rs @@ -17,9 +17,10 @@ declare_id!("BHz6MJGvo8PJaBFqaxyzgJYdY6o8h1rBgsRrUmnHCU9k"); pub mod solana { use super::*; - pub fn initialize(ctx: Context) -> Result<()> { + pub fn initialize(ctx: Context, mint:Pubkey) -> Result<()> { ctx.accounts.config.owner = ctx.accounts.owner.key(); ctx.accounts.config.nonce = 0; + ctx.accounts.config.mint = mint; Ok(()) } diff --git a/projects/xmint/handlers/evm.ts b/projects/xmint/handlers/evm.ts index 6a305ea..a7301e6 100644 --- a/projects/xmint/handlers/evm.ts +++ b/projects/xmint/handlers/evm.ts @@ -199,7 +199,7 @@ export async function createWrapped(src:string, target: string, vaa:string){ srcNetwork.tokenBridgeAddress, signer, targetNetwork.wormholeChainId, - tryNativeToUint8Array(targetDeployInfo.address, targetNetwork.wormholeChainId) + tryNativeToUint8Array(targetDeployInfo.tokenAddress, targetNetwork.wormholeChainId) ); console.log(`${src} Network has new PortalWrappedToken for ${target} network at ${foreignAddress}`); } @@ -432,7 +432,7 @@ export async function balance(src:string, target: string) : Promise { srcNetwork.tokenBridgeAddress, signer, targetNetwork.wormholeChainId, - tryNativeToUint8Array(targetDeploymentInfo.address, targetNetwork.wormholeChainId) + tryNativeToUint8Array(targetDeploymentInfo.tokenAddress, targetNetwork.wormholeChainId) ); const TKN = new ethers.Contract( diff --git a/projects/xmint/handlers/solana.ts b/projects/xmint/handlers/solana.ts index ea2c828..df77cc2 100644 --- a/projects/xmint/handlers/solana.ts +++ b/projects/xmint/handlers/solana.ts @@ -48,7 +48,6 @@ export async function deploy(src: string){ console.log(stdout); // Also initalize the contract - await new Promise((r) => setTimeout(r, 15000)) // wait for the chain to recognize the program //Initalize the Contract const xmint = new anchor.Program( IDL, @@ -61,15 +60,6 @@ export async function deploy(src: string){ const [configAcc, _] = findProgramAddressSync([ Buffer.from("config") ], xmint.programId); - - await xmint.methods - .initialize() - .accounts({ - config: configAcc, - owner: xmint.provider.publicKey, - systemProgram: anchor.web3.SystemProgram.programId - }) - .rpc(); // Deploy the SPL Token for Xmint const mint = await createMint( @@ -79,6 +69,18 @@ export async function deploy(src: string){ xmint.programId, 9 // We are using 9 to match the CLI decimal default exactly ); + await new Promise((r) => setTimeout(r, 15000)) // wait for the chain to recognize the program + + await xmint.methods + .initialize( + mint + ) + .accounts({ + config: configAcc, + owner: xmint.provider.publicKey, + systemProgram: anchor.web3.SystemProgram.programId + }) + .rpc(); // Store deploy info fs.writeFileSync(`./deployinfo/${src}.deploy.json`, JSON.stringify({ @@ -106,7 +108,7 @@ export async function attest(src: string, target:string, address:string = null){ if(!address){ address = srcDeployInfo.tokenAddress; } - console.log(`Attesting ${address} from ${target} network onto ${src}`); + console.log(`Attesting ${address} from ${src} network onto ${target}`); const tx = await attestFromSolana( connection, @@ -114,9 +116,12 @@ export async function attest(src: string, target:string, address:string = null){ srcNetwork.tokenBridgeAddress, srcKey.publicKey.toString(), address - ) - - const attestVaa = await fetchVaa(src, tx, true); + ); + tx.partialSign(srcKey); + const txid = await connection.sendRawTransaction(tx.serialize()); + console.log("TXID: ", txid); + + const attestVaa = await fetchVaa(src, txid, true); switch(targetNetwork.type){ case "evm": await evm.createWrapped(target, src, attestVaa); @@ -132,7 +137,7 @@ export async function attest(src: string, target:string, address:string = null){ * @param tx * @param portal */ -async function fetchVaa(src:string, tx, portal:boolean=false):Promise{ +async function fetchVaa(src:string, tx:string, portal:boolean=false):Promise{ const srcNetwork = config.networks[src]; const srcDeployInfo = JSON.parse(fs.readFileSync(`./deployinfo/${src}.deploy.json`).toString()); const srcKey = anchor.web3.Keypair.fromSecretKey(Uint8Array.from(JSON.parse((fs.readFileSync(`keypairs/${src}.key`).toString()) @@ -141,8 +146,16 @@ async function fetchVaa(src:string, tx, portal:boolean=false):Promise{ setDefaultWasm("node"); const emitterAddr = portal ? await getEmitterAddressSolana(srcNetwork.tokenBridgeAddress) : await getEmitterAddressSolana(srcDeployInfo.address); - const seq = parseSequenceFromLogSolana(await connection.getTransaction(tx)); - + let transaction = await connection.getTransaction(tx); + let timeElapsed = 0; + while (!transaction) { + await new Promise((r) => setTimeout(r, 1000)) // wait for the chain to recognize the program + transaction = await connection.getTransaction(tx); + timeElapsed += 1; + } + console.log(`VAA from TX(${tx}) found in ${timeElapsed}s`); + const seq = parseSequenceFromLogSolana(transaction); + console.log("Seq", seq); await new Promise((r) => setTimeout(r, 5000)); //wait for gaurdian to pick up messsage console.log( "Searching for: ", @@ -186,7 +199,7 @@ export async function createWrapped(src:string, target:string, vaa:string){ connection, srcNetwork.tokenBridgeAddress, targetNetwork.wormholeChainId, - tryNativeToUint8Array(targetDeployInfo.address, targetNetwork.wormholeChainId) + tryNativeToUint8Array(targetDeployInfo.tokenAddress, targetNetwork.wormholeChainId) ); console.log(`${src} Network has new PortalWrappedToken for ${target} network at ${foreignAddress}`); } @@ -272,7 +285,7 @@ export async function balance(src: string, target: string) : Promise{ connection, srcNetwork.tokenBridgeAddress, targetNetwork.wormholeChainId, - tryNativeToUint8Array(targetDeployInfo.address, targetNetwork.wormholeChainId) + tryNativeToUint8Array(targetDeployInfo.tokenAddress, targetNetwork.wormholeChainId) ); const tokenAccountAddress = await getOrCreateAssociatedTokenAccount( diff --git a/projects/xmint/tests/evm-evm.bash b/projects/xmint/tests/evm-evm.bash index 0b7a5cb..c54fc7a 100644 --- a/projects/xmint/tests/evm-evm.bash +++ b/projects/xmint/tests/evm-evm.bash @@ -1,3 +1,6 @@ +# Install Deps +cd chains/evm && forge install --no-git --no-commit && cd ../../ + # Deploy the code ts-node orchestrator.ts deploy evm0 ts-node orchestrator.ts deploy evm1 diff --git a/projects/xmint/tests/evm-solana.bash b/projects/xmint/tests/evm-solana.bash index 8014d41..6797993 100644 --- a/projects/xmint/tests/evm-solana.bash +++ b/projects/xmint/tests/evm-solana.bash @@ -1,20 +1,21 @@ # Build SOL code cd chains/solana && anchor build && cd ../../ +cd chains/evm && forge install --no-git --no-commit && cd ../../ # Deploy the code on EVM0 and SOL0 ts-node orchestrator.ts deploy evm0 ts-node orchestrator.ts deploy sol0 +# Register Apps EVM<>SOL +ts-node orchestrator.ts register-app evm0 sol0 +ts-node orchestrator.ts register-app sol0 evm0 + # Print Balances for EVM0 and SOL0 Keypairs ts-node orchestrator.ts balance evm0 evm0 ts-node orchestrator.ts balance evm0 sol0 ts-node orchestrator.ts balance sol0 sol0 ts-node orchestrator.ts balance sol0 evm0 -# Register Apps EVM<>SOL -ts-node orchestrator.ts register-app evm0 sol0 -ts-node orchestrator.ts register-app sol0 evm0 - # Buy SOL0-TOKEN with eth ts-node orchestrator.ts buy-token evm0 sol0 100 # Print SOL0 Balance diff --git a/projects/xmint/tests/test.bash b/projects/xmint/tests/test.bash index 53776f1..5cf8d63 100644 --- a/projects/xmint/tests/test.bash +++ b/projects/xmint/tests/test.bash @@ -1,13 +1,16 @@ +# Rerun solana validator +cd ../wormhole-local-validator && npm run solana && cd ../xmint + # Deploy the code on EVM0 and SOL0 ts-node orchestrator.ts deploy evm0 ts-node orchestrator.ts deploy sol0 +# Register Apps EVM<>SOL +ts-node orchestrator.ts register-app evm0 sol0 +ts-node orchestrator.ts register-app sol0 evm0 + # Print Balances for EVM0 and SOL0 Keypairs ts-node orchestrator.ts balance evm0 evm0 ts-node orchestrator.ts balance evm0 sol0 ts-node orchestrator.ts balance sol0 sol0 ts-node orchestrator.ts balance sol0 evm0 - -# Register Apps EVM<>SOL -ts-node orchestrator.ts register-app evm0 sol0 -ts-node orchestrator.ts register-app sol0 evm0