-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
201 lines (168 loc) · 5.46 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
const BigNumber = require("bignumber.js");
const qs = require("qs");
const web3 = require("web3");
import erc20abi from "./erc20abi.json";
let currentTrade = {};
let currentSelectSide;
let tokens;
async function init() {
await listAvailableTokens();
}
async function listAvailableTokens() {
console.log("Initialising...");
let response = await fetch("https://tokens.coingecko.com/uniswap/all.json");
// let response = await fetch(
// "https://wispy-bird-88a7.uniswap.workers.dev/?url=http://tokens.1inch.eth.link"
// );
let tokenListJSON = await response.json();
console.log("Listing available tokens: ", tokenListJSON);
tokens = tokenListJSON.tokens;
console.log("Tokens:", tokens);
// all tokens from the 'response'
let parent = document.getElementById("token_list");
for (const i in tokens) {
let div = document.createElement("div");
div.className = "token_row";
let html = `<img class="token_list_img" src="${tokens[i].logoURI}">
<span class="token_list_text">${tokens[i].symbol}</span>`;
div.innerHTML = html;
// populate the token box
div.onclick = () => {
selectToken(tokens[i]);
};
parent.appendChild(div);
}
}
async function selectToken(token) {
closeModal();
currentTrade[currentSelectSide] = token;
console.log("Current trade: ", currentTrade);
renderInterface();
}
function renderInterface() {
if (currentTrade.from) {
document.getElementById("from_token_img").src = currentTrade.from.logoURI;
document.getElementById("from_token_text").innerHTML =
currentTrade.from.symbol;
}
if (currentTrade.to) {
document.getElementById("to_token_img").src = currentTrade.to.logoURI;
document.getElementById("to_token_text").innerHTML = currentTrade.to.symbol;
}
}
async function connect() {
if (typeof window.ethereum !== "undefined") {
try {
console.log("Connecting...");
await ethereum.request({ method: "eth_requestAccounts" });
} catch (error) {
console.log(error);
}
document.getElementById("login_button").innerHTML = "Connected";
document.getElementById("swap_button").disabled = false;
} else {
document.getElementById("login_button").innerHTML =
"Please install MetaMask";
}
}
function openModal(side) {
currentSelectSide = side;
document.getElementById("token_modal").style.display = "block";
}
function closeModal() {
document.getElementById("token_modal").style.display = "none";
}
async function getPrice() {
console.log("Getting price... ");
if (
!currentTrade.from ||
!currentTrade.to ||
!document.getElementById("from_amount").value
)
return;
let amount = Number(
document.getElementById("from_amount").value *
10 ** currentTrade.from.decimals
);
const params = {
sellToken: currentTrade.from.address,
buyToken: currentTrade.to.address,
sellAmount: amount,
};
// getting the swap price
const response = await fetch(
`https://api.0x.org/swap/v1/price?${qs.stringify(params)}`
);
// parsing response
swapPriceJSON = await response.json();
console.log("Price: ", swapPriceJSON);
document.getElementById("to_amount").value =
swapPriceJSON.buyAmount / 10 ** currentTrade.to.decimals;
document.getElementById("gas_estimate").innerHTML =
swapPriceJSON.estimatedGas;
}
async function getQuote(account) {
console.log("Getting quote... ");
if (
!currentTrade.from ||
!currentTrade.to ||
!document.getElementById("from_amount").value
)
return;
let amount = Number(
document.getElementById("from_amount").value *
10 ** currentTrade.from.decimals
);
const params = {
sellToken: currentTrade.from.address,
buyToken: currentTrade.to.address,
sellAmount: amount,
takerAddress: account,
};
// getting the quote
const response = await fetch(
`https://api.0x.org/swap/v1/price?${qs.stringify(params)}`
);
// parsing response
swapQuoteJSON = await response.json();
console.log("Price: ", swapQuoteJSON);
document.getElementById("to_amount").value =
swapQuoteJSON.buyAmount / 10 ** currentTrade.to.decimals;
document.getElementById("gas_estimate").innerHTML =
swapQuoteJSON.estimatedGas;
return swapQuoteJSON;
}
async function trySwap() {
let accounts = await ethereum.request({ method: "eth_accounts" });
let takerAddress = accounts[0];
console.log("takerAddress: ", takerAddress);
const swapQuoteJSON = await getQuote(takerAddress);
// set token allowance
const web3 = new Web3(Web3.givenProvider);
const fromTokenAddress = currentTrade.from.address;
const ERC20ABI = erc20abi;
const ERC20TokenContract = new web3.eth.Contract(ERC20ABI, fromTokenAddress);
console.log("setup ERC20TokenContract: ", ERC20TokenContract);
// approve
const maxApproval = new BigNumber(2).pow(256).minus(1);
const tx = await ERC20TokenContract.methods
.approve(swapQuoteJSON.allowanceTarget, maxApproval)
.send({ from: takerAddress })
.then((tx) => {
console.log("tx: ", tx);
});
const receipt = await web3.eth.sendTransaction(swapQuoteJSON);
console.log("Receipt: ", receipt);
}
//
init();
document.getElementById("login_button").onclick = connect;
document.getElementById("from_token_select").onclick = () => {
openModal("from");
};
document.getElementById("to_token_select").onclick = () => {
openModal("to");
};
document.getElementById("modal_close").onclick = closeModal;
document.getElementById("from_amount").onblur = getPrice;
document.getElementById("swap_button").onclick = trySwap;