Fix the issue that cause pool balance out sync with asgard asset

Closes #588 (closed)

In some scenario swap fail because it fail to add the txout item , error as following

E[2020-08-14|23:26:13.153] fail to swap                                 module=main msg="0BBDCD99327E69070B82C2532E3F75EE41CC0F110F9871E41715A92E4AB19084: tbnb189az9plcke2c00vns0zfmllfpfdw67dtv25kgx ==> tbnb16d0xe94l32yyn8juysvss4ksydv9d5xy95clze (Memo: swap:BNB.LOK-3C0) 20000000000 BNB.RUNE-67C" error="2 errors occurred:\n\t* internal error\n\t* fail to prepare outbound tx: vault tthorpub1addwnpepqt6w94mndfw4lh6je9xz08cu6c2fulrd4lylx9vk60dqdm2n54g9gjuj439, does not have enough funds. Has 0 BNB.LOK-3C0, but requires 533304934 BNB.LOK-3C0\n\n: fail to add outbound tx"
I[2020-08-14|23:26:13.168] receive MsgSwap                              module=main requesttxhash=EFEEDC5948AFDBF4BDD1D5F01751BFE90B69783138A2E076C7AA9EA607A8FDB3 sourceasset=BNB.RUNE-67C targetasset=BNB.LOK-3C0 signer=tthor1cf6pdug87cdsrurwtxcn8vke45n3pgx73s374d
I[2020-08-14|23:26:13.168] tbnb189az9plcke2c00vns0zfmllfpfdw67dtv25kgx Swapping BNB.RUNE-67C(20000000000) -> BNB.LOK-3C0 to tbnb189az9plcke2c00vns0zfmllfpfdw67dtv25kgx (Fee 100000000) module=main
I[2020-08-14|23:26:13.168] Pre-Pool: 2848775576543Rune 75967074205Asset module=main
I[2020-08-14|23:26:13.168] Post-swap: 2868775576543Rune 75441153240Asset , user get:525920965  module=main
E[2020-08-14|23:26:13.200] fail to swap                                 module=main msg="EFEEDC5948AFDBF4BDD1D5F01751BFE90B69783138A2E076C7AA9EA607A8FDB3: tbnb189az9plcke2c00vns0zfmllfpfdw67dtv25kgx ==> tbnb16d0xe94l32yyn8juysvss4ksydv9d5xy95clze (Memo: swap:BNB.LOK-3C0) 20000000000 BNB.RUNE-67C" error="2 errors occurred:\n\t* internal error\n\t* fail to prepare outbound tx: vault tthorpub1addwnpepqt6w94mndfw4lh6je9xz08cu6c2fulrd4lylx9vk60dqdm2n54g9gjuj439, does not have enough funds. Has 0 BNB.LOK-3C0, but requires 525920965 BNB.LOK-3C0\n\n: fail to add outbound tx"

When this happen, the asset has be deduct from the pool , however vault did not send out anything to customer , it will do a refund instead.

At the moment , swap will deduct the emit asset from the pool before the tx out item get added to block , however add tx out item to block is not always success.

The fix in this PR include

  1. if current active Asgard doesn't have enough fund , it will choose a retiring vault to send out the fund
  2. If somehow swap fail due to fail to add tx out to block , then we restore the pool , as if nothing happened
Edited by Heimdall

Merge request reports

Loading