Skip to content

Commit

Permalink
[framework] emit Transfer UTXO Evnet to queue_event (#2476)
Browse files Browse the repository at this point in the history
* [framework] emit Transfer UTXO Evnet to queue_event

* fix: emit event condition

---------

Co-authored-by: mx819812523 <mx819812523@gamil.com>
  • Loading branch information
mx819812523 and mx819812523 authored Aug 21, 2024
1 parent 738dfc5 commit b43be4b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
14 changes: 14 additions & 0 deletions frameworks/bitcoin-move/doc/bitcoin.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- [Struct `TxProgressErrorLogEvent`](#0x4_bitcoin_TxProgressErrorLogEvent)
- [Struct `RepeatCoinbaseTxEvent`](#0x4_bitcoin_RepeatCoinbaseTxEvent)
- [Resource `BitcoinBlockStore`](#0x4_bitcoin_BitcoinBlockStore)
- [Struct `TransferUTXOEvent`](#0x4_bitcoin_TransferUTXOEvent)
- [Constants](#@Constants_0)
- [Function `genesis_init`](#0x4_bitcoin_genesis_init)
- [Function `get_tx`](#0x4_bitcoin_get_tx)
Expand All @@ -25,8 +26,10 @@
<pre><code><b>use</b> <a href="">0x1::option</a>;
<b>use</b> <a href="">0x1::string</a>;
<b>use</b> <a href="">0x1::vector</a>;
<b>use</b> <a href="">0x2::address</a>;
<b>use</b> <a href="">0x2::bcs</a>;
<b>use</b> <a href="">0x2::event</a>;
<b>use</b> <a href="">0x2::event_queue</a>;
<b>use</b> <a href="">0x2::object</a>;
<b>use</b> <a href="">0x2::signer</a>;
<b>use</b> <a href="">0x2::simple_multimap</a>;
Expand Down Expand Up @@ -79,6 +82,17 @@



<a name="0x4_bitcoin_TransferUTXOEvent"></a>

## Struct `TransferUTXOEvent`



<pre><code><b>struct</b> <a href="bitcoin.md#0x4_bitcoin_TransferUTXOEvent">TransferUTXOEvent</a> <b>has</b> <b>copy</b>, drop, store
</code></pre>



<a name="@Constants_0"></a>

## Constants
Expand Down
41 changes: 38 additions & 3 deletions frameworks/bitcoin-move/sources/bitcoin.move
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ module bitcoin_move::bitcoin{
use std::option::{Self, Option};
use std::vector;
use std::string::{Self, String};
use moveos_std::address::to_string;
use moveos_std::event_queue;

use moveos_std::timestamp;
use moveos_std::simple_multimap::SimpleMultiMap;
Expand Down Expand Up @@ -68,6 +70,14 @@ module bitcoin_move::bitcoin{
tx_ids: TableVec<address>,
}


struct TransferUTXOEvent has drop, store, copy {
txid: address,
sender: Option<address>,
receiver: address,
value: u64
}

public(friend) fun genesis_init(_genesis_account: &signer, genesis_block_height: u64, genesis_block_hash: address){
let btc_block_store = BitcoinBlockStore{
genesis_block: types::new_block_height_hash(genesis_block_height, genesis_block_hash),
Expand Down Expand Up @@ -154,12 +164,19 @@ module bitcoin_move::bitcoin{
let idx = 0;
let output_seals = simple_multimap::new<u32, UTXOSeal>();
let need_process_oridinal = need_process_oridinals(block_height);
let sender: Option<address> = option::none<address>();
let find_sender: bool = false;
while (idx < vector::length(txinput)) {
let txin = vector::borrow(txinput, idx);
let outpoint = *types::txin_previous_output(txin);
if (utxo::exists_utxo(outpoint)) {
let object_id = utxo::derive_utxo_id(outpoint);
let utxo_obj = utxo::take(object_id);
let utxo_owner = object::owner(&utxo_obj);
if (!find_sender && utxo_owner != @bitcoin_move) {
sender = option::some(utxo_owner);
find_sender = true;
};
if(need_process_oridinal) {
let (sat_points, utxo_flotsams) = ord::spend_utxo(&mut utxo_obj, tx, input_utxo_values, idx);
handle_sat_point(sat_points, &mut output_seals);
Expand Down Expand Up @@ -201,7 +218,7 @@ module bitcoin_move::bitcoin{
};

// create new utxo
handle_new_utxo(tx, &mut output_seals, false, block_height);
handle_new_utxo(tx, &mut output_seals, false, block_height, sender);

simple_multimap::drop(output_seals);
flotsams
Expand All @@ -215,7 +232,7 @@ module bitcoin_move::bitcoin{
};

// create new utxo
let repeat_txid = handle_new_utxo(tx, &mut output_seals, true, block_height);
let repeat_txid = handle_new_utxo(tx, &mut output_seals, true, block_height, option::none());
simple_multimap::drop(output_seals);
repeat_txid
}
Expand All @@ -236,7 +253,7 @@ module bitcoin_move::bitcoin{
// output_seals
}

fun handle_new_utxo(tx: &Transaction, output_seals: &mut SimpleMultiMap<u32, UTXOSeal>, is_coinbase: bool, block_height: u64) :bool {
fun handle_new_utxo(tx: &Transaction, output_seals: &mut SimpleMultiMap<u32, UTXOSeal>, is_coinbase: bool, block_height: u64, sender: Option<address>) :bool {
let txid = types::tx_id(tx);
let txoutput = types::tx_output(tx);
let idx = 0;
Expand Down Expand Up @@ -278,6 +295,24 @@ module bitcoin_move::bitcoin{
};
let owner_address = types::txout_object_address(txout);
utxo::transfer(utxo_obj, owner_address);
if (owner_address != @bitcoin_move){
event_queue::emit(to_string(&owner_address), TransferUTXOEvent{
txid,
sender,
receiver: owner_address,
value
})
};
if (option::is_some(&sender)){
let sender_address = option::extract(&mut sender);
event_queue::emit(to_string(&sender_address), TransferUTXOEvent{
txid,
sender,
receiver: owner_address,
value
});
};


//Auto create address mapping, we ensure when UTXO object create, the address mapping is recored
let bitcoin_address_opt = types::txout_address(txout);
Expand Down

0 comments on commit b43be4b

Please sign in to comment.