Skip to content

Commit

Permalink
add pool param provided by user in estimated method
Browse files Browse the repository at this point in the history
  • Loading branch information
oncloudit committed Nov 1, 2021
1 parent 3d7b782 commit d2b6889
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 59 deletions.
81 changes: 30 additions & 51 deletions modules/coinswap/coinswap.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,27 +182,24 @@ func (swap coinswapClient) SwapCoin(request SwapCoinRequest, baseTx sdk.BaseTx)
return response, nil
}

func (swap coinswapClient) BuyTokenWithAutoEstimate(paidTokenDenom string, boughtCoin sdk.Coin,
deadline int64,
baseTx sdk.BaseTx,
func (swap coinswapClient) BuyTokenWithAutoEstimate(paidTokenDenom string,
boughtCoin sdk.Coin, pool sdk.PoolInfo, deadline int64, baseTx sdk.BaseTx,
) (res *SwapCoinResponse, err error) {
var amount = sdk.ZeroInt()
switch {
case paidTokenDenom == sdk.BaseDenom:
amount, err = swap.EstimateBaseForBoughtToken(boughtCoin)
amount, err = swap.EstimateBaseForBoughtToken(boughtCoin, pool)
break
case boughtCoin.Denom == sdk.BaseDenom:
amount, err = swap.EstimateTokenForBoughtBase(paidTokenDenom, boughtCoin.Amount)
amount, err = swap.EstimateTokenForBoughtBase(paidTokenDenom, boughtCoin.Amount, pool)
break
default:
amount, err = swap.EstimateTokenForBoughtToken(paidTokenDenom, boughtCoin)
amount, err = swap.EstimateTokenForBoughtToken(paidTokenDenom, boughtCoin, pool)
break
}

if err != nil {
return nil, err
}

req := SwapCoinRequest{
Input: sdk.NewCoin(paidTokenDenom, amount),
Output: boughtCoin,
Expand All @@ -212,20 +209,19 @@ func (swap coinswapClient) BuyTokenWithAutoEstimate(paidTokenDenom string, bough
return swap.SwapCoin(req, baseTx)
}

func (swap coinswapClient) SellTokenWithAutoEstimate(gotTokenDenom string, soldCoin sdk.Coin,
deadline int64,
baseTx sdk.BaseTx,
func (swap coinswapClient) SellTokenWithAutoEstimate(gotTokenDenom string,
soldCoin sdk.Coin, pool sdk.PoolInfo, deadline int64, baseTx sdk.BaseTx,
) (res *SwapCoinResponse, err error) {
var amount = sdk.ZeroInt()
switch {
case gotTokenDenom == sdk.BaseDenom:
amount, err = swap.EstimateBaseForSoldToken(soldCoin)
amount, err = swap.EstimateBaseForSoldToken(soldCoin, pool)
break
case soldCoin.Denom == sdk.BaseDenom:
amount, err = swap.EstimateTokenForSoldBase(gotTokenDenom, soldCoin.Amount)
amount, err = swap.EstimateTokenForSoldBase(gotTokenDenom, soldCoin.Amount, pool)
break
default:
amount, err = swap.EstimateTokenForSoldToken(gotTokenDenom, soldCoin)
amount, err = swap.EstimateTokenForSoldToken(gotTokenDenom, soldCoin, pool)
break
}

Expand Down Expand Up @@ -284,76 +280,59 @@ func (swap coinswapClient) QueryAllPools(req sdk.PageRequest) (*QueryAllPoolsRes
}

func (swap coinswapClient) EstimateTokenForSoldBase(tokenDenom string,
soldBaseAmt sdk.Int,
) (sdk.Int, error) {
result, err := swap.QueryPool(tokenDenom)
if err != nil {
return sdk.ZeroInt(), err
}
fee := sdk.MustNewDecFromStr(result.Pool.Fee)
soldBaseAmt sdk.Int, pool sdk.PoolInfo) (sdk.Int, error) {
fee := sdk.MustNewDecFromStr(pool.Fee)
amount := getInputPrice(soldBaseAmt,
result.Pool.Standard.Amount, result.Pool.Token.Amount, fee)
pool.Standard.Amount, pool.Token.Amount, fee)
return amount, nil
}

func (swap coinswapClient) EstimateBaseForSoldToken(soldToken sdk.Coin) (sdk.Int, error) {
result, err := swap.QueryPool(soldToken.Denom)
if err != nil {
return sdk.ZeroInt(), err
}
fee := sdk.MustNewDecFromStr(result.Pool.Fee)
func (swap coinswapClient) EstimateBaseForSoldToken(soldToken sdk.Coin,
pool sdk.PoolInfo) (sdk.Int, error) {
fee := sdk.MustNewDecFromStr(pool.Fee)
amount := getInputPrice(soldToken.Amount,
result.Pool.Token.Amount, result.Pool.Standard.Amount, fee)
pool.Token.Amount, pool.Standard.Amount, fee)
return amount, nil
}

func (swap coinswapClient) EstimateTokenForSoldToken(boughtTokenDenom string,
soldToken sdk.Coin) (sdk.Int, error) {
soldToken sdk.Coin, pool sdk.PoolInfo) (sdk.Int, error) {
if boughtTokenDenom == soldToken.Denom {
return sdk.ZeroInt(), errors.New("invalid trade")
}

boughtBaseAmt, err := swap.EstimateBaseForSoldToken(soldToken)
boughtBaseAmt, err := swap.EstimateBaseForSoldToken(soldToken, pool)
if err != nil {
return sdk.ZeroInt(), err
}
return swap.EstimateTokenForSoldBase(boughtTokenDenom, boughtBaseAmt)
return swap.EstimateTokenForSoldBase(boughtTokenDenom, boughtBaseAmt, pool)
}

func (swap coinswapClient) EstimateTokenForBoughtBase(soldTokenDenom string,
exactBoughtBaseAmt sdk.Int) (sdk.Int, error) {
result, err := swap.QueryPool(soldTokenDenom)
if err != nil {
return sdk.ZeroInt(), err
}
fee := sdk.MustNewDecFromStr(result.Pool.Fee)
exactBoughtBaseAmt sdk.Int, pool sdk.PoolInfo) (sdk.Int, error) {
fee := sdk.MustNewDecFromStr(pool.Fee)
amount := getOutputPrice(exactBoughtBaseAmt,
result.Pool.Token.Amount, result.Pool.Standard.Amount, fee)
pool.Token.Amount, pool.Standard.Amount, fee)
return amount, nil
}

func (swap coinswapClient) EstimateBaseForBoughtToken(boughtToken sdk.Coin) (sdk.Int, error) {
result, err := swap.QueryPool(boughtToken.Denom)
if err != nil {
return sdk.ZeroInt(), err
}
fee := sdk.MustNewDecFromStr(result.Pool.Fee)
func (swap coinswapClient) EstimateBaseForBoughtToken(boughtToken sdk.Coin,
pool sdk.PoolInfo) (sdk.Int, error) {
fee := sdk.MustNewDecFromStr(pool.Fee)
amount := getOutputPrice(boughtToken.Amount,
result.Pool.Standard.Amount, result.Pool.Token.Amount, fee)
pool.Standard.Amount, pool.Token.Amount, fee)
return amount, nil
}

func (swap coinswapClient) EstimateTokenForBoughtToken(soldTokenDenom string,
boughtToken sdk.Coin) (sdk.Int, error) {
boughtToken sdk.Coin, pool sdk.PoolInfo) (sdk.Int, error) {
if soldTokenDenom == boughtToken.Denom {
return sdk.ZeroInt(), errors.New("invalid trade")
}

soldBaseAmt, err := swap.EstimateBaseForBoughtToken(boughtToken)
soldBaseAmt, err := swap.EstimateBaseForBoughtToken(boughtToken, pool)
if err != nil {
return sdk.ZeroInt(), err
}
return swap.EstimateTokenForBoughtBase(soldTokenDenom, soldBaseAmt)
return swap.EstimateTokenForBoughtBase(soldTokenDenom, soldBaseAmt, pool)
}

func GetLiquidityDenomFrom(denom string) (string, error) {
Expand Down
16 changes: 8 additions & 8 deletions modules/coinswap/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ type Client interface {
SwapCoin(request SwapCoinRequest,
baseTx sdk.BaseTx) (*SwapCoinResponse, error)

BuyTokenWithAutoEstimate(paidTokenDenom string, boughtCoin sdk.Coin,
BuyTokenWithAutoEstimate(paidTokenDenom string, boughtCoin sdk.Coin, pool sdk.PoolInfo,
deadline int64,
baseTx sdk.BaseTx,
) (res *SwapCoinResponse, err error)
SellTokenWithAutoEstimate(gotTokenDenom string, soldCoin sdk.Coin,
SellTokenWithAutoEstimate(gotTokenDenom string, soldCoin sdk.Coin, pool sdk.PoolInfo,
deadline int64,
baseTx sdk.BaseTx,
) (res *SwapCoinResponse, err error)
Expand All @@ -28,16 +28,16 @@ type Client interface {
QueryAllPools(pageReq sdk.PageRequest) (*QueryAllPoolsResponse, error)

EstimateTokenForSoldBase(tokenDenom string,
soldBase sdk.Int,
soldBase sdk.Int, pool sdk.PoolInfo,
) (sdk.Int, error)
EstimateBaseForSoldToken(soldToken sdk.Coin) (sdk.Int, error)
EstimateBaseForSoldToken(soldToken sdk.Coin, pool sdk.PoolInfo) (sdk.Int, error)
EstimateTokenForSoldToken(boughtTokenDenom string,
soldToken sdk.Coin) (sdk.Int, error)
soldToken sdk.Coin, pool sdk.PoolInfo) (sdk.Int, error)
EstimateTokenForBoughtBase(soldTokenDenom string,
boughtBase sdk.Int) (sdk.Int, error)
EstimateBaseForBoughtToken(boughtToken sdk.Coin) (sdk.Int, error)
boughtBase sdk.Int, pool sdk.PoolInfo) (sdk.Int, error)
EstimateBaseForBoughtToken(boughtToken sdk.Coin, pool sdk.PoolInfo) (sdk.Int, error)
EstimateTokenForBoughtToken(soldTokenDenom string,
boughtToken sdk.Coin) (sdk.Int, error)
boughtToken sdk.Coin, pool sdk.PoolInfo) (sdk.Int, error)
}

type AddLiquidityRequest struct {
Expand Down

0 comments on commit d2b6889

Please sign in to comment.