From f1a50e25a92ae3e21bec2c99ced8b98425f6f95b Mon Sep 17 00:00:00 2001 From: "john.xlm" <60260750+JFWooten4@users.noreply.github.com> Date: Tue, 26 Nov 2024 08:39:09 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=96=8C=EF=B8=8F=20Update=20prettier=20con?= =?UTF-8?q?fig=20note=20I=20was=20messing=20around=20with=20`prettier-plug?= =?UTF-8?q?in-sh`=20and=20figured=20the=20note=20could=20be=20more=20local?= =?UTF-8?q?ized=20in=20case=20we=20add=20tabWidths=20or=20such=20later=20o?= =?UTF-8?q?n=20here=20(albeit=20you=20can=20locally=20inspect=20the=20upst?= =?UTF-8?q?ream=20import=20for=20equivalent=20affect)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .prettierrc.js | 4 +- ...uidity-on-stellar-sdex-liquidity-pools.mdx | 240 +++++++++--------- 2 files changed, 121 insertions(+), 123 deletions(-) diff --git a/.prettierrc.js b/.prettierrc.js index 1fa059776..e89fae43d 100644 --- a/.prettierrc.js +++ b/.prettierrc.js @@ -1,12 +1,10 @@ module.exports = { ...require("@stellar/prettier-config"), - // This is mostly content, and prose wrap has a way of exploding markdown - // diffs. Override the default for a better experience. overrides: [ { files: "*.mdx", options: { - proseWrap: "never", + proseWrap: "never", // Minimize `mdx` diffs with simpler content lines }, }, ], diff --git a/docs/learn/encyclopedia/sdex/liquidity-on-stellar-sdex-liquidity-pools.mdx b/docs/learn/encyclopedia/sdex/liquidity-on-stellar-sdex-liquidity-pools.mdx index 112d40826..584bf28af 100644 --- a/docs/learn/encyclopedia/sdex/liquidity-on-stellar-sdex-liquidity-pools.mdx +++ b/docs/learn/encyclopedia/sdex/liquidity-on-stellar-sdex-liquidity-pools.mdx @@ -142,15 +142,15 @@ func main() { } func newTxBuilder(pubKey string) (*txnbuild.Transaction, error) { - account := horizonclient.AccountRequest{AccountID: pubKey} + account := horizonclient.AccountRequest{AccountID: pubKey} check(err) // Confirm account & sequence number - txEnvelopeParams := &txnbuild.TransactionParams{ - SourceAccount: &account, - BaseFee: txnbuild.MinBaseFee, - Timebounds: txnbuild.NewTimeout(360), - IncrementSequenceNum: true, - } - return txnbuild.NewTransaction(txEnvelopeParams) + txEnvelopeParams := &txnbuild.TransactionParams{ + SourceAccount: &account, + BaseFee: txnbuild.MinBaseFee, + Timebounds: txnbuild.NewTimeout(360), + IncrementSequenceNum: true, + } + return txnbuild.NewTransaction(txEnvelopeParams) } ``` @@ -695,17 +695,17 @@ public static void getSpotPrice(String[] args) throws Exception { ```go func getSpotPrice(server *horizonclient.Client, poolID string) { - poolRequest := horizonclient.LiquidityPoolRequest{LiquidityPoolID: poolID} - pool, err := server.LiquidityPoolDetail(poolRequest) - check(err) + poolRequest := horizonclient.LiquidityPoolRequest{LiquidityPoolID: poolID} + pool, err := server.LiquidityPoolDetail(poolRequest) + check(err) - reserveA, err := strconv.ParseFloat(pool.Reserves[0].Amount, 64) - check(err) - reserveB, err := strconv.ParseFloat(pool.Reserves[1].Amount, 64) - check(err) + reserveA, err := strconv.ParseFloat(pool.Reserves[0].Amount, 64) + check(err) + reserveB, err := strconv.ParseFloat(pool.Reserves[1].Amount, 64) + check(err) - spotPrice := reserveA / reserveB - fmt.Printf("Price: %.7f/%.7f = %.7f\n", reserveA, reserveB, spotPrice) + spotPrice := reserveA / reserveB + fmt.Printf("Price: %.7f/%.7f = %.7f\n", reserveA, reserveB, spotPrice) } ``` @@ -932,61 +932,61 @@ public static void preamble() throws Exception { ```go var secrets = []string{ - "SBGCD73TK2PTW2DQNWUYZSTCTHHVJPL4GZF3GVZMCDL6GYETYNAYOADN", - "SAAQFHI2FMSIC6OFPWZ3PDIIX3OF64RS3EB52VLYYZBX6GYB54TW3Q4U", - "SCJWYFTBDMDPAABHVJZE3DRMBRTEH4AIC5YUM54QGW57NUBM2XX6433P", + "SBGCD73TK2PTW2DQNWUYZSTCTHHVJPL4GZF3GVZMCDL6GYETYNAYOADN", + "SAAQFHI2FMSIC6OFPWZ3PDIIX3OF64RS3EB52VLYYZBX6GYB54TW3Q4U", + "SCJWYFTBDMDPAABHVJZE3DRMBRTEH4AIC5YUM54QGW57NUBM2XX6433P", } func distributeAssets(issuerKp, recipientKp *keypair.Full, assets []txnbuild.Asset) error { - tx, err := newTxBuilder(issuerKp.Address()) - check(err) - - ops := []txnbuild.Operation{} - for _, asset := range assets { - ops = append(ops, - &txnbuild.ChangeTrust{ - Line: asset, - SourceAccount: recipientKp.Address(), - }, - &txnbuild.Payment{ - Destination: recipientKp.Address(), - Asset: asset, - Amount: "100000", - SourceAccount: issuerKp.Address(), - }, - ) - } - - tx.Params.Operations = ops - tx, err := tx.BuildSignEncode(issuerKp, recipientKp) - check(err) - - _, err = server.SubmitTransactionXDR(tx) - return err + tx, err := newTxBuilder(issuerKp.Address()) + check(err) + + ops := []txnbuild.Operation{} + for _, asset := range assets { + ops = append(ops, + &txnbuild.ChangeTrust{ + Line: asset, + SourceAccount: recipientKp.Address(), + }, + &txnbuild.Payment{ + Destination: recipientKp.Address(), + Asset: asset, + Amount: "100000", + SourceAccount: issuerKp.Address(), + }, + ) + } + + tx.Params.Operations = ops + tx, err := tx.BuildSignEncode(issuerKp, recipientKp) + check(err) + + _, err = server.SubmitTransactionXDR(tx) + return err } // Function to order assets in "protocol order" func orderAssets(a, b txnbuild.Asset) (txnbuild.Asset, txnbuild.Asset) { - if a.LessThan(b) { - return a, b - } - return b, a + if a.LessThan(b) { + return a, b + } + return b, a } func preamble() { - kp0 := keypair.MustParseFull(secrets[0]) - kp1 := keypair.MustParseFull(secrets[1]) - kp2 := keypair.MustParseFull(secrets[2]) - - assetA, assetB := orderAssets( - txnbuild.CreditAsset{"A", kp0.Address()}, - txnbuild.CreditAsset{"B", kp0.Address()}, - ) - - err := distributeAssets(kp0, kp1, []txnbuild.Asset{assetA, assetB}) - check(err) - err = distributeAssets(kp0, kp2, []txnbuild.Asset{assetA, assetB}) - check(err) + kp0 := keypair.MustParseFull(secrets[0]) + kp1 := keypair.MustParseFull(secrets[1]) + kp2 := keypair.MustParseFull(secrets[2]) + + assetA, assetB := orderAssets( + txnbuild.CreditAsset{"A", kp0.Address()}, + txnbuild.CreditAsset{"B", kp0.Address()}, + ) + + err := distributeAssets(kp0, kp1, []txnbuild.Asset{assetA, assetB}) + check(err) + err = distributeAssets(kp0, kp2, []txnbuild.Asset{assetA, assetB}) + check(err) } ``` @@ -1405,36 +1405,36 @@ public Map removeLiquidity(KeyPair source, String poolId, String ```go func removeLiquidity(source *keypair.Full, poolID string, sharesAmount float64) { - poolRequest := horizonclient.LiquidityPoolRequest{LiquidityPoolID: poolID} - poolInfo, err := client.LiquidityPoolDetail(poolRequest) - check(err) - - totalShares, err := strconv.ParseFloat(poolInfo.TotalShares, 64) - check(err) - reserveA, err := strconv.ParseFloat(poolInfo.Reserves[0].Amount, 64) - check(err) - reserveB, err := strconv.ParseFloat(poolInfo.Reserves[1].Amount, 64) - check(err) - - minReserveA := (sharesAmount / totalShares) * reserveA * 0.95 // 95% safety factor - minReserveB := (sharesAmount / totalShares) * reserveB * 0.95 - - tx := newTxBuilder(source.Address()) - tx.Operations = []txnbuild.Operation{ - &txnbuild.LiquidityPoolWithdraw{ - LiquidityPoolID: poolID, - Amount: formatFloat(sharesAmount), - MinAmountA: formatFloat(minReserveA), - MinAmountB: formatFloat(minReserveB), - }, - } - - tx, err := tx.Build() - check(err) - signedTx, err := tx.Sign(txnbuild.NetworkTest, source) - check(err) - resp, err := client.SubmitTransaction(signedTx) - check(err) + poolRequest := horizonclient.LiquidityPoolRequest{LiquidityPoolID: poolID} + poolInfo, err := client.LiquidityPoolDetail(poolRequest) + check(err) + + totalShares, err := strconv.ParseFloat(poolInfo.TotalShares, 64) + check(err) + reserveA, err := strconv.ParseFloat(poolInfo.Reserves[0].Amount, 64) + check(err) + reserveB, err := strconv.ParseFloat(poolInfo.Reserves[1].Amount, 64) + check(err) + + minReserveA := (sharesAmount / totalShares) * reserveA * 0.95 // 95% safety factor + minReserveB := (sharesAmount / totalShares) * reserveB * 0.95 + + tx := newTxBuilder(source.Address()) + tx.Operations = []txnbuild.Operation{ + &txnbuild.LiquidityPoolWithdraw{ + LiquidityPoolID: poolID, + Amount: formatFloat(sharesAmount), + MinAmountA: formatFloat(minReserveA), + MinAmountB: formatFloat(minReserveB), + }, + } + + tx, err := tx.Build() + check(err) + signedTx, err := tx.Sign(txnbuild.NetworkTest, source) + check(err) + resp, err := client.SubmitTransaction(signedTx) + check(err) } ``` @@ -1560,43 +1560,43 @@ public static void main(String[] args) throws Exception { ```go func main() { kps := make([]*keypair.Full, len(secrets)) - for i, secret := range secrets { - kps[i] = keypair.MustParseFull(secret) - } - - // Simulate deposits from participants - for i := 1; i < len(kps); i++ { - kp := kps[i] - // Arbitrary deposit increasing amounts + for i, secret := range secrets { + kps[i] = keypair.MustParseFull(secret) + } + + // Simulate deposits from participants + for i := 1; i < len(kps); i++ { + kp := kps[i] + // Arbitrary deposit increasing amounts depositA := float64(i+1) * 1000 - depositB := float64(i+1) * 3000 // Scalar maintains 1:3 ratio + depositB := float64(i+1) * 3000 // Scalar maintains 1:3 ratio establishPoolTrustline(kp, poolID) addLiquidity(kp, poolID, depositA, depositB) - getSpotPrice(poolID) - } + getSpotPrice(poolID) + } - // kp1 removes all liquidity - kp1 := kps[1] - account, err := server.AccountDetail( + // kp1 removes all liquidity + kp1 := kps[1] + account, err := server.AccountDetail( horizonclient.AccountRequest{AccountID: kp1.Address()} ) - check(err) + check(err) - var balance float64 - for _, bal := range account.Balances { - if bal.AssetType == "liquidity_pool_shares" && + var balance float64 + for _, bal := range account.Balances { + if bal.AssetType == "liquidity_pool_shares" && bal.LiquidityPoolID == poolID { - balance, err = strconv.ParseFloat(bal.Balance, 64) - check(err) - break - } - } - if balance == 0 { - log.Fatalf("No liquidity pool shares found for kp1.") - } - removeLiquidity(kp1, poolID, balance) - getSpotPrice(poolID) + balance, err = strconv.ParseFloat(bal.Balance, 64) + check(err) + break + } + } + if balance == 0 { + log.Fatalf("No liquidity pool shares found for kp1.") + } + removeLiquidity(kp1, poolID, balance) + getSpotPrice(poolID) } ```