Skip to content
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.PHONY: fmt test

fmt:
gofmt -w .
gofmt -s -w .

test:
go test ./... -coverprofile=coverage.out
Expand Down
37 changes: 18 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,41 +1,40 @@
<p align="center"><img width="150" src="logo.png" alt="go-uniswap logo"></p>
<p align="center"><img width="100%" src="banner.png" alt="w3x banner"></p>

<div align="center">

[![build status](https://github.com/aicora/go-uniswap/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/aicora/go-uniswap/actions/workflows/ci.yml)
[![Codecov](https://img.shields.io/codecov/c/github/aicora/go-uniswap?logo=codecov&colorB=orange)](https://app.codecov.io/gh/aicora/go-uniswap)
[![Go Report Card](https://goreportcard.com/badge/github.com/aicora/go-uniswap)](https://goreportcard.com/report/github.com/aicora/go-uniswap)
[![Go Reference](https://img.shields.io/badge/Go%20Reference-blue.svg?logo=go&logoColor=white)](https://pkg.go.dev/github.com/aicora/go-uniswap)
[![License](https://img.shields.io/badge/license-MIT-green)](https://github.com/aicora/go-uniswap/blob/main/LICENSE)
[![Good First Issues](https://img.shields.io/github/issues/aicora/go-uniswap/good%20first%20issue?logo=github)](https://github.com/aicora/go-uniswap/issues?q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc+label%3A%22good+first+issue%22)
[![build status](https://github.com/aicora/w3x/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/aicora/w3x/actions/workflows/ci.yml)
[![Codecov](https://img.shields.io/codecov/c/github/aicora/w3x?logo=codecov&colorB=orange)](https://app.codecov.io/gh/aicora/w3x)
[![Go Report Card](https://goreportcard.com/badge/github.com/aicora/w3x)](https://goreportcard.com/report/github.com/aicora/w3x)
[![Go Reference](https://img.shields.io/badge/Go%20Reference-blue.svg?logo=go&logoColor=white)](https://pkg.go.dev/github.com/aicora/w3x)
[![License](https://img.shields.io/badge/license-MIT-green)](https://github.com/aicora/w3x/blob/main/LICENSE)
[![Good First Issues](https://img.shields.io/github/issues/aicora/w3x/good%20first%20issue?logo=github)](https://github.com/aicora/w3x/issues?q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc+label%3A%22good+first+issue%22)
[![Language](https://img.shields.io/badge/Language-Go-blue.svg?logo=go&logoColor=white)](https://golang.org/)

<p align="center">
<a href="./README.md">English</a> ·
</p>

</div>

# go‑uniswap
## W3X

w3x is a Web3 SDK enabling multi-chain trading, payments, wallets, and cross-chain operations through a single, unified API. Perfect for building SaaS platforms, DeFi aggregators, payment gateways, and wallet solutions quickly and securely.

> **go‑uniswap** — A centralized Go implementation of the Uniswap protocol.
## Quick Start

This repository implements the core logic of Uniswap (v4(>=)‑style) in Go — including swap math, liquidity management, ticks, and position accounting — in a clean, testable, and extensible way.
It’s designed for centralized engines, simulation, backtesting, and quoting engines.
```bash
go get github.com/aicora/w3x
```

## Changelog

Detailed changes for each release are documented in the [release notes](https://github.com/aicora/go-uniswap/releases).
Detailed changes for each release are documented in the [release notes](https://github.com/aicora/w3x/releases).

## Star History

[![Star History Chart](https://api.star-history.com/svg?repos=aicora/go-uniswap&type=timeline&legend=top-left)](https://www.star-history.com/#aicora/go-uniswap&type=timeline&legend=top-left)
[![Star History Chart](https://api.star-history.com/svg?repos=aicora/w3x&type=timeline&legend=top-left)](https://www.star-history.com/#aicora/w3x&type=timeline&legend=top-left)

## Contribution

Thank you to all the people who already contributed to go-uniswap!
Thank you to all the people who already contributed to w3x!

![Contributors](https://contrib.rocks/image?repo=aicora/go-uniswap)
![Contributors](https://contrib.rocks/image?repo=aicora/w3x)

## License

Expand Down
Empty file added aggregator/.gitkeep
Empty file.
Empty file added aggregator/1inch/.gitkeep
Empty file.
Empty file added aggregator/lifi/.gitkeep
Empty file.
2 changes: 1 addition & 1 deletion core/interfaces/currency.go → amm/interfaces/currency.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type ICurrency interface {
ChainId() uint

// Decimals returns the number of decimals the token uses.
Decimals() uint
Decimals() uint8

// Symbol returns the token symbol (e.g., "ETH", "USDT").
Symbol() string
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
17 changes: 5 additions & 12 deletions core/libraries/currency.go → amm/libraries/currency.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package libraries

import (
"github.com/aicora/go-uniswap/core/interfaces"
"github.com/aicora/w3x/amm/interfaces"
"github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -35,7 +35,7 @@ type Currency struct {
isNative bool // true if the currency is the native token of the chain
address common.Address // canonical token address
chainId uint // chain ID
decimals uint // number of token decimals (must be < 255)
decimals uint8 // number of token decimals
symbol string // token symbol
name string // token name
}
Expand All @@ -45,20 +45,13 @@ type Currency struct {
// Parameters:
// - chainID: the blockchain chain ID
// - address: token address; empty or zero address indicates native token
// - decimals: number of token decimals (must be < 255)
// - decimals: number of token decimals
// - symbol: token symbol
// - name: token name
//
// Returns:
// - ICurrency instance representing the token
//
// Panics:
// - if decimals >= 255
func NewCurrency(chainID uint, address common.Address, decimals uint, symbol string, name string) *Currency {
if decimals >= 255 {
panic("Token currency decimals must be less than 255")
}

func NewCurrency(chainID uint, address common.Address, decimals uint8, symbol string, name string) *Currency {
return &Currency{
isNative: isNative(address),
address: address,
Expand All @@ -85,7 +78,7 @@ func (c *Currency) ChainId() uint {
}

// Decimals returns the number of decimals used by the token.
func (c *Currency) Decimals() uint {
func (c *Currency) Decimals() uint8 {
return c.decimals
}

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package libraries
import (
"math/big"

"github.com/aicora/go-uniswap/core/utils"
"github.com/aicora/w3x/amm/utils"
"github.com/ethereum/go-ethereum/common"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"math/big"
"testing"

"github.com/aicora/go-uniswap/core/libraries"
"github.com/aicora/w3x/amm/libraries"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/assert"
)
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion core/libraries/pool.go → amm/libraries/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package libraries
import (
"math/big"

"github.com/aicora/go-uniswap/core/utils"
"github.com/aicora/w3x/amm/utils"
"github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
)
Expand Down
2 changes: 1 addition & 1 deletion core/libraries/pool_test.go → amm/libraries/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"math/big"
"testing"

"github.com/aicora/go-uniswap/core/utils"
"github.com/aicora/w3x/amm/utils"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/assert"
)
Expand Down
2 changes: 1 addition & 1 deletion core/libraries/poolkey.go → amm/libraries/poolkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/hex"
"math/big"

"github.com/aicora/go-uniswap/core/interfaces"
"github.com/aicora/w3x/amm/interfaces"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package libraries
import (
"testing"

"github.com/aicora/go-uniswap/core/interfaces"
"github.com/aicora/w3x/amm/interfaces"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"
)
Expand Down
2 changes: 1 addition & 1 deletion core/libraries/position.go → amm/libraries/position.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package libraries
import (
"math/big"

"github.com/aicora/go-uniswap/core/utils"
"github.com/aicora/w3x/amm/utils"
"github.com/pkg/errors"

"github.com/ethereum/go-ethereum/common"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"math/big"
"testing"

"github.com/aicora/go-uniswap/core/utils"
"github.com/aicora/w3x/amm/utils"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"
)
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package libraries
import (
"math/big"

"github.com/aicora/go-uniswap/core/utils"
"github.com/aicora/w3x/amm/utils"
"github.com/pkg/errors"
)

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions core/poolmanager.go → amm/poolmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"

"github.com/aicora/go-uniswap/core/libraries"
"github.com/aicora/go-uniswap/core/types"
"github.com/aicora/go-uniswap/core/utils"
"github.com/aicora/w3x/amm/libraries"
"github.com/aicora/w3x/amm/types"
"github.com/aicora/w3x/amm/utils"
)

var (
Expand Down
6 changes: 3 additions & 3 deletions core/poolmanager_test.go → amm/poolmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (

"github.com/ethereum/go-ethereum/common"

"github.com/aicora/go-uniswap/core/libraries"
"github.com/aicora/go-uniswap/core/types"
"github.com/aicora/go-uniswap/core/utils"
"github.com/aicora/w3x/amm/libraries"
"github.com/aicora/w3x/amm/types"
"github.com/aicora/w3x/amm/utils"
)

func mockPoolKey() libraries.PoolKey {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
34 changes: 0 additions & 34 deletions core/utils/sqrtpricemath.go → amm/utils/sqrtpricemath.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,6 @@ var (
ErrLiquidityLessThanZero = errors.New("liquidity is less than zero")
)

// SqrtPriceX96 calculates the square root of a token1/token0 ratio
// and returns it in Q64.96 fixed-point format using big.Int arithmetic.
//
// This is commonly used in AMM pools (e.g., Uniswap v4) to encode the
// price ratio in a compact high-precision format suitable for tick calculations.
//
// Formula:
//
// sqrtPriceX96 = sqrt(amount1 / amount0) * 2^96
//
// Parameters:
// - amount1: numerator amount (token1, in smallest unit, e.g., wei)
// - amount0: denominator amount (token0, in smallest unit)
//
// Returns:
// - *big.Int: sqrt(amount1/amount0) scaled by 2^96 (Q64.96 format)
// - error: if amount0 is zero
func SqrtPriceX96(amount1, amount0 *big.Int) (*big.Int, error) {
if amount0.Sign() == 0 {
return nil, ErrZeroDenominator
}

// Step 1: numerator * 2^192 to prepare for Q64.96 sqrt scaling
num := new(big.Int).Lsh(amount1, 192) // amount1 * 2^192

// Step 2: division by denominator
ratioX192 := new(big.Int).Div(num, amount0) // ratio * 2^192

// Step 3: square root
sqrtRatioX96 := new(big.Int).Sqrt(ratioX192) // sqrt(ratio * 2^192) = sqrt(ratio) * 2^96

return sqrtRatioX96, nil
}

// GetAmount0Delta calculates the amount of token0 required for a given
// liquidity between two sqrt price ratios, using big.Int arithmetic.
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,6 @@ import (
"github.com/stretchr/testify/require"
)

func TestSqrtPriceX96(t *testing.T) {
tests := []struct {
name string
amount1 *big.Int
amount0 *big.Int
wantGT int64
wantErr bool
}{
{
name: "normal values",
amount1: big.NewInt(2000_000_000), // e.g., 2000 USDC (6 decimals)
amount0: big.NewInt(1_000_000_000_000_000_000), // 1 ETH in wei
wantGT: 0,
},
{
name: "zero denominator",
amount1: big.NewInt(1000),
amount0: big.NewInt(0),
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
res, err := SqrtPriceX96(tt.amount1, tt.amount0)
if tt.wantErr {
require.Error(t, err)
return
}
require.NoError(t, err)
require.True(t, res.Cmp(big.NewInt(tt.wantGT)) > 0)
})
}
}

func TestGetAmount0Delta(t *testing.T) {
liq := big.NewInt(1000)
sqrtA := new(big.Int).Set(Q96)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Binary file added banner.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file added bridge/.gitkeep
Empty file.
Empty file added dex/balancer/.gitkeep
Empty file.
Empty file added dex/curve/.gitkeep
Empty file.
Empty file added dex/pancakeswap/.gitkeep
Empty file.
2 changes: 1 addition & 1 deletion sdks/core/addresses.go → dex/uniswap/core/addresses.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sdkcore
package uniswapsdkcore

type AddressMap map[ChainId]string

Expand Down
2 changes: 1 addition & 1 deletion sdks/core/chains.go → dex/uniswap/core/chains.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sdkcore
package uniswapsdkcore

// ChainId defines supported blockchain network IDs.
type ChainId uint64
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sdkcore
package uniswapsdkcore

/**
* ComputePriceImpact calculates the **price impact** of a trade relative to the mid price.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sdkcore
package uniswapsdkcore

import (
"math/big"
Expand Down
2 changes: 1 addition & 1 deletion sdks/core/constants.go → dex/uniswap/core/constants.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sdkcore
package uniswapsdkcore

import "math/big"

Expand Down
2 changes: 1 addition & 1 deletion sdks/core/create2.go → dex/uniswap/core/create2.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sdkcore
package uniswapsdkcore

import (
"github.com/ethereum/go-ethereum/common"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sdkcore
package uniswapsdkcore

import (
"testing"
Expand Down
Loading
Loading