vulpemventures / ocean

:ocean: Elements/Liquid wallet daemon
MIT License
5 stars 7 forks source link

[CLI] Loss of info when converting amount from btc to sats unit #76

Closed maximusmaximalistus closed 5 months ago

maximusmaximalistus commented 5 months ago

I add an mnemonic from tdex 0.9.3. My goal was to restore it and withdraw the funds. I installed ocean 0.2.2 using docker with help from dev Altafan.

$ docker-compose up -d oceand

The compose file is this one:

version: "3.7"

services:
  oceand:
    container_name: oceand
    image: ghcr.io/vulpemventures/oceand:latest
    restart: unless-stopped
    user: 0:0
    environment:
      - OCEAN_LOG_LEVEL=5
      - OCEAN_NO_TLS=true
      - OCEAN_NO_PROFILER=true
      - OCEAN_ELECTRUM_URL=ssl://blockstream.info:995
      - OCEAN_NETWORK=liquid
      - OCEAN_UTXO_EXPIRY_DURATION_IN_SECONDS=60
      - OCEAN_DB_TYPE=badger
    ports:
      - "18000:18000"
    volumes:
      - ./volumes/oceand/data:/home/ocean/.oceand
      - ./volumes/oceand/cli:/home/ocean/.ocean-cli
  tdexd:
    container_name: tdexd
    image: ghcr.io/tdex-network/tdexd:latest
    restart: unless-stopped
    user: 0:0
    depends_on:
      - oceand
    environment:
      - TDEX_WALLET_ADDR=oceand:18000
      - TDEX_LOG_LEVEL=5
      - TDEX_FEE_ACCOUNT_BALANCE_THRESHOLD=1000
      - TDEX_NO_MACAROONS=true
      - TDEX_NO_OPERATOR_TLS=true
      - TDEX_CONNECT_PROTO=http
    ports:
      - "9000:9000"
      - "9945:9945"
    volumes:
      - ./volumes/tdexd/data:/home/tdex/.tdex-daemon
      - ./volumes/tdexd/cli:/home/tdex/.tdex-operator
networks:
  default:
    name: nigiri
    external: true

The directory of the file looks like this:

$ tree .
.
├── docker-compose.yml
└── volumes
    └── oceand
        ├── cli
        └── data

After configuring alias $ alias ocean="docker exec oceand ocean" and initialization ocean config init --no-tls. The wallet was restored.

$ ocean wallet restore --password "" --mnemonic "" --root-path "m/84'/0'" --account-threshold 300

The wallet had 3 markets.

$ ocean wallet info
{
  "network": "NETWORK_MAINNET",
  "nativeAsset": "6f0279e9ed041c3d710a9f57d0c02928416460c4b722ae3457a11eec381c526d",
  "rootPath": "m/84'/0'",
  "birthdayBlockHash": "afafbbdfc52a45e51a3b634f391f952f6bdfd14ef74b34925954b4e20d0ad639",
  "birthdayBlockHeight": 1,
  "accounts": [
    {
      "namespace": "bip84-account5",
      "label": "",
      "derivationPath": "m/84'/0'/5'",
      "xpubs": [
        "xpub6CP8K6qmjDeSJbRWBmJBQFpsCtXjUcWfCEf5vjvVay2rHsgi8FfjfXL5wfxuRczjUjVbeL3Knw4eaqigT6rVsPg9sCGb7TAtNPeNRjdFZcR"
      ],
      "masterBlindingKey": "15dca54502550b7853b309e4f0d9129ef7cf78384c62a11536b68cbfaa48ca0b"
    },
    {
      "namespace": "bip84-account0",
      "label": "",
      "derivationPath": "m/84'/0'/0'",
      "xpubs": [
        "xpub6CP8K6qmjDeS7kawbTgecEH9bSnB5XEAUPUct8kFFEv4mVCqrf1PVwzhDekiNeGz2xp5BJcBgBzN789ZxHoe3u1nL7hMqB7AppLLDfBwBxs"
      ],
      "masterBlindingKey": "15dca54502550b7853b309e4f0d9129ef7cf78384c62a11536b68cbfaa48ca0b"
    },
    {
      "namespace": "bip84-account9",
      "label": "",
      "derivationPath": "m/84'/0'/9'",
      "xpubs": [
        "xpub6CP8K6qmjDeSV5PX8WoZf329D1NA9KHLZgkuvsGSz6CwamxdDnqoP7w5fPMgFmMksAzc2o9Ni86rn94QxHJmzDcXutM6op3n6UrxbbLFB3g"
      ],
      "masterBlindingKey": "15dca54502550b7853b309e4f0d9129ef7cf78384c62a11536b68cbfaa48ca0b"
    },
    {
      "namespace": "bip84-account8",
      "label": "",
      "derivationPath": "m/84'/0'/8'",
      "xpubs": [
        "xpub6CP8K6qmjDeSSvczPdyRr1hBdPoV1TeH3DuXahbYBynecmbbPiEphFt5NARPETxWDoFbBsVRwcfgnnyatkVDG7FWuARJNnU8tSET2SKAoSV"
      ],
      "masterBlindingKey": "15dca54502550b7853b309e4f0d9129ef7cf78384c62a11536b68cbfaa48ca0b"
    }
  ],
  "buildInfo": {
    "version": "dev",
    "commit": "none",
    "date": "unknown"
  }
}

The balances were:

$ ocean account balance --account-name bip84-account0
{
  "balance": {
    "6f0279e9ed041c3d710a9f57d0c02928416460c4b722ae3457a11eec381c526d": {
      "confirmedBalance": "19002",
      "unconfirmedBalance": "0",
      "lockedBalance": "0",
      "totalBalance": "19002"
    }
  }
}

$ ocean account balance --account-name bip84-account9
{
  "balance": {
    "6f0279e9ed041c3d710a9f57d0c02928416460c4b722ae3457a11eec381c526d": {
      "confirmedBalance": "1010",
      "unconfirmedBalance": "0",
      "lockedBalance": "0",
      "totalBalance": "1010"
    },
    "fdf208c97fdefdbb1cb85ce946bfa66b2c0b639eeeefad8b8fcc81843dcb4a0c": {
      "confirmedBalance": "109899252500000",
      "unconfirmedBalance": "0",
      "lockedBalance": "0",
      "totalBalance": "109899252500000"
    }
  }
}

$ ocean account balance --account-name bip84-account5
{
  "balance": {}
}

$ ocean account balance --account-name bip84-account9
{
  "balance": {
    "6f0279e9ed041c3d710a9f57d0c02928416460c4b722ae3457a11eec381c526d": {
      "confirmedBalance": "1010",
      "unconfirmedBalance": "0",
      "lockedBalance": "0",
      "totalBalance": "1010"
    },
    "fdf208c97fdefdbb1cb85ce946bfa66b2c0b639eeeefad8b8fcc81843dcb4a0c": {
      "confirmedBalance": "109899252500000",
      "unconfirmedBalance": "0",
      "lockedBalance": "0",
      "totalBalance": "109899252500000"
    }
  }
}

Funds from account9 were withdrawn.

$ ocean transaction transfer --account-name bip84-account9 --sats-per-byte 0.11 --receivers '{"address": "VJLGrQjdZZZjsYiJHiWXC9RfsrTpkD12e2XvfXteye6YEbFWf99dzEyCwTSgBSui9Ububzt17JsWz2dR", "asset": "6f0279e9ed041c3d710a9f57d0c02928416460c4b722ae3457a11eec381c526d", "amount": 0.0000101}' --receivers '{"address": "VJLGrQjdZZZjsYiJHiWXC9RfsrTpkD12e2XvfXteye6YEbFWf99dzEyCwTSgBSui9Ububzt17JsWz2dR", "asset": "fdf208c97fdefdbb1cb85ce946bfa66b2c0b639eeeefad8b8fcc81843dcb4a0c", "amount": 1098992.52500000}'

Since account8 had no L-BTC, everything in account0 was sent to it.

$ ocean account derive --account-name bip84-account8

$ ocean transaction transfer --account-name bip84-account0 --sats-per-byte 0.11 --receivers '{"address": "lq1qqg4fpdglex3tp9e30nmsak7gugcsw3auyh8m4zxdfvgjcf0xthcefem6rgkkhdn22lywmvmxwzhyv03cpvrs2954ajxrkj69a", "asset": "6f0279e9ed041c3d710a9f57d0c02928416460c4b722ae3457a11eec381c526d", "amount": 0.00019002}'

And then withdrawn also.

When I checked the balances.

$ ocean account balance --account-name bip84-account0
{
  "balance": {}
}

$ ocean account balance --account-name bip84-account8
{
  "balance": {}
}

$ ocean account balance --account-name bip84-account9
{
  "balance": {
    "fdf208c97fdefdbb1cb85ce946bfa66b2c0b639eeeefad8b8fcc81843dcb4a0c": {
      "confirmedBalance": "1",
      "unconfirmedBalance": "0",
      "lockedBalance": "0",
      "totalBalance": "1"
    }
  }
}

In account9 one unit was still there.

This is the transaction that was done to withdraw the all the funds.

https://blockstream.info/liquid/tx/97ac82d39f0ccf07ea35c23b9626b5d7468efd6ba16fc7399f59d6c997d61919

And this are the utxos.

$ ocean account utxos --account-name bip84-account9
{
  "spendableUtxos": {
    "accountName": "bip84-account9",
    "utxos": [
      {
        "txid": "97ac82d39f0ccf07ea35c23b9626b5d7468efd6ba16fc7399f59d6c997d61919",
        "index": 2,
        "asset": "fdf208c97fdefdbb1cb85ce946bfa66b2c0b639eeeefad8b8fcc81843dcb4a0c",
        "value": "1",
        "script": "0014de07f4f5eb43daf55dc1804b0c548c5726482227",
        "assetBlinder": "0b0762acb067dfc289fc2808d6fa7abcf53ad64f5e58b5a76f3ac714d6a4d7fc",
        "valueBlinder": "24c720a8af736d99ba9f780830151375b71fc3feb129a403141694b525f101f7",
        "accountName": "bip84-account9",
        "spentStatus": null,
        "confirmedStatus": {
          "txid": "",
          "blockInfo": {
            "hash": "01d68f65d8811717240443e0cfeee2b0a9d6a74b8bcf47559c636170e3e44989",
            "height": "2705964",
            "timestamp": "1706666708"
          },
          "txhex": ""
        },
        "redeemScript": ""
      }
    ]
  },
  "lockedUtxos": {
    "accountName": "bip84-account9",
    "utxos": []
  }
}

I open this issue since this can affect users in the future when trying to withdraw all funds. This asset is a memecoin and one unit isn't a big issue. But in other assets it could be. And huge thanks to dev Altafan for helping in this issue.

altafan commented 5 months ago

Thank you for the super detailed description, it will be easier for us to try to reproduce what you experienced.

At first look, I would say there may have been some loss of precision when converting the amount from client to server or something like that. We'll add more details on the cause of this once we investigate properly.

altafan commented 5 months ago

I identified the bug. Good news is that the bug is in the CLI when the amount expressed in bitcoin is converted to sats. Should be easy to fix it by using decimal for the conversion instead of the built-in cast.

altafan commented 5 months ago

@maximusmaximalistus I'm glad to inform you that this has been fixed in #81 and included in v0.2.3