Skip to content

Commit 8a2fe1d

Browse files
Add test for fetching liquidity pool
1 parent 5aea49e commit 8a2fe1d

File tree

1 file changed

+134
-43
lines changed
  • cmd/crates/soroban-test/tests/it/integration/ledger

1 file changed

+134
-43
lines changed

cmd/crates/soroban-test/tests/it/integration/ledger/entry.rs

Lines changed: 134 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ use soroban_cli::{
44
AccountId, AlphaNum4, AssetCode4, ConfigSettingId, ContractDataDurability, Hash,
55
LedgerEntryData, LedgerKey, LedgerKeyAccount, LedgerKeyConfigSetting, LedgerKeyContractCode,
66
LedgerKeyContractData, LedgerKeyData, LedgerKeyTrustLine, Limits, PublicKey, ScAddress, ScVal,
7-
String64, StringM, TrustLineAsset, Uint256, WriteXdr,ClaimantV0, Claimant, Operation,ClaimPredicate, Asset, CreateClaimableBalanceOp, OperationBody, ClaimantType, VecM, Transaction, TransactionEnvelope, TransactionResult, TransactionResultResult, ClaimableBalanceId, OperationResultTr, OperationResult, CreateClaimableBalanceResult, LedgerKeyClaimableBalance
7+
String64, StringM, TrustLineAsset, Uint256, WriteXdr,ClaimantV0, Claimant, Operation,ClaimPredicate, Asset, CreateClaimableBalanceOp, OperationBody, VecM, TransactionEnvelope, TransactionResult, TransactionResultResult, ClaimableBalanceId, OperationResultTr, OperationResult, CreateClaimableBalanceResult, LedgerKeyClaimableBalance,ChangeTrustOp, ChangeTrustAsset, LiquidityPoolParameters, LiquidityPoolConstantProductParameters, LedgerKeyLiquidityPool, PoolId
88
},
9-
tx::builder::{self, asset, TxExt},
9+
tx::builder::TxExt,
1010
config::{
11-
self,
12-
address::{self, UnresolvedMuxedAccount},
11+
address::UnresolvedMuxedAccount,
1312
locator,
14-
data, network, secret,
1513
},
1614
};
15+
use sha2::{Digest, Sha256};
16+
1717

1818
use soroban_rpc::GetTransactionResponse;
1919
use soroban_rpc::FullLedgerEntries;
@@ -25,7 +25,7 @@ use stellar_strkey::{ed25519::PublicKey as StrkeyPublicKeyEd25519, Contract};
2525
use crate::integration::util::{deploy_contract, test_address, DeployOptions, HELLO_WORLD};
2626

2727
// account data tests
28-
// todo: test with--offer,
28+
// todo: test with --offer,
2929
#[tokio::test]
3030
async fn ledger_entry_account_only_with_account_alias() {
3131
let sandbox = &TestEnv::new();
@@ -126,6 +126,7 @@ async fn ledger_entry_account_asset_usdc() {
126126
issue_asset(
127127
sandbox,
128128
&test_account_address,
129+
&issuer_alias,
129130
asset,
130131
limit,
131132
initial_balance,
@@ -148,7 +149,7 @@ async fn ledger_entry_account_asset_usdc() {
148149

149150
let (account_id, expected_account_key) =
150151
expected_account_ledger_key(&test_account_address).await;
151-
let (issuer_account_id, _) = expected_account_ledger_key(&issuer_address).await;
152+
let issuer_account_id = get_account_id(&issuer_address);
152153

153154
let trustline_asset = TrustLineAsset::CreditAlphanum4(AlphaNum4 {
154155
asset_code: AssetCode4(*b"usdc"),
@@ -339,7 +340,6 @@ async fn ledger_entry_contract_data() {
339340
}
340341

341342
// top level test
342-
// todo: test --pool-id,
343343
#[tokio::test]
344344
async fn ledger_entry_wasm_hash() {
345345
let sandbox = &TestEnv::new();
@@ -460,10 +460,10 @@ async fn ledger_entry_claimable_balance() {
460460
let sender_alias = "test";
461461
let sender = test_address(sandbox);
462462
let claimant = new_account(sandbox, "claimant");
463-
let tx = claimable_balance_tx_env(&sender, &claimant);
464-
let tx_xdr = tx.to_xdr_base64(Limits::none()).unwrap();
463+
let tx_env = claimable_balance_tx_env(&sender, &claimant);
464+
let tx_xdr = tx_env.to_xdr_base64(Limits::none()).unwrap();
465465
let updated_tx = update_seq_number(sandbox, &tx_xdr);
466-
let tx_output = sign_and_send(sandbox, &sender, sender_alias, &updated_tx).await;
466+
let tx_output = sign_and_send(sandbox, sender_alias, &updated_tx).await;
467467
let response: GetTransactionResponse = serde_json::from_str(&tx_output).expect("Failed to parse JSON");
468468
let id = extract_claimable_balance_id(response).unwrap();
469469

@@ -492,7 +492,57 @@ async fn ledger_entry_claimable_balance() {
492492
));
493493
}
494494

495+
#[tokio::test]
496+
async fn ledger_entry_liquidity_pool() {
497+
let sandbox = &TestEnv::new();
498+
let test_account_alias = "test";
499+
let test_account_address = test_address(sandbox);
500+
// issue usdc
501+
let issuer_alias = "test1";
502+
let issuer_address = new_account(sandbox, issuer_alias);
503+
let asset = &format!("usdc:{issuer_address}");
504+
let limit = 100_000;
505+
let initial_balance = 100;
506+
issue_asset(
507+
sandbox,
508+
&test_account_address,
509+
&issuer_alias,
510+
asset,
511+
limit,
512+
initial_balance,
513+
)
514+
.await;
515+
516+
// create liquidity pool
517+
let (tx_env, pool_id) = liquidity_pool_tx_env(&test_account_address, &issuer_address);
518+
let tx_xdr = tx_env.to_xdr_base64(Limits::none()).unwrap();
519+
let updated_tx = update_seq_number(sandbox, &tx_xdr);
520+
sign_and_send(sandbox, test_account_alias, &updated_tx).await;
495521

522+
// fetch the liquidity pool
523+
let output = sandbox
524+
.new_assert_cmd("ledger")
525+
.arg("entry")
526+
.arg("fetch")
527+
.arg("liquidity-pool")
528+
.arg(pool_id.to_string())
529+
.arg("--network")
530+
.arg("local")
531+
.assert()
532+
.success()
533+
.stdout_as_str();
534+
let parsed_output: FullLedgerEntries =
535+
serde_json::from_str(&output).expect("Failed to parse JSON");
536+
assert!(!parsed_output.entries.is_empty());
537+
let expected_key = LedgerKey::LiquidityPool(LedgerKeyLiquidityPool {
538+
liquidity_pool_id: PoolId(Hash(pool_id.0)),
539+
});
540+
assert_eq!(parsed_output.entries[0].key, expected_key);
541+
assert!(matches!(
542+
parsed_output.entries[0].val,
543+
LedgerEntryData::LiquidityPool { .. }
544+
));
545+
}
496546

497547
// Helper Fns
498548
fn new_account(sandbox: &TestEnv, name: &str) -> String {
@@ -507,9 +557,9 @@ fn new_account(sandbox: &TestEnv, name: &str) -> String {
507557
.stdout_as_str()
508558
}
509559

510-
async fn issue_asset(sandbox: &TestEnv, test: &str, asset: &str, limit: u64, initial_balance: u64) {
560+
async fn issue_asset(sandbox: &TestEnv, test_addr: &str, issuer_alias: &str, asset: &str, limit: u64, initial_balance: u64) {
511561
let client = sandbox.network.rpc_client().unwrap();
512-
let test_before = client.get_account(test).await.unwrap();
562+
let test_before = client.get_account(test_addr).await.unwrap();
513563
sandbox
514564
.new_assert_cmd("tx")
515565
.args([
@@ -524,57 +574,37 @@ async fn issue_asset(sandbox: &TestEnv, test: &str, asset: &str, limit: u64, ini
524574
.success()
525575
.stdout_as_str();
526576

527-
sandbox
528-
.new_assert_cmd("tx")
529-
.args(["new", "set-options", "--set-required"])
530-
.assert()
531-
.success();
532-
sandbox
533-
.new_assert_cmd("tx")
534-
.args([
535-
"new",
536-
"set-trustline-flags",
537-
"--asset",
538-
asset,
539-
"--trustor",
540-
test,
541-
"--set-authorize",
542-
"--source",
543-
"test1",
544-
])
545-
.assert()
546-
.success();
547-
548-
let after = client.get_account(test).await.unwrap();
577+
let after = client.get_account(test_addr).await.unwrap();
549578
assert_eq!(test_before.num_sub_entries + 1, after.num_sub_entries);
550579

551-
// Send a payment to the issuer
580+
// Send asset to the test
552581
sandbox
553582
.new_assert_cmd("tx")
554583
.args([
555584
"new",
556585
"payment",
557586
"--destination",
558-
test,
587+
test_addr,
559588
"--asset",
560589
asset,
561590
"--amount",
562591
initial_balance.to_string().as_str(),
563-
"--source=test1",
592+
"--source",
593+
issuer_alias
564594
])
565595
.assert()
566596
.success();
567597
}
568598

569599
async fn expected_account_ledger_key(account_addr: &str) -> (AccountId, LedgerKey) {
570-
let account_id = account_id(account_addr);
600+
let account_id = get_account_id(account_addr);
571601
let ledger_key = LedgerKey::Account(LedgerKeyAccount {
572602
account_id: account_id.clone(),
573603
});
574604
(account_id, ledger_key)
575605
}
576606

577-
fn account_id(account_addr: &str) -> AccountId {
607+
fn get_account_id(account_addr: &str) -> AccountId {
578608
let strkey = StrkeyPublicKeyEd25519::from_string(account_addr).unwrap().0;
579609

580610
let uint256 = Uint256(strkey);
@@ -610,7 +640,7 @@ async fn add_account_data(sandbox: &TestEnv, account_alias: &str, key: &str, val
610640
}
611641

612642
fn claimable_balance_tx_env(sender: &str, destination: &str) -> TransactionEnvelope{
613-
let destination_id = account_id(&destination);
643+
let destination_id = get_account_id(&destination);
614644
let claimant = Claimant::ClaimantTypeV0(
615645
ClaimantV0{
616646
destination: destination_id,
@@ -633,6 +663,46 @@ fn claimable_balance_tx_env(sender: &str, destination: &str) -> TransactionEnvel
633663
xdr::Transaction::new_tx(resolved_source, 1000, 1, create_op).into()
634664
}
635665

666+
fn liquidity_pool_tx_env(test_account_address: &str, usdc_issuer_address: &str) -> (TransactionEnvelope, Uint256){
667+
let issuer_account_id = get_account_id(&usdc_issuer_address);
668+
let usdc_asset = Asset::CreditAlphanum4(AlphaNum4 {
669+
asset_code: AssetCode4(*b"usdc"),
670+
issuer: issuer_account_id,
671+
});
672+
673+
let asset_a = Asset::Native;
674+
let asset_b = usdc_asset;
675+
let fee = 30;
676+
677+
let line = ChangeTrustAsset::PoolShare(
678+
LiquidityPoolParameters::LiquidityPoolConstantProduct(
679+
LiquidityPoolConstantProductParameters{
680+
asset_a: asset_a.clone(),
681+
asset_b: asset_b.clone(),
682+
fee
683+
}
684+
)
685+
);
686+
let op = Operation {
687+
source_account: None,
688+
body: OperationBody::ChangeTrust(
689+
ChangeTrustOp {
690+
line: line,
691+
limit: i64::MAX
692+
}
693+
)
694+
};
695+
696+
let source: UnresolvedMuxedAccount = test_account_address.parse().unwrap();
697+
let resolved_source = source.resolve_muxed_account_sync(&locator::Args::default(), None).unwrap();
698+
699+
let tx = xdr::Transaction::new_tx(resolved_source, 1000, 1, op).into();
700+
701+
let pool_id = compute_pool_id(asset_a.clone(), asset_b.clone(), fee);
702+
703+
(tx, pool_id)
704+
}
705+
636706
fn update_seq_number(sandbox: &TestEnv, tx_xdr: &str) -> String {
637707
sandbox
638708
.new_assert_cmd("tx")
@@ -645,7 +715,7 @@ fn update_seq_number(sandbox: &TestEnv, tx_xdr: &str) -> String {
645715
.stdout_as_str()
646716
}
647717

648-
async fn sign_and_send(sandbox: &TestEnv, account: &str, sign_with: &str, tx: &str) -> String {
718+
async fn sign_and_send(sandbox: &TestEnv, sign_with: &str, tx: &str) -> String {
649719
let tx_signed = sandbox
650720
.new_assert_cmd("tx")
651721
.arg("sign")
@@ -655,7 +725,6 @@ async fn sign_and_send(sandbox: &TestEnv, account: &str, sign_with: &str, tx: &s
655725
.assert()
656726
.success()
657727
.stdout_as_str();
658-
dbg!("{tx_signed}");
659728

660729
sandbox
661730
.new_assert_cmd("tx")
@@ -686,4 +755,26 @@ fn extract_claimable_balance_id(response: GetTransactionResponse) -> Option<Hash
686755
}
687756
}
688757
None
758+
}
759+
760+
fn compute_pool_id(asset_a: Asset, asset_b: Asset, fee: i32) -> Uint256 {
761+
let (asset_a, asset_b) = if asset_a < asset_b {
762+
(asset_a, asset_b)
763+
} else {
764+
(asset_b, asset_a)
765+
};
766+
767+
let pool_params = LiquidityPoolParameters::LiquidityPoolConstantProduct(
768+
LiquidityPoolConstantProductParameters {
769+
asset_a,
770+
asset_b,
771+
fee,
772+
},
773+
);
774+
775+
let mut hasher = Sha256::new();
776+
hasher.update(pool_params.to_xdr(Limits::none()).unwrap());
777+
let hash = hasher.finalize();
778+
779+
Uint256(hash.into())
689780
}

0 commit comments

Comments
 (0)