Skip to content

Commit

Permalink
💱 Translate removing AMM liquidity
Browse files Browse the repository at this point in the history
And simplify the go newTxBuilder preconditions
  • Loading branch information
JFWooten4 committed Nov 24, 2024
1 parent 535b09b commit be7abb0
Showing 1 changed file with 73 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -873,8 +873,7 @@ func newTxBuilder(source *horizonclient.AccountRequest) (*txnbuild.Transaction,
txParams := txnbuild.TransactionParams{
SourceAccount: &account,
BaseFee: txnbuild.MinBaseFee,
Preconditions: txnbuild.Preconditions{TimeBounds: txnbuild.NewTimeout(360)},
Operations: nil,
Timebounds: txnbuild.NewTimeout(360),
IncrementSequenceNum: true,
}
return txnbuild.NewTransaction(txParams)
Expand Down Expand Up @@ -1264,30 +1263,28 @@ If you own shares of a particular pool, you can withdraw reserves from it. The o
<CodeExample>

```python
def removeLiquidity(
source: Keypair, poolId: str, sharesAmount: Decimal
) -> dict[str, Any]:
def removeLiquidity(source, poolId, sharesAmount):
poolInfo = server.liquidity_pools().liquidity_pool(poolId).call()
totalShares = Decimal(poolInfo["total_shares"])
minReserveA = (
sharesAmount
/ totalShares
* Decimal(poolInfo["reserves"][0]["amount"])
* Decimal("0.95")
sharesAmount
/ totalShares
* Decimal(poolInfo["reserves"][0]["amount"])
* Decimal("0.95") # 95% safety factor
)
minReserveB = (
sharesAmount
/ totalShares
* Decimal(poolInfo["reserves"][1]["amount"])
* Decimal("0.95")
sharesAmount
/ totalShares
* Decimal(poolInfo["reserves"][1]["amount"])
* Decimal("0.95")
)
tx = (
newTxBuilder(source.public_key)
newTxBuilder(source.public_key)
.appendLiquidityPoolWithdrawOp(
liquidityPoolId=poolId,
amount=f"{sharesAmount:.7f}",
minAmountA=f"{minReserveA:.7f}",
minAmountB=f"{minReserveB:.7f}",
liquidityPoolId=poolId,
amount=f"{sharesAmount:.7f}",
minAmountA=f"{minReserveA:.7f}",
minAmountB=f"{minReserveB:.7f}",
)
.build()
)
Expand All @@ -1304,7 +1301,7 @@ function removeLiquidity(source, signer, poolId, sharesAmount) {
.then((poolInfo) => {
let totalShares = poolInfo.total_shares;
let minReserveA =
(sharesAmount / totalShares) * poolInfo.reserves[0].amount * 0.95;
(sharesAmount / totalShares) * poolInfo.reserves[0].amount * 0.95; // 95% safety factor
let minReserveB =
(sharesAmount / totalShares) * poolInfo.reserves[1].amount * 0.95;

Expand All @@ -1325,11 +1322,68 @@ function removeLiquidity(source, signer, poolId, sharesAmount) {
```

```java
public Map<String, Object> removeLiquidity(KeyPair source, String poolId, String sharesAmount) throws Exception {
Map<String, Object> poolInfo = server.liquidityPools()
.liquidityPool(poolId)
.call();

double totalShares = Double.parseDouble(poolInfo.get("total_shares").toString());
double reserveA = Double.parseDouble(((Map<String, String>) ((List<Object>) poolInfo.get("reserves")).get(0)).get("amount"));
double reserveB = Double.parseDouble(((Map<String, String>) ((List<Object>) poolInfo.get("reserves")).get(1)).get("amount"));

double minReserveA = (Double.parseDouble(sharesAmount) / totalShares) * reserveA * 0.95; // 95% safety factor
double minReserveB = (Double.parseDouble(sharesAmount) / totalShares) * reserveB * 0.95;

Transaction transaction = newTxBuilder(source)
.addOperation(
new LiquidityPoolWithdrawOperation.Builder(
poolId,
sharesAmount,
String.format("%.7f", minReserveA),
String.format("%.7f", minReserveB)
).build()
)
.build();

transaction.sign(source);
return server.submitTransaction(transaction);
}
```

```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

sourceAccount := &horizonclient.AccountRequest{AccountID: source.Address()}
tx := newTxBuilder(sourceAccount)
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)
}
```

</CodeExample>
Expand Down

0 comments on commit be7abb0

Please sign in to comment.