Skip to content

Commit

Permalink
refact gas market info and format code (#2693)
Browse files Browse the repository at this point in the history
Co-authored-by: mx819812523 <mx819812523@gamil.com>
  • Loading branch information
mx819812523 and mx819812523 authored Sep 26, 2024
1 parent 0e453b6 commit 93874a0
Show file tree
Hide file tree
Showing 2 changed files with 182 additions and 100 deletions.
142 changes: 78 additions & 64 deletions infra/rooch-portal-v2/contract/gas_market/sources/gas_market.move
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ module gas_market::gas_market {
use moveos_std::event_queue::{Subscriber, consume};
use moveos_std::event_queue;
use moveos_std::table;
use moveos_std::signer::{module_signer, address_of};
use moveos_std::account::{move_resource_to, borrow_mut_resource};
use moveos_std::table::Table;
use rooch_framework::account_coin_store;
use moveos_std::tx_context::sender;
Expand All @@ -23,14 +21,15 @@ module gas_market::gas_market {

struct AdminCap has store, key {}

struct RGasMarket has key, store{
struct RGasMarket has key, store {
rgas_store: Object<CoinStore<RGas>>,
unit_price: u256,
receive_btc_address: address,
market_info: MarketInfo,
is_open: bool
}

struct MarketInfo has key, store{
struct MarketInfo has key, store {
total_deposit: u256,
total_withdraw: u256,
buyer: Table<address, u256>,
Expand All @@ -47,83 +46,93 @@ module gas_market::gas_market {
const ErrorTokenPrice: u64 = 2;
const ErrorNoUncheckTxid: u64 = 3;


fun init() {
let rgas_market_obj = object::new_named_object(RGasMarket{
rgas_store: coin_store::create_coin_store<RGas>(),
unit_price: DEFAULT_UNIT_PRICE,
receive_btc_address: sender(),
is_open: true
});
let admin_cap = object::new_named_object(AdminCap{});

move_resource_to(&module_signer<MarketInfo>(), MarketInfo{
total_deposit: 0,
total_withdraw: 0,
buyer: table::new(),
uncheck_info: table::new()
});
let rgas_market_obj =
object::new_named_object(
RGasMarket {
rgas_store: coin_store::create_coin_store<RGas>(),
unit_price: DEFAULT_UNIT_PRICE,
receive_btc_address: sender(),
market_info: MarketInfo {
total_deposit: 0,
total_withdraw: 0,
buyer: table::new(),
uncheck_info: table::new()
},
is_open: true
}
);
let admin_cap = object::new_named_object(AdminCap {});

to_shared(event_queue::subscribe<ReceiveUTXOEvent>(to_string(&sender())));
transfer(admin_cap, sender());
to_shared(rgas_market_obj)
}

public entry fun add_rgas_coin(
account: &signer,
rgas_market_obj: &mut Object<RGasMarket>,
amount: u256
){
account: &signer, rgas_market_obj: &mut Object<RGasMarket>, amount: u256
) {
let rgas_market = object::borrow_mut(rgas_market_obj);
assert!(rgas_market.is_open, ErrorMarketNotOpen);
let rgas_coin = account_coin_store::withdraw<RGas>(account, amount);
coin_store::deposit(&mut rgas_market.rgas_store, rgas_coin);
let market_info_mut = borrow_mut_resource<MarketInfo>(address_of(&module_signer<MarketInfo>()));
market_info_mut.total_deposit = market_info_mut.total_deposit + amount

rgas_market.market_info.total_deposit = rgas_market.market_info.total_deposit + amount
}

public entry fun withdraw_rgas_coin(
_admin: &mut Object<AdminCap>,
rgas_market_obj: &mut Object<RGasMarket>,
amount: u256
){
) {
let rgas_market = object::borrow_mut(rgas_market_obj);
assert!(rgas_market.is_open, ErrorMarketNotOpen);

let rgas_coin = coin_store::withdraw<RGas>(&mut rgas_market.rgas_store, amount);
account_coin_store::deposit(sender(), rgas_coin);
let market_info_mut = borrow_mut_resource<MarketInfo>(address_of(&module_signer<MarketInfo>()));
market_info_mut.total_withdraw = market_info_mut.total_withdraw + amount
rgas_market.market_info.total_withdraw = rgas_market.market_info.total_withdraw
+ amount
}

public entry fun consume_event(
rgas_market_obj: &mut Object<RGasMarket>,
subscriber_obj: &mut Object<Subscriber<ReceiveUTXOEvent>>
){
) {
let rgas_market = object::borrow_mut(rgas_market_obj);
assert!(rgas_market.is_open, ErrorMarketNotOpen);
let consume_event = option::extract(&mut consume(subscriber_obj));
let (txid, sender, receiver, value) = unpack_receive_utxo_event(consume_event);
assert!(receiver == rgas_market.receive_btc_address, ErrorReceiverAddress);
let withdraw_amount = btc_to_rgas(value);
if (option::is_some(&sender)){
if (option::is_some(&sender)) {
let sender_addr = option::extract(&mut sender);
let rgas_coin = coin_store::withdraw<RGas>(&mut rgas_market.rgas_store, withdraw_amount);
let rgas_coin =
coin_store::withdraw<RGas>(&mut rgas_market.rgas_store, withdraw_amount);
account_coin_store::deposit(sender_addr, rgas_coin);
let market_info_mut = borrow_mut_resource<MarketInfo>(address_of(&module_signer<MarketInfo>()));
if (!table::contains(&market_info_mut.buyer, sender_addr)){
table::add(&mut market_info_mut.buyer, sender_addr, withdraw_amount)
}else {
let total_amount = *table::borrow(&market_info_mut.buyer, sender_addr) + withdraw_amount;
table::upsert(&mut market_info_mut.buyer, sender_addr, total_amount);
if (!table::contains(&rgas_market.market_info.buyer, sender_addr)) {
table::add(
&mut rgas_market.market_info.buyer, sender_addr, withdraw_amount
)
} else {
let total_amount =
*table::borrow(&rgas_market.market_info.buyer, sender_addr)
+ withdraw_amount;
table::upsert(
&mut rgas_market.market_info.buyer, sender_addr, total_amount
);
}
}else {
let market_info_mut = borrow_mut_resource<MarketInfo>(address_of(&module_signer<MarketInfo>()));
if (!table::contains(&market_info_mut.uncheck_info, txid)){
table::add(&mut market_info_mut.uncheck_info, txid, withdraw_amount)
}else {
let total_amount = *table::borrow(&market_info_mut.uncheck_info, txid) + withdraw_amount;
table::upsert(&mut market_info_mut.uncheck_info, txid, total_amount);
} else {
if (!table::contains(&rgas_market.market_info.uncheck_info, txid)) {
table::add(
&mut rgas_market.market_info.uncheck_info, txid, withdraw_amount
)
} else {
let total_amount =
*table::borrow(&rgas_market.market_info.uncheck_info, txid)
+ withdraw_amount;
table::upsert(
&mut rgas_market.market_info.uncheck_info, txid, total_amount
);
}
}
}
Expand All @@ -134,34 +143,41 @@ module gas_market::gas_market {
txid: address,
sender_addr: address,
amount: u256
){
) {
let rgas_market = object::borrow_mut(rgas_market_obj);
assert!(rgas_market.is_open, ErrorMarketNotOpen);
let rgas_coin = coin_store::withdraw<RGas>(&mut rgas_market.rgas_store, amount);
account_coin_store::deposit(sender_addr, rgas_coin);
let market_info_mut = borrow_mut_resource<MarketInfo>(address_of(&module_signer<MarketInfo>()));
assert!(table::contains(&market_info_mut.uncheck_info, txid), ErrorNoUncheckTxid);
let remaining_amount = *table::borrow(&market_info_mut.uncheck_info, txid) - amount;
assert!(
table::contains(&rgas_market.market_info.uncheck_info, txid),
ErrorNoUncheckTxid
);
let remaining_amount =
*table::borrow(&rgas_market.market_info.uncheck_info, txid) - amount;
if (remaining_amount > 0) {
table::upsert(&mut market_info_mut.uncheck_info, txid, remaining_amount);
}else {
let _ = table::remove(&mut market_info_mut.uncheck_info, txid);
table::upsert(
&mut rgas_market.market_info.uncheck_info, txid, remaining_amount
);
} else {
let _ = table::remove(&mut rgas_market.market_info.uncheck_info, txid);
};

if (!table::contains(&market_info_mut.buyer, sender_addr)){
table::add(&mut market_info_mut.buyer, sender_addr, amount)
}else {
let total_amount = *table::borrow(&market_info_mut.buyer, sender_addr) + amount;
table::upsert(&mut market_info_mut.buyer, sender_addr, total_amount);
if (!table::contains(&rgas_market.market_info.buyer, sender_addr)) {
table::add(&mut rgas_market.market_info.buyer, sender_addr, amount)
} else {
let total_amount =
*table::borrow(&rgas_market.market_info.buyer, sender_addr) + amount;
table::upsert(&mut rgas_market.market_info.buyer, sender_addr, total_amount);
}
}

public entry fun remove_uncheck(
_admin: &mut Object<AdminCap>,
txid: address,
){
let market_info_mut = borrow_mut_resource<MarketInfo>(address_of(&module_signer<MarketInfo>()));
table::remove(&mut market_info_mut.uncheck_info, txid);
rgas_market_obj: &mut Object<RGasMarket>,
txid: address
) {
let rgas_market = object::borrow_mut(rgas_market_obj);
table::remove(&mut rgas_market.market_info.uncheck_info, txid);
}

public fun btc_to_rgas(sats_amount: u64): u256 {
Expand All @@ -178,7 +194,7 @@ module gas_market::gas_market {
}

#[test]
fun test_btc_to_rgas(){
fun test_btc_to_rgas() {
let price_info = decimal_value::new(5005206000000, 8);
let token_price = value(&price_info);
let a = 100000000 * token_price / DEFAULT_UNIT_PRICE;
Expand All @@ -190,9 +206,7 @@ module gas_market::gas_market {
let rgas_amount = 500520600000000;
let price_info = decimal_value::new(5005206000000, 8);
let token_price = value(&price_info);
let b = rgas_amount * DEFAULT_UNIT_PRICE / token_price;
let b = rgas_amount * DEFAULT_UNIT_PRICE / token_price;
assert!(b == 100000000, 2);
}


}
Loading

0 comments on commit 93874a0

Please sign in to comment.