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

feat: initial version of the gnoswap contracts #1

Merged
merged 1 commit into from
Oct 6, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
32 changes: 32 additions & 0 deletions contracts/getters.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package gnoswap_optimizer

func GetVault(tokenId uint64) Vault {
vault := mustGetVault(tokenId)
return *vault
}

func GetVaultPaused(tokenId uint64) bool {
vault := mustGetVault(tokenId)
return vault.paused
}

func GetVaultToken0(tokenId uint64) string {
vault := mustGetVault(tokenId)
return vault.token0
}

func GetVaultToken1(tokenId uint64) string {
vault := mustGetVault(tokenId)
return vault.token1
}

func GetVaultFee(tokenId uint64) uint32 {
vault := mustGetVault(tokenId)
return vault.fee
}

func BalanceOf(tokenId uint64, account string) string {
vault := mustGetVault(tokenId)
balance := mustGetBalance(vault, account)
return balance.ToString()
}
7 changes: 7 additions & 0 deletions contracts/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module gno.land/r/gnoswap_optimizer

require (
gno.land/p/demo/avl v0.0.0-latest
gno.land/p/demo/ownable v0.0.0-latest
gno.land/r/gnoswap/v2/position v0.0.0-latest
)
149 changes: 149 additions & 0 deletions contracts/token_register.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package gnoswap_optimizer

import (
"fmt"
"std"

pusers "gno.land/p/demo/users"
u256 "gno.land/p/gnoswap/uint256"
"gno.land/r/gnoswap/v2/consts"
)

type GRC20Interface interface {
Transfer() func(to pusers.AddressOrName, amount uint64)
TransferFrom() func(from, to pusers.AddressOrName, amount uint64)
BalanceOf() func(owner pusers.AddressOrName) uint64
Approve() func(spender pusers.AddressOrName, amount uint64)
}

var (
registered = make(map[string]GRC20Interface)
locked = false // mutex
)

func GetRegisteredTokens() []string {
tokens := make([]string, 0, len(registered))
for k := range registered {
tokens = append(tokens, k)
}
return tokens
}

func RegisterGRC20Interface(pkgPath string, igrc20 GRC20Interface) {
caller := std.GetOrigCaller()
if caller != consts.TOKEN_REGISTER {
panic(fmt.Sprintf("unauthorized address(%s) to register", caller.String()))
}

pkgPath = handleNative(pkgPath)

_, found := registered[pkgPath]
if found {
panic(fmt.Sprintf("pkgPath(%s) already registered", pkgPath))
}

registered[pkgPath] = igrc20
}

func UnregisterGRC20Interface(pkgPath string) {
// only admin can unregister
caller := std.GetOrigCaller()
if caller != consts.TOKEN_REGISTER {
panic(fmt.Sprintf("unauthorized address(%s) to unregister", caller.String()))
}

pkgPath = handleNative(pkgPath)

_, found := registered[pkgPath]
if found {
delete(registered, pkgPath)
}
}

func transferByRegisterCall(pkgPath string, to std.Address, amount string) bool {
amountParsed := checkAmountRange(amount)
pkgPath = handleNative(pkgPath)

_, found := registered[pkgPath]
if !found {
panic(fmt.Sprintf("pkgPath(%s) not found", pkgPath))
}

if !locked {
locked = true
registered[pkgPath].Transfer()(pusers.AddressOrName(to), amountParsed)

defer func() {
locked = false
}()
} else {
panic("expected locked to be false")
}
return true
}

func transferFromByRegisterCall(pkgPath string, from, to std.Address, amount string) bool {
amountParsed := checkAmountRange(amount)
pkgPath = handleNative(pkgPath)

_, found := registered[pkgPath]
if !found {
panic(fmt.Sprintf("pkgPath(%s) not found", pkgPath))
}

if !locked {
locked = true
registered[pkgPath].TransferFrom()(pusers.AddressOrName(from), pusers.AddressOrName(to), amountParsed)

defer func() {
locked = false
}()
} else {
panic("expected locked to be false")
}
return true
}

func balanceOfByRegisterCall(pkgPath string, owner std.Address) uint64 {
pkgPath = handleNative(pkgPath)

_, found := registered[pkgPath]
if !found {
panic(fmt.Sprintf("pkgPath(%s) not found", pkgPath))
}

balance := registered[pkgPath].BalanceOf()(pusers.AddressOrName(owner))
return balance
}

func approveByRegisterCall(pkgPath string, spender std.Address, amount string) bool {
amountParsed := checkAmountRange(amount)
pkgPath = handleNative(pkgPath)

_, found := registered[pkgPath]
if !found {
panic(fmt.Sprintf("pkgPath(%s) not found", pkgPath))
}

registered[pkgPath].Approve()(pusers.AddressOrName(spender), amountParsed)

return true
}

func handleNative(pkgPath string) string {
if pkgPath == consts.GNOT {
return consts.WRAPPED_WUGNOT
}

return pkgPath
}

func checkAmountRange(amount string) uint64 {
// check amount is in uint64 range
amountParsed, err := u256.FromDecimal(amount)
if err != nil {
panic(fmt.Sprintf("amount(%s) is not in uint64 range", amount))
}

return amountParsed.Uint64()
}
13 changes: 13 additions & 0 deletions contracts/type.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package gnoswap_optimizer

import (
"gno.land/p/demo/avl"
)

type Vault struct {
balances *avl.Tree // address -> *u256.Uint
paused bool
token0 string
token1 string
fee uint32
}
59 changes: 59 additions & 0 deletions contracts/utils.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package gnoswap_optimizer

import (
"fmt"
"strconv"

u256 "gno.land/p/gnoswap/uint256"
"gno.land/r/gnoswap/v2/position"
)

func assertVaultPaused(vault *Vault) {
if vault.paused {
panic("Vault is paused")
}
}

func mustGetVault(tokenId uint64) *Vault {
vault, exists := vaults.Get(strconv.FormatUint(tokenId, 10))
if !exists {
panic(fmt.Sprintf("Vault %d not found", tokenId))
}
return vault.(*Vault)
}

func mustGetBalance(vault *Vault, account string) *u256.Uint {
balance, exists := vault.balances.Get(account)
if !exists {
panic(fmt.Sprintf("Balance for account %s not found", account))
}
parsedAmount, err := u256.FromDecimal(balance.(string))
if err != nil {
panic(err)
}
return parsedAmount
}

func increaseBalance(vault *Vault, account string, amount string) {
parsedAmount, err := u256.FromDecimal(amount)
if err != nil {
panic(err)
}

currentBalance := mustGetBalance(vault, account)
currentBalance.Add(currentBalance, parsedAmount)
vault.balances.Set(account, currentBalance.ToString())
}

// get a percentage of the total liquidity as an uint64
func computeLiquidityRatio(tokenId uint64, balance *u256.Uint) uint64 {
totalLiquidity := position.PositionGetPositionLiquidity(tokenId)

// get a percentage of the total liquidity as an uint64
percentage := new(u256.Uint)
percentage.Set(balance)
percentage.Mul(percentage, u256.NewUint(100))
percentage.Div(percentage, totalLiquidity)

return percentage.Uint64()
}
Loading