From bce40b3a584a0fabcb53fcfe4c260dc373bef913 Mon Sep 17 00:00:00 2001 From: ALPAC-4 <81249838+ALPAC-4@users.noreply.github.com> Date: Mon, 23 Dec 2024 13:17:34 +0900 Subject: [PATCH] fix: deprecate `finalize_arb` and make new version of it (#172) * fix: deprecate `finalize_arb` and make new version of it * fix: fix balance condition * chore: make precompile * fix: remove `generate_finalize_token_withdrawal_msg` function * chore: make precompile --- precompile/binaries/stdlib/minitswap.mv | Bin 21531 -> 21291 bytes .../initia_stdlib/sources/minitswap.move | 126 +++++------------- 2 files changed, 31 insertions(+), 95 deletions(-) diff --git a/precompile/binaries/stdlib/minitswap.mv b/precompile/binaries/stdlib/minitswap.mv index a835ec55040fab7ac7986a3280db512901fdea4a..3fc4a17ddfcc8fea4de96855f2adf8c7ceb62c64 100644 GIT binary patch delta 3964 zcmZu!X>e4>5uToR?wi@SJ5MXEgtTZEt*+Gp61I5~7CJ!U5FpOjU8shs&3s`C`#unMH`hfy|+E{tntzdN`zCV{e1;Ie#*0ok;vLWq&d!Y3^?x zqtOGcr)}r#!iy$i3b$O z7~L(9=>Sa(#}x52 z5+@Y#3_S&>74a-R3u?YZ6KYB-tHE>BPAVflPn%F`N?7OnVtun^((XkuaNf&wNZ`uSMY4Wbd{DW?APe?@PZ;H=t}{}>r_c2Z_t+&oAD;S z60pBThp(#jx9KRnrig3wFia@o9XehGuPf?ZD&Y-9yho3~n~Hd!z7?GQfL;scpU{43 zm|fdePg!001Df1iIi<3)etZ33{f@QX+C*skx}EbAp~29OwRT=2Btl1^x;3vgbQJ3G z?+DZvH{>?v&dzNpXe^jrFa~Jn+mcU0@**T(gqa1&SD-q7Mt-$D!>;ybc*%EQbwTp| ziI2>2O^!L`32JhU8cYLQw@sU5Db2K_8aMgGTTYrz{GNa8GPY;1d$_xQpu1;4ep@)Z zBeJP~uzR3?Z{LRQT|FCy>#d&df$qWHzS!oz!MPNvQ>zd|0Fm4Zrn zc~-^9Z2|bUdW%#MP8}M};${2@iqfj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veGrKPIS*c#*cNkq zl&L%$5OD~&;To73RF+dVFz{iijP@+da)~$qdLveoIfQpPMcg9!)4a;e)wtYX;OHI= z7BL#1ie`UEHqD<>F@pT@KDh{!(>e(+kO~fHa&_Ay1m5=QQmK?@=1+CnfX2e$cwT-n z|D1_gFD?$ty$fzJOMHBV9G2%6%#7h^mN*P7i~Bso=#`>vBHs8)%q8hBOu0Rx3j<@4 zFE)p_!l{MRatFBQP{%9QKLb@Kcm}RO$P9%1%fgQOv>?FCG>-R2#BrUN7~k_T;HXHb zZP_gQ=Uays?I=WP?7z}@o|DIDg5`xz4VNK|blR%Li_n6Z1HDMB!( z3)NF^GfU*UmgUJkZFx{CPqo!%9|538LtDKBj1gO;{i;a*u`MUQfa1?HJo2mXAt=*G zYw;%c7;u(;`8W}AP!25~fyMIs#W~(Vj71C)%TX!GK?5I?rAziLc^Hk07+|6ORC?Zn zGcJ8(%s%QdjHRlLc>ieVKCE_&+|EAmthN^Y(TB0l0vQ1poj5 diff --git a/precompile/modules/initia_stdlib/sources/minitswap.move b/precompile/modules/initia_stdlib/sources/minitswap.move index 2ed18e7f..a780fdf8 100644 --- a/precompile/modules/initia_stdlib/sources/minitswap.move +++ b/precompile/modules/initia_stdlib/sources/minitswap.move @@ -42,6 +42,7 @@ module initia_std::minitswap { const EINVAILD_METADATA: u64 = 16; const ESMALL_ARB_PROFIT: u64 = 17; const EVIRTUAL_POOL_EXISTS: u64 = 18; + const EDEPRECATED: u64 = 19; const A_PRECISION: u256 = 100; const U64_MAX: u128 = 18_446_744_073_709_551_615; @@ -1451,17 +1452,11 @@ module initia_std::minitswap { primary_fungible_store::deposit(signer::address_of(account), return_asset); } - public entry fun finalize_arb( - account: &signer, - arb_index: u64, - output_index: u64, - withdrawal_proofs: vector, - sender: address, - sequence: u64, - version: String, - state_root: String, - storage_root: String, - latest_block_hash: String + // Since MsgFinalizeWithdrawal can now be executed by anyone, + // Make executor triggers MsgFinalizeWithdrawal by themselves + // and then calls `finalize_arb_v2`. + public entry fun finalize_arb_v2( + account: &signer, arb_index: u64 ) acquires ModuleStore, VirtualPool { // check arb info let module_store = borrow_global(@initia_std); @@ -1470,46 +1465,41 @@ module initia_std::minitswap { &module_store.global_arb_batch_map, table_key::encode_u64(arb_index) ); - let pool = borrow_global(object::object_address(&*pool_obj)); + let pool_addr = object::object_address(&*pool_obj); + let pool = borrow_global(pool_addr); let arb_info = table::borrow( &pool.arb_batch_map, table_key::encode_u64(arb_index) ); - // execute finalize token withdrawal - let pool_signer = object::generate_signer_for_extending(&pool.extend_ref); - let withdrawal_msg = - generate_finalize_token_withdrawal_msg( - pool.op_bridge_id, - output_index, - withdrawal_proofs, - sender, - signer::address_of(&pool_signer), - sequence, - string::utf8(b"uinit"), - arb_info.ibc_op_init_sent, - version, - state_root, - storage_root, - latest_block_hash - ); - cosmos::stargate(&pool_signer, withdrawal_msg); + let uinit_pool_balance = coin::balance(pool_addr, init_metadata()); + assert!( + uinit_pool_balance >= arb_info.ibc_op_init_sent, + error::invalid_state(ENOT_ENOUGH_BALANCE) + ); // execute hook let module_signer = object::generate_signer_for_extending(&module_store.extend_ref); - cosmos::move_execute( - &module_signer, - @initia_std, - string::utf8(b"minitswap"), - string::utf8(b"finalize_arb_hook"), - vector[], - vector[ - bcs::to_bytes(&arb_index), - bcs::to_bytes(&signer::address_of(account)) - ] - ); + + finalize_arb_hook(&module_signer, arb_index, signer::address_of(account)); + } + + // deprecated + public entry fun finalize_arb( + _account: &signer, + _arb_index: u64, + _output_index: u64, + _withdrawal_proofs: vector, + _sender: address, + _sequence: u64, + _version: String, + _state_root: String, + _storage_root: String, + _latest_block_hash: String + ) { + abort(error::unavailable(EDEPRECATED)); } public entry fun finalize_arb_hook( @@ -2533,38 +2523,6 @@ module initia_std::minitswap { amount: u64 } - fun generate_finalize_token_withdrawal_msg( - bridge_id: u64, - output_index: u64, - withdrawal_proofs: vector, - sender: address, - receiver: address, - sequence: u64, - denom: String, - amount: u64, - version: String, - state_root: String, - storage_root: String, - latest_block_hash: String - ): vector { - json::marshal( - &FinalizeTokenWithdrawalRequest { - _type_: string::utf8(b"/opinit.ophost.v1.MsgFinalizeTokenWithdrawal"), - bridge_id, - output_index, - withdrawal_proofs, - sender: to_sdk(sender), - receiver: to_sdk(receiver), - sequence, - amount: CosmosCoin { denom, amount }, - version, - state_root, - storage_root, - latest_block_hash - } - ) - } - fun init_metadata(): Object { let addr = object::create_object_address(&@initia_std, b"uinit"); object::address_to_object(addr) @@ -3143,28 +3101,6 @@ module initia_std::minitswap { ); } - #[test] - fun test_finalize_token_withdrawal_msg() { - let msg = - generate_finalize_token_withdrawal_msg( - 1, - 2, - vector[string::utf8(b"abc"), string::utf8(b"123")], - @0x1, - @0x2, - 3, - string::utf8(b"uinit"), - 100, - string::utf8(b"version"), - string::utf8(b"state_root"), - string::utf8(b"storage_root"), - string::utf8(b"latest_block_hash") - ); - let json_str = - b"{\"@type\":\"/opinit.ophost.v1.MsgFinalizeTokenWithdrawal\",\"amount\":{\"amount\":\"100\",\"denom\":\"uinit\"},\"bridge_id\":\"1\",\"latest_block_hash\":\"latest_block_hash\",\"output_index\":\"2\",\"receiver\":\"init1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqzwsp0lj\",\"sender\":\"init1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqpqr5e3d\",\"sequence\":\"3\",\"state_root\":\"state_root\",\"storage_root\":\"storage_root\",\"version\":\"version\",\"withdrawal_proofs\":[\"abc\",\"123\"]}"; - assert!(msg == json_str, 0); - } - #[test] fun test_ibc_denom() { let ibc_denom =