Skip to content

Commit

Permalink
feat: withdraw automatically converts base token denoms (#646)
Browse files Browse the repository at this point in the history
  • Loading branch information
toteki committed Mar 14, 2022
1 parent 34c31dc commit 1ed9306
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 12 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,11 @@ PACKAGES_E2E=$(shell go list ./... | grep '/e2e')
TEST_PACKAGES=./...
TEST_TARGETS := test-unit test-unit-cover test-race test-e2e

test-unit: ARGS=-timeout=5m -tags='norace'
test-unit: ARGS=-timeout=10m -tags='norace'
test-unit: TEST_PACKAGES=$(PACKAGES_UNIT)
test-unit-cover: ARGS=-timeout=5m -tags='norace' -coverprofile=coverage.txt -covermode=atomic
test-unit-cover: ARGS=-timeout=10m -tags='norace' -coverprofile=coverage.txt -covermode=atomic
test-unit-cover: TEST_PACKAGES=$(PACKAGES_UNIT)
test-race: ARGS=-timeout=5m -race
test-race: ARGS=-timeout=10m -race
test-race: TEST_PACKAGES=$(PACKAGES_UNIT)
test-e2e: ARGS=-timeout=25m -v
test-e2e: TEST_PACKAGES=$(PACKAGES_E2E)
Expand Down
5 changes: 3 additions & 2 deletions proto/umee/leverage/v1beta1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ service Msg {
}

// MsgLendAsset represents a lender's request to lend a base asset type to the
// module
// module.
message MsgLendAsset {
string lender = 1;
cosmos.base.v1beta1.Coin amount = 2 [(gogoproto.nullable) = false];
}

// MsgWithdrawAsset represents a lender's request to withdraw a previously loaned
// base asset type from the module
// base asset type from the module. Amount can either be exact uTokens to withdraw
// or equivalent base assets.
message MsgWithdrawAsset {
string lender = 1;
cosmos.base.v1beta1.Coin amount = 2 [(gogoproto.nullable) = false];
Expand Down
24 changes: 21 additions & 3 deletions x/leverage/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,27 @@ func (k Keeper) LendAsset(ctx sdk.Context, lenderAddr sdk.AccAddress, loan sdk.C
}

// WithdrawAsset attempts to deposit uTokens into the leverage module in exchange
// for the original tokens loaned. If the uToken type is invalid or account balance
// insufficient on either side, we return an error.
func (k Keeper) WithdrawAsset(ctx sdk.Context, lenderAddr sdk.AccAddress, uToken sdk.Coin) error {
// for the original tokens loaned. Accepts either a uToken amount to withdraw or
// an equivalent base token amount to be converted automatically via exchange rate.
// If the token or uToken denom is invalid or account balance insufficient for either
// lender or module, we return an error.
func (k Keeper) WithdrawAsset(ctx sdk.Context, lenderAddr sdk.AccAddress, withdrawal sdk.Coin) error {
var (
uToken sdk.Coin
err error
)

if k.IsAcceptedToken(ctx, withdrawal.Denom) {
// Automatically convert base token input to equivalent uTokens
uToken, err = k.ExchangeToken(ctx, withdrawal)
if err != nil {
return err
}
} else {
// Otherwise use original input
uToken = withdrawal
}

if !k.IsAcceptedUToken(ctx, uToken.Denom) {
return sdkerrors.Wrap(types.ErrInvalidAsset, uToken.String())
}
Expand Down
4 changes: 2 additions & 2 deletions x/leverage/types/leverage.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions x/leverage/types/tx.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1ed9306

Please sign in to comment.