Submitting transactions
Bitcoin
To submit transactions to the Bitcoin network, the Bitcoin API exposes the bitcoin_send_transaction
method.
The following snippet shows how to send a signed transaction to the Bitcoin network:
- Motoko
- Rust
motoko/basic_bitcoin/src/basic_bitcoin/src/BitcoinApi.mo
/// Sends a (signed) transaction to the Bitcoin network.
///
/// Relies on the `bitcoin_send_transaction` endpoint.
/// See https://internetcomputer.org/docs/current/references/ic-interface-spec/#ic-bitcoin_send_transaction
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<system>(transaction_fee);
await management_canister_actor.bitcoin_send_transaction({
network;
transaction;
})
};
}
rust/basic_bitcoin/src/service/send_from_p2pkh_address.rs
bitcoin_send_transaction(&SendTransactionRequest {
network: ctx.network,
transaction: serialize(&signed_transaction),
})
.await
.unwrap();
// Return the transaction ID.
signed_transaction.compute_txid().to_string()
}