Skip to content

Commit

Permalink
Merge pull request #29 from zeta-chain/deposit-and-call
Browse files Browse the repository at this point in the history
separate deposit into two instructions: deposit, deposit_and_call
  • Loading branch information
brewmaster012 authored Sep 13, 2024
2 parents 3e31421 + 3081f3f commit 5ef421e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 23 deletions.
38 changes: 33 additions & 5 deletions programs/protocol-contracts-solana/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,35 @@ pub mod gateway {
Ok(())
}

pub fn deposit(ctx: Context<Deposit>, amount: u64, memo: Vec<u8>) -> Result<()> {
require!(memo.len() >= 20, Errors::MemoLengthTooShort);
require!(memo.len() <= 512, Errors::MemoLengthExceeded);
pub fn deposit(ctx: Context<Deposit>, amount: u64, receiver: [u8; 20]) -> Result<()> {
let pda = &mut ctx.accounts.pda;
require!(!pda.deposit_paused, Errors::DepositPaused);

let cpi_context = CpiContext::new(
ctx.accounts.system_program.to_account_info(),
system_program::Transfer {
from: ctx.accounts.signer.to_account_info().clone(),
to: ctx.accounts.pda.to_account_info().clone(),
},
);
system_program::transfer(cpi_context, amount)?;
msg!(
"{:?} deposits {:?} lamports to PDA; receiver {:?}",
ctx.accounts.signer.key(),
amount,
receiver,
);

Ok(())
}

pub fn deposit_and_call(
ctx: Context<Deposit>,
amount: u64,
receiver: [u8; 20],
message: Vec<u8>,
) -> Result<()> {
require!(message.len() <= 512, Errors::MemoLengthExceeded);

let pda = &mut ctx.accounts.pda;
require!(!pda.deposit_paused, Errors::DepositPaused);
Expand All @@ -100,9 +126,11 @@ pub mod gateway {
);
system_program::transfer(cpi_context, amount)?;
msg!(
"{:?} deposits {:?} lamports to PDA",
"{:?} deposits {:?} lamports to PDA and call contract {:?} with message {:?}",
ctx.accounts.signer.key(),
amount
amount,
receiver,
message,
);

Ok(())
Expand Down
27 changes: 9 additions & 18 deletions tests/protocol-contracts-solana.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as anchor from "@coral-xyz/anchor";
import {Program, web3} from "@coral-xyz/anchor";
import {Gateway} from "../target/types/gateway";
import * as spl from "@solana/spl-token";
import * as memo from "@solana/spl-memo";
import {randomFillSync} from 'crypto';
import { ec as EC } from 'elliptic';
import { keccak256 } from 'ethereumjs-util';
Expand Down Expand Up @@ -73,28 +72,13 @@ describe("some tests", () => {


it("deposit and withdraw 0.5 SOL from Gateway with ECDSA signature", async () => {
await gatewayProgram.methods.deposit(new anchor.BN(1_000_000_000), address).accounts({pda: pdaAccount}).rpc();
// const transaction = new anchor.web3.Transaction();
// transaction.add(
// web3.SystemProgram.transfer({
// fromPubkey: wallet.publicKey,
// toPubkey: pdaAccount,
// lamports: 1_000_000_000,
// })
// );
// await anchor.web3.sendAndConfirmTransaction(conn, transaction, [wallet]);
await gatewayProgram.methods.deposit(new anchor.BN(1_000_000_000), Array.from(address)).accounts({pda: pdaAccount}).rpc();
let bal1 = await conn.getBalance(pdaAccount);
console.log("pda account balance", bal1);
expect(bal1).to.be.gte(1_000_000_000);

const pdaAccountData = await gatewayProgram.account.pda.fetch(pdaAccount);
console.log(`pda account data: nonce ${pdaAccountData.nonce}`);
// const message_hash = fromHexString(
// "0a1e2723bd7f1996832b7ed7406df8ad975deba1aa04020b5bfc3e6fe70ecc29"
// );
// const signature = fromHexString(
// "58be181f57b2d56b0c252127c9874a8fbe5ebd04f7632fb3966935a3e9a765807813692cebcbf3416cb1053ad9c8c83af471ea828242cca22076dd04ddbcd253"
// );
const nonce = pdaAccountData.nonce;
const amount = new anchor.BN(500000000);
const to = wallet.publicKey;
Expand Down Expand Up @@ -125,6 +109,13 @@ describe("some tests", () => {
expect(bal3).to.be.gte(500_000_000);
})

it("deposit and call", async () => {
let bal1 = await conn.getBalance(pdaAccount);
await gatewayProgram.methods.depositAndCall(new anchor.BN(1_000_000_000), Array.from(address), Buffer.from("hello", "utf-8")).accounts({pda: pdaAccount}).rpc();
let bal2 = await conn.getBalance(pdaAccount);
expect(bal2-bal1).to.be.gte(1_000_000_000);
})

it("update TSS address", async () => {
const newTss = new Uint8Array(20);
randomFillSync(newTss);
Expand Down Expand Up @@ -158,7 +149,7 @@ describe("some tests", () => {

// now try deposit, should fail
try {
await gatewayProgram.methods.deposit(new anchor.BN(1_000_000), address).accounts({pda: pdaAccount}).rpc();
await gatewayProgram.methods.deposit(new anchor.BN(1_000_000), Array.from(address)).accounts({pda: pdaAccount}).rpc();
} catch (err) {
console.log("Error message: ", err.message);
expect(err).to.be.instanceof(anchor.AnchorError);
Expand Down

0 comments on commit 5ef421e

Please sign in to comment.