Skip to content

Commit

Permalink
improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
JayJay1024 committed Oct 16, 2023
1 parent 8da70e8 commit 93ff353
Show file tree
Hide file tree
Showing 72 changed files with 1,905 additions and 2,412 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 1 addition & 9 deletions packages/apps/public/images/token/eth.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions packages/apps/src/abi/faucet.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
},
{
"inputs": [{ "internalType": "address", "name": "", "type": "address" }],
"name": "allowFault",
"name": "allowFaucet",
"outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }],
"stateMutability": "view",
"type": "function"
Expand Down Expand Up @@ -100,7 +100,7 @@
},
{
"inputs": [{ "internalType": "uint256", "name": "amount", "type": "uint256" }],
"name": "fault",
"name": "faucet",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
Expand All @@ -117,7 +117,7 @@
},
{
"inputs": [],
"name": "maxFaultAllowed",
"name": "maxFaucetAllowed",
"outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }],
"stateMutability": "view",
"type": "function"
Expand Down Expand Up @@ -149,7 +149,7 @@
{ "inputs": [], "name": "renounceOwnership", "outputs": [], "stateMutability": "nonpayable", "type": "function" },
{
"inputs": [{ "internalType": "uint256", "name": "allowed", "type": "uint256" }],
"name": "setMaxFaultAllowed",
"name": "setMaxFaucetAllowed",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
Expand Down
16 changes: 16 additions & 0 deletions packages/apps/src/app/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,22 @@ body {
min-height: calc(100vh - 64px - 58px);
@apply mt-[64px] py-5;
}

.user-connect-wallet {
@apply bg-primary gap-middle px-large flex h-9 shrink-0 items-center rounded transition hover:opacity-80 lg:h-8 lg:hover:opacity-80 lg:active:translate-y-1;
}

.user-dropdown-item {
@apply gap-middle px-large py-small inline-flex items-center text-start transition hover:bg-white/10 lg:active:translate-y-1;
}

.tooltip-text {
@apply text-xs font-normal text-white;
}

.button {
@apply flex h-8 items-center justify-center lg:h-9 text-sm;
}
}

.page-bg {
Expand Down
5 changes: 4 additions & 1 deletion packages/apps/src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import TransferProvider from "@/providers/transfer-provider";
import { Metadata } from "next";
import dynamic from "next/dynamic";

Expand All @@ -12,7 +13,9 @@ export default function Home() {
return (
<main className="app-main">
<div className="px-middle container mx-auto lg:py-12">
<Transfer />
<TransferProvider>
<Transfer />
</TransferProvider>
</div>
</main>
);
Expand Down
55 changes: 30 additions & 25 deletions packages/apps/src/bridges/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export abstract class BaseBridge {
protected readonly targetNativeToken?: Token;
protected readonly crossInfo?: CrossChain;

protected readonly sourcePublicClient: ViemPublicClient;
protected readonly targetPublicClient: ViemPublicClient;
protected readonly sourcePublicClient: ViemPublicClient | undefined;
protected readonly targetPublicClient: ViemPublicClient | undefined;
protected readonly publicClient?: WagmiPublicClient; // The public client to which the wallet is connected
protected readonly walletClient?: WalletClient | null;

Expand All @@ -50,9 +50,6 @@ export abstract class BaseBridge {
c.target.network === args.targetChain?.network &&
c.target.symbol === args.targetToken?.symbol,
);
if (args.logo) {
this.logo = args.logo;
}

this.sourceChain = args.sourceChain;
this.targetChain = args.targetChain;
Expand All @@ -63,8 +60,10 @@ export abstract class BaseBridge {

this.walletClient = args.walletClient;
this.publicClient = args.publicClient;
this.sourcePublicClient = createPublicClient({ chain: args.sourceChain, transport: http() });
this.targetPublicClient = createPublicClient({ chain: args.targetChain, transport: http() });
if (args.sourceChain && args.targetChain) {
this.sourcePublicClient = createPublicClient({ chain: args.sourceChain, transport: http() });
this.targetPublicClient = createPublicClient({ chain: args.targetChain, transport: http() });
}
}

getLogo() {
Expand Down Expand Up @@ -97,8 +96,16 @@ export abstract class BaseBridge {
: undefined;
}

protected async validateNetwork(position: "source" | "target") {
const chain = position === "source" ? this.sourceChain : this.targetChain;
if (chain?.id !== (await this.publicClient?.getChainId())) {
throw new Error("Wrong network");
}
}

async getFee(_?: {
baseFee?: bigint;
protocolFee?: bigint;
liquidityFeeRate?: bigint;
transferAmount?: bigint;
}): Promise<{ value: bigint; token: Token } | undefined> {
Expand Down Expand Up @@ -140,36 +147,38 @@ export abstract class BaseBridge {
}

async getSourceBalance(address: Address) {
if (this.sourceToken) {
if (this.sourceToken && this.sourcePublicClient) {
return this.getBalance(address, this.sourceToken, this.sourcePublicClient);
}
}

async getTargetBalance(address: Address) {
if (this.targetToken) {
if (this.targetToken && this.targetPublicClient) {
return this.getBalance(address, this.targetToken, this.targetPublicClient);
}
}

private async getAllowance(owner: Address, spender: Address, token: Token, publicClient: ViemPublicClient) {
const abi = (await import("../abi/erc20.json")).default;
const value = (await publicClient.readContract({
address: token.address,
abi,
functionName: "allowance",
args: [owner, spender],
})) as unknown as bigint;
return { value, token };
if (token.type === "erc20") {
const abi = (await import("../abi/erc20.json")).default;
const value = (await publicClient.readContract({
address: token.address,
abi,
functionName: "allowance",
args: [owner, spender],
})) as unknown as bigint;
return { value, token };
}
}

async getSourceAllowance(owner: Address) {
if (this.contract && this.sourceToken) {
if (this.contract && this.sourceToken && this.sourcePublicClient) {
return await this.getAllowance(owner, this.contract.sourceAddress, this.sourceToken, this.sourcePublicClient);
}
}

async getTargetAllowance(owner: Address) {
if (this.contract && this.targetToken) {
if (this.contract && this.targetToken && this.targetPublicClient) {
return await this.getAllowance(owner, this.contract.targetAddress, this.targetToken, this.targetPublicClient);
}
}
Expand All @@ -190,18 +199,14 @@ export abstract class BaseBridge {
}

async sourceApprove(amount: bigint, owner: Address) {
if (this.sourceChain?.id !== (await this.publicClient?.getChainId())) {
throw new Error("Wrong network");
}
await this.validateNetwork("source");
if (this.sourceToken && this.contract) {
return this.approve(amount, owner, this.contract.sourceAddress, this.sourceToken);
}
}

async targetApprove(amount: bigint, owner: Address) {
if (this.targetChain?.id !== (await this.publicClient?.getChainId())) {
throw new Error("Wrong network");
}
await this.validateNetwork("target");
if (this.targetToken && this.contract) {
return this.approve(amount, owner, this.contract.targetAddress, this.targetToken);
}
Expand Down
31 changes: 14 additions & 17 deletions packages/apps/src/bridges/helix-lpbridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@ export class HelixLpBridge extends BaseBridge {
super(args);
this.initContract();

if (!args.logo) {
this.logo = {
horizontal: "helix-horizontal.svg",
symbol: "helix-symbol.svg",
};
}
this.logo = args.logo ?? {
horizontal: "helix-horizontal.svg",
symbol: "helix-symbol.svg",
};
this.name = "Helix(Fusion)";
this.estimateTime = { min: 1, max: 3 };
}
Expand All @@ -48,7 +46,7 @@ export class HelixLpBridge extends BaseBridge {
amount: bigint,
options?: { totalFee: bigint },
): Promise<TransactionReceipt | undefined> {
if (options && this.contract && this.crossInfo && this.walletClient) {
if (options && this.contract && this.crossInfo && this.walletClient && this.publicClient) {
const nonce = BigInt(Date.now()) + this.prefix;

const { args, value, functionName } =
Expand Down Expand Up @@ -81,17 +79,15 @@ export class HelixLpBridge extends BaseBridge {
value,
gas: this.getTxGasLimit(),
});
return await this.sourcePublicClient.waitForTransactionReceipt({ hash });
return await this.publicClient.waitForTransactionReceipt({ hash });
}
}

/**
* On target chain
*/
async refund(record: HistoryRecord) {
if (this.targetChain?.id !== (await this.publicClient?.getChainId())) {
throw new Error("Wrong network");
}
await this.validateNetwork("target");

if (this.contract && this.publicClient && this.walletClient) {
const { address } =
Expand Down Expand Up @@ -124,7 +120,7 @@ export class HelixLpBridge extends BaseBridge {
}

private async getBridgeFee() {
if (this.contract) {
if (this.contract && this.sourcePublicClient) {
const address = this.crossInfo?.action === "issue" ? this.contract.sourceAddress : this.contract.targetAddress;
const abi = (await import("@/abi/lpbridge.json")).default;
return this.sourcePublicClient.readContract({
Expand All @@ -136,7 +132,7 @@ export class HelixLpBridge extends BaseBridge {
}

async speedUp(record: HistoryRecord, fee: bigint) {
if (this.contract && this.walletClient) {
if (this.contract && this.walletClient && this.publicClient) {
const transferId = record.id.split("-").slice(-1);

const { args, value, functionName } =
Expand All @@ -162,16 +158,17 @@ export class HelixLpBridge extends BaseBridge {
value,
gas: this.getTxGasLimit(),
});
return await this.sourcePublicClient.waitForTransactionReceipt({ hash });
return await this.publicClient.waitForTransactionReceipt({ hash });
}
}

async getFee(args?: { baseFee?: bigint; liquidityFeeRate?: bigint; transferAmount?: bigint }) {
if (this.sourceToken && this.crossInfo?.baseFee) {
let value = ((args?.transferAmount || 0n) * BigInt(this.feePercent * 1000)) / 1000n + this.crossInfo.baseFee;
if (this.sourceToken && this.crossInfo?.baseFee && this.targetPublicClient) {
let value =
((args?.transferAmount || 0n) * BigInt(Math.floor(this.feePercent * 1000))) / 1000n + this.crossInfo.baseFee;

if (this.crossInfo.price) {
const gasPrice = await this.sourcePublicClient.getGasPrice();
const gasPrice = await this.targetPublicClient.getGasPrice();
const dynamicFee = gasPrice * this.crossInfo.price * this.relayGasLimit;
value += dynamicFee;
}
Expand Down
26 changes: 9 additions & 17 deletions packages/apps/src/bridges/helixbridge-dvmdvm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,10 @@ export class HelixBridgeDVMDVM extends BaseBridge {
this.initContract();
this.initSpecVersion();

if (args.logo) {
this.logo = {
horizontal: "helix-horizontal.svg",
symbol: "helix-symbol.svg",
};
}
this.logo = args.logo ?? {
horizontal: "helix-horizontal.svg",
symbol: "helix-symbol.svg",
};
this.name = "Helix(Legacy)";
}

Expand Down Expand Up @@ -73,9 +71,7 @@ export class HelixBridgeDVMDVM extends BaseBridge {
}

async lock(_: string, recipient: string, amount: bigint, options?: { totalFee: bigint }) {
if ((await this.publicClient?.getChainId()) !== this.sourceChain?.id) {
throw new Error("Wrong network");
}
await this.validateNetwork("source");

if (options && this.contract && this.specVersion && this.sourceToken && this.publicClient && this.walletClient) {
const { args, value, functionName } =
Expand Down Expand Up @@ -105,9 +101,7 @@ export class HelixBridgeDVMDVM extends BaseBridge {
}

async burn(_: string, recipient: string, amount: bigint, options?: { totalFee: bigint }) {
if ((await this.publicClient?.getChainId()) !== this.sourceChain?.id) {
throw new Error("Wrong network");
}
await this.validateNetwork("target");

if (options && this.contract && this.specVersion && this.sourceToken && this.publicClient && this.walletClient) {
const { args, value, functionName } =
Expand Down Expand Up @@ -150,9 +144,7 @@ export class HelixBridgeDVMDVM extends BaseBridge {
}

async refund(record: HistoryRecord) {
if (this.targetChain?.id !== (await this.publicClient?.getChainId())) {
throw new Error("Wrong network");
}
await this.validateNetwork("target");

if (this.contract && this.specVersion && this.publicClient && this.walletClient) {
const { abi, address, functionName } =
Expand Down Expand Up @@ -193,7 +185,7 @@ export class HelixBridgeDVMDVM extends BaseBridge {
}

async getFee() {
if (this.contract && this.sourceNativeToken) {
if (this.contract && this.sourceNativeToken && this.sourcePublicClient) {
const { abi, address } =
this.crossInfo?.action === "issue"
? { abi: (await import("@/abi/backing-dvmdvm.json")).default, address: this.contract.sourceAddress }
Expand All @@ -209,7 +201,7 @@ export class HelixBridgeDVMDVM extends BaseBridge {
}

async getDailyLimit() {
if (this.contract && this.sourceToken && this.targetToken) {
if (this.contract && this.sourceToken && this.targetToken && this.targetPublicClient) {
const { abi, address } =
this.crossInfo?.action === "redeem"
? { abi: (await import("@/abi/backing-dvmdvm.json")).default, address: this.contract.sourceAddress }
Expand Down
Loading

0 comments on commit 93ff353

Please sign in to comment.