Submitting transactions
Advanced
Bitcoin
Tutorial
Overview
To submit transactions to the Bitcoin network, the Bitcoin integration API exposes the following method:
bitcoin_send_transaction
: Submits a transaction to the Bitcoin network.
Sending transactions
To send transactions to the Bitcoin network, make a call to the bitcoin_send_transaction
Bitcoin API method. You can create a function in your canister to call this method such as:
- Motoko
- Rust
type ManagementCanisterActor = actor {
bitcoin_send_transaction : SendTransactionRequest -> async ();
};
let management_canister_actor : ManagementCanisterActor = actor("aaaaa-aa");
public func send_transaction(network : Network, transaction : [Nat8]) : async () {
let transaction_fee =
SEND_TRANSACTION_BASE_COST_CYCLES + transaction.size() * SEND_TRANSACTION_COST_CYCLES_PER_BYTE;
ExperimentalCycles.add(transaction_fee);
await management_canister_actor.bitcoin_send_transaction({
network;
transaction;
})
};
use ic_cdk::api::management_canister::bitcoin::{
BitcoinNetwork, GetBalanceRequest, GetCurrentFeePercentilesRequest, GetUtxosRequest,
GetUtxosResponse, MillisatoshiPerByte, Satoshi, SendTransactionRequest,
};
pub async fn send_transaction(network: BitcoinNetwork, transaction: Vec<u8>) {
let transaction_fee = SEND_TRANSACTION_BASE_CYCLES
+ (transaction.len() as u64) * SEND_TRANSACTION_PER_BYTE_CYCLES;
let res: Result<(), _> = call_with_payment(
Principal::management_canister(),
"bitcoin_send_transaction",
(SendTransactionRequest {
network: network.into(),
transaction,
},),
transaction_fee,
)
.await;
res.unwrap();
}
Then make a call to this canister's function with the command:
dfx canister call CANISTER_NAME send '(record { destination_address = "ADDRESS"; amount_in_satoshi = 100000000; })'
Replace CANISTER_NAME
with your canister's name and replace ADDRESS
with the destination Bitcoin address. Learn more about Bitcoin addresses.