-
Notifications
You must be signed in to change notification settings - Fork 158
/
routing.ts
118 lines (104 loc) · 2.83 KB
/
routing.ts
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import {
AlphaRouter,
ChainId,
SwapOptionsSwapRouter02,
SwapRoute,
SwapType,
} from '@uniswap/smart-order-router'
import { TradeType, CurrencyAmount, Percent, Token } from '@uniswap/sdk-core'
import { CurrentConfig } from '../config'
import {
getMainnetProvider,
getWalletAddress,
sendTransaction,
TransactionState,
getProvider,
} from './providers'
import {
MAX_FEE_PER_GAS,
MAX_PRIORITY_FEE_PER_GAS,
ERC20_ABI,
TOKEN_AMOUNT_TO_APPROVE_FOR_TRANSFER,
V3_SWAP_ROUTER_ADDRESS,
} from './constants'
import { fromReadableAmount } from './conversion'
import { ethers } from 'ethers'
export async function generateRoute(): Promise<SwapRoute | null> {
const router = new AlphaRouter({
chainId: ChainId.MAINNET,
provider: getMainnetProvider(),
})
const options: SwapOptionsSwapRouter02 = {
recipient: CurrentConfig.wallet.address,
slippageTolerance: new Percent(50, 10_000),
deadline: Math.floor(Date.now() / 1000 + 1800),
type: SwapType.SWAP_ROUTER_02,
}
const route = await router.route(
CurrencyAmount.fromRawAmount(
CurrentConfig.tokens.in,
fromReadableAmount(
CurrentConfig.tokens.amountIn,
CurrentConfig.tokens.in.decimals
).toString()
),
CurrentConfig.tokens.out,
TradeType.EXACT_INPUT,
options
)
return route
}
export async function executeRoute(
route: SwapRoute
): Promise<TransactionState> {
const walletAddress = getWalletAddress()
const provider = getProvider()
if (!walletAddress || !provider) {
throw new Error('Cannot execute a trade without a connected wallet')
}
const tokenApproval = await getTokenTransferApproval(CurrentConfig.tokens.in)
// Fail if transfer approvals do not go through
if (tokenApproval !== TransactionState.Sent) {
return TransactionState.Failed
}
const res = await sendTransaction({
data: route.methodParameters?.calldata,
to: V3_SWAP_ROUTER_ADDRESS,
value: route?.methodParameters?.value,
from: walletAddress,
maxFeePerGas: MAX_FEE_PER_GAS,
maxPriorityFeePerGas: MAX_PRIORITY_FEE_PER_GAS,
})
return res
}
export async function getTokenTransferApproval(
token: Token
): Promise<TransactionState> {
const provider = getProvider()
const address = getWalletAddress()
if (!provider || !address) {
console.log('No Provider Found')
return TransactionState.Failed
}
try {
const tokenContract = new ethers.Contract(
token.address,
ERC20_ABI,
provider
)
const transaction = await tokenContract.populateTransaction.approve(
V3_SWAP_ROUTER_ADDRESS,
fromReadableAmount(
TOKEN_AMOUNT_TO_APPROVE_FOR_TRANSFER,
token.decimals
).toString()
)
return sendTransaction({
...transaction,
from: address,
})
} catch (e) {
console.error(e)
return TransactionState.Failed
}
}