Loan handlers do not set the correct (original) ToAddress when making a MsgSwap
This Issue is prompted by this regression test TODO.
# TODO: Refund should be sent to the originating account not the lending module.
# - .end[]|select(.type == "refund")|.from == "{{ addr_thor_fox }}"
This commit which modifies it is relevant.
Referring to the structure of a MsgSwap,
it contains both a Tx field and a Destination field.
A Tx contains both a FromAddress field and a ToAddress field.
The ToAddress field has a meaning distinct from the Destination field: A swapper may send fund from the FromAddress to the ToAddress, with the Destination in the memo.
The FromAddress is the user-controlled address, the ToAddress may be an external-chain Asgard vault address, and the Destination may be a different user-controlled address.
When a swap is initiated in the form of a MsgDeposit, which unlike a MsgSend is submitted to the network itself without being sent directly to a receiving address,
the 'targetModule' network module is determined with a switch
according to the memo's transaction type,
the transaction's Coins sent from the user's address to the network module's address,
sdkErr = h.mgr.Keeper().SendFromAccountToModule(ctx, msg.GetSigners()[0], targetModule, msg.Coins)
and then the ToAddress of the transaction set to the address of that network module.
to, err := h.mgr.Keeper().GetModuleAddress(targetModule)
tx := common.NewTx(txID, from, to, coinsInMsg, common.Gas{gas}, msg.Memo)
The ToAddress is thus consistently where the funds first go when they leave the user's address.
In loan open, first a user sends funds to a vault address or (with MsgDeposit) the Pool Module address,
then they are swapped to Derived Asset which is sent to the Lending Module,
then TOR is minted when the loan is opened,
then (if the output asset isn't TOR) TOR is sent to the Pool Module and swapped to the output asset.
In loan repayment, first a user sends funds to a vault address or (with MsgDeposit) the Pool Module address,
then they are swapped to TOR which is sent to the Lending Module,
then TOR is burned when the loan is closed,
then the Derived Asset representing the collateral is sent to the Pool Module and swapped to the output asset.
For each swap, the loan handler prompting the swap makes a MsgSwap with NewMsgSwap
, containing a Tx
field with a ToAddress
field.
For the swaps from TOR or other Derived Asset the swap's input coins are sent to the Pool Module address which is thus for consistency the appropriate ToAddress;
for the initial swaps from non-derived coin the ToAddress should similarly be the same as the ToAddress of the original transaction.
To resolve this I currently propose the code in !2928 (merged)
'Set correct ToAddress when loan handlers make a MsgSwap'.