Submitting transactions
Advanced
Bitcoin
Tutorial
Overview
To submit transactions to the Bitcoin network, the Bitcoin integration API exposes the bitcoin_send_transaction
method.
Submitting transactions
The following snippet shows how to send a signed transaction to the Bitcoin network.
- 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;
})
};
View in the full example.
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();
}
View in the full example.