Skip to content

Commit

Permalink
feat(solana-tests): bringing back few omitted tests
Browse files Browse the repository at this point in the history
Signed-off-by: Mariusz Jasuwienas <[email protected]>
  • Loading branch information
mj-blockydevs committed Aug 11, 2024
1 parent 00ecbb6 commit 958de15
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 36 deletions.
4 changes: 2 additions & 2 deletions mm2src/mm2_main/tests/docker_tests/docker_tests_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ use web3::{transports::Http, Web3};
use crate::docker_tests::eth_docker_tests::{erc20_contract_checksum, fill_eth, fill_eth_erc20_with_private_key,
geth_account, swap_contract};
#[cfg(feature = "enable-solana")]
use crate::docker_tests::solana_common_tests::PASSPHRASE;
use crate::docker_tests::solana_common_tests::SOL_PASSPHRASE;

#[cfg(feature = "enable-solana")]
pub const SOL_USDC_PUBKEY: &str = "4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU";
Expand Down Expand Up @@ -1123,7 +1123,7 @@ pub fn _solana_supplied_node() -> MarketMakerIt {
"gui": "nogui",
"netid": 9000,
"dht": "on", // Enable DHT without delay.
"passphrase": PASSPHRASE,
"passphrase": SOL_PASSPHRASE,
"coins": coins,
"rpc_password": "pass",
"i_am_seed": true,
Expand Down
4 changes: 2 additions & 2 deletions mm2src/mm2_main/tests/docker_tests/solana_common_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use std::{collections::HashMap,

pub const SOLANA_CLIENT_URL: &str = "http://localhost:8899";
pub const SOL: &str = "SOL";
pub const PASSPHRASE: &str = "federal stay trigger hour exist success game vapor become comfort action phone bright ill target wild nasty crumble dune close rare fabric hen iron";
pub const ADDITIONAL_PASSPHRASE: &str =
pub const SOL_PASSPHRASE: &str = "federal stay trigger hour exist success game vapor become comfort action phone bright ill target wild nasty crumble dune close rare fabric hen iron";
pub const SOL_ADDITIONAL_PASSPHRASE: &str =
"spice describe gravity federal blast come thank unfair canal monkey style afraid";
pub const PROGRAM_ID: &str = "GCJUXKH4VeKzEtr9YgwaNWC3dJonFgsM3yMiBa64CZ8m";
pub const NON_EXISTENT_PASSPHRASE: &str = "non existent passphrase";
Expand Down
82 changes: 74 additions & 8 deletions mm2src/mm2_main/tests/docker_tests/solana_tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::docker_tests::{docker_tests_common::*,
solana_common_tests::{generate_key_pair_from_iguana_seed, solana_coin_for_test, SolanaNet,
ACCOUNT_PUBKEY, NON_EXISTENT_PASSPHRASE, PASSPHRASE, PROGRAM_ID, SOL,
SOLANA_CLIENT_URL}};
ACCOUNT_PUBKEY, NON_EXISTENT_PASSPHRASE, PROGRAM_ID, SOL,
SOLANA_CLIENT_URL, SOL_PASSPHRASE}};
use bitcrypto::sha256;
use coins::{solana::{solana_common::lamports_to_sol,
solana_sdk::{bs58, pubkey::Pubkey, signer::Signer}},
Expand Down Expand Up @@ -140,7 +140,7 @@ fn test_disable_solana_platform_coin_with_tokens() {
#[test]
#[cfg(not(target_arch = "wasm32"))]
fn solana_keypair_from_secp() {
let solana_key_pair = generate_key_pair_from_iguana_seed(PASSPHRASE.to_string());
let solana_key_pair = generate_key_pair_from_iguana_seed(SOL_PASSPHRASE.to_string());
assert_eq!(ACCOUNT_PUBKEY, solana_key_pair.pubkey().to_string());

let other_solana_keypair = generate_key_pair_from_iguana_seed("bob passphrase".to_string());
Expand All @@ -150,10 +150,76 @@ fn solana_keypair_from_secp() {
);
}

#[test]
#[cfg(not(target_arch = "wasm32"))]
pub fn solana_coin_creation() {
let (_, sol_coin) = solana_coin_for_test(SOL_PASSPHRASE.to_owned(), SolanaNet::Local);
assert_eq!(
sol_coin.my_address().unwrap(),
"FJktmyjV9aBHEShT4hfnLpr9ELywdwVtEL1w1rSWgbVf"
);
}

#[test]
#[cfg(not(target_arch = "wasm32"))]
pub fn test_sign_message() {
let passphrase = "spice describe gravity federal blast come thank unfair canal monkey style afraid".to_string();
let (_, sol_coin) = solana_coin_for_test(passphrase, SolanaNet::Local);
let signature = sol_coin.sign_message("test").unwrap();
assert_eq!(
signature,
"4dzKwEteN8nch76zPMEjPX19RsaQwGTxsbtfg2bwGTkGenLfrdm31zvn9GH5rvaJBwivp6ESXx1KYR672ngs3UfF"
);
}

#[test]
#[cfg(not(target_arch = "wasm32"))]
pub fn test_verify_message() {
let passphrase = "spice describe gravity federal blast come thank unfair canal monkey style afraid".to_string();
let (_, sol_coin) = solana_coin_for_test(passphrase, SolanaNet::Local);
let is_valid = sol_coin
.verify_message(
"4dzKwEteN8nch76zPMEjPX19RsaQwGTxsbtfg2bwGTkGenLfrdm31zvn9GH5rvaJBwivp6ESXx1KYR672ngs3UfF",
"test",
"8UF6jSVE1jW8mSiGqt8Hft1rLwPjdKLaTfhkNozFwoAG",
)
.unwrap();
assert!(is_valid);
}

#[test]
#[cfg(not(target_arch = "wasm32"))]
pub fn solana_validate_address() {
let (_, sol_coin) = solana_coin_for_test(SOL_PASSPHRASE.to_owned(), SolanaNet::Local);

// invalid len
let res = sol_coin.validate_address("invalidaddressobviously");
assert!(!res.is_valid);
let res = sol_coin.validate_address("GMtMFbuVgjDnzsBd3LLBfM4X8RyYcDGCM92tPq2PG6B2");
assert!(res.is_valid);

// Typo
let res = sol_coin.validate_address("Fr8fraJXAe1cFU81mF7NhHTrUzXjZAJkQE1gUQ11riH");
assert!(!res.is_valid);

// invalid len
let res = sol_coin.validate_address("r8fraJXAe1cFU81mF7NhHTrUzXjZAJkQE1gUQ11riHn");
assert!(!res.is_valid);
}

#[test]
#[cfg(not(target_arch = "wasm32"))]
pub fn solana_block_height() {
let (_, sol_coin) = solana_coin_for_test(SOL_PASSPHRASE.to_owned(), SolanaNet::Local);
let res = block_on(sol_coin.current_block().compat()).unwrap();
log!("block is : {}", res);
assert!(res > 0);
}

#[test]
#[cfg(not(target_arch = "wasm32"))]
fn solana_transaction_simulation() {
let (_, sol_coin) = solana_coin_for_test(PASSPHRASE.to_owned(), SolanaNet::Local);
let (_, sol_coin) = solana_coin_for_test(SOL_PASSPHRASE.to_owned(), SolanaNet::Local);
let request_amount = BigDecimal::try_from(0.0001).unwrap();
let valid_tx_details = block_on(
sol_coin
Expand Down Expand Up @@ -276,7 +342,7 @@ fn solana_transaction_simulations_max() {
#[test]
#[cfg(not(target_arch = "wasm32"))]
fn solana_test_transactions() {
let (_, sol_coin) = solana_coin_for_test(PASSPHRASE.to_owned(), SolanaNet::Local);
let (_, sol_coin) = solana_coin_for_test(SOL_PASSPHRASE.to_owned(), SolanaNet::Local);
let valid_tx_details = block_on(
sol_coin
.withdraw(WithdrawRequest::new(
Expand Down Expand Up @@ -310,7 +376,7 @@ fn solana_test_transactions() {
#[ignore]
#[cfg(not(target_arch = "wasm32"))]
fn solana_test_tx_history() {
let (_, sol_coin) = solana_coin_for_test(PASSPHRASE.to_owned(), SolanaNet::Local);
let (_, sol_coin) = solana_coin_for_test(SOL_PASSPHRASE.to_owned(), SolanaNet::Local);
let valid_tx_details = block_on(
sol_coin
.withdraw(WithdrawRequest::new(
Expand Down Expand Up @@ -341,7 +407,7 @@ fn solana_test_tx_history() {

#[test]
fn solana_coin_send_and_refund_maker_payment() {
let (_, coin) = solana_coin_for_test(PASSPHRASE.to_owned(), SolanaNet::Local);
let (_, coin) = solana_coin_for_test(SOL_PASSPHRASE.to_owned(), SolanaNet::Local);
let solana_program_id = bs58::decode(PROGRAM_ID).into_vec().unwrap_or_else(|e| {
log!("Failed to decode program ID: {}", e);
Vec::new()
Expand Down Expand Up @@ -386,7 +452,7 @@ fn solana_coin_send_and_refund_maker_payment() {

#[test]
fn solana_coin_send_and_spend_maker_payment() {
let (_, coin) = solana_coin_for_test(PASSPHRASE.to_owned(), SolanaNet::Local);
let (_, coin) = solana_coin_for_test(SOL_PASSPHRASE.to_owned(), SolanaNet::Local);
let solana_program_id = bs58::decode(PROGRAM_ID).into_vec().unwrap_or_else(|e| {
log!("Failed to decode program ID: {}", e);
Vec::new()
Expand Down
47 changes: 23 additions & 24 deletions mm2src/mm2_main/tests/docker_tests/spl_tests.rs
Original file line number Diff line number Diff line change
@@ -1,62 +1,61 @@
use crate::docker_tests::{docker_tests_common::SOL_USDC_PUBKEY,
solana_common_tests::{solana_coin_for_test, spl_coin_for_test, SolanaNet, ACCOUNT_PUBKEY,
ADDITIONAL_PASSPHRASE, PASSPHRASE}};
SOL_ADDITIONAL_PASSPHRASE, SOL_PASSPHRASE}};
use coins::{solana::solana_sdk, MarketCoinOps, MmCoin, WithdrawRequest};
use common::{block_on, Future01CompatExt};
use mm2_number::BigDecimal;
use std::{env, ops::Neg, str::FromStr};

const USDC: &str = "USDC";
const ADEX: &str = "ADEX";
const WSOL: &str = "WSOL";
const ADEX_PUBKEY: &str = "3e9KpjwQejx9Y7WkfaXJTybH6ecG7AdXoAoxk279hdFh";
const WSOL_PUBKEY: &str = "So11111111111111111111111111111111111111112";
const SIGNATURE: &str = "4dzKwEteN8nch76zPMEjPX19RsaQwGTxsbtfg2bwGTkGenLfrdm31zvn9GH5rvaJBwivp6ESXx1KYR672ngs3UfF";
const VERIFY_PUBKEY: &str = "8UF6jSVE1jW8mSiGqt8Hft1rLwPjdKLaTfhkNozFwoAG";
const VERIFY_MESSAGE: &str = "test";
const PUBKEY_FOR_USDC: &str = "CpMah17kQEL2wqyMKt3mZBdTnZbkbfx4nqmQMFDP5vwp";

#[test]
#[cfg(not(target_arch = "wasm32"))]
fn spl_coin_creation() {
let (_, sol_coin) = solana_coin_for_test(PASSPHRASE.to_owned(), SolanaNet::Local);
let (_, sol_coin) = solana_coin_for_test(SOL_PASSPHRASE.to_owned(), SolanaNet::Local);
let sol_spl_usdc_coin = spl_coin_for_test(
sol_coin,
USDC.to_string(),
6,
solana_sdk::pubkey::Pubkey::from_str(ADEX_PUBKEY).unwrap(),
solana_sdk::pubkey::Pubkey::from_str("3e9KpjwQejx9Y7WkfaXJTybH6ecG7AdXoAoxk279hdFh").unwrap(),
);

log!("address: {}", sol_spl_usdc_coin.my_address().unwrap());
assert_eq!(sol_spl_usdc_coin.my_address().unwrap(), ACCOUNT_PUBKEY,);
assert_eq!(sol_spl_usdc_coin.my_address().unwrap(), ACCOUNT_PUBKEY);
}

#[test]
#[cfg(not(target_arch = "wasm32"))]
fn test_sign_message() {
let (_, sol_coin) = solana_coin_for_test(ADDITIONAL_PASSPHRASE.to_owned(), SolanaNet::Local);
let (_, sol_coin) = solana_coin_for_test(SOL_ADDITIONAL_PASSPHRASE.to_owned(), SolanaNet::Local);
let sol_spl_usdc_coin = spl_coin_for_test(
sol_coin,
USDC.to_string(),
6,
solana_sdk::pubkey::Pubkey::from_str(PUBKEY_FOR_USDC).unwrap(),
solana_sdk::pubkey::Pubkey::from_str("CpMah17kQEL2wqyMKt3mZBdTnZbkbfx4nqmQMFDP5vwp").unwrap(),
);
let signature = sol_spl_usdc_coin.sign_message("TEST").unwrap();
assert_eq!(
signature,
"4dzKwEteN8nch76zPMEjPX19RsaQwGTxsbtfg2bwGTkGenLfrdm31zvn9GH5rvaJBwivp6ESXx1KYR672ngs3UfF"
);
let signature = sol_spl_usdc_coin.sign_message(VERIFY_MESSAGE).unwrap();
assert_eq!(signature, SIGNATURE);
}

#[test]
#[cfg(not(target_arch = "wasm32"))]
fn test_verify_message() {
let (_, sol_coin) = solana_coin_for_test(ADDITIONAL_PASSPHRASE.to_owned(), SolanaNet::Local);
let (_, sol_coin) = solana_coin_for_test(SOL_ADDITIONAL_PASSPHRASE.to_owned(), SolanaNet::Local);
let sol_spl_usdc_coin = spl_coin_for_test(
sol_coin,
USDC.to_string(),
6,
solana_sdk::pubkey::Pubkey::from_str(PUBKEY_FOR_USDC).unwrap(),
solana_sdk::pubkey::Pubkey::from_str("CpMah17kQEL2wqyMKt3mZBdTnZbkbfx4nqmQMFDP5vwp").unwrap(),
);
let is_valid = sol_spl_usdc_coin
.verify_message(SIGNATURE, VERIFY_MESSAGE, VERIFY_PUBKEY)
.verify_message(
"4dzKwEteN8nch76zPMEjPX19RsaQwGTxsbtfg2bwGTkGenLfrdm31zvn9GH5rvaJBwivp6ESXx1KYR672ngs3UfF",
"test",
"8UF6jSVE1jW8mSiGqt8Hft1rLwPjdKLaTfhkNozFwoAG",
)
.unwrap();
assert!(is_valid);
}
Expand All @@ -65,10 +64,10 @@ fn test_verify_message() {
#[cfg(not(target_arch = "wasm32"))]
fn spl_my_balance() {
let adex_token_address = env::var("ADEX_TOKEN_ADDRESS").expect("ADEX_TOKEN_ADDRESS not set");
let (_, sol_coin) = solana_coin_for_test(PASSPHRASE.to_owned(), SolanaNet::Local);
let (_, sol_coin) = solana_coin_for_test(SOL_PASSPHRASE.to_owned(), SolanaNet::Local);
let sol_spl_adex_coin = spl_coin_for_test(
sol_coin.clone(),
ADEX.to_string(),
"ADEX".to_string(),
9,
solana_sdk::pubkey::Pubkey::from_str(&adex_token_address).unwrap(),
);
Expand All @@ -79,9 +78,9 @@ fn spl_my_balance() {

let sol_spl_wsol_coin = spl_coin_for_test(
sol_coin,
WSOL.to_string(),
"WSOL".to_string(),
8,
solana_sdk::pubkey::Pubkey::from_str(WSOL_PUBKEY).unwrap(),
solana_sdk::pubkey::Pubkey::from_str("So11111111111111111111111111111111111111112").unwrap(),
);
let res = block_on(sol_spl_wsol_coin.my_balance().compat()).unwrap();
assert_eq!(res.spendable, BigDecimal::from(0));
Expand All @@ -92,7 +91,7 @@ fn spl_my_balance() {
#[ignore]
#[cfg(not(target_arch = "wasm32"))]
fn test_spl_transactions() {
let (_, sol_coin) = solana_coin_for_test(PASSPHRASE.to_owned(), SolanaNet::Local);
let (_, sol_coin) = solana_coin_for_test(SOL_PASSPHRASE.to_owned(), SolanaNet::Local);
let usdc_sol_coin = spl_coin_for_test(
sol_coin,
USDC.to_string(),
Expand Down

0 comments on commit 958de15

Please sign in to comment.