@@ -4,16 +4,16 @@ use soroban_cli::{
4
4
AccountId , AlphaNum4 , AssetCode4 , ConfigSettingId , ContractDataDurability , Hash ,
5
5
LedgerEntryData , LedgerKey , LedgerKeyAccount , LedgerKeyConfigSetting , LedgerKeyContractCode ,
6
6
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
8
8
} ,
9
- tx:: builder:: { self , asset , TxExt } ,
9
+ tx:: builder:: TxExt ,
10
10
config:: {
11
- self ,
12
- address:: { self , UnresolvedMuxedAccount } ,
11
+ address:: UnresolvedMuxedAccount ,
13
12
locator,
14
- data, network, secret,
15
13
} ,
16
14
} ;
15
+ use sha2:: { Digest , Sha256 } ;
16
+
17
17
18
18
use soroban_rpc:: GetTransactionResponse ;
19
19
use soroban_rpc:: FullLedgerEntries ;
@@ -25,7 +25,7 @@ use stellar_strkey::{ed25519::PublicKey as StrkeyPublicKeyEd25519, Contract};
25
25
use crate :: integration:: util:: { deploy_contract, test_address, DeployOptions , HELLO_WORLD } ;
26
26
27
27
// account data tests
28
- // todo: test with--offer,
28
+ // todo: test with --offer,
29
29
#[ tokio:: test]
30
30
async fn ledger_entry_account_only_with_account_alias ( ) {
31
31
let sandbox = & TestEnv :: new ( ) ;
@@ -126,6 +126,7 @@ async fn ledger_entry_account_asset_usdc() {
126
126
issue_asset (
127
127
sandbox,
128
128
& test_account_address,
129
+ & issuer_alias,
129
130
asset,
130
131
limit,
131
132
initial_balance,
@@ -148,7 +149,7 @@ async fn ledger_entry_account_asset_usdc() {
148
149
149
150
let ( account_id, expected_account_key) =
150
151
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) ;
152
153
153
154
let trustline_asset = TrustLineAsset :: CreditAlphanum4 ( AlphaNum4 {
154
155
asset_code : AssetCode4 ( * b"usdc" ) ,
@@ -339,7 +340,6 @@ async fn ledger_entry_contract_data() {
339
340
}
340
341
341
342
// top level test
342
- // todo: test --pool-id,
343
343
#[ tokio:: test]
344
344
async fn ledger_entry_wasm_hash ( ) {
345
345
let sandbox = & TestEnv :: new ( ) ;
@@ -460,10 +460,10 @@ async fn ledger_entry_claimable_balance() {
460
460
let sender_alias = "test" ;
461
461
let sender = test_address ( sandbox) ;
462
462
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 ( ) ;
465
465
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 ;
467
467
let response: GetTransactionResponse = serde_json:: from_str ( & tx_output) . expect ( "Failed to parse JSON" ) ;
468
468
let id = extract_claimable_balance_id ( response) . unwrap ( ) ;
469
469
@@ -492,7 +492,57 @@ async fn ledger_entry_claimable_balance() {
492
492
) ) ;
493
493
}
494
494
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 ;
495
521
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
+ }
496
546
497
547
// Helper Fns
498
548
fn new_account ( sandbox : & TestEnv , name : & str ) -> String {
@@ -507,9 +557,9 @@ fn new_account(sandbox: &TestEnv, name: &str) -> String {
507
557
. stdout_as_str ( )
508
558
}
509
559
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 ) {
511
561
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 ( ) ;
513
563
sandbox
514
564
. new_assert_cmd ( "tx" )
515
565
. args ( [
@@ -524,57 +574,37 @@ async fn issue_asset(sandbox: &TestEnv, test: &str, asset: &str, limit: u64, ini
524
574
. success ( )
525
575
. stdout_as_str ( ) ;
526
576
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 ( ) ;
549
578
assert_eq ! ( test_before. num_sub_entries + 1 , after. num_sub_entries) ;
550
579
551
- // Send a payment to the issuer
580
+ // Send asset to the test
552
581
sandbox
553
582
. new_assert_cmd ( "tx" )
554
583
. args ( [
555
584
"new" ,
556
585
"payment" ,
557
586
"--destination" ,
558
- test ,
587
+ test_addr ,
559
588
"--asset" ,
560
589
asset,
561
590
"--amount" ,
562
591
initial_balance. to_string ( ) . as_str ( ) ,
563
- "--source=test1" ,
592
+ "--source" ,
593
+ issuer_alias
564
594
] )
565
595
. assert ( )
566
596
. success ( ) ;
567
597
}
568
598
569
599
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) ;
571
601
let ledger_key = LedgerKey :: Account ( LedgerKeyAccount {
572
602
account_id : account_id. clone ( ) ,
573
603
} ) ;
574
604
( account_id, ledger_key)
575
605
}
576
606
577
- fn account_id ( account_addr : & str ) -> AccountId {
607
+ fn get_account_id ( account_addr : & str ) -> AccountId {
578
608
let strkey = StrkeyPublicKeyEd25519 :: from_string ( account_addr) . unwrap ( ) . 0 ;
579
609
580
610
let uint256 = Uint256 ( strkey) ;
@@ -610,7 +640,7 @@ async fn add_account_data(sandbox: &TestEnv, account_alias: &str, key: &str, val
610
640
}
611
641
612
642
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) ;
614
644
let claimant = Claimant :: ClaimantTypeV0 (
615
645
ClaimantV0 {
616
646
destination : destination_id,
@@ -633,6 +663,46 @@ fn claimable_balance_tx_env(sender: &str, destination: &str) -> TransactionEnvel
633
663
xdr:: Transaction :: new_tx ( resolved_source, 1000 , 1 , create_op) . into ( )
634
664
}
635
665
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
+
636
706
fn update_seq_number ( sandbox : & TestEnv , tx_xdr : & str ) -> String {
637
707
sandbox
638
708
. new_assert_cmd ( "tx" )
@@ -645,7 +715,7 @@ fn update_seq_number(sandbox: &TestEnv, tx_xdr: &str) -> String {
645
715
. stdout_as_str ( )
646
716
}
647
717
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 {
649
719
let tx_signed = sandbox
650
720
. new_assert_cmd ( "tx" )
651
721
. arg ( "sign" )
@@ -655,7 +725,6 @@ async fn sign_and_send(sandbox: &TestEnv, account: &str, sign_with: &str, tx: &s
655
725
. assert ( )
656
726
. success ( )
657
727
. stdout_as_str ( ) ;
658
- dbg ! ( "{tx_signed}" ) ;
659
728
660
729
sandbox
661
730
. new_assert_cmd ( "tx" )
@@ -686,4 +755,26 @@ fn extract_claimable_balance_id(response: GetTransactionResponse) -> Option<Hash
686
755
}
687
756
}
688
757
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 ( ) )
689
780
}
0 commit comments