|
1 | 1 | import * as anchor from "@coral-xyz/anchor"; |
2 | 2 | import { BN } from "@coral-xyz/anchor"; |
3 | | -import { BigNumber, ethers } from "ethers"; |
| 3 | +import { getApproveCheckedInstruction } from "@solana-program/token"; |
| 4 | +import { |
| 5 | + address, |
| 6 | + airdropFactory, |
| 7 | + appendTransactionMessageInstruction, |
| 8 | + createKeyPairFromBytes, |
| 9 | + createSignerFromKeyPair, |
| 10 | + getProgramDerivedAddress, |
| 11 | + lamports, |
| 12 | + pipe, |
| 13 | +} from "@solana/kit"; |
4 | 14 | import { |
5 | 15 | ASSOCIATED_TOKEN_PROGRAM_ID, |
6 | | - TOKEN_PROGRAM_ID, |
| 16 | + ExtensionType, |
7 | 17 | TOKEN_2022_PROGRAM_ID, |
8 | | - createMint, |
9 | | - getOrCreateAssociatedTokenAccount, |
10 | | - mintTo, |
11 | | - getAccount, |
| 18 | + TOKEN_PROGRAM_ID, |
12 | 19 | createApproveCheckedInstruction, |
13 | 20 | createEnableCpiGuardInstruction, |
| 21 | + createMint, |
14 | 22 | createReallocateInstruction, |
15 | | - ExtensionType, |
| 23 | + getAccount, |
| 24 | + getOrCreateAssociatedTokenAccount, |
| 25 | + mintTo, |
16 | 26 | } from "@solana/spl-token"; |
17 | | -import { PublicKey, Keypair, Transaction, sendAndConfirmTransaction } from "@solana/web3.js"; |
18 | | -import { common } from "./SvmSpoke.common"; |
| 27 | +import { Keypair, PublicKey, Transaction, sendAndConfirmTransaction } from "@solana/web3.js"; |
| 28 | +import { BigNumber, ethers } from "ethers"; |
| 29 | +import { SvmSpokeClient } from "../../src/svm"; |
| 30 | +import { DepositInput } from "../../src/svm/clients/SvmSpoke"; |
| 31 | +import { intToU8Array32, readEventsUntilFound, u8Array32ToBigNumber, u8Array32ToInt } from "../../src/svm/web3-v1"; |
19 | 32 | import { DepositDataValues } from "../../src/types/svm"; |
20 | | -import { intToU8Array32, readEventsUntilFound, u8Array32ToInt, u8Array32ToBigNumber } from "../../src/svm/web3-v1"; |
21 | 33 | import { MAX_EXCLUSIVITY_OFFSET_SECONDS } from "../../test-utils"; |
| 34 | +import { common } from "./SvmSpoke.common"; |
| 35 | +import { createDefaultSolanaClient, createDefaultTransaction, signAndSendTransaction } from "./utils"; |
22 | 36 | const { provider, connection, program, owner, seedBalance, initializeState, depositData } = common; |
23 | 37 | const { createRoutePda, getVaultAta, assertSE, assert, getCurrentTime, depositQuoteTimeBuffer, fillDeadlineBuffer } = |
24 | 38 | common; |
@@ -698,4 +712,82 @@ describe("svm_spoke.deposit", () => { |
698 | 712 | assert.include(err.toString(), "owner does not match"); |
699 | 713 | } |
700 | 714 | }); |
| 715 | + |
| 716 | + describe("codama client and solana kit", () => { |
| 717 | + it("Deposit with with solana kit and codama client", async () => { |
| 718 | + // typescript is not happy with the depositData object |
| 719 | + if (!depositData.inputToken || !depositData.depositor) { |
| 720 | + throw new Error("Input token or depositor is null"); |
| 721 | + } |
| 722 | + |
| 723 | + const rpcClient = createDefaultSolanaClient(); |
| 724 | + const signer = await createSignerFromKeyPair(await createKeyPairFromBytes(depositor.secretKey)); |
| 725 | + |
| 726 | + await airdropFactory(rpcClient)({ |
| 727 | + recipientAddress: signer.address, |
| 728 | + lamports: lamports(100000000000000n), |
| 729 | + commitment: "confirmed", |
| 730 | + }); |
| 731 | + |
| 732 | + const [eventAuthority] = await getProgramDerivedAddress({ |
| 733 | + programAddress: address(program.programId.toString()), |
| 734 | + seeds: ["__event_authority"], |
| 735 | + }); |
| 736 | + |
| 737 | + // note that we are using getApproveCheckedInstruction from @solana-program/token |
| 738 | + const approveIx = getApproveCheckedInstruction({ |
| 739 | + source: address(depositAccounts.depositorTokenAccount.toString()), |
| 740 | + mint: address(depositAccounts.mint.toString()), |
| 741 | + delegate: address(depositAccounts.state.toString()), |
| 742 | + owner: address(depositor.publicKey.toString()), |
| 743 | + amount: BigInt(depositData.inputAmount.toString()), |
| 744 | + decimals: tokenDecimals, |
| 745 | + }); |
| 746 | + |
| 747 | + const formattedDepositData = { |
| 748 | + depositor: address(depositData.depositor.toString()), |
| 749 | + recipient: address(depositData.recipient.toString()), |
| 750 | + inputToken: address(depositData.inputToken.toString()), |
| 751 | + outputToken: address(depositData.outputToken.toString()), |
| 752 | + inputAmount: BigInt(depositData.inputAmount.toString()), |
| 753 | + outputAmount: BigInt(depositData.outputAmount.toString()), |
| 754 | + destinationChainId: depositData.destinationChainId.toNumber(), |
| 755 | + exclusiveRelayer: address(depositData.exclusiveRelayer.toString()), |
| 756 | + quoteTimestamp: depositData.quoteTimestamp.toNumber(), |
| 757 | + fillDeadline: depositData.fillDeadline.toNumber(), |
| 758 | + exclusivityParameter: depositData.exclusivityParameter.toNumber(), |
| 759 | + message: depositData.message, |
| 760 | + }; |
| 761 | + |
| 762 | + const formattedAccounts = { |
| 763 | + state: address(depositAccounts.state.toString()), |
| 764 | + route: address(depositAccounts.route.toString()), |
| 765 | + depositorTokenAccount: address(depositAccounts.depositorTokenAccount.toString()), |
| 766 | + mint: address(depositAccounts.mint.toString()), |
| 767 | + tokenProgram: address(tokenProgram.toString()), |
| 768 | + program: address(program.programId.toString()), |
| 769 | + vault: address(vault.toString()), |
| 770 | + }; |
| 771 | + |
| 772 | + const depositInput: DepositInput = { |
| 773 | + ...formattedDepositData, |
| 774 | + ...formattedAccounts, |
| 775 | + eventAuthority, |
| 776 | + signer, |
| 777 | + }; |
| 778 | + |
| 779 | + const depositIx = await SvmSpokeClient.getDepositInstructionAsync(depositInput); |
| 780 | + |
| 781 | + const tx = await pipe( |
| 782 | + await createDefaultTransaction(rpcClient, signer), |
| 783 | + (tx) => appendTransactionMessageInstruction(approveIx, tx), |
| 784 | + (tx) => appendTransactionMessageInstruction(depositIx, tx), |
| 785 | + (tx) => signAndSendTransaction(rpcClient, tx) |
| 786 | + ); |
| 787 | + |
| 788 | + const events = await readEventsUntilFound(connection, tx, [program]); |
| 789 | + const event = events[0].data; // 0th event is the latest event; |
| 790 | + assertSE(event.depositId, intToU8Array32(1), "Deposit ID should match the expected hash"); |
| 791 | + }); |
| 792 | + }); |
701 | 793 | }); |
0 commit comments