Skip to content

Commit

Permalink
Adds sub-account support for escrows (#149)
Browse files Browse the repository at this point in the history
* Coins are now holded by sub-accounts and not by the escrow module

* Use cosmos-sdk v0.44 function Module() to generate unique addresses for each escrow, closes #99
  • Loading branch information
LeCodeurDuDimanche authored Nov 29, 2021
1 parent acbe692 commit 36dbc5c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
27 changes: 24 additions & 3 deletions x/escrow/keeper/escrow.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,8 @@ func (k Keeper) TransferToEscrow(
}

// Send the price to the module
err = k.bankKeeper.SendCoinsFromAccountToModule(ctx, buyer, types.ModuleName, escrow.Price)

err = k.transferCoinsToEscrow(ctx, buyer, escrow.Id, escrow.Price)
if err != nil {
return sdkerrors.Wrap(err, "Cannot send the coins to the escrow")
}
Expand Down Expand Up @@ -232,11 +233,11 @@ func (k Keeper) doSwap(ctx sdk.Context, escrow types.Escrow, buyer, seller sdk.A
brokerCoins, _ := sdk.NewDecCoinsFromCoins(escrow.Price...).MulDec(escrow.BrokerCommission).TruncateDecimal()
sellerCoins := escrow.Price.Sub(brokerCoins)

err = k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, broker, brokerCoins)
err = k.transferCoinsFromEscrow(ctx, escrow.Id, broker, brokerCoins)
if err != nil {
return sdkerrors.Wrap(err, "Cannot send the coins to the broker")
}
err = k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, seller, sellerCoins)
err = k.transferCoinsFromEscrow(ctx, escrow.Id, seller, sellerCoins)
if err != nil {
return sdkerrors.Wrap(err, "Cannot send the coins to the seller")
}
Expand Down Expand Up @@ -300,6 +301,26 @@ func (k Keeper) doObjectTransfer(ctx sdk.Context, from, to sdk.AccAddress, objec
return err
}

// transferCoinsFromAccountToAccountThroughModule sends coins from an account to another through the escrow module.
// It is intended to use between a module sub-account and a regular only.
func (k Keeper) transferCoinsFromAccountToAccountThroughModule(ctx sdk.Context, from sdk.AccAddress, to sdk.AccAddress, coins sdk.Coins) error {
err := k.bankKeeper.SendCoinsFromAccountToModule(ctx, from, types.ModuleName, coins)
if err != nil {
return err
}
return k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, to, coins)
}

// transferCoinsToEscrow transfers coins from an account to an escrow account
func (k Keeper) transferCoinsToEscrow(ctx sdk.Context, from sdk.AccAddress, toEscrowId string, coins sdk.Coins) error {
return k.transferCoinsFromAccountToAccountThroughModule(ctx, from, k.GetEscrowAddress(toEscrowId), coins)
}

// transferCoinsFromEscrow transfers coins from an escrow account to an account
func (k Keeper) transferCoinsFromEscrow(ctx sdk.Context, fromEscrowId string, to sdk.AccAddress, coins sdk.Coins) error {
return k.transferCoinsFromAccountToAccountThroughModule(ctx, k.GetEscrowAddress(fromEscrowId), to, coins)
}

func (k Keeper) addEscrowToDeadlineStore(ctx sdk.Context, escrow types.Escrow) {
k.getDeadlineStore(ctx).Set(types.GetDeadlineKey(escrow.Deadline, escrow.Id), types.GetEscrowKey(escrow.Id))
}
Expand Down
6 changes: 2 additions & 4 deletions x/escrow/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper
import (
"encoding/hex"
"fmt"
"github.com/cosmos/cosmos-sdk/types/address"
"time"

"github.com/cosmos/cosmos-sdk/store"
Expand Down Expand Up @@ -86,10 +87,7 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger {

// GetEscrowAddress returns the address of the escrow account
func (k Keeper) GetEscrowAddress(id string) sdk.AccAddress {
//TODO: use the changes introduced by https://github.com/cosmos/cosmos-sdk/pull/9088 in v0.43 to effectively have one account per escrow
return k.accountKeeper.GetModuleAddress(types.ModuleName)
// We could use this instead, but it is not optimal and increases probability of collision
// return k.accountKeeper.GetModuleAddress(types.ModuleName + id)
return address.Module(types.ModuleName, []byte(id))
}

func (k Keeper) ImportNextID(ctx sdk.Context, nextID uint64) {
Expand Down

0 comments on commit 36dbc5c

Please sign in to comment.