Skip to content

Commit

Permalink
initialize project
Browse files Browse the repository at this point in the history
  • Loading branch information
piavgh committed Jul 10, 2023
0 parents commit 8711004
Show file tree
Hide file tree
Showing 31 changed files with 2,515 additions and 0 deletions.
45 changes: 45 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
on: [push, pull_request]
name: Test
jobs:
test:
strategy:
matrix:
go-version: [1.20.x]
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go-version }}
- name: Checkout code
uses: actions/checkout@v2
- name: Test
run: go test ./...

test-cache:
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: 1.20.x
- name: Checkout code
uses: actions/checkout@v2
- uses: actions/cache@v2
with:
# In order:
# * Module download cache
# * Build cache (Linux)
# * Build cache (Mac)
# * Build cache (Windows)
path: |
~/go/pkg/mod
~/.cache/go-build
~/Library/Caches/go-build
%LocalAppData%\go-build
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Test
run: go test ./...
36 changes: 36 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# OS specific
.DS_Store

# Dependency directories
vendor/

# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Go workspace file
go.work

# IDE configuration files
uniswapv3-sdk/.idea/
.vscode/launch.json

# Test result
test/logs
test/result

# Environment Variables
.env

# Miscellaneous
command
app
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 daoleno

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
74 changes: 74 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Pancake V3 SDK

[![API Reference](https://camo.githubusercontent.com/915b7be44ada53c290eb157634330494ebe3e30a/68747470733a2f2f676f646f632e6f72672f6769746875622e636f6d2f676f6c616e672f6764646f3f7374617475732e737667)](https://pkg.go.dev/github.com/KyberNetwork/pancake-v3-sdk)
[![Test](https://github.com/KyberNetwork/pancake-v3-sdk/actions/workflows/test.yml/badge.svg)](https://github.com/KyberNetwork/pancake-v3-sdk/actions/workflows/test.yml)
[![Go Report Card](https://goreportcard.com/badge/github.com/KyberNetwork/pancake-v3-sdk)](https://goreportcard.com/report/github.com/KyberNetwork/pancake-v3-sdk)

🛠 A Go SDK for building applications on top of Pancake V3

## Installation

```sh
go get github.com/KyberNetwork/pancake-v3-sdk
```

## Usage

The following example shows how to create a pool, and get the inputAmount

```go
package main

import (
"fmt"
"math/big"

core "github.com/daoleno/uniswap-sdk-core/entities"
"github.com/KyberNetwork/pancake-v3-sdk/constants"
"github.com/KyberNetwork/pancake-v3-sdk/entities"
"github.com/KyberNetwork/pancake-v3-sdk/utils"
"github.com/ethereum/go-ethereum/common"
)

var (
USDC = core.NewToken(1, common.HexToAddress("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"), 6, "USDC", "USD Coin")
DAI = core.NewToken(1, common.HexToAddress("0x6B175474E89094C44Da98b954EedeAC495271d0F"), 18, "DAI", "Dai Stablecoin")
OneEther = big.NewInt(1e18)
)

func main() {
// create demo ticks
ticks := []entities.Tick{
{
Index: entities.NearestUsableTick(utils.MinTick, constants.TickSpacings[constants.FeeLow]),
LiquidityNet: OneEther,
LiquidityGross: OneEther,
},
{
Index: entities.NearestUsableTick(utils.MaxTick, constants.TickSpacings[constants.FeeLow]),
LiquidityNet: new(big.Int).Mul(OneEther, constants.NegativeOne),
LiquidityGross: OneEther,
},
}

// create tick data provider
p, err := entities.NewTickListDataProvider(ticks, constants.TickSpacings[constants.FeeLow])
if err != nil {
panic(err)
}

// new pool
pool, err := entities.NewPool(USDC, DAI, constants.FeeLow, utils.EncodeSqrtRatioX96(constants.One, constants.One), OneEther, 0, p)
if err != nil {
panic(err)
}

// USDC -> DAI
outputAmount := core.FromRawAmount(DAI, big.NewInt(98))
inputAmount, _, err := pool.GetInputAmount(outputAmount, nil)
if err != nil {
panic(err)
}
fmt.Println(inputAmount.ToSignificant(4))
}
```
47 changes: 47 additions & 0 deletions constants/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package constants

import (
"math/big"

"github.com/daoleno/uniswap-sdk-core/entities"
"github.com/ethereum/go-ethereum/common"
)

const PoolInitCodeHash = "0xe34f199b19b2b4f47f68442619d555527d244f78a3297ea89325f843f87b8b54"

var (
FactoryAddress = common.HexToAddress("0x1F98431c8aD98523631AE4a59f267346ea31F984")
AddressZero = common.HexToAddress("0x0000000000000000000000000000000000000000")
)

// The default factory enabled fee amounts, denominated in hundredths of bips.
type FeeAmount uint64

const (
FeeLowest FeeAmount = 100
FeeLow FeeAmount = 500
FeeMedium FeeAmount = 2500
FeeHigh FeeAmount = 10000

FeeMax FeeAmount = 1000000
)

// The default factory tick spacings by fee amount.
var TickSpacings = map[FeeAmount]int{
FeeLowest: 1,
FeeLow: 10,
FeeMedium: 50,
FeeHigh: 200,
}

var (
NegativeOne = big.NewInt(-1)
Zero = big.NewInt(0)
One = big.NewInt(1)

// used in liquidity amount math
Q96 = new(big.Int).Exp(big.NewInt(2), big.NewInt(96), nil)
Q192 = new(big.Int).Exp(Q96, big.NewInt(2), nil)

PercentZero = entities.NewFraction(big.NewInt(0), big.NewInt(1))
)
38 changes: 38 additions & 0 deletions entities/nearestusabletick.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package entities

import (
"math"

"github.com/KyberNetwork/pancake-v3-sdk/utils"
)

/**
* Returns the closest tick that is nearest a given tick and usable for the given tick spacing
* @param tick the target tick
* @param tickSpacing the spacing of the pool
*/
func NearestUsableTick(tick int, tickSpacing int) int {
if tickSpacing <= 0 {
panic("tickSpacing must be greater than 0")
}
if !(tick >= utils.MinTick && tick <= utils.MaxTick) {
panic("tick exceeds bounds")
}

rounded := Round(float64(tick)/float64(tickSpacing)) * float64(tickSpacing)
if rounded < utils.MinTick {
return int(rounded) + tickSpacing
}
if rounded > utils.MaxTick {
return int(rounded) - tickSpacing
}
return int(rounded)
}

// Round like javascript Math.round
// Note that this differs from many languages' round() functions, which often round this case to the next integer away from zero, instead giving a different result in the case of negative numbers with a fractional part of exactly 0.5.
// For example, -1.5 rounds to -2, but -1.5 rounds to -1.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round#description
func Round(x float64) float64 {
return math.Floor(x + 0.5)
}
39 changes: 39 additions & 0 deletions entities/nearestusabletick_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package entities

import (
"testing"

"github.com/KyberNetwork/pancake-v3-sdk/utils"
"github.com/stretchr/testify/assert"
)

func TestNearestUsableTick(t *testing.T) {
assert.Panics(t, func() { NearestUsableTick(1, 0) }, "panics if tickSpacing is 0")
assert.Panics(t, func() { NearestUsableTick(1, -5) }, "panics if tickSpacing is negative")
assert.Panics(t, func() { NearestUsableTick(utils.MaxTick+1, 1) }, "panics if tick is greater than MaxTick")
assert.Panics(t, func() { NearestUsableTick(utils.MinTick-1, 1) }, "panics if tick is smaller than MinTick")

type args struct {
ticks int
tickSpacing int
}
tests := []struct {
name string
args args
want int
}{
{name: "rounds at positive half", args: args{ticks: 5, tickSpacing: 10}, want: 10},
{name: "rounds down below positive half", args: args{ticks: 4, tickSpacing: 10}, want: 0},
{name: "rounds up for negative half 0", args: args{ticks: -5, tickSpacing: 10}, want: 0},
{name: "rounds up for negative half 1", args: args{ticks: -6, tickSpacing: 10}, want: -10},
{name: "cannot round past MinTick", args: args{ticks: utils.MinTick, tickSpacing: utils.MaxTick/2 + 100}, want: -(utils.MaxTick/2 + 100)},
{name: "cannot round past MaxTick", args: args{ticks: utils.MaxTick, tickSpacing: utils.MaxTick/2 + 100}, want: utils.MaxTick/2 + 100},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NearestUsableTick(tt.args.ticks, tt.args.tickSpacing); got != tt.want {
t.Errorf("NearestUsableTick() = %v, want %v", got, tt.want)
}
})
}
}
Loading

0 comments on commit 8711004

Please sign in to comment.