Skip to content

Commit

Permalink
uniswap swap logic
Browse files Browse the repository at this point in the history
  • Loading branch information
radkomih committed Dec 19, 2024
1 parent 987a4ae commit 5a34653
Show file tree
Hide file tree
Showing 8 changed files with 651 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
INFURA_ETHEREUM_MAINNET=""
PRIVATE_KEY=""
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.env
.env
tmp
anvil-state.json
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
INFURA_ETHEREUM_MAINNET=""
BLOCK_NUMBER=21432092

start-network:
anvil --fork-url $(INFURA_ETHEREUM_MAINNET) \
--fork-block-number 21422200 \
--fork-block-number $(BLOCK_NUMBER) \
--accounts 10 --balance 1000000 \
--mnemonic "test test test test test test test test test test test junk" \
--state anvil-state.json
Expand Down
34 changes: 34 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module github.com/LimeChain/vulti-mono

go 1.23.3

require github.com/ethereum/go-ethereum v1.14.12

require (
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/StackExchange/wmi v1.2.1 // indirect
github.com/bits-and-blooms/bitset v1.13.0 // indirect
github.com/consensys/bavard v0.1.13 // indirect
github.com/consensys/gnark-crypto v0.12.1 // indirect
github.com/crate-crypto/go-ipa v0.0.0-20240223125850-b1e8a79f509c // indirect
github.com/crate-crypto/go-kzg-4844 v1.0.0 // indirect
github.com/deckarep/golang-set/v2 v2.6.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
github.com/ethereum/c-kzg-4844 v1.0.0 // indirect
github.com/ethereum/go-verkle v0.1.1-0.20240829091221-dffa7562dbe9 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-ole/go-ole v1.3.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/holiman/uint256 v1.3.1 // indirect
github.com/mmcloughlin/addchain v0.4.0 // indirect
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible // indirect
github.com/supranational/blst v0.3.13 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.22.0 // indirect
rsc.io/tmplfunc v0.0.3 // indirect
)
178 changes: 178 additions & 0 deletions go.sum

Large diffs are not rendered by default.

92 changes: 92 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package main

import (
"log"
"math/big"
"time"

"os"

"github.com/LimeChain/vulti-mono/uniswap"
"github.com/ethereum/go-ethereum/common"
)

const (
rpcURL = "http://127.0.0.1:8545"
)

var (
uniswapV2RouterAddress = common.HexToAddress("0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D")

tokenInAddress = common.HexToAddress("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2") // WETH
tokenOutAddress = common.HexToAddress("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48") // UCDC

swapAmountIn = big.NewInt(1e18)
amountOutMin = big.NewInt(1)
)

var (
signerPrivateKey = os.Getenv("PRIVATE_KEY")
)

func logTokenBalances(client *uniswap.Client) {
tokenInBalance, err := client.GetTokenBalance(tokenInAddress)
if err != nil {
log.Printf("Error getting input token balance: %v", err)
return
}
log.Printf("input token balance: %s", tokenInBalance.String())

tokenOutBalance, err := client.GetTokenBalance(tokenOutAddress)
if err != nil {
log.Printf("Error getting output token balance: %v", err)
return
}
log.Printf("output token balance: %s", tokenOutBalance.String())
}

func main() {
cfg := uniswap.Config{
GasLimitBuffer: 50000,
SwapGasLimit: 1000000,
DeadlineDuration: 15 * time.Minute,
}

uniswapClient, err := uniswap.NewClient(rpcURL, uniswapV2RouterAddress, signerPrivateKey, cfg)
if err != nil {
log.Fatalf("Failed to initialize Uniswap client: %v", err)
}

tokensPair := []common.Address{tokenInAddress, tokenOutAddress}

// fetch token pair amount out
expectedAmountOut, err := uniswapClient.GetExpectedAmountOut(swapAmountIn, tokensPair)
if err != nil {
log.Fatalf("Failed to get expected amount out: %v", err)
}
log.Println("Expected amount out:", expectedAmountOut.String())

// calculate output amount with slippage
slippagePercentage := 1.0
amountOutMin := uniswapClient.CalculateAmountOutMin(expectedAmountOut, slippagePercentage)

// mint WETH
log.Println("Minting WETH...")
logTokenBalances(uniswapClient)
if err := uniswapClient.MintWETH(swapAmountIn, tokenInAddress); err != nil {
log.Fatalf("Failed to mint WETH: %v", err)
}
logTokenBalances(uniswapClient)

// approve Router to spend input token
log.Printf("Approving Uniswap Router to spend %s...", tokenInAddress.Hex())
if err := uniswapClient.ApproveERC20Token(tokenInAddress, uniswapV2RouterAddress, swapAmountIn); err != nil {
log.Fatalf("Failed to approve token: %v", err)
}

// swap tokens
if err := uniswapClient.SwapTokens(swapAmountIn, amountOutMin, tokensPair); err != nil {
log.Fatalf("Failed to swap tokens: %v", err)
}
logTokenBalances(uniswapClient)
}
Loading

0 comments on commit 5a34653

Please sign in to comment.