Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NeoSwaps.sell assetAmountOut does not match the dBalance of the Transfer transaction #504

Closed
Chralt98 opened this issue Jan 28, 2024 · 9 comments
Assignees
Labels
question Further information is requested

Comments

@Chralt98
Copy link
Member

Consider the following query:

query MyQuery {
  txs: historicalAccountBalances(limit: 100, orderBy: timestamp_ASC, where: {
    extrinsic: { hash_eq: "0x81a849e90fcb21dac2f59a866b92e2db9bd0a11f72d536a7d3feb902fbf5de9e" }
    accountId_eq: "dE17owFuqybtXatuqm4n4Qu1Gxjdatctj7SY224E19x86vQ7o",
  }) {
    accountId
    assetId
    dBalance
    blockNumber
    event
    extrinsic {
      hash
      name
    }
    id
    timestamp
  }
  swaps: historicalSwaps(limit: 10, orderBy: timestamp_ASC, where: {
    extrinsic: { hash_eq: "0x81a849e90fcb21dac2f59a866b92e2db9bd0a11f72d536a7d3feb902fbf5de9e"},
  }) {
    accountId
    assetAmountIn
    assetAmountOut
    assetIn
    assetOut
    blockNumber
    event
    extrinsic {
      name
      hash
    }
    id
    timestamp
  }
}

I got the following json output:

    "txs": [
      {
        "accountId": "dE17owFuqybtXatuqm4n4Qu1Gxjdatctj7SY224E19x86vQ7o",
        "assetId": "Ztg",
        "dBalance": "-245841498",
        "blockNumber": 4766465,
        "event": "Withdraw",
        "extrinsic": {
          "hash": "0x81a849e90fcb21dac2f59a866b92e2db9bd0a11f72d536a7d3feb902fbf5de9e",
          "name": "NeoSwaps.sell"
        },
        "id": "0004766465-b9c00-000003-6vQ7o",
        "timestamp": "2024-01-25T19:53:48.547000Z"
      },
      {
        "accountId": "dE17owFuqybtXatuqm4n4Qu1Gxjdatctj7SY224E19x86vQ7o",
        "assetId": "{\"categoricalOutcome\":[359,0]}",
        "dBalance": "-7520187",
        "blockNumber": 4766465,
        "event": "Transfer",
        "extrinsic": {
          "hash": "0x81a849e90fcb21dac2f59a866b92e2db9bd0a11f72d536a7d3feb902fbf5de9e",
          "name": "NeoSwaps.sell"
        },
        "id": "0004766465-b9c00-000005-6vQ7o",
        "timestamp": "2024-01-25T19:53:48.547000Z"
      },
      {
        "accountId": "dE17owFuqybtXatuqm4n4Qu1Gxjdatctj7SY224E19x86vQ7o",
        "assetId": "{\"categoricalOutcome\":[359,0]}",
        "dBalance": "-2352790000000",
        "blockNumber": 4766465,
        "event": "Transfer",
        "extrinsic": {
          "hash": "0x81a849e90fcb21dac2f59a866b92e2db9bd0a11f72d536a7d3feb902fbf5de9e",
          "name": "NeoSwaps.sell"
        },
        "id": "0004766465-b9c00-000007-6vQ7o",
        "timestamp": "2024-01-25T19:53:48.547000Z"
      },
      {
        "accountId": "dE17owFuqybtXatuqm4n4Qu1Gxjdatctj7SY224E19x86vQ7o",
        "assetId": "{\"foreignAsset\":0}",
        "dBalance": "1536636475008",
        "blockNumber": 4766465,
        "event": "Transfer",
        "extrinsic": {
          "hash": "0x81a849e90fcb21dac2f59a866b92e2db9bd0a11f72d536a7d3feb902fbf5de9e",
          "name": "NeoSwaps.sell"
        },
        "id": "0004766465-b9c00-000014-6vQ7o",
        "timestamp": "2024-01-25T19:53:48.547000Z"
      }
    ],
    "swaps": [
      {
        "accountId": "dE17owFuqybtXatuqm4n4Qu1Gxjdatctj7SY224E19x86vQ7o",
        "assetAmountIn": "2352790000000",
        "assetAmountOut": "1567996403069",
        "assetIn": "{\"categoricalOutcome\":[359,0]}",
        "assetOut": "{\"foreignAsset\":0}",
        "blockNumber": 4766465,
        "event": "SellExecuted",
        "extrinsic": {
          "name": "NeoSwaps.sell",
          "hash": "0x81a849e90fcb21dac2f59a866b92e2db9bd0a11f72d536a7d3feb902fbf5de9e"
        },
        "id": "0004766465-b9c00-000015",
        "timestamp": "2024-01-25T19:53:48.547000Z"
      }
    ],

The assetAmountOut of 1567996403069 does not match the respective Transfer transaction, which is of dBalance 1536636475008. Where went the missing 1567996403069 - 1536636475008 = 31359928061 amount?

@Chralt98 Chralt98 added the question Further information is requested label Jan 28, 2024
@Chralt98
Copy link
Member Author

Chralt98 commented Jan 28, 2024

Additional question: What is the 7520187 Transfer amount telling us?

@maltekliemann I don't really remember how the neo-swaps works in detail. Can I have a second pair of eyes on this?

@Chralt98
Copy link
Member Author

Chralt98 commented Jan 28, 2024

It seems like the AMM behaviour is explicitly represented in the transaction events. I weakly remember how neo-swaps works, but I guess it was like additionally buying (of amount 1567996403069 - 1536636475008 = 31359928061) in this case with DOT a full set internally and selling all not wanted outcome assets to the pool to get the desired outcome asset (of amount 7520187), which is sent to the pool. Is this correct?

Oh my bad, we have SellExecuted here, that means that it is the reverse case. I described BuyExecuted above.

SellExecuted like in this case should work like the following (according to the neo-swaps docs):

Use 7520187 of outcome asset to sell for a complete set of outcome assets and then exchanging the complete set for DOT. But why do we get the lesser 1536636475008 amount of DOT out then and not 1567996403069?

@maltekliemann
Copy link

maltekliemann commented Jan 28, 2024

I admit the SellExecuted docs are slightly unclear on this, so I fixed them: zeitgeistpm/zeitgeist#1250

Informant sold a position. amount_out is the amount of collateral received by who, including swap and external fees with swap and external fees not yet deducted. The actual amount received is amount_out - swap_fee_amount - external_fee_amount.

The amount_out value is the amount leaving the pool, with swap fees and external fees not yet deducted. The missing 31_359_928_061 is exactly the value of the swap_fee_amount field, and there are no external fees.

Edit. The phrase "leaving the pool" isn't exactly right either. It's actually leaving the market account AKA prize pool, but you get the idea.

@saboonikhil
Copy link
Member

The amount_out value is the amount leaving the pool, with swap fees and external fees not yet deducted. The missing 31_359_928_061 is exactly the value of the swap_fee_amount field, and there are no external fees.

When is the swap_fee_amount deducted? Which account gets this amount?

@Chralt98
Copy link
Member Author

@maltekliemann Why there are no external fees charged? @saboonikhil Can we please extract the swap_fee_amount and external_fee_amount out of the neo-swap events?

@Chralt98
Copy link
Member Author

@maltekliemann What is this amount 7520187? Why does it go to the treasury here? When I hear treasury, I instantly think of fees. But why should the treasury ever receive outcome asset fees? In this case it is categoricalOutcome[359, 0]. This makes no sense atm to me.

@maltekliemann
Copy link

The account got dusted, hence the transfer to the treasury.

@maltekliemann
Copy link

The amount_out value is the amount leaving the pool, with swap fees and external fees not yet deducted. The missing 31_359_928_061 is exactly the value of the swap_fee_amount field, and there are no external fees.

When is the swap_fee_amount deducted? Which account gets this amount?

During the transaction. The pool account receives the fee, and LPs can then withdraw it. The pool account can be calculated from the pool ID in the same fashion as for the legacy pools.

@maltekliemann
Copy link

@maltekliemann Why there are no external fees charged?

Because the pool has creator fees set to zero.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants