Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(nft-swap): complete refund methods #2129

Merged
merged 121 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from 90 commits
Commits
Show all changes
121 commits
Select commit Hold shift + click to select a range
394fe4d
use NFT_MAKER_SWAP_V2 in nft_swap_v2 mod and in spend erc721 test
laruh Apr 18, 2024
44f542d
deploy contracts before tokens, add constructor to EtomicSwapMakerNft…
laruh Apr 18, 2024
ff1006a
use Sepolia in dockerized nft tests
laruh Apr 22, 2024
aebf756
global variables and send nft back to maker
laruh Apr 23, 2024
1966fcf
add web3 HTTPS support in mm2_main tests
laruh Apr 23, 2024
1922bff
wait for send_back_tx confirmation
laruh Apr 23, 2024
c07e8e2
call contract addresses with functions
laruh Apr 23, 2024
ebac6e6
impl check_token_uri and make chains field in ClearNftDbReq optional …
laruh Apr 4, 2024
f9f4eab
doc comms
laruh Apr 24, 2024
ad0ce31
Merge remote-tracking branch 'origin/dev' into standalone-nft-maker-s…
laruh Apr 25, 2024
4a4dfc0
Merge remote-tracking branch 'refs/remotes/origin/dev' into standalon…
laruh Apr 26, 2024
c703913
fix merge conflicts
laruh Apr 26, 2024
fe498fa
impl wait_pending_transactions
laruh Apr 26, 2024
18fa53e
increase max nonce by 1
laruh Apr 26, 2024
b9a6719
Revert "increase max nonce by 1"
laruh Apr 26, 2024
9a23f20
Merge remote-tracking branch 'origin/dev' into standalone-nft-maker-s…
laruh Apr 29, 2024
998708b
impl get_or_create_nft function
laruh Apr 29, 2024
364d795
additional logs, ignore erc1155
laruh Apr 29, 2024
cb187c7
deploy new SEPOLIA_ERC721_CONTRACT
laruh Apr 29, 2024
9d4e3ee
fix linter
laruh Apr 29, 2024
495e7a9
run both nft send payment tests
laruh Apr 29, 2024
e917b0b
proxy: send nft reqs with payload data in body to satisfy proxy HttpG…
laruh May 6, 2024
f868a50
Merge remote-tracking branch 'refs/remotes/origin/dev' into standalon…
laruh May 8, 2024
e94fac7
provide UrlSignWrapper structure
laruh May 9, 2024
2e8c662
additionally include chain field into UrlSignWrapper
laruh May 9, 2024
0fbf75d
increase erc721 tx confirmation time to 150, add more logs
laruh May 9, 2024
03860e1
review: use map_err for generate_gui_auth_signed_validation, doc comm…
laruh May 10, 2024
b8b05d1
proxy: send regular http post request to proxy for nft feature
laruh May 12, 2024
3de2b92
make erc721 and erc1155 time locks different in tests
laruh May 12, 2024
f2625fd
Merge remote-tracking branch 'refs/remotes/origin/dev' into standalon…
laruh May 13, 2024
930a593
Merge remote-tracking branch 'refs/remotes/origin/dev' into standalon…
laruh May 14, 2024
350abd6
review: impl lock_coins() to keep coins field in CoinsContext private
laruh May 14, 2024
36eaf38
review: rename Gui-Auth types to align with new names in the KomodoDe…
laruh May 15, 2024
4478ccb
review: use "http-rustls-tls" feature in web3 crate for tests
laruh May 15, 2024
7882e6e
review: send Http GET request to activate NFT
laruh May 16, 2024
d03b6d1
review: support GET requests to KomodoDeFi Proxy layer and to 3rd API…
laruh May 17, 2024
ff92a20
impl build_and_send_request func to avoid code repetition
laruh May 17, 2024
a2c47f8
review: panic in wait_for_geth_node_ready if Deth node is not ready a…
laruh May 17, 2024
d6cbfd8
Merge remote-tracking branch 'refs/remotes/origin/dev' into standalon…
laruh May 21, 2024
9c3ff07
merge: fmt, log tx_hash() in NFT payments instead of bytes, import et…
laruh May 21, 2024
0bedba3
send only signed message as header in nft
laruh May 23, 2024
7976761
Merge remote-tracking branch 'refs/remotes/origin/dev' into standalon…
laruh May 24, 2024
a6ad6fc
wip: refund_nft_maker_payment_v2
laruh May 26, 2024
5daf723
wip: refund_nft_maker_payment_v2_secret
laruh May 27, 2024
2daec7a
test send_send_and_refund_erc721_maker_payment_timelock ignoring ERC1155
laruh May 27, 2024
6992931
log info in send_send_and_refund_erc721_maker_payment_timelock
laruh May 27, 2024
3f07f1f
log token_id=2 send_send_and_refund_erc721_maker_payment_timelock
laruh May 27, 2024
0079362
test only refund_nft_maker_payment_v2_timelock
laruh May 28, 2024
0c164fb
test only refund_nft_maker_payment_v2_timelock
laruh May 28, 2024
63af2bc
use global_nft_with_random_privkey in send_send_and_refund_erc721_mak…
laruh May 28, 2024
e2f8ccf
call maker_global_nft.refund_nft_maker_payment_v2_timelock
laruh May 28, 2024
dad6988
use get_erc712_owner func in refund erc721 test
laruh May 28, 2024
9b1a700
add geth_ prefix to Address functions in eth tests
laruh May 28, 2024
fc4d6e4
impl [test] send_send_and_refund_erc1155_maker_payment_timelock
laruh May 28, 2024
cc8e78e
test the refund payment with taker secret
laruh May 29, 2024
87b297a
use Geth node for swap tests
laruh May 29, 2024
780b1e8
reduce code duplication
laruh May 30, 2024
f789e91
provide NftSwapInfo type
laruh May 30, 2024
aa1d70f
use coins config file for NFT activation
laruh May 31, 2024
b9f2a98
reduce code duplication using `validate_state` func
laruh Jun 2, 2024
ece5152
refactor Quicknode HTTP request: move `signed_message` to header
laruh Jun 4, 2024
920861e
tests: increase wait for confirm time and add sleep duration
laruh Jun 5, 2024
e478711
eth tpu v2: deploy maker and taker swap v2 contracts in tests. add ma…
laruh Jun 7, 2024
9803fd5
eth tpu v2: dont include "nft_maker_swap_v2_contract" in eth/erc20 ac…
laruh Jun 8, 2024
2828be0
eth tpu v2: provide init_watchers_swap_contract in swap_watcher_tests.rs
laruh Jun 8, 2024
04d99e4
eth tpu v2: impl TakerCoinSwapOpsV2 for EthCoin, send_taker_funding WIP
laruh Jun 10, 2024
d31566d
eth_docker_tests.rs change sleep Duration in some tests
laruh Jun 10, 2024
4064999
eth tpu v2: impl TakerCoinSwapOpsV2 for EthCoin WIP
laruh Jun 14, 2024
b986279
Merge remote-tracking branch 'refs/remotes/origin/dev' into nft-refun…
laruh Jun 17, 2024
e636da1
avoid code duplication in input_tokens vector
laruh Jun 17, 2024
40ffb65
review: rename validate_state fnc, use extend plus chain in etomic_sw…
laruh Jun 17, 2024
888108c
review: move contract bytes strings to txt files, remove "the"
laruh Jun 18, 2024
8143cc2
review: move code to EthCoinType::Nft arm
laruh Jun 20, 2024
b391a16
state machine: fix log info text related to sign_and_send_taker_fundi…
laruh Jun 22, 2024
5f34f1c
Merge remote-tracking branch 'refs/remotes/origin/dev' into standalon…
laruh Jun 22, 2024
e672485
review: provide log!, instead println! in tests
laruh Jun 22, 2024
986c5e7
Merge remote-tracking branch 'refs/remotes/origin/standalone-nft-make…
laruh Jun 22, 2024
d998062
review: swap part of ERC1155 nfts in eth_docker_tests.rs
laruh Jun 23, 2024
ff4d306
eth tpu v2: leave comms about preimages
laruh Jun 26, 2024
2ae0b04
eth tpu v2: use Public from ethereum_types in impl ParseCoinAssocType…
laruh Jun 28, 2024
0d54567
Merge remote-tracking branch 'refs/remotes/origin/dev' into standalon…
laruh Jun 28, 2024
602206d
Merge remote-tracking branch 'refs/remotes/origin/standalone-nft-make…
laruh Jun 28, 2024
604d182
change sleep duration in send_and_spend_erc721_maker_payment
laruh Jun 28, 2024
36d94b9
change sleep duration in send_and_refund_erc721_maker_payment_timeloc…
laruh Jun 28, 2024
ce6673b
change sleep duration in send_and_refund_erc721_maker_payment_timelock
laruh Jun 28, 2024
933d4b9
change sleep duration in send_and_refund_erc1155_maker_payment_timelock
laruh Jun 28, 2024
1e90600
eth tpu v2: add swap_v2_contracts Opt param in EthActivationV2Request…
laruh Jul 1, 2024
b9049bf
state machine: change doc comment related to sign_and_send_taker_fund…
laruh Jul 3, 2024
8e4cd71
eth tpu v2: send_taker_funding Erc20 WIP (need to optimize code)
laruh Jul 3, 2024
4e9f902
change sleep duration in send_and_refund_erc721_maker_payment_timelock
laruh Jul 3, 2024
801a688
Merge remote-tracking branch 'refs/remotes/origin/dev' into standalon…
laruh Jul 8, 2024
1e050d2
fmt code after merge
laruh Jul 8, 2024
2447763
Merge remote-tracking branch 'refs/remotes/origin/standalone-nft-make…
laruh Jul 8, 2024
fa3f553
fix merge conflicts
laruh Jul 8, 2024
eddeb1a
make "proxy_auth" Optional
laruh Jul 10, 2024
7cbb941
Merge remote-tracking branch 'refs/remotes/origin/dev' into standalon…
laruh Jul 10, 2024
87cc404
Merge remote-tracking branch 'refs/remotes/origin/standalone-nft-make…
laruh Jul 10, 2024
191b5f0
Merge remote-tracking branch 'origin/dev' into standalone-nft-maker-s…
laruh Jul 18, 2024
734edbc
Merge remote-tracking branch 'origin/standalone-nft-maker-swap-contra…
laruh Jul 18, 2024
7b69886
fmt and clippy
laruh Jul 18, 2024
b13ab34
Merge remote-tracking branch 'origin/dev' into nft-refund-payment-geth
laruh Jul 19, 2024
dab5bd9
fmt, clippy
laruh Jul 19, 2024
cac2d0d
review: remove gas_limit module from eth_swap_v2.rs
laruh Jul 21, 2024
071a1f6
review: use `etomic_swap_id` function
laruh Jul 21, 2024
439243a
review: use fixed size arrays
laruh Jul 22, 2024
c629d7a
review: provide eth total payment to sign_and_send_transaction
laruh Jul 25, 2024
ed08df9
review: fix typo
laruh Jul 25, 2024
89e543f
review: update log to "Sent taker funding spend (taker payment)"
laruh Jul 25, 2024
28bc18a
review: remove legacy "derive_htlc_pubkey" function from swap_v2, use…
laruh Jul 25, 2024
69a07a8
Merge remote-tracking branch 'origin/dev' into nft-refund-payment-geth
laruh Jul 26, 2024
7570df7
gas limit: TODO about new consts and params for v2 calls
laruh Jul 26, 2024
3042e60
todo burnFee support
laruh Jul 26, 2024
e1e2242
review: update doc comments for taker and maker secret hashes in Send…
laruh Jul 26, 2024
fcd394d
review: specify timelock doc comments for UTXO and smart contracts in…
laruh Jul 26, 2024
6a231a3
review: remove wait_for_confirmation_until and use funding_time_lock …
laruh Jul 26, 2024
c58808a
review: use Public::from_slice directly in parse_pubkey
laruh Jul 29, 2024
1a5bf73
review: use public_to_address to get address from pubkey
laruh Jul 29, 2024
5d6f943
review: use dex_fee plus payment_amount for eth_total_payment
laruh Jul 29, 2024
2007a70
review: extract sign_and_send_transaction outside the if/else
laruh Jul 29, 2024
9aca0d7
review: minify json
laruh Jul 29, 2024
fe730fa
review: drop variables, return result value directly
laruh Aug 2, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
240 changes: 203 additions & 37 deletions mm2src/coins/eth.rs

Large diffs are not rendered by default.

159 changes: 159 additions & 0 deletions mm2src/coins/eth/eth_swap_v2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
use super::eth::{addr_from_raw_pubkey, gas_limit, wei_from_big_decimal, EthCoin, EthCoinType, SignedEthTx,
TAKER_SWAP_V2};
use super::{SendTakerFundingArgs, Transaction, TransactionErr};
use enum_derives::EnumFromStringify;
use ethabi::Token;
use ethcore_transaction::Action;
use ethereum_types::{Address, U256};
use futures::compat::Future01CompatExt;
use std::convert::TryInto;

struct TakerFundingArgs<'a> {
dex_fee: U256,
payment_amount: U256,
maker_address: Address,
taker_secret_hash: &'a [u8],
maker_secret_hash: &'a [u8],
shamardy marked this conversation as resolved.
Show resolved Hide resolved
funding_time_lock: u32,
payment_time_lock: u32,
}

impl EthCoin {
pub(crate) async fn send_taker_funding_impl(
&self,
args: SendTakerFundingArgs<'_>,
) -> Result<SignedEthTx, TransactionErr> {
let taker_swap_v2_contract = self
.swap_v2_contracts
.as_ref()
.map(|contracts| contracts.taker_swap_v2_contract)
.ok_or_else(|| TransactionErr::Plain(ERRL!("Expected swap_v2_contracts to be Some, but found None")))?;
let dex_fee = try_tx_s!(wei_from_big_decimal(&args.dex_fee.fee_amount().into(), self.decimals));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this use dex_fee.total_spend_amount()? Splitting the payment is done in the contract I think. c.c. @dimxy

Copy link
Member Author

@laruh laruh Jul 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Splitting the payment is done in the contract I think

if new swap contract is r2r then we should review it in etomic swap
UPD: oh may be it is in our inner send tx functionality

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right now we just send dexfee to feeAddress when maker spends payment

Yep, and total_spend_amount will handle this in a right way. When fee splitting is implemented for EVM swaps v2, we won't have to remember changing this part.

Copy link
Member Author

@laruh laruh Jul 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shamardy as current swap v2 contracts doesnt have burn functionality, I suggest to leave dex_fee.fee_amount()
I will add a TODO noting the need for burnFee functionality support (added todo comment 3042e60).

The preferable approach is in wip I suppose. I would suggest to provide burnFee as an additional param in the swap contract function. And burn fee sending it to zero address right after sending dexFee to feeAddress in spend payment function.

Copy link
Member Author

@laruh laruh Jul 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed, burnfee will either stay in contract or sent to an address where these fees will be traded for KMD that will be burned.

Got it. I suggest to send burnFee to burnAddress to not to mix trading payments and burn fees on swap contract address.

As discussed, total_spend_amount should be used for eth_total_payment

Yep, now agree, if burnFee should be managed by EtomicSwapTakerV2 then for eth payment I will include burn fee into eth_total_payment calculation.
But it should be added to eth_total_payment when we impl burnFee managing in EtomicSwapTakerV2, right now it doesnt have any burnFee functionality https://github.com/KomodoPlatform/etomic-swap/blob/5e15641cbf41766cd5b37b4d71842c270773f788/contracts/EtomicSwapTakerV2.sol

Do you want me to update taker swap contract and provide burnFee field with burnFeeAddress till Wednesday?
If you would like to refactor smart contract later I suggest not to use total_spend_amount as EtomicSwapTakerV2 doesnt have burnFee support.
it will lead to the behaviour where we send dex_fee + burn_fee as dexFee to dexFeeAddress (in spendTakerPayment function)

        emit TakerPaymentSpent(id, makerSecret);

        if (tokenAddress == address(0)) {
            payable(msg.sender).transfer(amount);
            payable(dexFeeAddress).transfer(dexFee);
        } else {
            IERC20 token = IERC20(tokenAddress);
            token.safeTransfer(msg.sender, amount);
            token.safeTransfer(dexFeeAddress, dexFee);
        }

Copy link
Member Author

@laruh laruh Jul 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed, total_spend_amount should be used for eth_total_payment

It means that dex_fee will be fee_amount + burn_amount, but we dont support burnFee right now. It will lead to payable(dexFeeAddress).transfer(dexFee); as I answered in the message above

Screenshot 2024-07-29 at 20 55 23

Copy link
Collaborator

@shamardy shamardy Jul 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But it should be added to eth_total_payment when we impl burnFee managing in EtomicSwapTakerV2, right now it doesnt have any burnFee functionality https://github.com/KomodoPlatform/etomic-swap/blob/5e15641cbf41766cd5b37b4d71842c270773f788/contracts/EtomicSwapTakerV2.sol

It's fine by me to add it later as long as we remember to change it. I just suggested to use total_spend_amount now to not forget to change it later and it will be the same as fee_amount now in defi framework since we didn't add burn fee to EVM TPU in defi framework as well.

Do you want me to update taker swap contract and provide burnFee field with burnFeeAddress till Wednesday?

No need to, we will add it at the same time we start work on EVM TPU burnfee on defi framework side.

If you would like to refactor smart contract later I suggest not to use total_spend_amount as EtomicSwapTakerV2 doesnt have burnFee support.
it will lead to the behaviour where we send dex_fee + burn_fee as dexFee to dexFeeAddress (in spendTakerPayment function)

Not really since burn_fee will be 0 until we support it in defi framework. And burn fee is taken from the whole fee anyways, we don't let taker pay more so that we can burn KMD, they pay the same total fee with or without burning. But as I answered above, as long as you/we remember to change it later, you can leave it as it is. But total_spend_amount was added by @onur-ozkan to use it for total fees whether we burn or not to abstract the burning part, so that other devs can use it without looking into burn fees code.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just suggested to use total_spend_amount now to not forget to change it later and it will be the same as fee_amount now in defi framework since we didn't add burn fee to EVM TPU in defi framework as well.

But total_spend_amount was added by @onur-ozkan to use it for total fees whether we burn or not to abstract the burning part, so that other devs can use it without looking into burn fees code.

I suppose it depends on the feature. If the swap contract supports only one fee type, then its important to provide the exact fee value in the rust code. Otherwise, the current rust code may not align with the smart contract logic, even if we provide a burn fee of 0.

Its also better to handle fees separately if we agree to provide burnFee as a separate field in the smart contract functions.

I added todo sub point in the [FR] Implement a Fee Splitting and Burning mechanism to burn KMD and support the Komodo blockchain issue check list #2010 (comment)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose it depends on the feature. If the swap contract supports only one fee type, then its important to provide the exact fee value in the rust code. Otherwise, the current rust code may not align with the smart contract logic, even if we provide a burn fee of 0.

It does align fine since DexFee::WithBurn is only used for KMD currently

dex_fee: dex_fee_amount_from_taker_coin(&t, maker_coin_ticker, &taker_amount),


I am not gonna fight you more on this though :)


let payment_amount = try_tx_s!(wei_from_big_decimal(
&(args.trading_amount + args.premium_amount),
self.decimals
));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we include the dex fee in the total amount to be sent?

P.S. this is how it's done for utxo

let total_amount = &args.dex_fee.total_spend_amount().to_decimal() + &args.premium_amount + &args.trading_amount;

Copy link
Member Author

@laruh laruh Jul 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I see, as in ethTakerPayment we subtract dexfee from msg.value, then we should add dex_fee to total amount, when we send EthCoinType::Eth case.
https://github.com/KomodoPlatform/etomic-swap/blob/5e15641cbf41766cd5b37b4d71842c270773f788/contracts/EtomicSwapTakerV2.sol#L63
In erc20 case we send U256::from(0) so no need to add dexfee tot total payment.

But why my send-refund tests for eth passed then? paymentHash == takerPayments[id].paymentHash check should fail and tx should be reverted by swap contract. Will find out

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I see, as in ethTakerPayment we subtract dexfee from msg.value, then we should add dex_fee to total amount, when we send EthCoinType::Eth case.
In erc20 case we send U256::from(0) so no need to add dexfee tot total payment.

Yeah, only needed for Eth / platform coin case apparently. Also, for taker approval, we send dex fee separate from amount for both eth / platform coin and tokens

    function takerPaymentApprove(
        bytes32 id,
        uint256 amount,
        uint256 dexFee,
        address maker,
        bytes32 takerSecretHash,
        bytes32 makerSecretHash,
        address tokenAddress
    ) external {

But why my send-refund tests for eth passed then?

can you point me to this test so that I can give it a check as well.

Copy link
Member Author

@laruh laruh Jul 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you point me to this test so that I can give it a check as well.

Its from second pr https://github.com/KomodoPlatform/komodo-defi-framework/pull/2169/files#diff-4bfd82ccb22215a7516f1ac3f5d1913588bee7d70ed917a3f3e5b6309b5a234a

such tests were provided
send_and_refund_taker_funding_by_secret_eth
send_and_refund_taker_funding_by_secret_erc20
send_and_refund_taker_funding_timelock_eth
send_and_refund_taker_funding_timelock_erc20

I understood why they were passing, its bcz I didnt add wait_for_confirmations after calling refund methods.

I have Waiting for confirmations… Failed. issues right now using Geth node for eth and erc20
https://github.com/KomodoPlatform/komodo-defi-framework/actions/runs/10057619461/job/27798973002#step:6:1513

Refund test of Erc20 token fails confirmation waiting right on Taker sent ERC20 funding stage. ("approve" method is used)
ETH refund also has confirmation issue, but Taker sent ETH funding tx is confirmed, however next Taker refunded ETH funding txz confirmation failing.

Right now Im working on sepolia deploy to see how swaps will work in this environment

log

failures:

---- docker_tests::eth_docker_tests::send_and_refund_taker_funding_by_secret_erc20 stdout ----
23 11:07:00, eth_docker_tests:1641] taker_address type: 0x7fd41dbfcd45ce2f56360f000a709f3ec863a073
23 11:07:00, eth_docker_tests:1642] coin type: Erc20 { platform: "ETH", token_addr: 0x3e52d447ff3d37f6e734aeda922cf92ec3865b57 }
23 11:07:04, eth_docker_tests:1645] Taker ERC20 balance before payment: 10000000000
23 11:07:08, eth_docker_tests:1647] Taker ETH balance before payment: 100000000000000000000
23 11:07:08, eth_swap_v2:102] Allowed: 0, payment_amount: 1000000000000000000
23 11:07:08, eth_docker_tests:1665] Taker sent ERC20 funding, tx hash 39ffa47522aad82d0cefee97355341ca092c45758fa1d4f5d69b6560d39e816a
· 2024-07-23 11:07:08 +0000 [ERC20DEV] Waiting for confirmations… Failed.
thread 'docker_tests::eth_docker_tests::send_and_refund_taker_funding_by_secret_erc20' panicked at 'called `Result::unwrap()` on an `Err` value: "eth:5287] Internal: eth:5287] Tx receipt Receipt { transaction_hash: 0x39ffa47522aad82d0cefee97355341ca092c45758fa1d4f5d69b6560d39e816a, transaction_index: 0, block_hash: Some(0xc1172e140a1f45456757f45912ad190b88d29de1e42a538831d29cdd11e6e685), block_number: Some(106), from: 0x7fd41dbfcd45ce2f56360f000a709f3ec863a073, to: Some(0xac510a9cbd5c7f4156273c952937f2e959e6febc), cumulative_gas_used: 148753, gas_used: Some(148753), contract_address: None, logs: [], status: Some(0), root: None, logs_bloom: 0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000, transaction_type: Some(0), effective_gas_price: Some(1000001727) } status of ERC20DEV tx 0x39ffa47522aad82d0cefee97355341ca092c45758fa1d4f5d69b6560d39e816a is failed"', mm2src/mm2_main/tests/docker_tests/eth_docker_tests.rs:1416:55
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

---- docker_tests::eth_docker_tests::send_and_refund_taker_funding_by_secret_eth stdout ----
23 11:07:18, eth_docker_tests:1575] Taker ETH balance before payment: 100000000000000000000
23 11:07:18, eth_docker_tests:1592] Taker sent ETH funding, tx hash: 1c7e278b95e17ba586263e28cc6d65056561c9d6c12db3a85c5c48fb3b787251
· 2024-07-23 11:07:18 +0000 [ETH] Waiting for confirmations… Confirmed.
23 11:07:24, eth_docker_tests:1597] Taker ETH balance after payment: 98899946424975301925
23 11:07:24, eth_docker_tests:1614] Taker refunded ETH funding by secret, tx hash: f3cfe97746ff0a53dfbd4d1fa48907dd3e7c167988fc3199640d6512745f8b58
· 2024-07-23 11:07:24 +0000 [ETH] Waiting for confirmations… Failed.
thread 'docker_tests::eth_docker_tests::send_and_refund_taker_funding_by_secret_eth' panicked at 'called `Result::unwrap()` on an `Err` value: "eth:5287] Internal: eth:5287] Tx receipt Receipt { transaction_hash: 0xf3cfe97746ff0a53dfbd4d1fa48907dd3e7c167988fc3199640d6512745f8b58, transaction_index: 0, block_hash: Some(0xd949957f2e97a3d09a2af12515695f39c3fda5f4ee732b33f931388cee833f55), block_number: Some(122), from: 0xe1854a4c47382440ef9618dea9f178cb7cb09e8d, to: Some(0xac510a9cbd5c7f4156273c952937f2e959e6febc), cumulative_gas_used: 27329, gas_used: Some(27329), contract_address: None, logs: [], status: Some(0), root: None, logs_bloom: 0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000, transaction_type: Some(0), effective_gas_price: Some(1000000239) } status of ETH tx 0xf3cfe97746ff0a53dfbd4d1fa48907dd3e7c167988fc3199640d6512745f8b58 is failed"', mm2src/mm2_main/tests/docker_tests/eth_docker_tests.rs:1416:55


failures:
    docker_tests::eth_docker_tests::send_and_refund_taker_funding_by_secret_erc20
    docker_tests::eth_docker_tests::send_and_refund_taker_funding_by_secret_eth

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, we should include dex fee to total payment for eth, will fix it.

@shamardy I understood what was the problem of failing confirmation of eth refund tests. there was a mistake in swap id code line, so tx was reverted by swap contract with Invalid payment state. Must be PaymentSen error https://sepolia.etherscan.io/tx/0x423762b259fbbd5667a0cc38a53a52fef062cc15289edc4a87b4dc353851b3b8
I think I was confused by my previous experience of failing nft tests, when in Geth confirmations were failing, while they were working on Sepolia.

Now I can confirm that eth refund methods works (on sepolia)
https://github.com/KomodoPlatform/komodo-defi-framework/actions/runs/10075604842/job/27854240559?pr=2169#step:6:1398

test docker_tests::eth_docker_tests::send_and_refund_taker_funding_by_secret_eth ... ok
test docker_tests::eth_docker_tests::send_and_refund_taker_funding_timelock_eth ... ok

I will keep working on Sepolia, as it provides the info why tx was reverted.

Copy link
Member Author

@laruh laruh Jul 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we include the dex fee in the total amount to be sent?

Done c629d7a

let maker_address = try_tx_s!(addr_from_raw_pubkey(args.maker_pub));
shamardy marked this conversation as resolved.
Show resolved Hide resolved

let funding_time_lock: u32 = try_tx_s!(args.funding_time_lock.try_into());
let payment_time_lock: u32 = try_tx_s!(args.payment_time_lock.try_into());
let funding_args = TakerFundingArgs {
dex_fee,
payment_amount,
maker_address,
taker_secret_hash: args.taker_secret_hash,
maker_secret_hash: args.maker_secret_hash,
funding_time_lock,
payment_time_lock,
};

match &self.coin_type {
EthCoinType::Eth => {
let data = try_tx_s!(self.prepare_taker_eth_funding_data(&funding_args).await);
self.sign_and_send_transaction(
payment_amount,
Action::Call(taker_swap_v2_contract),
data,
U256::from(gas_limit::ETH_PAYMENT),
Copy link
Collaborator

@shamardy shamardy Jul 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should start using gas limits from the gas_limit parameter, similar to the below.

let gas = U256::from(self.gas_limit.eth_payment);
self.sign_and_send_transaction(value, Action::Call(swap_contract_address), data, gas)

We should also set constants and custom params for v2 swap functions gas limits c.c. @dimxy

Copy link
Member Author

@laruh laruh Jul 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used self.gas_limit in next pr #2169

payment_amount,
Action::Call(taker_swap_v2_contract),
data,
U256::from(self.gas_limit.eth_payment),

I will add gas limit changes into this #2129 pr as well then
UPD: done cac2d0d

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we need to add new gas params for contract v2 calls (as they have different gas consumption)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we need to add new gas params for contract v2 calls (as they have different gas consumption)

Yeah, you should add new consts and params for v2 calls @laruh. You can set the consts same as v1 for now (in this PR) until we know what are good values for them. Please add an item to your checklist about setting correct values for v2 gas_limit consts to remember in next PRs.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added todo comment 7570df7

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Todo comments can be lost (forgotten about), we should also have it in the issue checklist, the issue checklist will be checked before closing the issue in case we forgot something.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it, added todo point here #1895 (comment)

)
.compat()
.await
},
EthCoinType::Erc20 {
platform: _,
token_addr,
} => {
let allowed = self
.allowance(taker_swap_v2_contract)
.compat()
.await
.map_err(|e| TransactionErr::Plain(ERRL!("{}", e)))?;
let data = try_tx_s!(self.prepare_taker_erc20_funding_data(&funding_args, *token_addr).await);
if allowed < payment_amount {
let approved_tx = self.approve(taker_swap_v2_contract, U256::max_value()).compat().await?;
self.wait_for_required_allowance(
taker_swap_v2_contract,
payment_amount,
args.wait_for_confirmation_until,
)
.compat()
.await
.map_err(|e| {
TransactionErr::Plain(ERRL!(
"Allowed value was not updated in time after sending approve transaction {:02x}: {}",
approved_tx.tx_hash_as_bytes(),
e
))
})?;
self.sign_and_send_transaction(
U256::from(0),
Action::Call(taker_swap_v2_contract),
data,
U256::from(gas_limit::ERC20_PAYMENT),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

)
.compat()
.await
} else {
self.sign_and_send_transaction(
U256::from(0),
Action::Call(taker_swap_v2_contract),
data,
U256::from(gas_limit::ERC20_PAYMENT),
)
.compat()
.await
}
},
EthCoinType::Nft { .. } => Err(TransactionErr::ProtocolNotSupported(
"NFT protocol is not supported for ETH and ERC20 Swaps".to_string(),
)),
}
}

async fn prepare_taker_eth_funding_data(&self, args: &TakerFundingArgs<'_>) -> Result<Vec<u8>, PrepareTxDataError> {
let function = TAKER_SWAP_V2.function("ethTakerPayment")?;
let id = self.etomic_swap_v2_id(args.funding_time_lock, args.payment_time_lock, args.taker_secret_hash);
let data = function.encode_input(&[
Token::FixedBytes(id),
Token::Uint(args.dex_fee),
Token::Address(args.maker_address),
Token::FixedBytes(args.taker_secret_hash.to_vec()),
Token::FixedBytes(args.maker_secret_hash.to_vec()),
Token::Uint(args.funding_time_lock.into()),
Token::Uint(args.payment_time_lock.into()),
])?;
Ok(data)
}

async fn prepare_taker_erc20_funding_data(
&self,
args: &TakerFundingArgs<'_>,
token_addr: Address,
) -> Result<Vec<u8>, PrepareTxDataError> {
let function = TAKER_SWAP_V2.function("erc20TakerPayment")?;
let id = self.etomic_swap_v2_id(args.funding_time_lock, args.payment_time_lock, args.taker_secret_hash);
let data = function.encode_input(&[
Token::FixedBytes(id),
Token::Uint(args.payment_amount),
Token::Uint(args.dex_fee),
Token::Address(token_addr),
Token::Address(args.maker_address),
Token::FixedBytes(args.taker_secret_hash.to_vec()),
Token::FixedBytes(args.maker_secret_hash.to_vec()),
Token::Uint(args.funding_time_lock.into()),
Token::Uint(args.payment_time_lock.into()),
])?;
Ok(data)
}
}

#[allow(dead_code)]
#[derive(Debug, Display, EnumFromStringify)]
enum PrepareTxDataError {
#[from_stringify("ethabi::Error")]
#[display(fmt = "Abi error: {}", _0)]
AbiError(String),
#[display(fmt = "Internal error: {}", _0)]
Internal(String),
}
2 changes: 1 addition & 1 deletion mm2src/coins/eth/eth_wasm_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async fn init_eth_coin_helper() -> Result<(MmArc, MmCoinEnum), String> {

let req = json!({
"urls":ETH_SEPOLIA_NODES,
"swap_contract_address":ETH_SEPOLIA_SWAP_CONTRACT
"swap_contract_address":ETH_SEPOLIA_SWAP_CONTRACT,
});
Ok((ctx.clone(), lp_coininit(&ctx, "ETH", &req).await?))
}
Expand Down
1 change: 1 addition & 0 deletions mm2src/coins/eth/for_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ pub(crate) fn eth_coin_from_keypair(
priv_key_policy: key_pair.into(),
derivation_method: Arc::new(DerivationMethod::SingleAddress(my_address)),
swap_contract_address: Address::from_str(ETH_SEPOLIA_SWAP_CONTRACT).unwrap(),
swap_v2_contracts: None,
fallback_swap_contract,
contract_supports_watchers: false,
ticker,
Expand Down
Loading
Loading