-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
149 lines (112 loc) · 3.52 KB
/
index.js
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
const getQuote = async (params) => {
const endpoint = "v2/quote"
const quoteUrl = `${PATH_FINDER_API_URL}/${endpoint}`
console.log(quoteUrl)
try {
const res = await axios.get(quoteUrl, { params })
return res.data;
} catch (e) {
console.error(`Fetching quote data from pathfinder: ${e}`)
}
}
const checkAndSetAllowance = async (wallet, tokenAddress, approvalAddress, amount) => {
if (tokenAddress === ethers.constants.AddressZero) {
return
}
const erc20 = new ethers.Contract(tokenAddress, erc20_abi, wallet);
const allowance = await erc20.allowance(await wallet.getAddress(), approvalAddress);
if (allowance.lt(amount)) {
const approveTx = await erc20.approve(approvalAddress, amount, {gasPrice: await wallet.provider.getGasPrice()});
try {
await approveTx.wait();
console.log(`Transaction mined succesfully: ${approveTx.hash}`)
}
catch (error) {
console.log(`Transaction failed with error: ${error}`)
}
}
else{
console.log("enough allowance")
alert("enough allowance")
}
}
const getTransaction = async (params, quoteData) => {
const endpoint = "v2/transaction"
const txDataUrl = `${PATH_FINDER_API_URL}/${endpoint}`
console.log(txDataUrl)
try {
const res = await axios.post(txDataUrl, {
...quoteData,
slippageTolerance: 0.5,
senderAddress: account,
receiverAddress: account,
})
return res.data;
} catch (e) {
console.error(`Fetching tx data from pathfinder: ${e}`)
}
}
const params ={
'fromTokenAddress': from,
'toTokenAddress': to,
'amount': amount,
'fromTokenChainId': "80001",
'toTokenChainId': "43113",
'partnerId': "0",
}
//quotedata
const quoteData = await getQuote(params);
console.log(quoteData)
if(window.ethereum) {
console.log('detected');
try {
const accounts = await window.ethereum.request({
method: "eth_requestAccounts",
});
console.log(accounts[0])
const provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = provider.getSigner();
//check allowance
await checkAndSetAllowance(
signer,
from,
quoteData.allowanceTo,
ethers.constants.MaxUint256
);
}
catch(err) {
console.log(err)
}
}
//execute the transaction
if(window.ethereum) {
console.log('detected');
try {
const accounts = await window.ethereum.request({
method: "eth_requestAccounts",
});
console.log(accounts[0])
const provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = provider.getSigner();
const txResponse = await getTransaction({
'fromTokenAddress': from,
'toTokenAddress': to,
'fromTokenChainId': "80001",
'toTokenChainId': "43113", // Fuji
'widgetId': 0, // get your unique wdiget id by contacting us on Telegram
}, quoteData); // params have been defined in step 1 and quoteData has also been fetched in step 1
// sending the transaction using the data given by the pathfinder
const tx = await signer.sendTransaction(txResponse.txn)
try {
await tx.wait();
console.log(`Transaction mined successfully: ${tx.hash}`)
alert(`Transaction mined successfully: ${tx.hash}`)
}
catch (error) {
console.log(`Transaction failed with error: ${error}`)
}
}
catch(err) {
console.log(err)
}
}