7 common transactions on Stellar and Soroban to explore using Mercury and how to retrieve them
Tags: soroban, mercury, stellar
Published: 2023-12-28 22:23:25

In the following article we will be describing the transactions that we will be exploring using Mercury. We will be using the following transactions:
    - Send Payments
    - Receive Payments
    - Liquidity Pool Deposit
    - Liquidity Pool Withdrawal
    - Soroswap Add Liquidity
    - Soroswap Remove Liquidity
    - Soroswap Swap tokens
Some of them are using Stellar Classic, and some of them are in Soroban Network.

We have developed [mercury-sdk](https://github.com/paltalabs/mercury-sdk) to facilitate the exploration of the transactions.

In order to work with mercury-sdk you need to create an instance of the Mercury class. You can do it as follows:
```typescript
import { Mercury } from "mercury-sdk";

const mercuryInstance = new Mercury({
  backendEndpoint: process.env.MERCURY_BACKEND_ENDPOINT,
  graphqlEndpoint: process.env.MERCURY_GRAPHQL_ENDPOINT,
  email: process.env.MERCURY_TESTER_EMAIL,
  password: process.env.MERCURY_TESTER_PASSWORD,
});
```


## Payments
Sent and received payments are the most common transactions in Stellar. They are used to send and receive assets between accounts. 
You can retrieve the information as:
```javascript
import { 
    getSentPaymentsParser, 
    getReceivedPaymentsParser,
     } from "mercury-sdk";
async function someFunction() {
  //Sent payments
  const sentPaymentsResponse = await mercuryInstance.getSentPayments({
    publicKey: "someStellarAddress",
  });

  if (sentPaymentsResponse.ok) {
    const sentPaymentsParsedData = getSentPaymentsParser(
      sentPaymentsResponse.data!
    );
    console.log(sentPaymentsParsedData);
  }

  //Received payments
  const receivedPaymentsResponse = await mercuryInstance.getReceivedPayments({
    publicKey: "someStellarAddress",
  });

  if (receivedPaymentsResponse.ok) {
    const receivedPaymentsParsedData = getReceivedPaymentsParser(
      receivedPaymentsResponse.data!
    );
    console.log(receivedPaymentsParsedData);
  }
}
```
example response:
```sentPaymentsParsedData
[
  {
    "from": "GCQF7MWR7QO2QKD7F72WG2H5L3TBE53DN67OYBXQEDEWIYOSYVP5D7CJ",
    "to": "GD3PVP6AYFOVWXYFYTX7JM7WMYPKINCTUXZNXOYKAO5U2AUARARDDM45",
    "amount": "10000000000",
    "assetNative": false,
    "asset": "PALTA:GDA7K7I3XGH5HKUH5NYFYY3JBKUAJ3DSGLVGJHBDLUTKCUYVMUOVR2ZM",
    "ledger": 162443,
    "timestamp": 1703774254
  },
  {
    "from": "GCQF7MWR7QO2QKD7F72WG2H5L3TBE53DN67OYBXQEDEWIYOSYVP5D7CJ",
    "to": "GD3PVP6AYFOVWXYFYTX7JM7WMYPKINCTUXZNXOYKAO5U2AUARARDDM45",
    "amount": "10000000000",
    "assetNative": true,
    "ledger": 162444,
    "timestamp": 1703774259
  }
]

receivedPaymentsParsedData
[
  {
    "from": "GDA7K7I3XGH5HKUH5NYFYY3JBKUAJ3DSGLVGJHBDLUTKCUYVMUOVR2ZM",
    "to": "GCQF7MWR7QO2QKD7F72WG2H5L3TBE53DN67OYBXQEDEWIYOSYVP5D7CJ",
    "amount": "1000000000000",
    "assetNative": false,
    "asset": "PALTA:GDA7K7I3XGH5HKUH5NYFYY3JBKUAJ3DSGLVGJHBDLUTKCUYVMUOVR2ZM",
    "ledger": 162440,
    "timestamp": 1703774238
  }
]
```


## Liquidity Pool Operations
Deposit and Withdrawal operations are used to add and remove liquidity from a liquidity pool. These operations are used in the built-in Stellar AMM. 

Example code:
```javascript
  //Liquidity Pool Withdraw
  const liquidityPoolWithdrawResponse =
    await mercuryInstance.getLiquidityPoolWithdraw({
      publicKey: "someStellarAddress",
    });

  if (liquidityPoolWithdrawResponse.ok) {
    const liquidityPoolWithdrawParsedData = getLiquidityPoolWithdrawParser(
      liquidityPoolWithdrawResponse.data!
    );
  }

  //Liquidity Pool Deposit
  const liquidityPoolDepositResponse =
    await mercuryInstance.getLiquidityPoolDeposit({
      publicKey: "someStellarAddress",
    });

  if (liquidityPoolDepositResponse.ok) {
    const liquidityPoolDepositParsedData = getLiquidityPoolDepositParser(
      liquidityPoolDepositResponse.data!
    );
  }

```
example response:
```
liquidityPoolWithdrawParsedData
[
  {
    "source": 402241,
    "poolId": "\\xc7403b2778be92e967f714a6cbff8181a4ec993ff2a3538a4114744150c4a72a",
    "amount": "1000000000",
    "ledger": 162449,
    "timestamp": 1703774286
  }
]

liquidityPoolDepositParsedData
[
  {
    "source": 402238,
    "poolId": "\\xc7403b2778be92e967f714a6cbff8181a4ec993ff2a3538a4114744150c4a72a",
    "maxAmountA": "20000000000",
    "maxAmountB": "10000000000",
    "minPriceN": 9,
    "minPriceD": 5,
    "maxPriceN": 11,
    "maxPriceD": 5,
    "ledger": 162446,
    "timestamp": 1703774270
  }
]
```

## Soroswap Operations
Soroswap operations are used to add and remove liquidity from a liquidity pool, and to swap tokens. These operations are used in the Soroswap AMM. Which is different from the built-in Stellar AMM. Because in Soroswap there will be tokens that are not Stellar Assets.

When doing swaps, instead of the pair of asset we have the path of the swap. The path is the sequence of assets that the swap will go through. 

Example code:
```javascript
  const getContractEventsRes = await mercuryInstance.getContractEvents({
    contractId: routerContractAddress,
  });
  const parsedContractEvents = getContractEventsParser(getContractEventsRes.data!);
  const eventByPublicKey = parsedContractEvents.filter((event) => event.to === publicKey);
```

example response:
```json
[
  {
    "amount_a": "2000000",
    "amount_b": "2000000",
    "liquidity": "1999000",
    "pair": "CAMCPA2QDCC2K3DJOSRKRGQXETHQND7KK46VOEW3YLL5OP7BDXNB37YF",
    "to": "GBDJYBFPYUY7XXI5XCT473VJRT7PRGMRA2AJ2TKUKGPLJ5ZGVPJYKEAR",
    "token_a": "CBKZMWGE7E3VDJRCHRNLAHKYL2JMCC4METUJA262WXKN2SANJXVQYE3N",
    "token_b": "CCFB3TPPJHDLDYRTPJEFYJ37FOVASDCIZEJ3DYKOFUQO4WJHHZ7SML2D",
    "topic1": "SoroswapRouter",
    "topic2": "add"
  },
  {
    "amounts": [
      "100000",
      "94965"
    ],
    "path": [
      "CBKZMWGE7E3VDJRCHRNLAHKYL2JMCC4METUJA262WXKN2SANJXVQYE3N",
      "CCFB3TPPJHDLDYRTPJEFYJ37FOVASDCIZEJ3DYKOFUQO4WJHHZ7SML2D"
    ],
    "to": "GBDJYBFPYUY7XXI5XCT473VJRT7PRGMRA2AJ2TKUKGPLJ5ZGVPJYKEAR",
    "topic1": "SoroswapRouter",
    "topic2": "swap"
  },
  {
    "amount_a": "105",
    "amount_b": "95",
    "liquidity": "100",
    "pair": "CAMCPA2QDCC2K3DJOSRKRGQXETHQND7KK46VOEW3YLL5OP7BDXNB37YF",
    "to": "GBDJYBFPYUY7XXI5XCT473VJRT7PRGMRA2AJ2TKUKGPLJ5ZGVPJYKEAR",
    "token_a": "CBKZMWGE7E3VDJRCHRNLAHKYL2JMCC4METUJA262WXKN2SANJXVQYE3N",
    "token_b": "CCFB3TPPJHDLDYRTPJEFYJ37FOVASDCIZEJ3DYKOFUQO4WJHHZ7SML2D",
    "topic1": "SoroswapRouter",
    "topic2": "remove"
  }
]
```

The complete code can be checked on https://github.com/paltalabs/mercury-client/blob/main/scripts/mercurySdkTester/retrieve7txs.ts