Skip to content

Commit 0991928

Browse files
authored
feat(svm): deposit tests with codama and solana kit (#917)
Signed-off-by: Pablo Maldonado <pablo@umaproject.org>
1 parent 1e8bbbd commit 0991928

File tree

3 files changed

+108
-10
lines changed

3 files changed

+108
-10
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"@openzeppelin/contracts-upgradeable": "4.9.6",
5454
"@scroll-tech/contracts": "^0.1.0",
5555
"@solana-developers/helpers": "^2.4.0",
56+
"@solana-program/token": "^0.5.1",
5657
"@solana/kit": "^2.1.0",
5758
"@solana/spl-token": "^0.4.6",
5859
"@solana/web3.js": "^1.31.0",

test/svm/SvmSpoke.Deposit.ts

Lines changed: 102 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,38 @@
11
import * as anchor from "@coral-xyz/anchor";
22
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";
414
import {
515
ASSOCIATED_TOKEN_PROGRAM_ID,
6-
TOKEN_PROGRAM_ID,
16+
ExtensionType,
717
TOKEN_2022_PROGRAM_ID,
8-
createMint,
9-
getOrCreateAssociatedTokenAccount,
10-
mintTo,
11-
getAccount,
18+
TOKEN_PROGRAM_ID,
1219
createApproveCheckedInstruction,
1320
createEnableCpiGuardInstruction,
21+
createMint,
1422
createReallocateInstruction,
15-
ExtensionType,
23+
getAccount,
24+
getOrCreateAssociatedTokenAccount,
25+
mintTo,
1626
} 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";
1932
import { DepositDataValues } from "../../src/types/svm";
20-
import { intToU8Array32, readEventsUntilFound, u8Array32ToInt, u8Array32ToBigNumber } from "../../src/svm/web3-v1";
2133
import { MAX_EXCLUSIVITY_OFFSET_SECONDS } from "../../test-utils";
34+
import { common } from "./SvmSpoke.common";
35+
import { createDefaultSolanaClient, createDefaultTransaction, signAndSendTransaction } from "./utils";
2236
const { provider, connection, program, owner, seedBalance, initializeState, depositData } = common;
2337
const { createRoutePda, getVaultAta, assertSE, assert, getCurrentTime, depositQuoteTimeBuffer, fillDeadlineBuffer } =
2438
common;
@@ -698,4 +712,82 @@ describe("svm_spoke.deposit", () => {
698712
assert.include(err.toString(), "owner does not match");
699713
}
700714
});
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+
});
701793
});

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2507,6 +2507,11 @@
25072507
bs58 "^6.0.0"
25082508
dotenv "^16.4.5"
25092509

2510+
"@solana-program/token@^0.5.1":
2511+
version "0.5.1"
2512+
resolved "https://registry.npmjs.org/@solana-program/token/-/token-0.5.1.tgz#10e327df23f05a7f892fd33a9b6418f17dd62296"
2513+
integrity sha512-bJvynW5q9SFuVOZ5vqGVkmaPGA0MCC+m9jgJj1nk5m20I389/ms69ASnhWGoOPNcie7S9OwBX0gTj2fiyWpfag==
2514+
25102515
"@solana/accounts@2.1.0":
25112516
version "2.1.0"
25122517
resolved "https://registry.npmjs.org/@solana/accounts/-/accounts-2.1.0.tgz#23fa5ba20dd7af9bfb84abc638096378b62c7276"

0 commit comments

Comments
 (0)