Skip to content

Commit

Permalink
Gateway: add memo parameter to deposit instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
brewmaster012 committed Jun 22, 2024
1 parent cee00c1 commit f255646
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ signer/fee payer is.

In both `withdraw` and `withdrawAndCall` instructions,
the ECDSA signed message_hash must commit to the
`nonce`, `amount`, and recipient address. See the
`nonce`, `amount`, and `to` address. See the
check in these instructions like:
```rust
let mut concatenated_buffer = Vec::new();
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
},
"dependencies": {
"@coral-xyz/anchor": "^0.30.0",
"@solana/spl-memo": "^0.2.5",
"@solana/spl-token": "^0.4.6",
"elliptic": "^6.5.5",
"ethereumjs-util": "^7.1.5",
Expand Down
10 changes: 8 additions & 2 deletions programs/protocol-contracts-solana/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ pub enum Errors {
DepositToAddressMismatch,
#[msg("MessageHashMismatch")]
MessageHashMismatch,
#[msg("MemoLengthExceeded")]
MemoLengthExceeded,
}

declare_id!("94U5AHQMKkV5txNJ17QPXWoh474PheGou6cNP2FEuL1d");
Expand All @@ -45,7 +47,8 @@ pub mod gateway {
Ok(())
}

pub fn deposit(ctx: Context<Deposit>, amount: u64) -> Result<()> {
pub fn deposit(ctx: Context<Deposit>, amount: u64, memo: Vec<u8>) -> Result<()> {
require!(memo.len() <= 512, Errors::MemoLengthExceeded);
let cpi_context = CpiContext::new(
ctx.accounts.system_program.to_account_info(),
system_program::Transfer {
Expand All @@ -54,11 +57,13 @@ pub mod gateway {
},
);
system_program::transfer(cpi_context, amount)?;
msg!("{:?} deposits {:?} lamports to PDA", ctx.accounts.signer.key(), amount);

Ok(())
}

pub fn deposit_spl_token(ctx: Context<DepositSplToken>, amount: u64) -> Result<()> {
pub fn deposit_spl_token(ctx: Context<DepositSplToken>, amount: u64, memo: Vec<u8>) -> Result<()> {
require!(memo.len() <= 512, Errors::MemoLengthExceeded);
let token = &ctx.accounts.token_program;
let from = &ctx.accounts.from;

Expand All @@ -78,6 +83,7 @@ pub mod gateway {
},
);
transfer(xfer_ctx, amount)?;

msg!("deposit spl token successfully");

Ok(())
Expand Down
26 changes: 20 additions & 6 deletions tests/protocol-contracts-solana.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import * as anchor from "@coral-xyz/anchor";
import {Program} 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';
import { bufferToHex } from 'ethereumjs-util';
import {expect} from 'chai';
import {ecdsaRecover} from 'secp256k1';



const ec = new EC('secp256k1');
const keyPair = ec.genKeyPair();
console.log("private key", keyPair.getPrivate('hex'));
Expand Down Expand Up @@ -124,15 +127,26 @@ describe("some tests", () => {
true
);
console.log("pda_ata address", pda_ata.address.toString());
await gatewayProgram.methods.depositSplToken(new anchor.BN(1_000_000)).accounts(
const tx = new web3.Transaction();
const memoInst = memo.createMemoInstruction(
"this is a memo",
[wallet.publicKey],
);
tx.add(memoInst);
const depositInst = await gatewayProgram.methods.depositSplToken(
new anchor.BN(1_000_000),
Buffer.from("hello", "utf-8")).accounts(
{
from: tokenAccount.address,
to: pda_ata.address,
}
)
.rpc();
).instruction();
tx.add(depositInst);
const txsig = await anchor.web3.sendAndConfirmTransaction(conn, tx, [wallet]);


try {
await gatewayProgram.methods.depositSplToken(new anchor.BN(1_000_000)).accounts(
await gatewayProgram.methods.depositSplToken(new anchor.BN(1_000_000), Buffer.from("world", "utf-8")).accounts(
{
from: tokenAccount.address,
to: wallet_ata,
Expand Down Expand Up @@ -215,7 +229,7 @@ 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)).accounts({pda: pdaAccount}).rpc();
await gatewayProgram.methods.deposit(new anchor.BN(1_000_000_000), Buffer.from("hello")).accounts({pda: pdaAccount}).rpc();
// const transaction = new anchor.web3.Transaction();
// transaction.add(
// web3.SystemProgram.transfer({
Expand Down
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,13 @@
"@solana/codecs-core" "2.0.0-preview.2"
"@solana/codecs-numbers" "2.0.0-preview.2"

"@solana/spl-memo@^0.2.5":
version "0.2.5"
resolved "https://registry.npmjs.org/@solana/spl-memo/-/spl-memo-0.2.5.tgz"
integrity sha512-0Zx5t3gAdcHlRTt2O3RgGlni1x7vV7Xq7j4z9q8kKOMgU03PyoTbFQ/BSYCcICHzkaqD7ZxAiaJ6dlXolg01oA==
dependencies:
buffer "^6.0.3"

"@solana/spl-token-group@^0.0.4":
version "0.0.4"
resolved "https://registry.npmjs.org/@solana/spl-token-group/-/spl-token-group-0.0.4.tgz"
Expand Down

0 comments on commit f255646

Please sign in to comment.