-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
651 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
INFURA_ETHEREUM_MAINNET="" | ||
PRIVATE_KEY="" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
.env | ||
.env | ||
tmp | ||
anvil-state.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.