Skip to content

Commit

Permalink
Add integration test for failed liquidity pool operations (#3911)
Browse files Browse the repository at this point in the history
  • Loading branch information
2opremio authored Sep 14, 2021
1 parent 67969b9 commit 7b83dc4
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions services/horizon/internal/integration/protocol18_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,10 @@ func TestLiquidityPoolHappyPath(t *testing.T) {
tt.Equal("400.0000000", op2.ReservesDeposited[0].Amount)
tt.Equal(fmt.Sprintf("USD:%s", master.Address()), op2.ReservesDeposited[1].Asset)
tt.Equal("777.0000000", op2.ReservesDeposited[1].Amount)
tt.Equal("native", op2.ReservesMax[0].Asset)
tt.Equal("400.0000000", op2.ReservesMax[0].Amount)
tt.Equal(fmt.Sprintf("USD:%s", master.Address()), op2.ReservesMax[1].Asset)
tt.Equal("777.0000000", op2.ReservesMax[1].Amount)
tt.Equal("557.4943946", op2.SharesReceived)

op3 := (ops.Embedded.Records[2]).(operations.PathPayment)
Expand Down Expand Up @@ -676,3 +680,82 @@ func TestLiquidityPoolRevoke(t *testing.T) {
tt.Equal(master.Address(), ef12.Account)
tt.Equal("64e163b66108152665ee325cc333211446277c86bfe021b9da6bb1769b0daea1", ef12.LiquidityPoolID)
}

func TestLiquidityPoolFailedDepositAndWithdraw(t *testing.T) {
tt := assert.New(t)
itest := NewProtocol18Test(t)

keys, accounts := itest.CreateAccounts(2, "1000")
shareKeys, shareAccount := keys[0], accounts[0]

nonExistentPoolID := [32]byte{0xca, 0xfe}

// Failing deposit
tx, err := itest.CreateSignedTransaction(shareAccount, []*keypair.Full{shareKeys},
&txnbuild.LiquidityPoolDeposit{
LiquidityPoolID: nonExistentPoolID,
MaxAmountA: "400",
MaxAmountB: "777",
MinPrice: "0.5",
MaxPrice: "2",
},
)
_, err = itest.Client().SubmitTransaction(tx)
tt.Error(err)
hash, err := tx.HashHex(integration.StandaloneNetworkPassphrase)
tt.NoError(err)
opsResponse, err := itest.Client().Operations(horizonclient.OperationRequest{
ForTransaction: hash,
})
tt.NoError(err)
tt.Len(opsResponse.Embedded.Records, 1)
deposit := (opsResponse.Embedded.Records[0]).(operations.LiquidityPoolDeposit)
tt.Equal("liquidity_pool_deposit", deposit.Type)
tt.Equal("cafe000000000000000000000000000000000000000000000000000000000000", deposit.LiquidityPoolID)
tt.Equal("0.5000000", deposit.MinPrice)
tt.Equal("2.0000000", deposit.MaxPrice)
tt.Equal("", deposit.ReservesDeposited[0].Asset)
tt.Equal("0.0000000", deposit.ReservesDeposited[0].Amount)
tt.Equal("", deposit.ReservesDeposited[1].Asset)
tt.Equal("0.0000000", deposit.ReservesDeposited[1].Amount)
tt.Equal("", deposit.ReservesMax[0].Asset)
tt.Equal("400.0000000", deposit.ReservesMax[0].Amount)
tt.Equal("", deposit.ReservesMax[1].Asset)
tt.Equal("777.0000000", deposit.ReservesMax[1].Amount)
tt.Equal("0.0000000", deposit.SharesReceived)

// Failing withdrawal
tx, err = itest.CreateSignedTransaction(shareAccount, []*keypair.Full{shareKeys},
&txnbuild.LiquidityPoolWithdraw{
LiquidityPoolID: nonExistentPoolID,
Amount: amount.StringFromInt64(int64(10)),
MinAmountA: "10",
MinAmountB: "20",
},
)
_, err = itest.Client().SubmitTransaction(tx)
tt.Error(err)

hash, err = tx.HashHex(integration.StandaloneNetworkPassphrase)
tt.NoError(err)
opsResponse, err = itest.Client().Operations(horizonclient.OperationRequest{
ForTransaction: hash,
})
tt.NoError(err)
tt.Len(opsResponse.Embedded.Records, 1)
withdrawal := (opsResponse.Embedded.Records[0]).(operations.LiquidityPoolWithdraw)
tt.Equal("liquidity_pool_withdraw", withdrawal.Type)
tt.Equal("cafe000000000000000000000000000000000000000000000000000000000000", withdrawal.LiquidityPoolID)

tt.Equal("", withdrawal.ReservesMin[0].Asset)
tt.Equal("10.0000000", withdrawal.ReservesMin[0].Amount)
tt.Equal("", withdrawal.ReservesMin[1].Asset)
tt.Equal("20.0000000", withdrawal.ReservesMin[1].Amount)

tt.Equal("", withdrawal.ReservesReceived[0].Asset)
tt.Equal("0.0000000", withdrawal.ReservesReceived[0].Amount)
tt.Equal("", withdrawal.ReservesReceived[1].Asset)
tt.Equal("0.0000000", withdrawal.ReservesReceived[1].Amount)

tt.Equal("0.0000010", withdrawal.Shares)
}

0 comments on commit 7b83dc4

Please sign in to comment.