Skip to content

Commit a240f44

Browse files
committed
transfer the fees charged for ibc trx to the relay account
1 parent ccdf0de commit a240f44

File tree

2 files changed

+70
-16
lines changed

2 files changed

+70
-16
lines changed

ibc.token/include/ibc.token/ibc.token.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,11 @@ namespace eosio {
161161
void setpegbool( symbol_code symcode, string which, bool value );
162162

163163
[[eosio::action]]
164-
void setpegtkfee( symbol_code symcode, asset fee );
164+
void setpegtkfee( symbol_code symcode,
165+
name kind,
166+
name fee_mode,
167+
asset fee_fixed,
168+
double fee_ratio );
165169

166170
[[eosio::action]]
167171
void unregtoken( name table, symbol_code sym_code );

ibc.token/src/ibc.token.cpp

Lines changed: 65 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -431,18 +431,36 @@ namespace eosio {
431431
eosio_assert( false, "unkown config item" );
432432
}
433433

434-
void token::setpegtkfee( symbol_code symcode, asset fee) {
434+
void token::setpegtkfee( symbol_code symcode,
435+
name kind,
436+
name fee_mode,
437+
asset fee_fixed,
438+
double fee_ratio ) {
435439
check_admin_auth();
436440

437441
const auto& st = get_currency_stats( symcode );
438-
eosio_assert( fee.symbol == st.supply.symbol && fee.amount >= 0, "service_fee_fixed invalid" );
439-
eosio_assert( fee.amount * 10 <= st.min_once_withdraw.amount, "failed_fee.amount * 10 <= min_once_withdraw.amount assert failed");
442+
eosio_assert( fee_mode == "fixed"_n || fee_mode == "ratio"_n, "mode can only be fixed or ratio");
443+
eosio_assert( fee_fixed.symbol == st.supply.symbol && fee_fixed.amount >= 0, "service_fee_fixed invalid" );
444+
eosio_assert( 0 <= fee_ratio && fee_ratio <= 0.05 , "service_fee_ratio invalid");
440445

441-
_stats.modify( st, same_payer, [&]( auto& r ) {
442-
r.failed_fee = fee;
443-
});
446+
if ( kind == "success"_n ){
447+
eosio_assert( fee_fixed.amount * 5 <= st.min_once_withdraw.amount, "service_fee_fixed.amount * 5 <= min_once_withdraw.amount assert failed");
448+
_stats.modify( st, same_payer, [&]( auto& r ) {
449+
r.service_fee_mode = fee_mode;
450+
r.service_fee_fixed = fee_fixed;
451+
r.service_fee_ratio = fee_ratio;
452+
});
453+
} else if ( kind == "failed"_n ){
454+
eosio_assert( fee_fixed.amount * 10 <= st.min_once_withdraw.amount, "failed_fee.amount * 10 <= min_once_withdraw.amount assert failed");
455+
_stats.modify( st, same_payer, [&]( auto& r ) {
456+
r.failed_fee = fee_fixed;
457+
});
458+
} else {
459+
eosio_assert( false, "kind must be \"success\" or \"failed\"");
460+
}
444461
}
445462

463+
446464
void token::unregtoken( name table, symbol_code sym_code ){
447465
check_admin_auth();
448466

@@ -923,9 +941,32 @@ namespace eosio {
923941
});
924942

925943
add_balance( _self, new_quantity, _self );
944+
945+
int64_t diff = 0;
946+
if ( to != pch.thischain_free_account && (! from_free_account) ){
947+
if ( st.service_fee_mode == "fixed"_n ){
948+
diff = st.service_fee_fixed.amount;
949+
} else {
950+
diff = int64_t( new_quantity.amount * st.service_fee_ratio );
951+
}
952+
}
953+
954+
eosio_assert( diff >= 0, "internal error, service_fee_ratio config error.");
955+
956+
auto final_quantity = asset( 0, st.supply.symbol );
957+
auto fee_quantity = asset( 0, st.supply.symbol );
958+
959+
final_quantity.amount = new_quantity.amount > diff ? new_quantity.amount - diff : 1; // 1 is used to avoid withdraw failure
960+
fee_quantity.amount = new_quantity.amount - final_quantity.amount;
961+
962+
if ( relay != _self ){
963+
transfer_action_type action_data{ _self, relay, fee_quantity, "send ibc trx fee to relay account." };
964+
action( permission_level{ _self, "active"_n }, _self, "transfer"_n, action_data ).send();
965+
}
966+
926967
if( to != _self ) { /// @tag 1: important 'to != _self' logic, avoid inline invoke action 'transfer_notify' or 'withdraw'
927968
if ( memo_info.notes.size() > 250 ) memo_info.notes.resize( 250 );
928-
transfer_action_type action_data{ _self, to, new_quantity, memo_info.notes };
969+
transfer_action_type action_data{ _self, to, final_quantity, memo_info.notes };
929970
action( permission_level{ _self, "active"_n }, _self, "transfer"_n, action_data ).send();
930971
}
931972

@@ -951,6 +992,12 @@ namespace eosio {
951992
chain.balance -= new_quantity;
952993
});
953994

995+
_accepts.modify( acpt, same_payer, [&]( auto& r ) {
996+
r.accept -= new_quantity;
997+
r.total_cash += new_quantity;
998+
r.total_cash_times += 1;
999+
});
1000+
9541001
int64_t diff = 0;
9551002
if ( to != pch.thischain_free_account && (! from_free_account) ){
9561003
if ( acpt.service_fee_mode == "fixed"_n ){
@@ -961,14 +1008,17 @@ namespace eosio {
9611008
}
9621009

9631010
eosio_assert( diff >= 0, "internal error, service_fee_ratio config error");
964-
new_quantity.amount = new_quantity.amount > diff ? new_quantity.amount - diff : 1; // 1 is used to avoid withdraw failure
965-
eosio_assert( new_quantity.amount > 0, "must issue positive quantity" );
9661011

967-
_accepts.modify( acpt, same_payer, [&]( auto& r ) {
968-
r.accept -= new_quantity;
969-
r.total_cash += new_quantity;
970-
r.total_cash_times += 1;
971-
});
1012+
auto final_quantity = asset( 0, acpt.accept.symbol );
1013+
auto fee_quantity = asset( 0, acpt.accept.symbol );
1014+
1015+
final_quantity.amount = new_quantity.amount > diff ? new_quantity.amount - diff : 1; // 1 is used to avoid withdraw failure
1016+
fee_quantity.amount = new_quantity.amount - final_quantity.amount;
1017+
1018+
if ( relay != _self ){
1019+
transfer_action_type action_data{ _self, relay, fee_quantity, "send ibc trx fee to relay account" };
1020+
action( permission_level{ _self, "active"_n }, acpt.original_contract, "transfer"_n, action_data ).send();
1021+
}
9721022

9731023
if( to != _self ) { /// @tag 1: important 'to != _self' logic, avoid inline invoke action 'transfer_notify' or 'withdraw'
9741024
bool jump = false;
@@ -978,7 +1028,7 @@ namespace eosio {
9781028

9791029
if ( ! jump ){
9801030
if ( memo_info.notes.size() > 250 ) memo_info.notes.resize( 250 );
981-
transfer_action_type action_data{ _self, to, new_quantity, memo_info.notes };
1031+
transfer_action_type action_data{ _self, to, final_quantity, memo_info.notes };
9821032
action( permission_level{ _self, "active"_n }, acpt.original_contract, "transfer"_n, action_data ).send();
9831033
}
9841034
}

0 commit comments

Comments
 (0)