Skip to content

Commit

Permalink
chore: remove error message function prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
notJoon committed Dec 20, 2024
1 parent 8206bed commit ae78769
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 39 deletions.
6 changes: 3 additions & 3 deletions router/protocol_fee_swap.gno
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ func SetSwapFee(fee uint64) {

setSwapFee(fee)

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

std.Emit(
"SetSwapFee",
"prevAddr", prevAddr,
"prevRealm", prevRealm,
"prevRealm", prevPkgPath,
"fee", ufmt.Sprintf("%d", fee),
)
}
Expand All @@ -98,7 +98,7 @@ func setSwapFee(fee uint64) {
if fee > 10000 {
panic(addDetailToError(
errInvalidSwapFee,
ufmt.Sprintf("protocol_fee_swap.gno__setSwapFee() || fee(%d) must be in range 0 ~ 10000", fee),
ufmt.Sprintf("fee(%d) must be in range 0 ~ 10000", fee),
))
}

Expand Down
46 changes: 22 additions & 24 deletions router/router_dry.gno
Original file line number Diff line number Diff line change
Expand Up @@ -16,50 +16,49 @@ import (
func DrySwapRoute(
inputToken string,
outputToken string,
_amountSpecified string, // int256
_amountSpecified string,
swapType string,
strRouteArr string, // []string
quoteArr string, // []int
) string { // uint256
if swapType != "EXACT_IN" && swapType != "EXACT_OUT" {
panic(addDetailToError(
errInvalidSwapType,
ufmt.Sprintf("router_dry.gno__DrySwapRoute() || unknown swapType(%s)", swapType),
))
}
strRouteArr string,
quoteArr string,
) string {
assertNotASwapType(swapType)

amountSpecified, err := i256.FromDecimal(_amountSpecified)
if err != nil {
panic(err.Error())
}

// TODO (@notJoon): Faster using byte comparison rather than strings.Split() in a small size string.
routes := strings.Split(strRouteArr, ",")
quotes := strings.Split(quoteArr, ",")

validateInput(amountSpecified, swapType, routes, quotes)

if swapType == "EXACT_OUT" {
if swapType == ExactOut {
amountSpecified = i256.Zero().Neg(amountSpecified)
}

resultAmountIn := u256.Zero()
resultAmountOut := u256.Zero()

for i, route := range routes {
numHops := strings.Count(route, "*POOL*") + 1
quote, _ := strconv.Atoi(quotes[i])

if numHops < 1 || numHops > 3 {
numHops := strings.Count(route, POOL_SEPARATOR) + 1
quote, err := strconv.Atoi(quotes[i])
if err != nil {
panic(addDetailToError(
errInvalidInput,
ufmt.Sprintf("router_dry.gno__DrySwapRoute() || number of hops(%d) must be 1~3", numHops),
ufmt.Sprintf("quote(%s) is not a valid integer", quotes[i]),
))
}

assertHopsInRange(numHops)

toSwap := i256.Zero().Mul(amountSpecified, i256.NewInt(int64(quote)))
toSwap = toSwap.Div(toSwap, i256.NewInt(100))

if numHops == 1 { // SINGLE
// TODO (@notJoon): use pattern matching and fall-through
// TODO (@notJoon): Is it possible for an invalid SwapType to get this far?
if numHops == 1 {
amountIn, amountOut := handleSingleSwap(route, toSwap, true)
resultAmountIn = new(u256.Uint).Add(resultAmountIn, amountIn)
resultAmountOut = new(u256.Uint).Add(resultAmountOut, amountOut)
Expand All @@ -68,28 +67,27 @@ func DrySwapRoute(
resultAmountIn = new(u256.Uint).Add(resultAmountIn, amountIn)
resultAmountOut = new(u256.Uint).Add(resultAmountOut, amountOut)
}

}

return processResult(swapType, resultAmountIn, resultAmountOut, amountSpecified)
}

func processResult(swapType string, resultAmountIn, resultAmountOut *u256.Uint, amountSpecified *i256.Int) string {
switch swapType {
case "EXACT_IN":
case ExactIn:
if !i256.FromUint256(resultAmountIn).Eq(amountSpecified) {
return "-1"
}
return resultAmountOut.ToString()
case "EXACT_OUT":
case ExactOut:
if i256.FromUint256(resultAmountOut).Lt(amountSpecified) {
return "-1"
}
return resultAmountIn.ToString()
default:
panic(addDetailToError(
errInvalidSwapType,
ufmt.Sprintf("router_dry.gno__processResult() || unknown swapType(%s)", swapType),
))
assertNotASwapType(swapType)
}

// redundant return.
return "-1"
}
4 changes: 2 additions & 2 deletions router/swap_inner.gno
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func getMinTick(fee uint32) int32 {
default:
panic(addDetailToError(
errInvalidPoolFeeTier,
ufmt.Sprintf("swap_inner.gno__getMinTick() || unknown fee(%d)", fee),
ufmt.Sprintf("unknown fee(%d)", fee),
))
}
}
Expand All @@ -153,7 +153,7 @@ func getMaxTick(fee uint32) int32 {
default:
panic(addDetailToError(
errInvalidPoolFeeTier,
ufmt.Sprintf("swap_inner.gno__getMaxTick() || unknown fee(%d)", fee),
ufmt.Sprintf("unknown fee(%d)", fee),
))
}
}
16 changes: 8 additions & 8 deletions router/token_register.gno
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func RegisterGRC20Interface(pkgPath string, igrc20 GRC20Interface) {
if !(prevAddr == consts.TOKEN_REGISTER || prevPath == consts.INIT_REGISTER_PATH || strings.HasPrefix(prevPath, "gno.land/r/g1er355fkjksqpdtwmhf5penwa82p0rhqxkkyhk5")) {
panic(addDetailToError(
errNoPermission,
ufmt.Sprintf("token_register.gno__RegisterGRC20Interface() || only register(%s) can register token, called from %s", consts.TOKEN_REGISTER, prevAddr),
ufmt.Sprintf("only register(%s) can register token, called from %s", consts.TOKEN_REGISTER, prevAddr),
))
}

Expand All @@ -51,7 +51,7 @@ func RegisterGRC20Interface(pkgPath string, igrc20 GRC20Interface) {
if found {
panic(addDetailToError(
errAlreadyRegistered,
ufmt.Sprintf("token_register.gno__RegisterGRC20Interface() || token(%s) already registered", pkgPath),
ufmt.Sprintf("token(%s) already registered", pkgPath),
))
}

Expand Down Expand Up @@ -84,7 +84,7 @@ func transferByRegisterCall(pkgPath string, to std.Address, amount uint64) bool
if !found {
panic(addDetailToError(
errNotRegistered,
ufmt.Sprintf("token_register.gno__transferByRegisterCall() || token(%s) not registered", pkgPath),
ufmt.Sprintf("token(%s) not registered", pkgPath),
))
}

Expand All @@ -98,7 +98,7 @@ func transferByRegisterCall(pkgPath string, to std.Address, amount uint64) bool
} else {
panic(addDetailToError(
errLocked,
ufmt.Sprintf("token_register.gno__transferByRegisterCall() || expected locked(%t) to be false", locked),
ufmt.Sprintf("expected locked(%t) to be false", locked),
))
}

Expand All @@ -112,7 +112,7 @@ func transferFromByRegisterCall(pkgPath string, from, to std.Address, amount uin
if !found {
panic(addDetailToError(
errNotRegistered,
ufmt.Sprintf("token_register.gno__transferFromByRegisterCall() || token(%s) not registered", pkgPath),
ufmt.Sprintf("token(%s) not registered", pkgPath),
))
}

Expand All @@ -126,7 +126,7 @@ func transferFromByRegisterCall(pkgPath string, from, to std.Address, amount uin
} else {
panic(addDetailToError(
errLocked,
ufmt.Sprintf("token_register.gno__transferFromByRegisterCall() || expected locked(%t) to be false", locked),
ufmt.Sprintf("expected locked(%t) to be false", locked),
))
}
return true
Expand All @@ -139,7 +139,7 @@ func balanceOfByRegisterCall(pkgPath string, owner std.Address) uint64 {
if !found {
panic(addDetailToError(
errNotRegistered,
ufmt.Sprintf("token_register.gno__balanceOfByRegisterCall() || token(%s) not registered", pkgPath),
ufmt.Sprintf("token(%s) not registered", pkgPath),
))
}

Expand All @@ -154,7 +154,7 @@ func approveByRegisterCall(pkgPath string, spender std.Address, amount uint64) b
if !found {
panic(addDetailToError(
errNotRegistered,
ufmt.Sprintf("token_register.gno__approveByRegisterCall() || token(%s) not registered", pkgPath),
ufmt.Sprintf("token(%s) not registered", pkgPath),
))
}

Expand Down
4 changes: 2 additions & 2 deletions router/wrap_unwrap.gno
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ func wrap(ugnotAmount uint64) {
if ugnotAmount <= 0 {
panic(addDetailToError(
errWrapUnwrap,
ufmt.Sprintf("wrap.gno__wrap() || cannot wrap 0 ugnot"),
ufmt.Sprintf("cannot wrap 0 ugnot"),
))
}

if ugnotAmount < consts.UGNOT_MIN_DEPOSIT_TO_WRAP {
panic(addDetailToError(
errWugnotMinimum,
ufmt.Sprintf("wrap.gno__wrap() || amount(%d) < minimum(%d)", ugnotAmount, consts.UGNOT_MIN_DEPOSIT_TO_WRAP),
ufmt.Sprintf("amount(%d) < minimum(%d)", ugnotAmount, consts.UGNOT_MIN_DEPOSIT_TO_WRAP),
))
}

Expand Down

0 comments on commit ae78769

Please sign in to comment.