Skip to content

Commit

Permalink
feat: update icy vault fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
Polybius93 committed Nov 8, 2024
1 parent 7d991fe commit 1b7d2b1
Show file tree
Hide file tree
Showing 10 changed files with 430 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { useDispatch, useSelector } from 'react-redux';

import { Button, HStack, Image, Text, UseToastOptions, VStack, useToast } from '@chakra-ui/react';
import { useOneClickYieldPSBT } from '@hooks/use-icy-psbt.ts';
import { usePSBT } from '@hooks/use-psbt';
import {
BitcoinWalletContext,
BitcoinWalletContextState,
Expand Down Expand Up @@ -70,7 +69,7 @@ export function OneClickYieldContainer(): React.JSX.Element {
}
}

async function handleWithdraw(withdrawAmount: number, vaultUUID: string): Promise<void> {
async function handleWithdraw(withdrawAmount: number): Promise<void> {
try {
setIsSubmitting(true);
await handleSignWithdrawTransaction(withdrawAmount);
Expand All @@ -88,7 +87,7 @@ export function OneClickYieldContainer(): React.JSX.Element {
const transactionState: TransactionState = {
transactionStep: 0,
vaultType: '',
vaultUUID: '',
vault: undefined,
};
resetBitcoinWalletContext();
switch (activeTransaction) {
Expand All @@ -108,7 +107,7 @@ export function OneClickYieldContainer(): React.JSX.Element {
case 'mint':
return handleDeposit(assetAmount);
case 'burn':
return handleWithdraw(assetAmount, transactionInformation.vaultUUID);
return handleWithdraw(assetAmount);
default:
throw new Error('Invalid Transaction Type');
}
Expand All @@ -118,7 +117,7 @@ export function OneClickYieldContainer(): React.JSX.Element {
const transactionState: TransactionState = {
transactionStep: 1,
vaultType,
vaultUUID: '',
vault: undefined,
};
dispatch(oneClickYieldActions.setMintTransactionStep(transactionState));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function ActionButtonGroup({
if (vaultState === VaultState.PENDING) return false;

const isWithdrawButtonDisabled = vaultTotalLockedValue === 0;
const isDepositButtonDisabled = vaultTotalLockedValue === vaultTotalMintedValue;
const isDepositButtonDisabled = vaultTotalLockedValue !== vaultTotalMintedValue;

return (
<VStack w={'100%'}>
Expand Down
133 changes: 133 additions & 0 deletions src/app/functions/icy.functions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import { VaultDetails } from '@models/one-click-yield.models';
import { RawVault } from 'dlc-btc-lib/models';
import { ethers } from 'ethers';

export interface IntegrationInfo {
address: string; // Integration Address
strategy: string;
isActive: boolean;
totalShares: number;
supportedRewardTokens: string[];
vaults: string[];
rewardRatePerSecond: number;
lastRewardTime: number;
pendingRewards: number;
vaultAddress: string;
rewardTokenAddress: string;
}

export const fetchAllICYVaultsForUser = async (taprootPubKey: string): Promise<VaultDetails[]> => {
try {
const provider = new ethers.providers.StaticJsonRpcProvider('http://127.0.0.1:8545', undefined);

const poolMerchantContract = new ethers.Contract(
ethereumNetworkConfiguration.poolMerchant.address,
poolMerchantABI,
provider
);

const dlcManagerContract = new ethers.Contract(
ethereumNetworkConfiguration.dlcManager.address,
dlcManagerABI,
provider
);

const allVaultUUIDs = await poolMerchantContract.getVaultsByTaprootPubKey(taprootPubKey);

const vaultDetailsPromises = allVaultUUIDs.map(async (vaultUUID: string) => {
const details = await poolMerchantContract.getVaultDetails(vaultUUID);
const dlcVault: RawVault = await dlcManagerContract.getVault(vaultUUID);
return {
uuid: vaultUUID,
integration: details.integration.toString(),
shares: Number(details.shares),
valueMinted: Number(details.valueMinted),
allocated: Number(details.allocated),
unallocated: Number(details.unallocated),
rewardTokens: details.rewardTokens.map((token: string) => token.toString()),
lastClaimedAt: details.lastClaimedAt.map((time: bigint) => Number(time)),
harvestedRewards: details.harvestedRewards.map((reward: bigint) => Number(reward)),
pendingRewards: details.pendingRewards.map((reward: bigint) => Number(reward)),
dlcVault: {
uuid: dlcVault.uuid,
protocolContract: dlcVault.protocolContract,
timestamp: Number(dlcVault.timestamp),
valueLocked: Number(dlcVault.valueLocked),
valueMinted: Number(dlcVault.valueMinted),
creator: dlcVault.creator,
status: Number(dlcVault.status),
fundingTxId: dlcVault.fundingTxId,
closingTxId: dlcVault.closingTxId,
wdTxId: dlcVault.wdTxId,
btcFeeRecipient: dlcVault.btcFeeRecipient,
btcMintFeeBasisPoints: Number(dlcVault.btcMintFeeBasisPoints),
btcRedeemFeeBasisPoints: Number(dlcVault.btcRedeemFeeBasisPoints),
taprootPubKey: dlcVault.taprootPubKey,
},
};
});

return Promise.all(vaultDetailsPromises);
} catch (error) {
throw new Error(
`Error fetching Attestor Group Public Key from Ethereum Network [${ethereumNetwork}]: ${error}`
);
}
};

export const fetchIntegrationInfos = async (): Promise<IntegrationInfo[]> => {
try {
const provider = new ethers.providers.StaticJsonRpcProvider('http://127.0.0.1:8545', undefined);

const poolMerchantContract = new ethers.Contract(
ethereumNetworkConfiguration.poolMerchant.address,
poolMerchantABI,
provider
);

const activeIntegrations: string[] = await poolMerchantContract.getAllActiveIntegrations();
console.log('Active Integrations:', activeIntegrations);

const integrationDetailsPromises = activeIntegrations.map(
async (integrationAddress: string) => {
const integration = await poolMerchantContract.integrations(integrationAddress);
const vaults: string[] =
await poolMerchantContract.getIntegrationVaults(integrationAddress);
const integrationContract = new ethers.Contract(
integrationAddress,
integrationSampleABI,
provider
);

const rewardRatePerSecond = await integrationContract.rewardRatePerSecond();
const lastRewardTime = await integrationContract.lastRewardTime();
const pendingRewardsArray: number[] = await integrationContract.getPendingRewards();
const pendingRewards = pendingRewardsArray[0]; // assuming only 1 reward token
const vaultAddress = await integrationContract.vault();
const rewardTokenAddress = await integrationContract.rewardToken();

return {
address: integrationAddress,
strategy: integration.strategy.toString(),
isActive: integration.isActive,
totalShares: Number(integration.totalShares),
supportedRewardTokens: integration.supportedRewardTokens?.map((token: string) =>
token.toString()
),
vaults: vaults.map((vault: string) => vault.toString()),
rewardRatePerSecond: Number(rewardRatePerSecond),
lastRewardTime: Number(lastRewardTime),
pendingRewards: Number(pendingRewards),
vaultAddress: vaultAddress.toString(),
rewardTokenAddress: rewardTokenAddress.toString(),
};
}
);

return Promise.all(integrationDetailsPromises);
} catch (error) {
throw new Error(
`Error fetching integration information from Ethereum Network [${ethereumNetwork}]: ${error}`
);
}
};
1 change: 1 addition & 0 deletions src/app/functions/vault.functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { RawVault } from 'dlc-btc-lib/models';
import { unshiftValue } from 'dlc-btc-lib/utilities';

export function formatVault(vault: RawVault): Vault {
console.log('vault', vault);
return {
uuid: vault.uuid,
timestamp: vault.timestamp.toNumber(),
Expand Down
7 changes: 4 additions & 3 deletions src/app/hooks/use-icy-psbt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { submit1CYWithdrawDepositPSBT } from 'dlc-btc-lib/attestor-request-funct
import { getAttestorGroupPublicKey } from 'dlc-btc-lib/ethereum-functions';
import { RawVault, Transaction } from 'dlc-btc-lib/models';
import { BigNumber } from 'ethers';
import { bytesToHex } from 'viem';

import { useLeather } from './use-leather';

Expand Down Expand Up @@ -95,11 +96,11 @@ export function useOneClickYieldPSBT(): UseOneClickYieldPSBTReturnType {

await submit1CYWithdrawDepositPSBT([appConfiguration.coordinatorURL], {
vaultUUID: mockVault.uuid,
withdrawDepositPSBT: fundingTransaction.hex,
withdrawDepositPSBT: bytesToHex(fundingTransaction.toPSBT()).slice(2),
attestorChainID: ethereumAttestorChainID,
userBitcoinTaprootPublicKey: dlcHandler.getTaprootDerivedPublicKey(),
valueLocked: mockVault.valueLocked.toString(),
integrationAddress: '0x259F05AC8216f0A522AAE4EdE5A9F54FDd9FC41b',
integrationAddress: paddedHexString,
});

setBitcoinDepositAmount(depositAmount);
Expand Down Expand Up @@ -138,7 +139,7 @@ export function useOneClickYieldPSBT(): UseOneClickYieldPSBTReturnType {
attestorChainID: ethereumAttestorChainID,
userBitcoinTaprootPublicKey: dlcHandler.getTaprootDerivedPublicKey(),
valueLocked: mockVault.valueLocked.toString(),
integrationAddress: '0x259F05AC8216f0A522AAE4EdE5A9F54FDd9FC41b',
integrationAddress: paddedHexString,
});

resetBitcoinWalletContext();
Expand Down
Loading

0 comments on commit 1b7d2b1

Please sign in to comment.