Skip to content

Commit

Permalink
Merge branch 'main' into refact-staker
Browse files Browse the repository at this point in the history
  • Loading branch information
notJoon authored Dec 20, 2024
2 parents 8b7b1dd + 38d013a commit ba7d799
Show file tree
Hide file tree
Showing 51 changed files with 2,869 additions and 888 deletions.
39 changes: 26 additions & 13 deletions _deploy/r/gnoswap/common/access.gno
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ import (
"gno.land/r/gnoswap/v1/consts"
)

const (
ErrNoPermission = "caller(%s) has no permission"
)

func AssertCaller(caller, addr std.Address) error {
if caller != addr {
return ufmt.Errorf("caller(%s) has no permission", caller.String())
return ufmt.Errorf(ErrNoPermission, caller.String())
}
return nil
}
Expand All @@ -23,56 +27,65 @@ func SatisfyCond(cond bool) error {

func AdminOnly(caller std.Address) error {
if caller != consts.ADMIN {
return ufmt.Errorf("caller(%s) has no permission", caller.String())
return ufmt.Errorf(ErrNoPermission, caller.String())
}
return nil
}

func GovernanceOnly(caller std.Address) error {
if caller != consts.GOV_GOVERNANCE_ADDR {
return ufmt.Errorf("caller(%s) has no permission", caller.String())
return ufmt.Errorf(ErrNoPermission, caller.String())
}
return nil
}

func GovStakerOnly(caller std.Address) error {
if caller != consts.GOV_STAKER_ADDR {
return ufmt.Errorf("caller(%s) has no permission", caller.String())
return ufmt.Errorf(ErrNoPermission, caller.String())
}
return nil
}

func RouterOnly(caller std.Address) error {
if caller != consts.ROUTER_ADDR {
return ufmt.Errorf("caller(%s) has no permission", caller.String())
return ufmt.Errorf(ErrNoPermission, caller.String())
}
return nil
}

func PositionOnly(caller std.Address) error {
if caller != consts.POSITION_ADDR {
return ufmt.Errorf("caller(%s) has no permission", caller.String())
return ufmt.Errorf(ErrNoPermission, caller.String())
}
return nil
}

func StakerOnly(caller std.Address) error {
if caller != consts.STAKER_ADDR {
return ufmt.Errorf("caller(%s) has no permission", caller.String())
return ufmt.Errorf(ErrNoPermission, caller.String())
}
return nil
}

func TokenRegisterOnly(caller std.Address) error {
if caller != consts.TOKEN_REGISTER {
return ufmt.Errorf("caller(%s) has no permission", caller.String())
func LaunchpadOnly(caller std.Address) error {
if caller != consts.LAUNCHPAD_ADDR {
return ufmt.Errorf(ErrNoPermission, caller.String())
}
return nil
}

func LaunchpadOnly(caller std.Address) error {
if caller != consts.LAUNCHPAD_ADDR {
return ufmt.Errorf("caller(%s) has no permission", caller.String())
func EmissionOnly(caller std.Address) error {
if caller != consts.EMISSION_ADDR {
return ufmt.Errorf(ErrNoPermission, caller.String())
}
return nil
}

// DEPRECATED
// TODO: remove after r/grc20reg is applied for all contracts
func TokenRegisterOnly(caller std.Address) error {
if caller != consts.TOKEN_REGISTER {
return ufmt.Errorf(ErrNoPermission, caller.String())
}
return nil
}
Expand Down
136 changes: 136 additions & 0 deletions _deploy/r/gnoswap/common/access_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package common

import (
"std"
"testing"

"gno.land/p/demo/testutils"
"gno.land/p/demo/uassert"

"gno.land/r/gnoswap/v1/consts"
)

var (
addr01 = testutils.TestAddress("addr01")
addr02 = testutils.TestAddress("addr02")
)

func TestAssertCaller(t *testing.T) {
t.Run("same caller", func(t *testing.T) {
uassert.NoError(t, AssertCaller(addr01, addr01))
})

t.Run("different caller", func(t *testing.T) {
uassert.Error(t, AssertCaller(addr01, addr02))
})
}

func TestSatisfyCond(t *testing.T) {
t.Run("true", func(t *testing.T) {
uassert.NoError(t, SatisfyCond(true))
})

t.Run("false", func(t *testing.T) {
uassert.Error(t, SatisfyCond(false))
})
}

func TestAdminOnly(t *testing.T) {
t.Run("caller is admin", func(t *testing.T) {
uassert.NoError(t, AdminOnly(consts.ADMIN))
})

t.Run("caller is not admin", func(t *testing.T) {
uassert.Error(t, AdminOnly(addr01))
})
}

func TestGovernanceOnly(t *testing.T) {
t.Run("caller is governance", func(t *testing.T) {
uassert.NoError(t, GovernanceOnly(consts.GOV_GOVERNANCE_ADDR))
})

t.Run("caller is not governance", func(t *testing.T) {
uassert.Error(t, GovernanceOnly(addr01))
})
}

func TestGovStakerOnly(t *testing.T) {
t.Run("caller is gov staker", func(t *testing.T) {
uassert.NoError(t, GovStakerOnly(consts.GOV_STAKER_ADDR))
})

t.Run("caller is not gov staker", func(t *testing.T) {
uassert.Error(t, GovStakerOnly(addr01))
})
}

func TestRouterOnly(t *testing.T) {
t.Run("caller is router", func(t *testing.T) {
uassert.NoError(t, RouterOnly(consts.ROUTER_ADDR))
})

t.Run("caller is not router", func(t *testing.T) {
uassert.Error(t, RouterOnly(addr01))
})
}

func TestPositionOnly(t *testing.T) {
t.Run("caller is position", func(t *testing.T) {
uassert.NoError(t, PositionOnly(consts.POSITION_ADDR))
})

t.Run("caller is not position", func(t *testing.T) {
uassert.Error(t, PositionOnly(addr01))
})
}

func TestStakerOnly(t *testing.T) {
t.Run("caller is staker", func(t *testing.T) {
uassert.NoError(t, StakerOnly(consts.STAKER_ADDR))
})

t.Run("caller is not staker", func(t *testing.T) {
uassert.Error(t, StakerOnly(addr01))
})
}

func TestLaunchpadOnly(t *testing.T) {
t.Run("caller is launchpad", func(t *testing.T) {
uassert.NoError(t, LaunchpadOnly(consts.LAUNCHPAD_ADDR))
})

t.Run("caller is not launchpad", func(t *testing.T) {
uassert.Error(t, LaunchpadOnly(addr01))
})
}

func TestEmissionOnly(t *testing.T) {
t.Run("caller is emission", func(t *testing.T) {
uassert.NoError(t, EmissionOnly(consts.EMISSION_ADDR))
})

t.Run("caller is not emission", func(t *testing.T) {
uassert.Error(t, EmissionOnly(addr01))
})
}

func TestTokenRegisterOnly(t *testing.T) {
t.Run("caller is token register", func(t *testing.T) {
uassert.NoError(t, TokenRegisterOnly(consts.TOKEN_REGISTER))
})

t.Run("caller is not token register", func(t *testing.T) {
uassert.Error(t, TokenRegisterOnly(addr01))
})
}

func TestUserOnly(t *testing.T) {
t.Run("caller is user", func(t *testing.T) {
uassert.NoError(t, UserOnly(std.NewUserRealm(addr01)))
})

t.Run("caller is not user", func(t *testing.T) {
uassert.Error(t, UserOnly(std.NewCodeRealm("gno.land/r/realm")))
})
}
8 changes: 0 additions & 8 deletions _deploy/r/gnoswap/common/gno.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1 @@
module gno.land/r/gnoswap/v1/common

require (
gno.land/p/demo/ufmt v0.0.0-latest
gno.land/p/gnoswap/int256 v0.0.0-latest
gno.land/p/gnoswap/pool v0.0.0-latest
gno.land/p/gnoswap/uint256 v0.0.0-latest
gno.land/r/gnoswap/v1/consts v0.0.0-latest
)
14 changes: 9 additions & 5 deletions _deploy/r/gnoswap/consts/consts.gno
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import (

// GNOSWAP SERVICE
const (
ADMIN std.Address = "g17290cwvmrapvp869xfnhhawa8sm9edpufzat7d" // Admin
DEV_OPS std.Address = "g1mjvd83nnjee3z2g7683er55me9f09688pd4mj9" // DevOps

ADMIN std.Address = "g17290cwvmrapvp869xfnhhawa8sm9edpufzat7d"
DEV_OPS std.Address = "g1mjvd83nnjee3z2g7683er55me9f09688pd4mj9"
TOKEN_REGISTER std.Address = "g1er355fkjksqpdtwmhf5penwa82p0rhqxkkyhk5"

TOKEN_REGISTER_NAMESPACE string = "gno.land/r/g1er355fkjksqpdtwmhf5penwa82p0rhqxkkyhk5"
Expand All @@ -21,7 +20,8 @@ const (
GNOT string = "gnot"
WRAPPED_WUGNOT string = "gno.land/r/demo/wugnot"

UGNOT_MIN_DEPOSIT_TO_WRAP uint64 = 1000 // defined in https://github.com/gnolang/gno/blob/81a88a2976ba9f2f9127ebbe7fb7d1e1f7fa4bd4/examples/gno.land/r/demo/wugnot/wugnot.gno#L19
// defined in https://github.com/gnolang/gno/blob/81a88a2976ba9f2f9127ebbe7fb7d1e1f7fa4bd4/examples/gno.land/r/demo/wugnot/wugnot.gno#L19
UGNOT_MIN_DEPOSIT_TO_WRAP uint64 = 1000
)

// CONTRACT PATH & ADDRESS
Expand Down Expand Up @@ -91,9 +91,11 @@ const (

MAX_UINT128 string = "340282366920938463463374607431768211455"
MAX_UINT160 string = "1461501637330902918203684832716283019655932542975"
MAX_INT256 string = "57896044618658097711785492504343953926634992332820282019728792003956564819968"
MAX_UINT256 string = "115792089237316195423570985008687907853269984665640564039457584007913129639935"

MAX_INT128 string = "170141183460469231731687303715884105727"
MAX_INT256 string = "57896044618658097711785492504343953926634992332820282019728792003956564819968"

// Tick Related
MIN_TICK int32 = -887272
MAX_TICK int32 = 887272
Expand All @@ -108,6 +110,8 @@ const (
Q64 string = "18446744073709551616" // 2 ** 64
Q96 string = "79228162514264337593543950336" // 2 ** 96
Q128 string = "340282366920938463463374607431768211456" // 2 ** 128

Q128_RESOLUTION uint = 128
)

// TIMESTAMP & DAY
Expand Down
20 changes: 20 additions & 0 deletions _deploy/r/gnoswap/gns/_helper_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package gns

import (
"testing"

"gno.land/p/demo/grc/grc20"
"gno.land/p/demo/ownable"

"gno.land/r/gnoswap/v1/consts"
)

func testResetGnsTokenObject(t *testing.T) {
t.Helper()

Token, privateLedger = grc20.NewToken("Gnoswap", "GNS", 6)
UserTeller = Token.CallerTeller()
owner = ownable.NewWithAddress(consts.ADMIN)

privateLedger.Mint(owner.Owner(), INITIAL_MINT_AMOUNT)
}
12 changes: 0 additions & 12 deletions _deploy/r/gnoswap/gns/gno.mod
Original file line number Diff line number Diff line change
@@ -1,13 +1 @@
module gno.land/r/gnoswap/v1/gns

require (
gno.land/p/demo/grc/grc20 v0.0.0-latest
gno.land/p/demo/json v0.0.0-latest
gno.land/p/demo/ownable v0.0.0-latest
gno.land/p/demo/ufmt v0.0.0-latest
gno.land/p/demo/users v0.0.0-latest
gno.land/r/demo/users v0.0.0-latest
gno.land/r/demo/grc20reg v0.0.0-latest
gno.land/r/gnoswap/v1/common v0.0.0-latest
gno.land/r/gnoswap/v1/consts v0.0.0-latest
)
Loading

0 comments on commit ba7d799

Please sign in to comment.