Skip to content

Commit fb5513f

Browse files
authored
deposit through example program (#105)
1 parent fb0adba commit fb5513f

File tree

4 files changed

+62
-3
lines changed

4 files changed

+62
-3
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

programs/examples/connected/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ idl-build = ["anchor-lang/idl-build", "anchor-spl/idl-build"]
2020
anchor-lang = { version = "=0.31.1" }
2121
anchor-spl = { version = "=0.31.1" }
2222
spl-associated-token-account = { version = "6.0.0", features = ["no-entrypoint"] }
23+
gateway = { path = "../../gateway", features = ["no-entrypoint", "cpi"] }

programs/examples/connected/src/lib.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,28 @@ pub mod connected {
8484

8585
Ok(())
8686
}
87+
88+
pub fn trigger_deposit(
89+
ctx: Context<TriggerDeposit>,
90+
amount: u64,
91+
receiver: [u8; 20],
92+
revert_options: Option<gateway::RevertOptions>,
93+
) -> Result<()> {
94+
let gateway_program = ctx.accounts.gateway_program.to_account_info();
95+
96+
let cpi_accounts = gateway::cpi::accounts::Deposit {
97+
signer: ctx.accounts.signer.to_account_info(),
98+
pda: ctx.accounts.gateway_pda.to_account_info(),
99+
system_program: ctx.accounts.system_program.to_account_info(),
100+
};
101+
102+
let cpi_program = gateway_program;
103+
let cpi_ctx = CpiContext::new(cpi_program, cpi_accounts);
104+
105+
gateway::cpi::deposit(cpi_ctx, amount, receiver, revert_options)?;
106+
107+
Ok(())
108+
}
87109
}
88110

89111
#[derive(Accounts)]
@@ -122,6 +144,21 @@ pub struct OnRevert<'info> {
122144
pub system_program: Program<'info, System>,
123145
}
124146

147+
#[derive(Accounts)]
148+
pub struct TriggerDeposit<'info> {
149+
#[account(mut)]
150+
pub signer: Signer<'info>,
151+
152+
#[account(mut)]
153+
/// CHECK: Validated by the gateway program via seeds
154+
pub gateway_pda: UncheckedAccount<'info>,
155+
156+
/// CHECK: Only used for CPI
157+
pub gateway_program: UncheckedAccount<'info>,
158+
159+
pub system_program: Program<'info, System>,
160+
}
161+
125162
#[account]
126163
pub struct Pda {
127164
pub last_sender: [u8; 20],

tests/gateway.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -535,13 +535,33 @@ describe("Gateway", () => {
535535
}
536536
});
537537

538+
it("Deposit through connected program", async () => {
539+
const balanceBefore = await conn.getBalance(pdaAccount);
540+
await connectedProgram.methods
541+
.triggerDeposit(
542+
new anchor.BN(1_000_000_000),
543+
Array.from(address),
544+
revertOptions
545+
)
546+
.accounts({
547+
gatewayPda: pdaAccount,
548+
gatewayProgram: gatewayProgram.programId,
549+
})
550+
.rpc();
551+
552+
const balanceAfter = await conn.getBalance(pdaAccount);
553+
// amount + deposit fee
554+
expect(balanceAfter - balanceBefore).to.eq(1_000_000_000 + 2_000_000);
555+
});
556+
538557
it("Deposit and withdraw 0.5 SOL from Gateway with ECDSA signature", async () => {
558+
const balanceBefore = await conn.getBalance(pdaAccount);
539559
await gatewayProgram.methods
540560
.deposit(new anchor.BN(1_000_000_000), Array.from(address), revertOptions)
541561
.rpc();
542-
let bal1 = await conn.getBalance(pdaAccount);
562+
let balanceAfter = await conn.getBalance(pdaAccount);
543563
// amount + deposit fee
544-
expect(bal1).to.be.gte(1_000_000_000 + 2_000_000);
564+
expect(balanceAfter - balanceBefore).to.eq(1_000_000_000 + 2_000_000);
545565
const pdaAccountData = await gatewayProgram.account.pda.fetch(pdaAccount);
546566
const nonce = pdaAccountData.nonce;
547567
const amount = new anchor.BN(500000000);
@@ -578,7 +598,7 @@ describe("Gateway", () => {
578598
})
579599
.rpc();
580600
let bal2 = await conn.getBalance(pdaAccount);
581-
expect(bal2).to.be.eq(bal1 - 500_000_000);
601+
expect(bal2).to.be.eq(balanceAfter - 500_000_000);
582602
let bal3 = await conn.getBalance(to);
583603
expect(bal3).to.be.gte(500_000_000);
584604
});

0 commit comments

Comments
 (0)