nervosnetwork / mercury

Building on top of ckb-indexer, Mercury provides handy integration features for Nervos CKB.
MIT License
23 stars 15 forks source link

Mercury free balance does not reflect all capacity available in an a DAO cell that is in the withdrawal phase and mature. #447

Closed jordanmack closed 2 years ago

jordanmack commented 2 years ago

The free balance being returned by Mercury does not seem to reflect all of the capacity available in this specific instance with a DAO cell in the withdrawal phase that is mature.

There are seven cells currently on ckb1...mkus: CKB Cell
200 CKB DAO (deposit phase)
200 CKB DAO (deposit phase)
200 CKB DAO (deposit phase)
200 CKB DAO (withdrawal phase mature)
1227.37 CKB Basic Cell
139.20 CKB Basic Cell
200 CKB Basic Cell

Balances from Mercury are:

key value
Free 1664.57737955 CKB
Occupied 408 CKB
Frozen 294CKB

To calculate the free amount of 1664, Mercury includes the 98 CKB from the DAO cell in the withdrawal phase but excludes the 102 CKB occupied capacity by that cell. 200 - 102 + 1227 + 139 + 200 = 1664

Mercury is generating transactions using the DAO cell in the withdrawal phase since it is mature. The 102 CKB required on the cell should not be subtracted in this case because this cell can be spent the same as a basic cell. 200 + 1227 + 139 + 200 = 1766

This amount of 1766 is relevant to developers because they need a way to represent what balance is available for the user to send using Mercury to generate transactions.

Raw requests are below:

echo '{
  "id": 42,
  "jsonrpc": "2.0",
  "method": "get_balance",
  "params": [
    {
      "item": {
        "type": "Address",
        "value": "ckb1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsq0nmjcs850x9c46thjshlw9nlwsq3ztpsc75mkus"
      },
      "asset_infos": [
        {
          "asset_type": "CKB",
          "udt_hash": "0x0000000000000000000000000000000000000000000000000000000000000000"
        }
      ],
      "tip_block_number": null
    }
  ]
}' \
| tr -d '\n' \
| curl -H 'content-type: application/json' -d @- https://Mercury-mainnet.ckbapp.dev/0.4
{
  "jsonrpc": "2.0",
  "result": {
    "balances": [
      {
        "ownership": "ckb1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsq0nmjcs850x9c46thjshlw9nlwsq3ztpsc75mkus",
        "asset_info": {
          "asset_type": "CKB",
          "udt_hash": "0x0000000000000000000000000000000000000000000000000000000000000000"
        },
        "free": "0x26c1a782e3",
        "occupied": "0x97fde9800",
        "frozen": "0x6d8606600"
      }
    ],
    "tip_block_number": "0x7b123d"
  },
  "id": 42
}
echo '{
    "id": 2,
    "jsonrpc": "2.0",
    "method": "get_cells",
    "params": [
        {
            "script": {
                "code_hash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
                "hash_type": "type",
                "args": "0xf3dcb103d1e62e2ba5de50bfdc59fdd00444b0c3"
            },
            "script_type": "lock"
        },
        "asc",
        "0x64"
    ]
}' \
| curl -H 'content-type: application/json' -d @- \
https://mainnet.ckb.dev/indexer
{
  "jsonrpc": "2.0",
  "result": {
    "last_cursor": "0x409bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce801f3dcb103d1e62e2ba5de50bfdc59fdd00444b0c3000000000079ef5b0000000100000000",
    "objects": [
      {
        "block_number": "0x743803",
        "out_point": {
          "index": "0x0",
          "tx_hash": "0xfad306ca6b3bbf1d3d066455b5e44a8ddaf1c157268315dc9a7f312348180066"
        },
        "output": {
          "capacity": "0x4a817c800",
          "lock": {
            "args": "0xf3dcb103d1e62e2ba5de50bfdc59fdd00444b0c3",
            "code_hash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
            "hash_type": "type"
          },
          "type": {
            "args": "0x",
            "code_hash": "0x82d76d1b75fe2fd9a27dfbaa65a039221a380d76c926f378d3f81cf3e7e13f2e",
            "hash_type": "type"
          }
        },
        "output_data": "0x0000000000000000",
        "tx_index": "0x1"
      },
      {
        "block_number": "0x74380c",
        "out_point": {
          "index": "0x0",
          "tx_hash": "0xe8d3ef8fe0679a160a4306eb7fd63ea2537d368b812a69890b1c38e95893a32e"
        },
        "output": {
          "capacity": "0x4a817c800",
          "lock": {
            "args": "0xf3dcb103d1e62e2ba5de50bfdc59fdd00444b0c3",
            "code_hash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
            "hash_type": "type"
          },
          "type": {
            "args": "0x",
            "code_hash": "0x82d76d1b75fe2fd9a27dfbaa65a039221a380d76c926f378d3f81cf3e7e13f2e",
            "hash_type": "type"
          }
        },
        "output_data": "0x0000000000000000",
        "tx_index": "0x4"
      },
      {
        "block_number": "0x75db78",
        "out_point": {
          "index": "0x0",
          "tx_hash": "0x368db5ab9d88ad1d398b514c99c7e923f107189d2cc713fa0a768ba4d48b8087"
        },
        "output": {
          "capacity": "0x4a817c800",
          "lock": {
            "args": "0xf3dcb103d1e62e2ba5de50bfdc59fdd00444b0c3",
            "code_hash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
            "hash_type": "type"
          },
          "type": {
            "args": "0x",
            "code_hash": "0x82d76d1b75fe2fd9a27dfbaa65a039221a380d76c926f378d3f81cf3e7e13f2e",
            "hash_type": "type"
          }
        },
        "output_data": "0x0000000000000000",
        "tx_index": "0x4"
      },
      {
        "block_number": "0x75db87",
        "out_point": {
          "index": "0x0",
          "tx_hash": "0x13dbd3261a8d3fe322327fbdb10e484980596a9432413e9b70cc1e317bef2354"
        },
        "output": {
          "capacity": "0x4a817c800",
          "lock": {
            "args": "0xf3dcb103d1e62e2ba5de50bfdc59fdd00444b0c3",
            "code_hash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
            "hash_type": "type"
          },
          "type": {
            "args": "0x",
            "code_hash": "0x82d76d1b75fe2fd9a27dfbaa65a039221a380d76c926f378d3f81cf3e7e13f2e",
            "hash_type": "type"
          }
        },
        "output_data": "0x41f8720000000000",
        "tx_index": "0x5"
      },
      {
        "block_number": "0x75db87",
        "out_point": {
          "index": "0x1",
          "tx_hash": "0x13dbd3261a8d3fe322327fbdb10e484980596a9432413e9b70cc1e317bef2354"
        },
        "output": {
          "capacity": "0x1c93b224de",
          "lock": {
            "args": "0xf3dcb103d1e62e2ba5de50bfdc59fdd00444b0c3",
            "code_hash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
            "hash_type": "type"
          },
          "type": null
        },
        "output_data": "0x",
        "tx_index": "0x5"
      },
      {
        "block_number": "0x782d3a",
        "out_point": {
          "index": "0x1",
          "tx_hash": "0xf0e7b75739b5aa028dfccbe8a73991bbebd6bc987bcba3c081c5a2146e7a712c"
        },
        "output": {
          "capacity": "0x33dbd7405",
          "lock": {
            "args": "0xf3dcb103d1e62e2ba5de50bfdc59fdd00444b0c3",
            "code_hash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
            "hash_type": "type"
          },
          "type": null
        },
        "output_data": "0x",
        "tx_index": "0x2"
      },
      {
        "block_number": "0x79ef5b",
        "out_point": {
          "index": "0x0",
          "tx_hash": "0xefc4098d5880f9b1ffa3fb59e9b18bbd456354f0a6273b45701390250cf3c5ba"
        },
        "output": {
          "capacity": "0x4a817c800",
          "lock": {
            "args": "0xf3dcb103d1e62e2ba5de50bfdc59fdd00444b0c3",
            "code_hash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
            "hash_type": "type"
          },
          "type": null
        },
        "output_data": "0x",
        "tx_index": "0x1"
      }
    ]
  },
  "id": 2
}
EthanYuan commented 2 years ago

Your analysis is correct. We found that for cells in the withdrawing state, get_balance is missing some calculations.

This problem is fixed by this https://github.com/nervosnetwork/mercury/pull/449, and we will arrange to release a new version as soon as possible.

Thanks a lot for your feedback.

EthanYuan commented 2 years ago

Mercury v0.4.3 has been released and the public service has been updated.