Skip to content

Commit

Permalink
fix burn/yeet vouchers
Browse files Browse the repository at this point in the history
  • Loading branch information
zkSoju committed Mar 27, 2023
1 parent 0cbd98b commit 072e646
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 42 deletions.
66 changes: 47 additions & 19 deletions components/AmmTabContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import useDammData from "../lib/hooks/data/useDammData";
import useMint from "../lib/hooks/mint/useMint";
import useSwap from "../lib/hooks/swap/useSwap";
import useSyncL1 from "../lib/hooks/sync/useSyncL1";
import { ApprovalState } from "../lib/hooks/useApproval";
import useApproval, { ApprovalState } from "../lib/hooks/useApproval";
import { useChainDefaults } from "../lib/hooks/useDefaults";
import useTokenApproval from "../lib/hooks/useTokenApproval";
import { formatCurrencyAmount } from "../lib/utils/formatCurrencyAmount";
Expand All @@ -20,7 +20,7 @@ import {
formatTransactionAmount,
} from "../lib/utils/formatNumbers";
import { ChainId } from "../sdk";
import { DVE_LP } from "../sdk/constants";
import { DVE_LP, PAIR_ADDRESS } from "../sdk/constants";
import { useBurnStore } from "../state/burn/useBurnStore";
import { useDerivedBurnInfo } from "../state/burn/useDerivedBurnInfo";
import { useDerivedMintInfo } from "../state/mint/useDerivedMintInfo";
Expand Down Expand Up @@ -163,13 +163,19 @@ const SwapTabContent = () => {
currencyBalances: burnBalances,
} = useDerivedBurnInfo();

const { approve: approveVoucherA, state: approveVoucherStateA } =
useTokenApproval(burnAmounts[Field.CURRENCY_A]);
const { approve: approveVoucherA, state: approveVoucherStateA } = useApproval(
burnAmounts[Field.CURRENCY_A],
PAIR_ADDRESS[expectedChainId as ChainId]
);

const { approve: approveVoucherB, state: approveVoucherStateB } =
useTokenApproval(burnAmounts[Field.CURRENCY_B]);
console.log(PAIR_ADDRESS[expectedChainId as ChainId], approveVoucherStateA);

const { burn } = useBurn(
const { approve: approveVoucherB, state: approveVoucherStateB } = useApproval(
burnAmounts[Field.CURRENCY_B],
PAIR_ADDRESS[expectedChainId as ChainId]
);

const { burn, yeet } = useBurn(
burnAmounts[Field.CURRENCY_A],
burnAmounts[Field.CURRENCY_B],
approveVoucherStateA,
Expand Down Expand Up @@ -212,15 +218,19 @@ const SwapTabContent = () => {
burn?.();
};

const handleYeet = () => {
yeet?.();
};

/////////////////////////////

const { data } = useDammData(
const { data: dammData } = useDammData(
currencies[Field.CURRENCY_A],
currencies[Field.CURRENCY_B],
DVE_LP[ChainId.ETHEREUM_GOERLI]
);

const { data: ammData } = useAmmData(
const { data: ammData, balances } = useAmmData(
currencies[Field.CURRENCY_A],
currencies[Field.CURRENCY_B],
expectedChainId
Expand Down Expand Up @@ -340,9 +350,14 @@ const SwapTabContent = () => {

<Tabs.Content value="tab4">
<TabContentContainer>
<p className="mb-2 text-xs uppercase tracking-widest text-white/50">
Available Claims
</p>
<div className="mb-2 flex justify-between">
<p className="text-xs uppercase tracking-widest text-white/50">
Available Claims
</p>
<p className="mr-4 text-xs uppercase tracking-widest text-white/50">
L1 | L2
</p>
</div>
<div className="mb-2 flex w-full items-center justify-between rounded-sm border-l-2 border-sky-400 bg-gradient-to-r from-sky-400/5 to-transparent p-4 py-2">
<div className="flex items-center">
<div className="relative mr-4 h-4 w-4">
Expand All @@ -353,7 +368,8 @@ const SwapTabContent = () => {
</p>
</div>
<p className="text-sm text-white">
{data?.marked0 && formatCurrencyAmount(data.marked0, 6)}
{dammData?.marked0 && formatCurrencyAmount(dammData.marked0, 6)} |{" "}
{balances?.[0] && formatCurrencyAmount(balances[0], 6)}
</p>
</div>
<div className="mb-2 flex w-full items-center justify-between rounded-sm border-l-2 border-sky-400 bg-gradient-to-r from-sky-400/5 to-transparent p-4 py-2">
Expand All @@ -366,7 +382,8 @@ const SwapTabContent = () => {
</p>
</div>
<p className="text-sm text-white">
{data?.marked1 && formatCurrencyAmount(data.marked1, 6)}
{dammData?.marked1 && formatCurrencyAmount(dammData.marked1, 6)} |{" "}
{balances?.[1] && formatCurrencyAmount(balances[1], 6)}
</p>
</div>
<div className="mb-2 mt-2 h-px w-full bg-white/5" />
Expand Down Expand Up @@ -395,18 +412,23 @@ const SwapTabContent = () => {
>
{(() => {
if (
!data?.marked0 ||
!data?.marked1 ||
!dammData?.marked0 ||
!dammData?.marked1 ||
!balances ||
!balances[0] ||
!balances[1] ||
!burnAmounts[Field.CURRENCY_A] ||
!burnAmounts[Field.CURRENCY_B]
) {
return <Button disabled text="Enter an amount" />;
}
if (
burnAmounts[Field.CURRENCY_A].greaterThan(data.marked0) ||
burnAmounts[Field.CURRENCY_B].greaterThan(data.marked1)
(burnAmounts[Field.CURRENCY_A].greaterThan(balances[0]) ||
burnAmounts[Field.CURRENCY_B].greaterThan(balances[1])) &&
(burnAmounts[Field.CURRENCY_A].greaterThan(dammData.marked0) ||
burnAmounts[Field.CURRENCY_B].greaterThan(dammData.marked1))
) {
return <Button disabled text="Sync before" />;
return <Button disabled text="Insufficient earmarked" />;
}
if (approveVoucherStateA === ApprovalState.NOT_APPROVED) {
return (
Expand All @@ -424,6 +446,12 @@ const SwapTabContent = () => {
/>
);
}
if (
burnAmounts[Field.CURRENCY_A].lessThan(balances[0]) ||
burnAmounts[Field.CURRENCY_B].lessThan(balances[1])
) {
return <Button onClick={handleYeet} text="Yeet vouchers" />;
}
})()}
</InteractButton>
</TabContentContainer>
Expand Down
2 changes: 1 addition & 1 deletion components/DammTabContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ const DammTabContent = () => {
<div className="mb-4 flex items-center">
<BiRefresh className="mr-4 rounded-sm border border-white/10 p-2 text-4xl text-white" />
<div className="flex flex-col">
<h4 className="text-white">Syncing</h4>
<h4 className="text-white">Syncs Incoming</h4>
<p className="text-xs text-white/50">
Update reserves with L2 AMM
</p>
Expand Down
25 changes: 25 additions & 0 deletions lib/hooks/burn/useBurn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import { useNetwork } from "wagmi";
import { ChainId, Currency, CurrencyAmount, PAIR_ADDRESS } from "../../../sdk";
import {
usePairBurnVouchers,
usePairYeetVouchers,
usePreparePairBurnVouchers,
usePreparePairYeetVouchers,
} from "../../../src/generated";
import { ApprovalState } from "../useApproval";

Expand All @@ -15,6 +17,7 @@ export default function useBurn(
approvalState2: ApprovalState | undefined
): {
burn: () => void;
yeet: () => void;
} {
const { chain } = useNetwork();

Expand Down Expand Up @@ -47,7 +50,29 @@ export default function useBurn(

const { write } = usePairBurnVouchers(config);

const { config: yeetConfig } = usePreparePairYeetVouchers({
address: pairAddress as `0x${string}`,
args: [
BigNumber.from(voucher1ToBurn?.quotient.toString() || 0),
BigNumber.from(voucher2ToBurn?.quotient.toString() || 0),
],
enabled:
!!voucher1ToBurn &&
!!voucher2ToBurn &&
approvalState1 === ApprovalState.APPROVED &&
approvalState2 === ApprovalState.APPROVED,
});

console.log(
"yeetConfig",
voucher1ToBurn?.quotient.toString(),
voucher2ToBurn?.quotient.toString()
);

const { write: yeetWrite } = usePairYeetVouchers(yeetConfig);

return {
burn: () => write?.(),
yeet: () => yeetWrite?.(),
};
}
19 changes: 17 additions & 2 deletions lib/hooks/data/useAmmData.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { useContractReads, useNetwork } from "wagmi";
import { pairAbi } from "../../../abis/Pair";
import { ChainId, Currency, CurrencyAmount, PAIR_ADDRESS } from "../../../sdk";
import {
ChainId,
Currency,
CurrencyAmount,
PAIR_ADDRESS,
Token,
} from "../../../sdk";
import { useTokenBalances } from "../useTokenBalance";

export default function useAmmData(
currency1: Currency | undefined,
Expand All @@ -11,6 +18,7 @@ export default function useAmmData(
reserve0: CurrencyAmount<Currency> | undefined;
reserve1: CurrencyAmount<Currency> | undefined;
} | null;
balances: (CurrencyAmount<Currency> | undefined)[];
} {
const { chain } = useNetwork();

Expand Down Expand Up @@ -47,12 +55,19 @@ export default function useAmmData(
watch: true,
});

const amounts = useTokenBalances(
[currency1 as Token, currency2 as Token],
pairAddress
);

if (!data?.[0] || !data?.[1] || !currency1 || !currency2)
return { data: null };
return { data: null, balances: [] };

return {
data: {
reserve0: CurrencyAmount.fromRawAmount(currency1, data[0].toString()),
reserve1: CurrencyAmount.fromRawAmount(currency1, data[1].toString()),
},
balances: amounts,
};
}
10 changes: 5 additions & 5 deletions lib/hooks/data/useDammData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
Currency,
CurrencyAmount,
DOVE_ADDRESS,
LZ_CHAIN,
HL_DOMAIN,
} from "../../../sdk";
import { doveABI } from "../../../src/generated";

Expand All @@ -30,14 +30,14 @@ export default function useDammData(
chainId: ChainId.ETHEREUM_GOERLI,
};

const lzChainId = useMemo(() => {
const hlDomainId = useMemo(() => {
if (!chain) return;

if (chain.id === ChainId.ARBITRUM_GOERLI) {
return LZ_CHAIN[ChainId.ARBITRUM_GOERLI];
return HL_DOMAIN[ChainId.ARBITRUM_GOERLI];
}
if (chain.id === ChainId.POLYGON_MUMBAI) {
return LZ_CHAIN[ChainId.POLYGON_MUMBAI];
return HL_DOMAIN[ChainId.POLYGON_MUMBAI];
} else {
return 0;
}
Expand All @@ -60,7 +60,7 @@ export default function useDammData(
{
...doveContract,
functionName: "marked",
args: [lzChainId ?? 0],
args: [hlDomainId ?? 0],
},
],
watch: true,
Expand Down
2 changes: 0 additions & 2 deletions lib/hooks/provide/useProvideLiquidity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ export default function useProvideLiquidity(
approvalState2 === ApprovalState.APPROVED,
});

console.log(config);

const { write } = useL1RouterAddLiquidity(config);

return {
Expand Down
2 changes: 0 additions & 2 deletions lib/hooks/sync/useFinalizeSyncL1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ export default function useFinalizeSyncL1(
skip: !expectedChainId,
});

console.log("data", data);

// TODO: use proper syncID
const { config } = usePrepareDoveFinalizeSyncFromL2({
address: DOVE_ADDRESS[ChainId.ETHEREUM_GOERLI] as `0x${string}`,
Expand Down
2 changes: 0 additions & 2 deletions lib/hooks/sync/useSyncL1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ export default function useSyncL1(): {
}
}, [chain]);

console.log(ammAddress);

const { config } = usePreparePairSyncToL1({
address: ammAddress as `0x${string}`,
args: [ethers.utils.parseEther("0.1"), ethers.utils.parseEther("0.2")],
Expand Down
9 changes: 7 additions & 2 deletions lib/hooks/useApproval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default function useApproval(

const { config: approvalConfig } = usePrepareErc20Approve({
address: amountToApprove?.currency?.isToken
? amountToApprove.currency.address as `0x${string}`
? (amountToApprove.currency.address as `0x${string}`)
: undefined,
args: [
spender as `0x${string}`,
Expand All @@ -37,6 +37,11 @@ export default function useApproval(
approvalState === ApprovalState.NOT_APPROVED,
});

console.log(
amountToApprove?.currency?.isToken &&
(amountToApprove.currency.address as `0x${string}`)
);

const { write } = useErc20Approve(approvalConfig);

return {
Expand All @@ -56,7 +61,7 @@ function useApprovalStateForSpender(
? (amountToApprove.currency.address as `0x${string}`)
: undefined,
args: [address as `0x${string}`, spender as `0x${string}`],
watch: true,
watch: true,
});

if (!amountToApprove) return ApprovalState.UNKNOWN;
Expand Down
7 changes: 0 additions & 7 deletions lib/hooks/withdraw/useWithdrawLiquidity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,6 @@ export default function useWithdrawLiquidity(
enabled: !!token0Data && !!token1Data && !!amount,
});

console.log(
token0Data,
token1Data,
amount?.numerator.toString(),
quotedData?.amountA
);

const { config } = usePrepareL1RouterRemoveLiquidity({
address: L1_ROUTER_ADDRESS[ChainId.ETHEREUM_GOERLI] as `0x${string}`,
args: [
Expand Down

1 comment on commit 072e646

@vercel
Copy link

@vercel vercel bot commented on 072e646 Mar 27, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

dove-interface – ./

dove-interface-zksoju.vercel.app
dove-interface.vercel.app
dove-interface-git-main-zksoju.vercel.app

Please sign in to comment.