ckb-cell / rgbpp-sdk

Utilities for Bitcoin and RGB++ asset integration
ISC License
52 stars 16 forks source link

feat(ckb|example): Dynamic fetching cell deps deployed by TypeID #191

Closed duanyytop closed 2 months ago

duanyytop commented 2 months ago

Problem

Although Lumos provides a function refreshTypeIdCellDeps to refresh the cell deps of a upgraded TypeID script, it requires the dApp developers themselves to handle it manually.

But usually a dApp developer doesn't know when a TypeID script is updated.

This PR introduces a mechanism to automatically get the TypeID scripts' latest cell deps, trying to make things easier.

Design Goal and Main Changes

Those that need to be optimized

Fetching the GitHub JSON file is not stable or slow for some users, we should do something to optimize it.

The GitHub JSON file path is https://github.com/ckb-cell/rgbpp-sdk/pull/191/files#diff-3039d7f71cfd89a5a23296b2cccdb966bc9132c2bf938a39a08bf0aeceed5d58R21

The tests that need to be run

I have tested the examples of xudt-on-ckb and using the integration tests of https://github.com/ckb-cell/rgbpp-sdk/pull/169 to test more examples is needed. cc @Dawn-githup

Flouse commented 2 months ago

Those that need to be optimized

Fetching the GitHub JSON file is not stable or slow for some users, we should do something to optimize it.

The GitHub JSON file path is https://github.com/ckb-cell/rgbpp-sdk/pull/191/files#diff-3039d7f71cfd89a5a23296b2cccdb966bc9132c2bf938a39a08bf0aeceed5d58R21

May need a CDN for the static JSON content and test the speed comparing to fetching TypeID script cell deps through ckb-indexer directly.

ahonn commented 2 months ago

Setup CDN for https://raw.githubusercontent.com/ckb-cell/typeid-contract-cell-deps/main/deployment/cell-deps.json

We can try using https://www.jsdelivr.com/?docs=gh, they provide a free, fast CDN. For example: https://cdn.jsdelivr.net/gh/ckb-cell/typeid-contract-cell-deps@main/deployment/cell-deps.json

duanyytop commented 2 months ago

Those that need to be optimized

Fetching the GitHub JSON file is not stable or slow for some users, we should do something to optimize it. The GitHub JSON file path is https://github.com/ckb-cell/rgbpp-sdk/pull/191/files#diff-3039d7f71cfd89a5a23296b2cccdb966bc9132c2bf938a39a08bf0aeceed5d58R21

May need a CDN for the static JSON content and test the speed comparing to fetching TypeID script cell deps through ckb-indexer directly.

# RGB++ Lock deployment type script
➜  rgbpp echo '{
    "id": 2,
    "jsonrpc": "2.0",
    "method": "get_cells",
    "params": [
        {
            "script": {
                "code_hash": "0x00000000000000000000000000000000000000000000000000545950455f4944",
                "hash_type": "type",
                "args": "0xa3bc8441df149def76cfe15fec7b1e51d949548bc27fb7a75e9d4b3ef1c12c7f"
            },
            "script_type": "type"
        },
        "asc",
        "0x64"
    ]
}' \
| curl -o /dev/null -s -w 'Total: %{time_total}s\n' -H 'content-type: application/json' -d @- \
https://testnet.ckb.dev/indexer
Total: 0.710650s

# CDN
➜  rgbpp curl -o /dev/null -s -w 'Total: %{time_total}s\n' https://cdn.jsdelivr.net/gh/ckb-cell/typeid-contract-cell-deps@main/deployment/cell-deps.json

Total: 0.197599s

# raw github url
➜  rgbpp curl -o /dev/null -s -w 'Total: %{time_total}s\n' https://raw.githubusercontent.com/ckb-cell/typeid-contract-cell-deps/main/deployment/cell-deps.json
Total: 0.559571s
➜  rgbpp curl -o /dev/null -s -w 'Total: %{time_total}s\n' https://raw.githubusercontent.com/ckb-cell/typeid-contract-cell-deps/main/deployment/cell-deps.json
Total: 0.279097s
duanyytop commented 2 months ago
const request = (url: string) => axios.get(url, { timeout: 2000 });

const fetchCellDepsJson = async () => {
  try {
    const response = await Promise.any([request(GITHUB_CELL_DEPS_JSON_URL), request(CDN_GITHUB_CELL_DEPS_JSON_URL)]);
    return response.data as CellDepsObject;
  } catch (error) {
    // console.error('Error fetching cell deps:', error);
  }
};

The CDN and timeout have been added to make the cell deps fetching faster and more stable in https://github.com/ckb-cell/rgbpp-sdk/pull/191/commits/1310d40741e4c7286910ea61a0959c607325895d

duanyytop commented 2 months ago

Update https://github.com/ckb-cell/typeid-contract-cell-deps automatically through a GitHub action workflow (maybe every 5 mins)

I think the update of the cell-ceps.json of typeid-contract-cell-deps should not block this PR. And I will implement the feature in the typeid-contract-cell-deps repository later.

duanyytop commented 2 months ago

Update https://github.com/ckb-cell/typeid-contract-cell-deps automatically through a GitHub action workflow (maybe every 5 mins)

I think the update of the cell-ceps.json of typeid-contract-cell-deps should not block this PR. And I will implement the feature in the typeid-contract-cell-deps repository later.

The task of updating latest cell deps has been triggering and the update time is not strictly 5 minutes. https://github.com/ckb-cell/typeid-contract-cell-deps/actions/workflows/update.yml