Skip to content

Commit

Permalink
fix: native sol out in sendFundsToUser
Browse files Browse the repository at this point in the history
  • Loading branch information
composable-kvn committed Dec 19, 2024
1 parent 40f1bc5 commit a118535
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 31 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ ascii = "1.1.0"
base64 = { version = "0.21", default-features = false, features = ["alloc"] }
blake3 = { version = "1.3.3", default-features = false }
borsh = { version = "0.10.3", default-features = false }
bs58 = { version = "0.5.0", default-features = false }
bs58 = { version = "0.5.0", features = ["alloc"] }
bytemuck = { version = "1.14", default-features = false }
chrono = { version = "0.4", default-features = false }
clap = { version = "4.4.18", features = ["derive"] }
Expand Down
1 change: 1 addition & 0 deletions solana/bridge-escrow/programs/bridge-escrow/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ trie-ids = { workspace = true, features = ["borsh"] }
wasm = { workspace = true }
itertools = "0.10.5"
solana-ibc = { workspace = true, features = ["cpi"] }
bs58.workspace = true

[dev-dependencies]
anchor-client.workspace = true
Expand Down
13 changes: 11 additions & 2 deletions solana/bridge-escrow/programs/bridge-escrow/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,17 +313,24 @@ pub mod bridge_escrow {
// Handle Native SOL Transfer
if intent.token_out == System::id().to_string() {
// Perform SOL transfer from Solver to User

let solver = accounts.solver.clone();
let out_user_account = accounts.receiver.clone().unwrap();

// check `receiver` address if it is same as `intent.user_out`
require!(intent.user_out == out_user_account.key.to_string(), ErrorCode::InvalidSolOut);

let ix = solana_program::system_instruction::transfer(
solver.key,
&intent.user_in,
out_user_account.key,
amount_out,
);

invoke(
&ix,
&[
solver.to_account_info(),
accounts.user_token_out_account.to_account_info(),
out_user_account.to_account_info(),
accounts.system_program.to_account_info(),
],
)?;
Expand Down Expand Up @@ -1007,6 +1014,8 @@ pub enum ErrorCode {
InvalidMemoFormat,
#[msg("Invalid token out")]
InvalidTokenOut,
#[msg("Invalid Sol Receiver")]
InvalidSolOut,
#[msg("Auctioneer cannot update an amountOut less than current amountOut")]
InvalidAmountOut,
#[msg("new_intent.token_in != ctx.accounts.user_token_account.mint")]
Expand Down
62 changes: 34 additions & 28 deletions solana/bridge-escrow/programs/bridge-escrow/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use anchor_spl::associated_token::{self, get_associated_token_address};
use anyhow::Result;
use spl_token::instruction::initialize_mint2;
use spl_token::solana_program::system_instruction::create_account;
use bs58;

use crate::IntentPayload;

Expand Down Expand Up @@ -58,25 +59,34 @@ fn simulate_tx(client: RpcClient, transaction: Transaction) {
}

#[test]
//#[ignore = "Requires local validator to run"]
// test on devnet
fn test_native_mint_transfer() -> Result<()> {
// Setup the client and wallet
let auctioneer =
Rc::new(read_keypair_file("../../../solana-ibc/keypair.json").unwrap());

let private_key_bytes = bs58::decode("").into_vec()?;
let auctioneer = Rc::new(Keypair::from_bytes(&private_key_bytes)?);

let private_key_bytes = bs58::decode("").into_vec()?;
let solver = Rc::new(Keypair::from_bytes(&private_key_bytes)?);

let private_key_bytes = bs58::decode("").into_vec()?;
let user = Rc::new(Keypair::from_bytes(&private_key_bytes)?);

// let auctioneer =
// Rc::new(read_keypair_file("../../../solana-ibc/keypair.json").unwrap());

let client = Client::new_with_options(
Cluster::from_str("http://127.0.0.1:8899").unwrap(),//Cluster::Localnet,
Cluster::from_str("https://late-bitter-panorama.solana-devnet.quiknode.pro/702f2ed5c8c7efeee31dd889a5653d0ec218ceb9/").unwrap(),//Cluster::Localnet,
auctioneer.clone(),
CommitmentConfig::processed(),
);

let program = client.program(crate::ID)?;
let sol_rpc_client = program.rpc();

let lamports = 20_000_000_000;

let solver = Rc::new(Keypair::new());
let user = Rc::new(Keypair::new());
// let lamports = 20_000_000_000;
// let solver = Rc::new(Keypair::new());
// let user = Rc::new(Keypair::new());
let token_in_keypair = Keypair::new();
let token_in = token_in_keypair.pubkey();
let token_out_keypair = Keypair::new();
Expand All @@ -89,9 +99,9 @@ fn test_native_mint_transfer() -> Result<()> {
println!("User {:?}", user.to_bytes());
println!("Solver {:?}", solver.to_bytes());

airdrop(&program_rpc, auctioneer.pubkey(), lamports);
airdrop(&program_rpc, user.pubkey(), lamports);
airdrop(&program_rpc, solver.pubkey(), lamports);
// airdrop(&program_rpc, auctioneer.pubkey(), lamports);
// airdrop(&program_rpc, user.pubkey(), lamports);
// airdrop(&program_rpc, solver.pubkey(), lamports);

let auctioneer_state =
Pubkey::find_program_address(&[crate::AUCTIONEER_SEED], &crate::ID).0;
Expand Down Expand Up @@ -208,8 +218,7 @@ fn test_native_mint_transfer() -> Result<()> {
// Initialize the program to define the auctioneer
println!("\nInitializing the program");

if !program_rpc.get_account(&auctioneer_state).is_ok() {
let sig = program
let sig = program
.request()
.accounts(crate::accounts::Initialize {
authority: auctioneer.pubkey(),
Expand All @@ -226,12 +235,11 @@ fn test_native_mint_transfer() -> Result<()> {
skip_preflight: true,
..Default::default()
})
// .signed_transaction()
.unwrap();

println!("Signature: {}", sig);
} else {
println!("Program already initialized. Skipping Initialization ...");
}
println!("Signature: {}", sig);



let token_in_escrow_addr =
Expand Down Expand Up @@ -260,7 +268,7 @@ fn test_native_mint_transfer() -> Result<()> {
intent_id: intent_id.clone(),
user_in: user.pubkey(), // Must match the ctx.accounts.user key in the contract
user_out: user.pubkey().to_string(),
token_in: Pubkey::default(), // token_in,
token_in: token_in,
amount_in: TRANSFER_AMOUNT,
token_out: Pubkey::default().to_string(),// token_out.to_string(),
amount_out: amount_out.to_string(), // Amount out as a string
Expand All @@ -273,10 +281,10 @@ fn test_native_mint_transfer() -> Result<()> {
.request()
.accounts(crate::accounts::EscrowAndStoreIntent {
user: user.pubkey(),
user_token_account: Some(user_token_in_addr),
user_token_account: user_token_in_addr,
auctioneer_state,
token_mint: Some(token_in),
escrow_token_account: Some(token_in_escrow_addr),
token_mint: token_in,
escrow_token_account: token_in_escrow_addr,
intent: intent_state,
token_program: anchor_spl::token::ID,
associated_token_program: associated_token::ID,
Expand Down Expand Up @@ -347,13 +355,12 @@ fn test_native_mint_transfer() -> Result<()> {
solver_token_in_account: Some(solver_token_in_addr),
solver_token_out_account: solver_token_out_addr,
user_token_out_account: user_token_out_addr,
user_account: Some(user.pubkey()),
token_program: anchor_spl::token::ID,
associated_token_program: anchor_spl::associated_token::ID,
system_program: anchor_lang::solana_program::system_program::ID,
// Cross-chain related fields, set to None since it's single domain
ibc_program: None,
receiver: None,
receiver: Some(user.pubkey()),
storage: None,
trie: None,
chain: None,
Expand Down Expand Up @@ -615,10 +622,10 @@ fn escrow_bridge_program() -> Result<()> {
.request()
.accounts(crate::accounts::EscrowAndStoreIntent {
user: user.pubkey(),
user_token_account: Some(user_token_in_addr),
user_token_account: user_token_in_addr,
auctioneer_state,
token_mint: Some(token_in),
escrow_token_account: Some(token_in_escrow_addr),
token_mint: token_in,
escrow_token_account: token_in_escrow_addr,
intent: intent_state,
token_program: anchor_spl::token::ID,
associated_token_program: associated_token::ID,
Expand Down Expand Up @@ -689,13 +696,12 @@ fn escrow_bridge_program() -> Result<()> {
solver_token_in_account: Some(solver_token_in_addr),
solver_token_out_account: solver_token_out_addr,
user_token_out_account: user_token_out_addr,
user_account: Some(user.pubkey()),
token_program: anchor_spl::token::ID,
associated_token_program: anchor_spl::associated_token::ID,
system_program: anchor_lang::solana_program::system_program::ID,
// Cross-chain related fields, set to None since it's single domain
ibc_program: None,
receiver: None,
receiver: Some(user.pubkey()),
storage: None,
trie: None,
chain: None,
Expand Down

0 comments on commit a118535

Please sign in to comment.