Skip to content

Commit

Permalink
feat: fix slippage input handling
Browse files Browse the repository at this point in the history
  • Loading branch information
zugdev committed Nov 6, 2024
1 parent 9b319cf commit 74be426
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
16 changes: 12 additions & 4 deletions src/pages/mint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ async function linkMintButton() {
const dollarAmountInput = document.getElementById("dollarAmount") as HTMLInputElement;
const forceCollateralOnly = document.getElementById("forceCollateralOnly") as HTMLInputElement;

const dollarOutMinInput = document.getElementById("dollarOutMin") as HTMLInputElement;
const maxCollateralInInput = document.getElementById("maxCollateralIn") as HTMLInputElement;
const maxGovernanceInInput = document.getElementById("maxGovernanceIn") as HTMLInputElement;

const updateButtonState = async () => {
const selectedCollateralIndex = collateralSelect.value;
const dollarAmountRaw = dollarAmountInput.value;
Expand All @@ -195,14 +199,18 @@ async function linkMintButton() {
dollarAmountInput.addEventListener("input", updateButtonState);
forceCollateralOnly.addEventListener("change", updateButtonState);

// Send transaction
mintButton.addEventListener("click", async () => {
const selectedCollateralIndex = collateralSelect.value;
const dollarAmountRaw = dollarAmountInput.value;
const dollarAmount = ethers.utils.parseUnits(dollarAmountRaw || "0", 18);
const dollarOutMin = ethers.BigNumber.from("0");
const maxCollateralIn = ethers.constants.MaxUint256;
const maxGovernanceIn = ethers.constants.MaxUint256;

// use provided slippage values or default to min/max
const dollarOutMin = dollarOutMinInput.value ? ethers.utils.parseUnits(dollarOutMinInput.value, 18) : ethers.BigNumber.from("0");

const maxCollateralIn = maxCollateralInInput.value ? ethers.utils.parseUnits(maxCollateralInInput.value, 18) : ethers.constants.MaxUint256;

const maxGovernanceIn = maxGovernanceInInput.value ? ethers.utils.parseUnits(maxGovernanceInInput.value, 18) : ethers.constants.MaxUint256;

const isForceCollateralOnlyChecked = forceCollateralOnly.checked;

try {
Expand Down
10 changes: 8 additions & 2 deletions src/pages/redeem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ async function linkRedeemButton() {
const redeemButton = document.getElementById("redeemButton") as HTMLButtonElement;
const collateralSelect = document.getElementById("collateralSelect") as HTMLSelectElement;
const dollarAmountInput = document.getElementById("dollarAmount") as HTMLInputElement;
const collateralOutMinInput = document.getElementById("collateralOutMin") as HTMLInputElement;
const governanceOutMinInput = document.getElementById("governanceOutMin") as HTMLInputElement;

const updateButtonState = async () => {
const selectedCollateralIndex = collateralSelect.value;
Expand All @@ -163,15 +165,19 @@ async function linkRedeemButton() {
redeemButton.disabled = !appState.getIsConnectedState() || !selectedCollateralIndex || !dollarAmount || dollarAmount.isZero();
};

// Attach event listeners to update the button state whenever inputs change
collateralSelect.addEventListener("change", updateButtonState);
dollarAmountInput.addEventListener("input", updateButtonState);

redeemButton.addEventListener("click", async () => {
const selectedCollateralIndex = collateralSelect.value;
const dollarAmountRaw = dollarAmountInput.value;
const dollarAmount = ethers.utils.parseUnits(dollarAmountRaw || "0", 18);
const collateralOutMin = ethers.BigNumber.from("0");
const governanceOutMin = ethers.BigNumber.from("0");

// use provided slippage values or default to min/max
const collateralOutMin = collateralOutMinInput.value ? ethers.utils.parseUnits(collateralOutMinInput.value, 18) : ethers.BigNumber.from("0");

const governanceOutMin = governanceOutMinInput.value ? ethers.utils.parseUnits(governanceOutMinInput.value, 18) : ethers.BigNumber.from("0");

try {
const signerDiamondContract = diamondContract.connect(userSigner);
Expand Down

0 comments on commit 74be426

Please sign in to comment.