Skip to content

Commit

Permalink
Spike/use proofs endpoint (#171)
Browse files Browse the repository at this point in the history
* use proofs endpoint WIP

* map _id to eventId

* add options for _id & eventId

* remove `sendHistoricalWithdrawEthereumRequest`

* use for fresh & historic withdrawEthereum requests

* use `sendWithdrawEthereumRequest`

* auto expand advanced if some unclaimed withdrawals
  • Loading branch information
aidan-starke authored Apr 21, 2022
1 parent 231df2f commit 850cad7
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 37 deletions.
15 changes: 12 additions & 3 deletions components/BridgeWithdrawAdvanced.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, VFC } from "react";
import { useEffect, useState, VFC } from "react";
import { IntrinsicElements } from "@/types";
import { css } from "@emotion/react";
import {
Expand All @@ -21,12 +21,21 @@ const BridgeWithdrawAdvanced: VFC<
advancedExpanded: expanded,
setAdvancedExpanded: setExpanded,
advancedMounted,
unclaimedWithdrawals,
updateUnclaimedWithdrawals,
}: any = useBridge();
const [firstRender, setFirstRender] = useState<boolean>(true);

useEffect(() => {
if (expanded) void updateUnclaimedWithdrawals();
}, [updateUnclaimedWithdrawals, expanded]);
if (expanded || firstRender) updateUnclaimedWithdrawals();
}, [expanded, firstRender, updateUnclaimedWithdrawals]);

useEffect(() => {
if (!unclaimedWithdrawals || !firstRender) return;

setExpanded(true);
setFirstRender(false);
}, [unclaimedWithdrawals, firstRender, setExpanded]);

return (
<div {...props} css={styles.root}>
Expand Down
14 changes: 2 additions & 12 deletions hooks/useHistoricalWithdrawRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import {
ensureEthereumChain,
sendWithdrawEthereumRequest,
} from "@/utils";
import { EthEventProof } from "@cennznet/api/derives/ethBridge/types";
import { EthyEventId } from "@cennznet/types";
import { useCallback } from "react";

export default function useHistoricalWithdrawRequest(): (
Expand All @@ -19,7 +17,6 @@ export default function useHistoricalWithdrawRequest(): (
const {
transferInput,
transferSelect,
transferMetaMaskAddress,
setTxIdle,
setTxPending,
setTxSuccess,
Expand All @@ -44,24 +41,18 @@ export default function useHistoricalWithdrawRequest(): (
setTxPending();
await ensureEthereumChain(extension);
await ensureBridgeWithdrawActive(api, metaMaskWallet);
const eventProof: EthEventProof = await api.derive.ethBridge.eventProof(
unclaimed.eventProofId as unknown as EthyEventId
);

console.log({ eventProof });

setTxPending({
relayerStatus: "EthereumConfirming",
});

sendWithdrawEthereumRequest(
api,
eventProof,
unclaimed.eventProof,
unclaimed.transferAmount,
unclaimed.transferAsset,
unclaimed.beneficiary,
metaMaskWallet.getSigner(),
eventProof.blockHash
metaMaskWallet.getSigner()
)
.then((withdrawTx) => {
withdrawTx.on("txHashed", () => {
Expand Down Expand Up @@ -111,7 +102,6 @@ export default function useHistoricalWithdrawRequest(): (
extension,
api,
metaMaskWallet,
transferMetaMaskAddress,
updateMetaMaskBalances,
updateCENNZBalances,
setTxSuccess,
Expand Down
11 changes: 11 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,15 @@ export interface WithdrawClaim {
transferAsset: BridgedEthereumToken;
transferAmount: Balance;
beneficiary: string;
eventProof: HistoricalEventProof;
}

export interface HistoricalEventProof {
_id?: string;
eventId?: string;
validatorSetId: string;
validators: [];
r: string[];
s: string[];
v: number[];
}
14 changes: 14 additions & 0 deletions utils/fetchUnclaimedEventProof.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { BRIDGE_RELAYER_URL } from "@/constants";
import { HistoricalEventProof } from "@/types";

export default async function fetchUnclaimedEventProof(
eventProofId: number
): Promise<HistoricalEventProof> {
return await fetch(`${BRIDGE_RELAYER_URL}/proofs/${eventProofId}`)
.then((response) => {
if (!response.ok) throw new Error("No event proof found");

return response.json();
})
.catch((err) => console.log(err.message));
}
11 changes: 10 additions & 1 deletion utils/fetchUnclaimedWithdrawals.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { Api } from "@cennznet/api";
import { BridgedEthereumToken, WithdrawClaim } from "@/types";
import { BRIDGE_RELAYER_URL } from "@/constants";
import { Balance, fetchBridgeTokens, getDaysHoursMinutes } from "@/utils";
import {
Balance,
fetchBridgeTokens,
fetchUnclaimedEventProof,
getDaysHoursMinutes,
} from "@/utils";

/**
* Fetch unclaimed withdraws for the selected account
Expand Down Expand Up @@ -39,6 +44,9 @@ export default async function fetchUnclaimedWithdrawals(
withdrawal.amount.trim(),
transferAsset
);
const eventProof = await fetchUnclaimedEventProof(
Number(withdrawal.proofId)
);

return {
assetId: Number(withdrawal.assetId),
Expand All @@ -48,6 +56,7 @@ export default async function fetchUnclaimedWithdrawals(
transferAsset: transferAsset as BridgedEthereumToken,
transferAmount,
beneficiary: withdrawal.beneficiary,
eventProof: { ...eventProof, eventId: eventProof._id },
};
})
);
Expand Down
1 change: 1 addition & 0 deletions utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export { default as trackPageView } from "@/utils/trackPageView";
export { default as getSellAssetExtrinsic } from "@/utils/getSellAssetExtrinsic";
export { default as selectMap } from "@/utils/selectMap";
export { default as fetchUnclaimedWithdrawals } from "@/utils/fetchUnclaimedWithdrawals";
export { default as fetchUnclaimedEventProof } from "@/utils/fetchUnclaimedEventProof";
export { default as CENNZTransaction } from "@/utils/CENNZTransaction";
export { default as EthereumTransaction } from "@/utils/EthereumTransaction";
export { default as waitForEventProof } from "@/utils/waitForEventProof";
Expand Down
46 changes: 25 additions & 21 deletions utils/sendWithdrawEthereumRequest.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,39 @@
import { BridgedEthereumToken } from "@/types";
import { Balance, EthereumTransaction, getBridgeContract } from "@/utils";
import getERC20PegContract from "@/utils/getERC20PegContract";
import { BridgedEthereumToken, HistoricalEventProof } from "@/types";
import {
Balance,
EthereumTransaction,
getERC20PegContract,
getBridgeContract,
} from "@/utils";
import { Api } from "@cennznet/api";
import { EthEventProof } from "@cennznet/api/derives/ethBridge/types";
import { BigNumber, ethers } from "ethers";
import { TransactionResponse } from "@ethersproject/abstract-provider";

export default async function sendWithdrawEthereumRequest(
api: Api,
eventProof: EthEventProof,
eventProof: EthEventProof | HistoricalEventProof,
transferAmount: Balance,
transferAsset: BridgedEthereumToken,
ethereumAddress: string,
signer: ethers.Signer,
blockHash?: string
signer: ethers.Signer
): Promise<EthereumTransaction> {
const notaryKeys = !!blockHash
? ((
await api.query.ethBridge.notaryKeys.at(blockHash)
).toJSON() as string[])
: ((await api.query.ethBridge.notaryKeys()).toJSON() as string[]);
let validators: string[];
if (!eventProof.validators) {
const notaryKeys = (
await api.query.ethBridge.notaryKeys()
).toJSON() as string[];

const validators = notaryKeys.map((validator) => {
if (
validator ===
"0x000000000000000000000000000000000000000000000000000000000000000000"
)
return ethers.constants.AddressZero;
validators = notaryKeys.map((validator) => {
if (
validator ===
"0x000000000000000000000000000000000000000000000000000000000000000000"
)
return ethers.constants.AddressZero;

return ethers.utils.computeAddress(validator);
});
return ethers.utils.computeAddress(validator);
});
}

const bridgeContract = getBridgeContract<"OnBehalf">(signer);
const pegContract = getERC20PegContract<"OnBehalf">(signer);
Expand All @@ -38,7 +42,7 @@ export default async function sendWithdrawEthereumRequest(
transferAsset.address,
transferAmount.toBigNumber(),
ethereumAddress,
{ ...eventProof, validators },
{ ...eventProof, validators: eventProof.validators ?? validators },
{ value: verificationFee }
);
const tx = new EthereumTransaction();
Expand All @@ -47,7 +51,7 @@ export default async function sendWithdrawEthereumRequest(
transferAsset.address,
transferAmount.toBigNumber(),
ethereumAddress,
{ ...eventProof, validators },
{ ...eventProof, validators: eventProof.validators ?? validators },
{ value: verificationFee, gasLimit: (gasFee.toNumber() * 1.02).toFixed() }
)
.then((pegTx: TransactionResponse) => {
Expand Down

0 comments on commit 850cad7

Please sign in to comment.