demergent-labs / azle

A WebAssembly runtime for TypeScript and JavaScript on ICP
MIT License
208 stars 36 forks source link

Implement Bitcoin functionality #565

Closed lastmjs closed 2 years ago

lastmjs commented 2 years ago

Resources:

dansteren commented 2 years ago

Quick progress update.

I've created all the necessary TS types in https://github.com/demergent-labs/azle/pull/627. Once that's merged in you should no longer need to declare your own types. You can just import the management canister and start calling methods on it directly.

Example canister:

import { CanisterResult, Update, Variant } from 'azle';
import {
    BitcoinNetwork,
    ManagementCanister,
    Satoshi
} from 'azle/canisters/management';

export type ExecuteGetBalanceResult = Variant<{
    ok: Satoshi;
    err: string;
}>;

export function* get_balance(address: string): Update<ExecuteGetBalanceResult> {
    const BITCOIN_API_CYCLE_COST = 100_000_000n;

    const canister_result: CanisterResult<Satoshi> =
        yield ManagementCanister.bitcoin_get_balance({
            address,
            min_confirmations: null,
            network: BitcoinNetwork.Regtest
        }).with_cycles(BITCOIN_API_CYCLE_COST);

    return canister_result;
}

I'm still working on creating a fully functioning example. I have one mostly working on the 565_bitcoin branch.

Where I'm currently hitting snags:

dansteren commented 2 years ago

Made good progress today. The test suite is almost complete. I've got it downloading and running the bitcoin daemon inline. The test is getting hung up when mining blocks for some reason though. I make a call out to the bitcoin-cli and it never returns. So figuring that out is my next step.

But we've discovered the reason behind why the canister was always returning a balance of 0. The replica takes a little longer to pick up the state changes so we'll just have to add a delay in and then the balances should show up.