Skip to content

Commit

Permalink
Merge pull request #123 from gnoswap-labs/GSW-675-use-common-wrapped-…
Browse files Browse the repository at this point in the history
…ugnot

GSW-675 feat: use common wrapped ugnot
  • Loading branch information
notJoon committed Dec 13, 2023
2 parents 0785cc2 + 9cadf01 commit 01c0729
Show file tree
Hide file tree
Showing 51 changed files with 1,990 additions and 3,912 deletions.
7 changes: 3 additions & 4 deletions _setup/gns/gns.gno
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ import (
var (
gns *grc20.AdminToken
admin std.Address = "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5" // TODO: helper to change admin
ira std.Address = "g1d9exzh6lta047h6lta047h6lta047h6l8ylkpa" // ToBeChanged: account for gnoswap staker's internal reward
admins []string
)

func init() {
gns = grc20.NewAdminToken("Gnoswap", "GNS", 6)
gns.Mint(admin, 500_000_000_000_000) // @administrator (1M)
gns.Mint(admin, 500_000_000_000_000) // @administrator
gns.Mint(ira, 500_000_000_000_000) // @ira

stakerAddr := std.DerivePkgAddr("gno.land/r/demo/staker")
admins = append(admins, string(stakerAddr))
Expand Down Expand Up @@ -55,7 +57,6 @@ func Allowance(owner, spender users.AddressOrName) uint64 {
// setters.

func Transfer(to users.AddressOrName, amount uint64) {
// caller := std.GetOrigCaller()
caller := std.PrevRealm().Addr()
err := gns.Transfer(caller, to.Resolve(), amount)
if err != nil {
Expand All @@ -64,7 +65,6 @@ func Transfer(to users.AddressOrName, amount uint64) {
}

func Approve(spender users.AddressOrName, amount uint64) {
// caller := std.GetOrigCaller()
caller := std.PrevRealm().Addr()
err := gns.Approve(caller, spender.Resolve(), amount)
if err != nil {
Expand All @@ -73,7 +73,6 @@ func Approve(spender users.AddressOrName, amount uint64) {
}

func TransferFrom(from, to users.AddressOrName, amount uint64) {
// caller := std.GetOrigCaller()
caller := std.PrevRealm().Addr()
err := gns.TransferFrom(caller, from.Resolve(), to.Resolve(), amount)
if err != nil {
Expand Down
43 changes: 14 additions & 29 deletions _setup/grc20_wrapper/grc20wrapper.gno
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ func init() {
rRegistry.RegisterGRC20Interface("gno.land/r/demo/gns", GnsTokenInterface{})
sRegistry.RegisterGRC20Interface("gno.land/r/demo/gns", GnsTokenInterface{})

pRegistry.RegisterGRC20Interface("gno.land/r/demo/obl", OblTokenInterface{})
rRegistry.RegisterGRC20Interface("gno.land/r/demo/obl", OblTokenInterface{})
sRegistry.RegisterGRC20Interface("gno.land/r/demo/obl", OblTokenInterface{})

pRegistry.RegisterGRC20Interface("gno.land/r/demo/qux", QuxTokenInterface{})
rRegistry.RegisterGRC20Interface("gno.land/r/demo/qux", QuxTokenInterface{})
sRegistry.RegisterGRC20Interface("gno.land/r/demo/qux", QuxTokenInterface{})
Expand All @@ -51,35 +55,16 @@ func init() {
rRegistry.RegisterGRC20Interface("gno.land/r/demo/st5", SwapTest5TokenCTokenInterface{})
sRegistry.RegisterGRC20Interface("gno.land/r/demo/st5", SwapTest5TokenCTokenInterface{})

pRegistry.RegisterGRC20Interface("gno.land/r/st11", SwapTest11TokenCTokenInterface{})
rRegistry.RegisterGRC20Interface("gno.land/r/st11", SwapTest11TokenCTokenInterface{})
sRegistry.RegisterGRC20Interface("gno.land/r/st11", SwapTest11TokenCTokenInterface{})

pRegistry.RegisterGRC20Interface("gno.land/r/st22", SwapTest22TokenCTokenInterface{})
rRegistry.RegisterGRC20Interface("gno.land/r/st22", SwapTest22TokenCTokenInterface{})
sRegistry.RegisterGRC20Interface("gno.land/r/st22", SwapTest22TokenCTokenInterface{})

pRegistry.RegisterGRC20Interface("gno.land/r/st33", SwapTest33TokenCTokenInterface{})
rRegistry.RegisterGRC20Interface("gno.land/r/st33", SwapTest33TokenCTokenInterface{})
sRegistry.RegisterGRC20Interface("gno.land/r/st33", SwapTest33TokenCTokenInterface{})

pRegistry.RegisterGRC20Interface("gno.land/r/st44", SwapTest44TokenCTokenInterface{})
rRegistry.RegisterGRC20Interface("gno.land/r/st44", SwapTest44TokenCTokenInterface{})
sRegistry.RegisterGRC20Interface("gno.land/r/st44", SwapTest44TokenCTokenInterface{})

pRegistry.RegisterGRC20Interface("gno.land/r/st55", SwapTest55TokenCTokenInterface{})
rRegistry.RegisterGRC20Interface("gno.land/r/st55", SwapTest55TokenCTokenInterface{})
sRegistry.RegisterGRC20Interface("gno.land/r/st55", SwapTest55TokenCTokenInterface{})

pRegistry.RegisterGRC20Interface("gno.land/r/testtokena", TestTokenATokenInterface{})
rRegistry.RegisterGRC20Interface("gno.land/r/testtokena", TestTokenATokenInterface{})
sRegistry.RegisterGRC20Interface("gno.land/r/testtokena", TestTokenATokenInterface{})
// TO BE USED
// pRegistry.RegisterGRC20Interface("gno.land/r/demo/testtokena", TestTokenATokenInterface{})
// rRegistry.RegisterGRC20Interface("gno.land/r/demo/testtokena", TestTokenATokenInterface{})
// sRegistry.RegisterGRC20Interface("gno.land/r/demo/testtokena", TestTokenATokenInterface{})

pRegistry.RegisterGRC20Interface("gno.land/r/testtokenb", TestTokenBTokenInterface{})
rRegistry.RegisterGRC20Interface("gno.land/r/testtokenb", TestTokenBTokenInterface{})
sRegistry.RegisterGRC20Interface("gno.land/r/testtokenb", TestTokenBTokenInterface{})
// pRegistry.RegisterGRC20Interface("gno.land/r/demo/testtokenb", TestTokenBTokenInterface{})
// rRegistry.RegisterGRC20Interface("gno.land/r/demo/testtokenb", TestTokenBTokenInterface{})
// sRegistry.RegisterGRC20Interface("gno.land/r/demo/testtokenb", TestTokenBTokenInterface{})

pRegistry.RegisterGRC20Interface("gno.land/r/testtokenc", TestTokenCTokenInterface{})
rRegistry.RegisterGRC20Interface("gno.land/r/testtokenc", TestTokenCTokenInterface{})
sRegistry.RegisterGRC20Interface("gno.land/r/testtokenc", TestTokenCTokenInterface{})
// pRegistry.RegisterGRC20Interface("gno.land/r/demo/testtokenc", TestTokenCTokenInterface{})
// rRegistry.RegisterGRC20Interface("gno.land/r/demo/testtokenc", TestTokenCTokenInterface{})
// sRegistry.RegisterGRC20Interface("gno.land/r/demo/testtokenc", TestTokenCTokenInterface{})
}
33 changes: 33 additions & 0 deletions _setup/grc20_wrapper/wrapper_obl.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package grc20wrapper

import (
"gno.land/r/demo/users"

"gno.land/r/demo/obl"

pRegistry "gno.land/r/demo/pool"
rRegistry "gno.land/r/demo/router"
sRegistry "gno.land/r/demo/staker"
)

type OblTokenInterface struct{}

func (OblTokenInterface) Transfer() func(to users.AddressOrName, amount uint64) {
return obl.Transfer
}

func (OblTokenInterface) TransferFrom() func(from, to users.AddressOrName, amount uint64) {
return obl.TransferFrom
}

func (OblTokenInterface) BalanceOf() func(owner users.AddressOrName) uint64 {
return obl.BalanceOf
}

func (OblTokenInterface) Approve() func(spender users.AddressOrName, amount uint64) {
return obl.Approve
}

var _ pRegistry.GRC20Interface = OblTokenInterface{}
var _ rRegistry.GRC20Interface = OblTokenInterface{}
var _ sRegistry.GRC20Interface = OblTokenInterface{}
1 change: 1 addition & 0 deletions _setup/obl/obl.gno
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var (

func init() {
obl = grc20.NewAdminToken("OnblocToken", "OBL", 6)
obl.Mint(admin, 500_000_000_000_000) // @administrator (1M)
}

// method proxies as public functions.
Expand Down
172 changes: 57 additions & 115 deletions _setup/wugnot/wugnot.gno
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
// ** IMPORTANT **
/*
This realm is meant to be support swapping between GRC20 tokens and native coins.
It is not meant to be used as a standalone token.
User can not call transfer not transferFrom only gnoswap can.

All of admin related funcitons and Facuet() will be removed in the future.
*/

package wugnot

import (
Expand All @@ -15,99 +6,53 @@ import (

"gno.land/p/demo/grc/grc20"
"gno.land/p/demo/ufmt"
"gno.land/r/demo/users"

"gno.land/p/demo/testutils"
"gno.land/r/demo/users"
)

var (
wugnot *grc20.AdminToken
admin std.Address = "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5" // TODO: helper to change admin
// wugnot is the admin token, able to mint and burn.
// wugnot *grc20.AdminToken = grc20.NewAdminToken("wrapped GNOT", "wugnot", 0) // orig
wugnot *grc20.AdminToken = grc20.NewAdminToken("Wrapped GNOT", "wugnot", 6)

poolAddr = std.DerivePkgAddr("gno.land/r/demo/pool")
posAddr = std.DerivePkgAddr("gno.land/r/demo/position")
routerAddr = std.DerivePkgAddr("gno.land/r/demo/router")
stakerAddr = std.DerivePkgAddr("gno.land/r/demo/staker")
// WUGNOT is the banker usable by users directly.
WUGNOT = wugnot.GRC20()
)

func init() {
wugnot = grc20.NewAdminToken("WrappedUGNOT", "WUGNOT", 6)

// for swap
var (
lp01 = testutils.TestAddress("lp01") // Liquidity Provider 01
lp02 = testutils.TestAddress("lp02") // Liquidity Provider 02
lp03 = testutils.TestAddress("lp03") // Liquidity Provider 03
tr01 = testutils.TestAddress("tr01") // Trader 01
ci01 = testutils.TestAddress("ci01") // Create Incentive Caller
)

wugnot.Approve(lp01, poolAddr, 50000000000)
wugnot.Approve(lp02, poolAddr, 50000000000)
wugnot.Approve(lp03, poolAddr, 50000000000)
wugnot.Approve(tr01, poolAddr, 50000000000)

wugnot.Approve(ci01, stakerAddr, 50000000000)
}

// method proxies as public functions.
//

// getters.
func GetGRC20() *grc20.AdminToken {
return wugnot
}
const (
ugnotMinDeposit uint64 = 1
wugnotMinDeposit uint64 = 1
)

func TotalSupply() uint64 {
return wugnot.TotalSupply()
}
func Deposit() { // wrap
caller := std.PrevRealm().Addr()
sent := std.GetOrigSend()
amount := sent.AmountOf("ugnot")

func BalanceOf(owner users.AddressOrName) uint64 {
balance, err := wugnot.BalanceOf(owner.Resolve())
if err != nil {
panic(err)
if uint64(amount) < ugnotMinDeposit {
panic(ufmt.Sprintf("Deposit below minimum: %d/%d ugnot.", amount, ugnotMinDeposit))
}
return balance
wugnot.Mint(caller, uint64(amount))
}

func Allowance(owner, spender users.AddressOrName) uint64 {
allowance, err := wugnot.Allowance(owner.Resolve(), spender.Resolve())
if err != nil {
panic(err)
func Withdraw(amount uint64) { // unwrap
if amount < wugnotMinDeposit {
panic(ufmt.Sprintf("Deposit below minimum: %d/%d wugnot.", amount, wugnotMinDeposit))
}
return allowance
}

// setters.

// faucet.

func Faucet() {
// FIXME: add limits?
// FIXME: add payment in gnot?
caller := std.PrevRealm().Addr()
wugnot.Mint(caller, 1000*10000) // 1k
}
pkgaddr := std.GetOrigPkgAddr()

func FaucetL() {
// FIXME: add limits?
// FIXME: add payment in gnot?
caller := std.PrevRealm().Addr()
wugnot.Mint(caller, 50000000000000) // 50_000_000_000
}

// administration.

func Mint(address users.AddressOrName, amount uint64) {
caller := std.PrevRealm().Addr()
assertIsAdmin(caller)
wugnot.Mint(address.Resolve(), amount)
}
callerBal, _ := wugnot.BalanceOf(caller)
if callerBal < amount {
panic(ufmt.Sprintf("Insufficient balance: %d available, %d needed.", callerBal, amount))
}

func Burn(address users.AddressOrName, amount uint64) {
caller := std.PrevRealm().Addr()
assertIsAdmin(caller)
wugnot.Burn(address.Resolve(), amount)
// send swapped ugnots to caller
banker := std.GetBanker(std.BankerTypeRealmSend)
send := std.Coins{{"ugnot", int64(amount)}}
banker.SendCoins(pkgaddr, caller, send)
wugnot.Burn(caller, amount)
}

// render.
Expand All @@ -129,57 +74,54 @@ func Render(path string) string {
}
}

func assertIsAdmin(address std.Address) {
if address != admin {
panic("restricted access")
}
}
// XXX: if we could call WUGNOT.XXX instead of XXX from gnokey, then, all the following lines would not be needed.

// WRAP & UNWRAP
func Wrap(address users.AddressOrName, amount uint64) {
caller := std.PrevRealm().Addr()
if !(caller == poolAddr || caller == posAddr || caller == routerAddr || caller == stakerAddr) {
panic("only pool, position, router, staker contract can wrap")
}
// direct getters.
// XXX: remove them in favor of q_call wugnot.XXX

wugnot.Mint(address.Resolve(), amount) // mint to user
func TotalSupply() uint64 {
return wugnot.TotalSupply()
}

func Unwrap(address users.AddressOrName, amount uint64) {
caller := std.PrevRealm().Addr()
if !(caller == poolAddr || caller == posAddr || caller == routerAddr || caller == stakerAddr) {
panic("only pool, position, router, staker contract can unwrap")
func BalanceOf(owner users.AddressOrName) uint64 {
balance, err := wugnot.BalanceOf(owner.Resolve())
if err != nil {
panic(err)
}
return balance
}

wugnot.Burn(address.Resolve(), amount) // burn from user
func Allowance(owner, spender users.AddressOrName) uint64 {
allowance, err := wugnot.Allowance(owner.Resolve(), spender.Resolve())
if err != nil {
panic(err)
}
return allowance
}

// setters.
//

func Transfer(to users.AddressOrName, amount uint64) {
caller := std.PrevRealm().Addr()
if !(caller == poolAddr || caller == posAddr || caller == routerAddr || caller == stakerAddr) {
panic("only pool, position, router, staker contract can transfer")
}
err := wugnot.Transfer(caller, to.Resolve(), amount)
if err != nil {
panic(err.Error())
panic(err)
}
}

func TransferFrom(from, to users.AddressOrName, amount uint64) {
func Approve(spender users.AddressOrName, amount uint64) {
caller := std.PrevRealm().Addr()
if !(caller == poolAddr || caller == posAddr || caller == routerAddr || caller == stakerAddr) {
panic("only pool, position, router, staker contract can transferFrom")
}
err := wugnot.TransferFrom(caller, from.Resolve(), to.Resolve(), amount)
err := wugnot.Approve(caller, spender.Resolve(), amount)
if err != nil {
panic(err.Error())
panic(err)
}
}

func Approve(spender users.AddressOrName, amount uint64) {
func TransferFrom(from, to users.AddressOrName, amount uint64) {
caller := std.PrevRealm().Addr()
err := wugnot.Approve(caller, spender.Resolve(), amount)
err := wugnot.TransferFrom(caller, from.Resolve(), to.Resolve(), amount)
if err != nil {
panic(err.Error())
panic(err)
}
}
Loading

0 comments on commit 01c0729

Please sign in to comment.