@@ -10,6 +10,7 @@ import { getOrCreateAssociatedTokenAccount } from "@solana/spl-token";
1010import { Connected } from "../target/types/connected" ;
1111import { SYSTEM_PROGRAM_ID } from "@coral-xyz/anchor/dist/cjs/native/system" ;
1212import { ConnectedSpl } from "../target/types/connected_spl" ;
13+ import { ComputeBudgetProgram } from "@solana/web3.js" ;
1314
1415const ec = new EC ( "secp256k1" ) ;
1516// read private key from hex dump
@@ -20,6 +21,7 @@ const keyPair = ec.keyFromPrivate(
2021const usdcDecimals = 6 ;
2122const chain_id = 111111 ;
2223const chain_id_bn = new anchor . BN ( chain_id ) ;
24+ const maxPayloadSize = 750 ;
2325
2426async function mintSPLToken (
2527 conn : anchor . web3 . Connection ,
@@ -242,6 +244,77 @@ describe("Gateway", () => {
242244 }
243245 } ) ;
244246
247+ it ( "Deposit 1_000_000 USDC with above max payload size should fail" , async ( ) => {
248+ const pda_ata = await getOrCreateAssociatedTokenAccount (
249+ conn ,
250+ wallet ,
251+ mint . publicKey ,
252+ pdaAccount ,
253+ true
254+ ) ;
255+ const tokenAccount = await getOrCreateAssociatedTokenAccount (
256+ conn ,
257+ wallet ,
258+ mint . publicKey ,
259+ wallet . publicKey
260+ ) ;
261+ try {
262+ await gatewayProgram . methods
263+ . depositSplTokenAndCall (
264+ new anchor . BN ( 2_000_000 ) ,
265+ Array . from ( address ) ,
266+ Buffer . from ( Array ( maxPayloadSize + 1 ) . fill ( 1 ) )
267+ )
268+ . accounts ( {
269+ from : tokenAccount . address ,
270+ to : pda_ata . address ,
271+ mintAccount : mint . publicKey ,
272+ } )
273+ . rpc ( { commitment : "processed" } ) ;
274+ throw new Error ( "Expected error not thrown" ) ;
275+ } catch ( err ) {
276+ expect ( err ) . to . be . instanceof ( anchor . AnchorError ) ;
277+ expect ( err . message ) . to . include ( "MemoLengthExceeded" ) ;
278+ }
279+ } ) ;
280+
281+ it ( "Deposit 1_000_000 USDC with with max payload size" , async ( ) => {
282+ const pda_ata = await getOrCreateAssociatedTokenAccount (
283+ conn ,
284+ wallet ,
285+ mint . publicKey ,
286+ pdaAccount ,
287+ true
288+ ) ;
289+ const tokenAccount = await getOrCreateAssociatedTokenAccount (
290+ conn ,
291+ wallet ,
292+ mint . publicKey ,
293+ wallet . publicKey
294+ ) ;
295+ let acct = await spl . getAccount ( conn , pda_ata . address ) ;
296+ const bal1 = acct . amount ;
297+
298+ await gatewayProgram . methods
299+ . depositSplTokenAndCall (
300+ new anchor . BN ( 2_000_000 ) ,
301+ Array . from ( address ) ,
302+ Buffer . from ( Array ( maxPayloadSize ) . fill ( 1 ) )
303+ )
304+ . accounts ( {
305+ from : tokenAccount . address ,
306+ to : pda_ata . address ,
307+ mintAccount : mint . publicKey ,
308+ } )
309+ . preInstructions ( [
310+ ComputeBudgetProgram . setComputeUnitLimit ( { units : 400000 } ) ,
311+ ] )
312+ . rpc ( { commitment : "processed" } ) ;
313+ acct = await spl . getAccount ( conn , pda_ata . address ) ;
314+ const bal2 = acct . amount ;
315+ expect ( bal2 - bal1 ) . to . be . eq ( 2_000_000n ) ;
316+ } ) ;
317+
245318 it ( "Deposit 1_000_000 USDC to Gateway" , async ( ) => {
246319 let pda_ata = await getOrCreateAssociatedTokenAccount (
247320 conn ,
@@ -376,7 +449,7 @@ describe("Gateway", () => {
376449 expect ( err ) . to . be . instanceof ( anchor . AnchorError ) ;
377450 expect ( err . message ) . to . include ( "NonceMismatch" ) ;
378451 const account4 = await spl . getAccount ( conn , pda_ata ) ;
379- expect ( account4 . amount ) . to . be . eq ( 2_500_000n ) ;
452+ expect ( account4 . amount ) . to . be . eq ( 4_500_000n ) ;
380453 }
381454
382455 try {
@@ -418,7 +491,7 @@ describe("Gateway", () => {
418491 expect ( err ) . to . be . instanceof ( anchor . AnchorError ) ;
419492 expect ( err . message ) . to . include ( "ConstraintAssociated" ) ;
420493 const account4 = await spl . getAccount ( conn , pda_ata ) ;
421- expect ( account4 . amount ) . to . be . eq ( 2_500_000n ) ;
494+ expect ( account4 . amount ) . to . be . eq ( 4_500_000n ) ;
422495 }
423496 } ) ;
424497
@@ -1608,6 +1681,39 @@ describe("Gateway", () => {
16081681 }
16091682 } ) ;
16101683
1684+ it ( "Deposit and call with above max payload size should fail" , async ( ) => {
1685+ try {
1686+ await gatewayProgram . methods
1687+ . depositAndCall (
1688+ new anchor . BN ( 1_000_000_000 ) ,
1689+ Array . from ( address ) ,
1690+ Buffer . from ( Array ( maxPayloadSize + 1 ) . fill ( 1 ) )
1691+ )
1692+ . rpc ( ) ;
1693+ throw new Error ( "Expected error not thrown" ) ;
1694+ } catch ( err ) {
1695+ expect ( err ) . to . be . instanceof ( anchor . AnchorError ) ;
1696+ expect ( err . message ) . to . include ( "MemoLengthExceeded" ) ;
1697+ }
1698+ } ) ;
1699+
1700+ it ( "Deposit and call with max payload size" , async ( ) => {
1701+ const bal1 = await conn . getBalance ( pdaAccount ) ;
1702+ const txsig = await gatewayProgram . methods
1703+ . depositAndCall (
1704+ new anchor . BN ( 1_000_000_000 ) ,
1705+ Array . from ( address ) ,
1706+ Buffer . from ( Array ( maxPayloadSize ) . fill ( 1 ) )
1707+ )
1708+ . preInstructions ( [
1709+ ComputeBudgetProgram . setComputeUnitLimit ( { units : 400000 } ) ,
1710+ ] )
1711+ . rpc ( { commitment : "processed" } ) ;
1712+ await conn . getParsedTransaction ( txsig , "confirmed" ) ;
1713+ const bal2 = await conn . getBalance ( pdaAccount ) ;
1714+ expect ( bal2 - bal1 ) . to . be . gte ( 1_000_000_000 ) ;
1715+ } ) ;
1716+
16111717 it ( "Deposit and call" , async ( ) => {
16121718 let bal1 = await conn . getBalance ( pdaAccount ) ;
16131719 const txsig = await gatewayProgram . methods
0 commit comments