Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 38 additions & 32 deletions scripts/amm.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ const button = document.getElementById("swapButton");
const text = document.getElementById("swapText");
const loader = document.getElementById("swapLoader");
const toAmountCurrency = document.getElementById("to-currency");
const arrowImg = document.querySelector(".card-arrow img");
const maxButton = document.getElementById("max-button");
const fromAmountBox = document.getElementById("from-amount");
let handleSwitch, handleMax;
arrowImg.removeEventListener("click", handleSwitch);
maxButton.removeEventListener("click", handleMax);
fromAmountBox.removeEventListener("input", calculateConversion);

const walletsConfig = [
{
Expand Down Expand Up @@ -112,8 +119,8 @@ async function setPublicBalances() {
} catch (e) {
window.alert(
"could not get balance for " +
wallet +
", please check your .env file configuration"
wallet +
", please check your .env file configuration"
);
$(`#${label}Row`).text(wallet);
$(`#${label}Base`).text("n/a");
Expand All @@ -125,7 +132,10 @@ async function setPublicBalances() {
}

function loadAndDisplay() {
setPublicBalances();
setPublicBalances().then(() => {
maxButton.addEventListener("click", handleMax);
});

prepareSwitchButtonClick();
}

Expand Down Expand Up @@ -292,16 +302,13 @@ async function swapToken(
}
enableSwapButton();
} catch (err) {
window.alert(
"Transaction failed, check console for details."
);
window.alert("Transaction failed, check console for details.");
console.log(err);
enableSwapButton();
}
}

const maxButton = document.getElementById("max-button");
maxButton.addEventListener("click", async () => {
handleMax = () => {
const fromAmountBox = document.getElementById("from-amount");
const fromCurrencyDropdown = document.getElementById("from-currency");
const fromCurrency = fromCurrencyDropdown.value;
Expand All @@ -327,7 +334,7 @@ maxButton.addEventListener("click", async () => {
}
}
calculateConversion();
});
};

$("#swapButton").on("click", () => {
if (fromAmountBox.value == 0 || isNaN(fromAmountBox.value)) {
Expand All @@ -352,7 +359,6 @@ function enableSwapButton() {
button.disabled = false;
}

const fromAmountBox = document.getElementById("from-amount");
fromAmountBox.addEventListener("input", calculateConversion);

async function calculateConversionDetails(
Expand All @@ -363,32 +369,32 @@ async function calculateConversionDetails(
) {
const spotPrice = isSynthIn
? await charon.calcSpotPrice(
await charon.recordBalance(),
await charon.recordBalanceSynth(),
0
)
await charon.recordBalance(),
await charon.recordBalanceSynth(),
0
)
: await charon.calcSpotPrice(
await charon.recordBalanceSynth(),
await charon.recordBalance(),
0
);
await charon.recordBalanceSynth(),
await charon.recordBalance(),
0
);
const expectedOut = spotPrice * inputValue;
const exitFee = inputValue * ethers.utils.formatEther(await charon.fee());
const adjustedIn = inputValue - exitFee;
const minAmountOut = isSynthIn
? await charon.calcSingleOutGivenIn(
await charon.recordBalance(),
await charon.recordBalanceSynth(),
ethers.utils.parseEther(adjustedIn.toFixed(18).toString()),
0,
false
)
await charon.recordBalance(),
await charon.recordBalanceSynth(),
ethers.utils.parseEther(adjustedIn.toFixed(18).toString()),
0,
false
)
: await charon.calcOutGivenIn(
await charon.recordBalance(),
await charon.recordBalanceSynth(),
ethers.utils.parseEther(adjustedIn.toFixed(18).toString()),
0
);
await charon.recordBalance(),
await charon.recordBalanceSynth(),
ethers.utils.parseEther(adjustedIn.toFixed(18).toString()),
0
);
const slippage = (minAmountOut - expectedOut) / expectedOut;
try {
const feeData = await provider.getFeeData();
Expand Down Expand Up @@ -513,13 +519,12 @@ async function calculateConversion() {
function prepareSwitchButtonClick() {
const fromAmountInput = document.getElementById("from-amount");
const toAmountInput = document.getElementById("to-amount");
const arrowImg = document.querySelector(".card-arrow img");
const fromCurrencyDropdown = document.getElementById("from-currency");
const toCurrencyDropdown = document.getElementById("to-currency");
const fromCurrencyStyle = fromCurrencyDropdown.style;
const toCurrencyStyle = toCurrencyDropdown.style;
let temp, selectedIndex, toCurrencyOptions;
arrowImg.addEventListener("click", () => {
handleSwitch = () => {
if (toAmountInput.value === "...") return;
if (fromCurrencyDropdown.value == "chd") {
temp = fromAmountInput.value;
Expand Down Expand Up @@ -554,7 +559,8 @@ function prepareSwitchButtonClick() {
fromCurrencyDropdown.disabled = true;
}
calculateConversion();
});
};
arrowImg.addEventListener("click", handleSwitch);
}

async function fetchCryptoPrice(fromCurrency) {
Expand Down
2 changes: 1 addition & 1 deletion scripts/bridgeModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ function getCharon(chain) {
function getChainID(chain) {
switch (chain) {
case "sepolia":
return 5;
return 11155111;
case "chiado":
return 10200;
case "mumbai":
Expand Down
2 changes: 1 addition & 1 deletion scripts/chd.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ async function setData() {
function getChainID(chain) {
switch (chain) {
case "sepolia":
return 5;
return 11155111;
case "chiado":
return 10200;
case "mumbai":
Expand Down
8 changes: 4 additions & 4 deletions scripts/sendModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ function readUTXOs() {
function getChainID(chain) {
switch (chain) {
case "sepolia":
return 5;
return 11155111;
case "mumbai":
return 80001;
case "chiado":
Expand Down Expand Up @@ -488,7 +488,7 @@ function showLoadingAnimation() {
}
function getChain(_id) {
switch (_id) {
case 5:
case 11155111:
return "sepolia";
case 80001:
return "mumbai";
Expand All @@ -505,7 +505,7 @@ function getChain(_id) {

function getPrivateBalance(_id) {
switch (_id) {
case 5:
case 11155111:
return psVal;
case 80001:
return pmVal;
Expand All @@ -522,7 +522,7 @@ function getPrivateBalance(_id) {

function getPublicBalance(_id) {
switch (_id) {
case 5:
case 11155111:
return sVal;
case 80001:
return mVal;
Expand Down