Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: router #431

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
450 changes: 450 additions & 0 deletions router/_helper_test.gno

Large diffs are not rendered by default.

225 changes: 0 additions & 225 deletions router/comptue_routes.gno

This file was deleted.

13 changes: 0 additions & 13 deletions router/gno.mod
Original file line number Diff line number Diff line change
@@ -1,14 +1 @@
module gno.land/r/gnoswap/v1/router

require (
gno.land/p/demo/ufmt v0.0.0-latest
gno.land/p/demo/users v0.0.0-latest
gno.land/p/gnoswap/int256 v0.0.0-latest
gno.land/p/gnoswap/uint256 v0.0.0-latest
gno.land/r/demo/wugnot v0.0.0-latest
gno.land/r/gnoswap/v1/common v0.0.0-latest
gno.land/r/gnoswap/v1/consts v0.0.0-latest
gno.land/r/gnoswap/v1/emission v0.0.0-latest
gno.land/r/gnoswap/v1/pool v0.0.0-latest
gno.land/r/gnoswap/v1/staker v0.0.0-latest
)
11 changes: 0 additions & 11 deletions router/gno_helper.gno

This file was deleted.

46 changes: 26 additions & 20 deletions router/protocol_fee_swap.gno
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ import (
u256 "gno.land/p/gnoswap/uint256"
)

const (
defaultSwapFee = uint64(15) // 0.15%
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

afaik, it should be value of bps
@onlyhyde need double check

)

var (
swapFee = uint64(15) // 0.15%
swapFee = defaultSwapFee
)

func handleSwapFee(
outputToken string,
amount *u256.Uint,
isDry bool,
) *u256.Uint {
if swapFee <= 0 {
return amount
Expand All @@ -28,19 +31,17 @@ func handleSwapFee(
feeAmount.Div(feeAmount, u256.NewUint(10000))
feeAmountUint64 := feeAmount.Uint64()

if !isDry {
transferFromByRegisterCall(outputToken, std.PrevRealm().Addr(), consts.PROTOCOL_FEE_ADDR, feeAmountUint64)
transferFromByRegisterCall(outputToken, std.PrevRealm().Addr(), consts.PROTOCOL_FEE_ADDR, feeAmountUint64)

prevAddr, prevRealm := getPrev()
prevAddr, prevRealm := getPrev()

std.Emit(
"SwapRouteFee",
"prevAddr", prevAddr,
"prevRealm", prevRealm,
"internal_tokenPath", outputToken,
"internal_amount", ufmt.Sprintf("%d", feeAmountUint64),
)
}
std.Emit(
"SwapRouteFee",
"prevAddr", prevAddr,
"prevRealm", prevRealm,
"internal_tokenPath", outputToken,
"internal_amount", ufmt.Sprintf("%d", feeAmountUint64),
)

toUserAfterProtocol := new(u256.Uint).Sub(amount, feeAmount)
return toUserAfterProtocol
Expand All @@ -58,7 +59,9 @@ func SetSwapFeeByAdmin(fee uint64) {
panic(err)
}

setSwapFee(fee)
if err := setSwapFee(fee); err != nil {
panic(err)
}

prevAddr, prevRealm := getPrev()

Expand All @@ -79,7 +82,9 @@ func SetSwapFee(fee uint64) {
panic(err)
}

setSwapFee(fee)
if err := setSwapFee(fee); err != nil {
panic(err)
}

prevAddr, prevRealm := getPrev()

Expand All @@ -91,16 +96,17 @@ func SetSwapFee(fee uint64) {
)
}

func setSwapFee(fee uint64) {
func setSwapFee(fee uint64) error {
common.IsHalted()

// 10000 (bps) = 100%
if fee > 10000 {
panic(addDetailToError(
errInvalidSwapFee,
ufmt.Sprintf("protocol_fee_swap.gno__setSwapFee() || fee(%d) must be in range 0 ~ 10000", fee),
))
return ufmt.Errorf(
"%s: fee must be in range 0 to 10000. got %d",
errInvalidSwapFee.Error(), fee,
)
}

swapFee = fee
return nil
}
Loading
Loading