Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: initialize #3

Open
wants to merge 49 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
f6708d9
chore: initialize
kingsley-einstein Sep 26, 2024
f64e5c3
chore: made updates
kingsley-einstein Sep 26, 2024
a648dea
chore: made updates
kingsley-einstein Sep 26, 2024
6726d0f
feat: add providers list modal
kingsley-einstein Sep 27, 2024
f8f571e
feat: create functions to interact with blockchain
kingsley-einstein Oct 2, 2024
001d880
chore: make requested changes
kingsley-einstein Oct 2, 2024
95d2015
chore: made requested changes
kingsley-einstein Oct 3, 2024
5a9ea09
feat: begin working on minting page
kingsley-einstein Oct 5, 2024
2182135
chore: small update to let the guys know I'm still on this
kingsley-einstein Oct 11, 2024
e320389
chore: update ui
kingsley-einstein Oct 12, 2024
cb8adb1
chore: add tailwindcss for convenience
kingsley-einstein Oct 19, 2024
0addf0f
chore: update minting page
kingsley-einstein Oct 19, 2024
7e705c1
chore: update mint.ts
kingsley-einstein Oct 22, 2024
6a0e398
chore: made updates
kingsley-einstein Oct 23, 2024
67f5d1a
chore: finish minting page
kingsley-einstein Oct 25, 2024
5be5157
chore: finish
kingsley-einstein Oct 25, 2024
393f476
chore: finish
kingsley-einstein Oct 26, 2024
e0a6a68
chore: made requested changes
kingsley-einstein Oct 27, 2024
e8ecee2
chore: effect changes
kingsley-einstein Oct 28, 2024
c10d37c
chore: effect changes
kingsley-einstein Oct 28, 2024
ef28c3e
feat: add QA helper scripts
rndquu Oct 29, 2024
b444135
Merge pull request #1 from rndquu/feat/qa-helper-scripts
kingsley-einstein Oct 30, 2024
6995d6e
chore: apply requested changes
kingsley-einstein Oct 30, 2024
d7baf39
feat: add increase UUSD thresholds helper script
rndquu Oct 31, 2024
fbdf50a
Merge pull request #2 from rndquu/feat/add-increase-uusd-thresholds
kingsley-einstein Oct 31, 2024
4e0aeb5
chore: apply required changes
kingsley-einstein Oct 31, 2024
8bdc118
chore: apply required changes
kingsley-einstein Oct 31, 2024
1deeb47
chore: made fixes
kingsley-einstein Nov 8, 2024
809ec2e
fix: knip issues
kingsley-einstein Nov 8, 2024
283223e
feat: add UBQ fund test script
rndquu Nov 11, 2024
6fe4bf0
Merge pull request #3 from rndquu/chore/add-ubq-fund-helper
kingsley-einstein Nov 11, 2024
f07a96b
chore: remove hardcoded decimals
kingsley-einstein Nov 11, 2024
f3e8c47
chore: implement updates
kingsley-einstein Nov 29, 2024
0e3599d
chore: implement updates
kingsley-einstein Nov 29, 2024
b34b57c
chore: implement requested updates
kingsley-einstein Dec 4, 2024
2baee72
chore: implement updates
kingsley-einstein Dec 12, 2024
7a2b220
chore: implement updates
kingsley-einstein Dec 12, 2024
ec3a5c7
chore: add runtime type validation
0x4007 Dec 13, 2024
0764687
chore: implement ui fixes
kingsley-einstein Dec 13, 2024
0333eaf
chore: implement ui fixes
kingsley-einstein Dec 13, 2024
61d1096
Merge branch 'development' of github.com:kingsley-einstein/uusd.ubq.f…
kingsley-einstein Dec 13, 2024
4302bb4
chore: fix buttons display issue
kingsley-einstein Dec 17, 2024
608ca63
chore: drop slippage requirement
kingsley-einstein Dec 22, 2024
a89eecd
chore: fix issues
kingsley-einstein Dec 26, 2024
7a02d38
chore: fix issues
kingsley-einstein Dec 26, 2024
6e6c30e
chore: disable inputs when in allowance
kingsley-einstein Dec 29, 2024
8d2d31a
chore: remove setInterval polling
kingsley-einstein Dec 30, 2024
8328f62
chore: remove setInterval polling
kingsley-einstein Dec 30, 2024
d849153
chore: made requested updates
kingsley-einstein Jan 14, 2025
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
3 changes: 3 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as wallet from "./scripts/connect-wallet";
import * as ui from "./scripts/ui";
import * as mint from "./scripts/mint";
import * as redeem from "./scripts/redeem";

export async function mainModule() {
ui.initUiEvents();
Expand All @@ -10,6 +11,8 @@ export async function mainModule() {
await wallet.connectIfAuthorized();
await mint.initCollateralList();
kingsley-einstein marked this conversation as resolved.
Show resolved Hide resolved
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mint and redeem collateralList is the same, but idk if its worth asking for this change at this point

await mint.initUiEvents();
await redeem.initCollateralList();
await redeem.initUiEvents();
}

mainModule()
Expand Down
12 changes: 12 additions & 0 deletions src/scripts/connect-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ export async function connectWallet(providerType: "metamask" | "trust" | "coinba
if (providersModal.open) {
providersModal.close();
}

const currentChain = await provider.request<string>({ method: "eth_chainId" });

if (currentChain !== "0x1") {
await provider.request<null>({ method: "wallet_switchEthereumChain", params: [{ chainId: "0x1" }] });
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is bad hardcoding, was hard to spot

Copy link

@zugdev zugdev Jan 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use publicClient.chain.id.toString() instead

}
} catch (error) {
updateConnectButtonText("");
}
Expand Down Expand Up @@ -118,6 +124,12 @@ export async function connectIfAuthorized() {
updateConnectButtonText(truncateString(account as string));
connectPrompt.classList.add("hidden");
whiteContainer.classList.replace("hidden", "flex");

const currentChain = await provider.request<string>({ method: "eth_chainId" });

if (currentChain !== "0x1") {
await provider.request<null>({ method: "wallet_switchEthereumChain", params: [{ chainId: "0x1" }] });
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use publicClient.chain.id.toString() instead of hardcoding chainId

}
}
}
}
Expand Down
46 changes: 45 additions & 1 deletion src/scripts/faucet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const publicClient = createPublicClient({
transport: http(),
});

const CLIENT_OR_ACCOUNT_ERROR = "Client or account not initialized";

export async function getAllCollaterals() {
kingsley-einstein marked this conversation as resolved.
Show resolved Hide resolved
const contract = getContract({
abi,
Expand Down Expand Up @@ -98,6 +100,48 @@ export async function mintDollar(collateralIndex: bigint, dollarAmount: bigint,
return Promise.reject(error);
}
} else {
return Promise.reject(new Error("Client or account not initialized"));
return Promise.reject(new Error(CLIENT_OR_ACCOUNT_ERROR));
}
}

export async function redeemDollar(collateralIndex: bigint, dollarAmount: bigint) {
const client = getConnectedClient();

if (client !== null && client.account) {
try {
return await client.writeContract({
abi,
address,
functionName: "redeemDollar",
args: [collateralIndex, dollarAmount, BigInt(0), BigInt(0)],
kingsley-einstein marked this conversation as resolved.
Show resolved Hide resolved
chain: mainnet,
account: client.account,
});
} catch (error) {
return Promise.reject(error);
}
} else {
return Promise.reject(new Error(CLIENT_OR_ACCOUNT_ERROR));
}
}

export async function collectionRedemption(collateralIndex: bigint) {
const client = getConnectedClient();

if (client !== null && client.account) {
try {
return await client.writeContract({
abi,
address,
functionName: "collectRedemption",
args: [collateralIndex],
chain: mainnet,
account: client.account,
});
} catch (error) {
return Promise.reject(error);
}
} else {
return Promise.reject(new Error(CLIENT_OR_ACCOUNT_ERROR));
}
}
21 changes: 10 additions & 11 deletions src/scripts/mint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ let isOneToOne = false;

const collateralRecord: Record<string | number, `0x${string}`> = {};
const toastActions = new ToastActions();
const pubClient = createPublicClient({
kingsley-einstein marked this conversation as resolved.
Show resolved Hide resolved
chain: mainnet,
transport: http(),
});

(() => {
setInterval(() => {
Expand All @@ -30,11 +34,6 @@ const toastActions = new ToastActions();
})();

void (async () => {
const pubClient = createPublicClient({
chain: mainnet,
transport: http(),
});

pubClient.watchBlocks({
onBlock: async (block) => {
toastActions.showToast({
Expand Down Expand Up @@ -135,19 +134,19 @@ export async function initUiEvents() {
}

if (governanceInput !== null) {
governanceInput.addEventListener("change", (ev) => {
governanceInput.addEventListener("input", (ev) => {
maxGovernanceIn = Number((ev.target as HTMLInputElement).value || "0");
});
}

if (dollarInput !== null) {
dollarInput.addEventListener("change", (ev) => {
dollarInput.addEventListener("input", (ev) => {
dollarAmount = Number((ev.target as HTMLInputElement).value || "0");
});
}

if (collateralInput !== null) {
collateralInput.addEventListener("change", (ev) => {
collateralInput.addEventListener("input", (ev) => {
maxCollateralIn = Number((ev.target as HTMLInputElement).value || "0");
});
}
Expand All @@ -167,7 +166,7 @@ export async function initUiEvents() {
const err = error as Error;
toastActions.showToast({
toastType: "error",
msg: err.message,
msg: err.name,
});
}
});
Expand All @@ -186,14 +185,14 @@ export async function initUiEvents() {
mintButton.disabled = false;
toastActions.showToast({
toastType: "success",
msg: `Successfully approved: <a href="https://etherscan.io/tx/${txHash}" target="_blank">View on explorer</a>`,
msg: `Successfully minted: <a href="https://etherscan.io/tx/${txHash}" target="_blank">View on explorer</a>`,
});
} catch (error) {
mintButton.disabled = false;
const err = error as Error;
toastActions.showToast({
toastType: "error",
msg: err.message,
msg: err.name,
});
}
});
Expand Down
124 changes: 124 additions & 0 deletions src/scripts/redeem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import { type WriteContractErrorType, createPublicClient, http, parseUnits } from "viem";
import { collectionRedemption, getAllCollaterals, getCollateralInformation, redeemDollar } from "./faucet";
import { collateralSelect, collectRedemptionButton, dollarInput, redeemDollarButton } from "./ui";
import { mainnet } from "viem/chains";
import { ToastActions } from "./toast";

let selectedCollateralIndex = 0;
let dollarAmount = 0;
let blockOfRedemption = BigInt(0);

const toastActions = new ToastActions();
const pubClient = createPublicClient({
chain: mainnet,
transport: http(),
});

(() => {
setInterval(() => {
if (redeemDollarButton !== null) {
redeemDollarButton.disabled = dollarAmount <= 0;
}
}, 500);
})();

void (async () => {
pubClient.watchBlocks({
onBlock: async (block) => {
const currentBlock = Number(block.number);
toastActions.showToast({
toastType: "info",
msg: `New block mined: ${currentBlock}`,
});

try {
const bOfRedemption = Number(blockOfRedemption);
collectRedemptionButton.disabled = bOfRedemption === 0 || currentBlock - bOfRedemption < 2;
} catch (error) {
const err = error as Error;
toastActions.showToast({
toastType: "error",
msg: err.message,
});
}
},
});
})();

export async function initCollateralList() {
if (collateralSelect !== null) {
const collaterals = await getAllCollaterals();
const collateralInformation = await Promise.all(collaterals.map(getCollateralInformation));

const options = collateralInformation.map((info) => {
const option = document.createElement("option");

option.value = String(info.index);
option.innerText = info.symbol;

return option;
});

options.forEach((option) => {
collateralSelect.appendChild(option);
});
}
}

export async function initUiEvents() {
if (collateralSelect !== null) {
collateralSelect.addEventListener("change", (ev) => {
selectedCollateralIndex = Number((ev.target as HTMLSelectElement).value);
});
}

if (dollarInput !== null) {
dollarInput.addEventListener("input", (ev) => {
dollarAmount = Number((ev.target as HTMLInputElement).value || "0");
});
}

if (redeemDollarButton !== null) {
redeemDollarButton.addEventListener("click", async () => {
try {
redeemDollarButton.disabled = true;
const dollarAmountBi = parseUnits(dollarAmount.toString(), 18);
kingsley-einstein marked this conversation as resolved.
Show resolved Hide resolved
const txHash = await redeemDollar(BigInt(selectedCollateralIndex), dollarAmountBi);
redeemDollarButton.disabled = false;
blockOfRedemption = await pubClient.getBlockNumber();
toastActions.showToast({
toastType: "success",
msg: `Successfully redeemed: <a href="https://etherscan.io/tx/${txHash}" target="_blank">View on explorer</a>`,
});
} catch (error) {
redeemDollarButton.disabled = false;
const err = error as WriteContractErrorType;
toastActions.showToast({
toastType: "error",
msg: err.name,
});
}
});
}

if (collectRedemptionButton !== null) {
collectRedemptionButton.addEventListener("click", async () => {
try {
collectRedemptionButton.disabled = true;
const txHash = await collectionRedemption(BigInt(selectedCollateralIndex));
collectRedemptionButton.disabled = false;
toastActions.showToast({
toastType: "success",
msg: `Successfully collected redemption: <a href="https://etherscan.io/tx/${txHash}" target="_blank">View on explorer</a>`,
});
} catch (error) {
collectRedemptionButton.disabled = false;
const err = error as WriteContractErrorType;
toastActions.showToast({
toastType: "error",
msg: err.name,
});
}
});
}
}
4 changes: 4 additions & 0 deletions src/scripts/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export const allowanceButton = document.querySelector("#allowance-button") as HT
export const mintButton = document.querySelector("#mint-button") as HTMLButtonElement;
export const governanceCheckBox = document.querySelector("#governance-check") as HTMLInputElement;

// Redeem UI
export const redeemDollarButton = document.querySelector("#redeem-dollar-button") as HTMLButtonElement;
export const collectRedemptionButton = document.querySelector("#collect-redemption-button") as HTMLButtonElement;

// Providers
const metamaskConnectButton = document.querySelector("#mm-button") as HTMLButtonElement;
const cbConnectButton = document.querySelector("#cb-button") as HTMLButtonElement;
Expand Down
6 changes: 2 additions & 4 deletions static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
<div class="hidden lg:flex w-full border-b border-b-[#fff] justify-evenly items-center gap-3 py-3">
<a href="/">Home</a>
<a href="/mint.html">Mint</a>
<a href="#">Redeem</a>
<a href="#">Stake</a>
<a href="/redeem.html">Redeem</a>
<button class="btn btn-primary btn-md px-3 py-2" connectButton></button>
</div>

Expand All @@ -22,8 +21,7 @@
<ul class="menu min-h-screen w-64 bg-[#000] px-4 py-5">
<li><a href="/">Home</a></li>
<li><a href="/mint.html">Mint</a></li>
<li><a href="#">Redeem</a></li>
<li><a href="#">Stake</a></li>
<li><a href="/redeem.html">Redeem</a></li>
<li class="w-full">
<button class="btn btn-primary btn-sm px-3 py-2 w-full" connectButton></button>
</li>
Expand Down
14 changes: 6 additions & 8 deletions static/mint.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
<div class="hidden lg:flex w-full border-b border-b-[#fff] justify-evenly items-center gap-3 py-3">
<a href="/">Home</a>
<a href="/mint.html">Mint</a>
<a href="#">Redeem</a>
<a href="#">Stake</a>
<a href="/redeem.html">Redeem</a>
<button class="btn btn-primary btn-md px-3 py-2" connectButton></button>
</div>

Expand All @@ -25,8 +24,7 @@
<ul class="menu min-h-screen w-64 bg-[#000] px-4 py-5">
<li><a href="/">Home</a></li>
<li><a href="/mint.html">Mint</a></li>
<li><a href="#">Redeem</a></li>
<li><a href="#">Stake</a></li>
<li><a href="/redeem.html">Redeem</a></li>
<li class="w-full">
<button class="btn btn-primary btn-sm px-3 py-2 w-full" connectButton></button>
</li>
Expand Down Expand Up @@ -411,15 +409,15 @@
</select>
<label class="input input-bordered input-primary flex items-center gap-2 capitalize w-full py-1">
dollar amount
<input class="grow" type="number" />
<input id="dollar-input" class="grow" type="number" />
</label>
<label class="input input-bordered input-primary flex items-center gap-2 capitalize w-full py-1">
max. collateral amount
<input class="grow" type="number" />
<input id="collateral-input" class="grow" type="number" />
</label>
<label id="governance-input" class="input input-bordered input-primary flex items-center gap-2 capitalize w-full py-1">
<label class="input input-bordered input-primary flex items-center gap-2 capitalize w-full py-1">
max. governance amount
<input class="grow" type="number" />
<input id="governance-input" class="grow" type="number" />
</label>
<label class="label cursor-pointer w-full">
<span class="label-text font-[500]">Mint with governance</span>
Expand Down
Loading