-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
73 lines (63 loc) · 2 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"math/big"
"os"
"time"
"log"
"github.com/KyberNetwork/uniswapv3-sdk-uint256/constants"
"github.com/KyberNetwork/uniswapv3-sdk-uint256/entities"
"github.com/KyberNetwork/uniswapv3-sdk-uint256/examples/helper"
"github.com/KyberNetwork/uniswapv3-sdk-uint256/periphery"
coreEntities "github.com/daoleno/uniswap-sdk-core/entities"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
)
func init() {
log.SetFlags(log.Lshortfile | log.LstdFlags)
}
func main() {
client, err := ethclient.Dial(helper.PolygonRPC)
if err != nil {
log.Fatal(err)
}
wallet := helper.InitWallet(os.Getenv("MY_PRIVATE_KEY"))
if wallet == nil {
log.Fatal("init wallet failed")
}
pool, err := helper.ConstructV3Pool(client, helper.WMATIC, helper.AMP, uint64(constants.FeeMedium))
if err != nil {
log.Fatal(err)
}
//0.01%
slippageTolerance := coreEntities.NewPercent(big.NewInt(1), big.NewInt(1000))
//after 5 minutes
d := time.Now().Add(time.Minute * time.Duration(15)).Unix()
deadline := big.NewInt(d)
// single trade input
// single-hop exact input
r, err := entities.NewRoute([]*entities.Pool{pool}, helper.WMATIC, helper.AMP)
if err != nil {
log.Fatal(err)
}
swapValue := helper.FloatStringToBigInt("0.1", 18)
trade, err := entities.FromRoute(r, coreEntities.FromRawAmount(helper.WMATIC, swapValue), coreEntities.ExactInput)
if err != nil {
log.Fatal(err)
}
log.Printf("%v %v\n", trade.Swaps[0].InputAmount.Quotient(), trade.Swaps[0].OutputAmount.Wrapped().Quotient())
params, err := periphery.SwapCallParameters([]*entities.Trade{trade}, &periphery.SwapOptions{
SlippageTolerance: slippageTolerance,
Recipient: wallet.PublicKey,
Deadline: deadline,
})
if err != nil {
log.Fatal(err)
}
log.Printf("calldata = 0x%x\n", params.Value.String())
tx, err := helper.TryTX(client, common.HexToAddress(helper.ContractV3SwapRouterV1),
swapValue, params.Calldata, wallet)
if err != nil {
log.Fatal(err)
}
log.Println(tx.Hash().String())
}