Expand description
§ic-cdk-bitcoin-canister
This crate provides functionality for making inter-canister calls to the Bitcoin canisters.
The Bitcoin canisters allow for interactions with the Bitcoin network from within the Internet Computer. This crate includes functions and types that facilitate these interactions, adhering to the Bitcoin Canisters Interface Specification.
The types are re-exported from the ic_btc_interface crate.
§Bounded-wait vs. Unbounded-wait
Interacting with the Bitcoin canisters involves making inter-canister calls, which can be either bounded-wait or unbounded-wait.
Most of the functions in this crate use the bounded-wait calls because they only read state.
The only function that uses the unbounded-wait call is bitcoin_send_transaction.
If the default behavior is not suitable for a particular use case, the Call struct can be used directly to make the call.
For example, bitcoin_get_utxos makes an bounded-wait call. If an unbounded-wait call is preferred, the call can be made as follows:
use ic_cdk_bitcoin_canister::{cost_get_utxos, get_bitcoin_canister_id, GetUtxosRequest, GetUtxosResponse, Network};
use ic_cdk::call::Call;
async fn example() -> ic_cdk::call::CallResult<GetUtxosResponse> {
let arg = GetUtxosRequest {
address: "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq".to_string(),
network: Network::Mainnet.into(),
filter: None,
};
let canister_id = get_bitcoin_canister_id(Network::from(arg.network));
let cycles = cost_get_utxos(&arg);
let res: GetUtxosResponse = Call::unbounded_wait(canister_id, "bitcoin_get_utxos")
.with_arg(&arg)
.with_cycles(cycles)
.await?
.candid()?;
Ok(res)
}§Cycle Cost
All the Bitcoin canister methods require cycles to be attached to the call. The helper functions in this crate automatically calculate the required cycles and attach them to the call.
For completeness, this crate also provides functions to calculate the cycle cost:
cost_get_utxoscost_get_balancecost_get_current_fee_percentilescost_get_block_headerscost_send_transaction
§Bitcoin Canister ID
The Bitcoin canister ID is determined by the network.
The helper functions in this crate automatically determine the canister ID based on the network field in the request.
For completeness, the get_bitcoin_canister_id function can be used to get the canister ID manually.
Structs§
- Blockchain
Info - Information about the blockchain as seen by the canister.
- Config
- The config of the canister.
- Fees
- GetBalance
Request - GetBlock
Headers Request - A request for getting the block headers from a given height.
- GetBlock
Headers Response - The response returned for a request for getting the block headers from a given height.
- GetCurrent
FeePercentiles Request - A request for getting the current fee percentiles.
- GetUtxos
Request - A request for getting the UTXOs for a given address.
- GetUtxos
Response - The response returned for a request to get the UTXOs of a given address.
- Init
Config - The config used to initialize the canister.
- OutPoint
- A reference to a transaction output.
- Send
Transaction Request - SetConfig
Request - A request to update the canister’s config.
- Txid
- Utxo
- An unspent transaction output.
Enums§
- Canister
Arg - Flag
- GetBalance
Error - GetBlock
Headers Error - Errors when processing a
get_block_headersrequest. - GetUtxos
Error - Errors when processing a
get_utxosrequest. - Network
- Network
InRequest - A network enum that allows both upper and lowercase variants. Supporting both variants allows us to be compatible with the spec (lowercase) while not breaking current dapps that are using uppercase variants.
- Send
Transaction Error - Txid
From StrError - Utxos
Filter - A filter used when requesting UTXOs.
- Utxos
Filter InRequest - A UtxosFilter enum that allows both upper and lowercase variants. Supporting both variants allows us to be compatible with the spec (lowercase) while not breaking current dapps that are using uppercase variants.
Functions§
- bitcoin_
get_ balance - Gets the current balance of a Bitcoin address in Satoshi.
- bitcoin_
get_ block_ headers - Gets the block headers in the provided range of block heights.
- bitcoin_
get_ current_ fee_ percentiles - Gets the Bitcoin transaction fee percentiles.
- bitcoin_
get_ utxos - Gets all unspent transaction outputs (UTXOs) associated with the provided address.
- bitcoin_
send_ transaction - Sends a Bitcoin transaction to the Bitcoin network.
- cost_
get_ balance - Gets the cycles cost for the
bitcoin_get_balancefunction. - cost_
get_ block_ headers - Gets the cycles cost for the
bitcoin_get_block_headersfunction. - cost_
get_ current_ fee_ percentiles - Gets the cycles cost for the
bitcoin_get_current_fee_percentilesfunction. - cost_
get_ utxos - Gets the cycles cost for the
bitcoin_get_utxosfunction. - cost_
send_ transaction - Gets the cycles cost for the
bitcoin_send_transactionfunction. - get_
bitcoin_ canister_ id - Gets the canister ID of the Bitcoin canister for the specified network.
- get_
blockchain_ info - Gets information about the current state of the Bitcoin blockchain as seen by the canister.