diff --git a/apps/contracts/Cargo.lock b/apps/contracts/Cargo.lock index 64415e34..234d602b 100644 --- a/apps/contracts/Cargo.lock +++ b/apps/contracts/Cargo.lock @@ -94,7 +94,7 @@ checksum = "8c3c1a368f70d6cf7302d78f8f7093da241fb8e8807c05cc9e51a125895a6d5b" [[package]] name = "blend_strategy" -version = "1.0.0" +version = "0.1.0" dependencies = [ "defindex-strategy-core", "soroban-sdk", @@ -281,21 +281,21 @@ dependencies = [ [[package]] name = "defindex-factory" -version = "1.0.0" +version = "0.1.0" dependencies = [ "soroban-sdk", ] [[package]] name = "defindex-strategy-core" -version = "1.0.0" +version = "0.1.0" dependencies = [ "soroban-sdk", ] [[package]] name = "defindex-vault" -version = "1.0.0" +version = "0.1.0" dependencies = [ "defindex-strategy-core", "soroban-sdk", @@ -449,7 +449,7 @@ checksum = "28dea519a9695b9977216879a3ebfddf92f1c08c05d984f8996aecd6ecdc811d" [[package]] name = "fixed_apr_strategy" -version = "1.0.0" +version = "0.1.0" dependencies = [ "defindex-strategy-core", "soroban-sdk", @@ -540,7 +540,7 @@ dependencies = [ [[package]] name = "hodl_strategy" -version = "1.0.0" +version = "0.1.0" dependencies = [ "defindex-strategy-core", "soroban-sdk", @@ -1200,7 +1200,7 @@ dependencies = [ [[package]] name = "soroswap_strategy" -version = "1.0.0" +version = "0.1.0" dependencies = [ "defindex-strategy-core", "soroban-sdk", @@ -1519,7 +1519,7 @@ checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" [[package]] name = "xycloans_adapter" -version = "1.0.0" +version = "0.1.0" dependencies = [ "defindex-strategy-core", "soroban-sdk", diff --git a/apps/contracts/Cargo.toml b/apps/contracts/Cargo.toml index 9dee20c8..79b3c9e0 100644 --- a/apps/contracts/Cargo.toml +++ b/apps/contracts/Cargo.toml @@ -6,10 +6,11 @@ exclude = [ resolver = "2" [workspace.package] -version = "1.0.0" +version = "0.1.0" edition = "2021" license = "GPL-3.0" repository = "https://github.com/paltalabs/defindex" +homepage = "https://defindex.io" [workspace.dependencies] soroban-sdk = "21.7.6" diff --git a/apps/contracts/strategies/core/Cargo.toml b/apps/contracts/strategies/core/Cargo.toml index 7949f80b..a32161a9 100644 --- a/apps/contracts/strategies/core/Cargo.toml +++ b/apps/contracts/strategies/core/Cargo.toml @@ -1,11 +1,15 @@ [package] name = "defindex-strategy-core" +description = "A foundational library for developing and integrating strategies into the DeFindex ecosystem, providing reusable abstractions, events, and custom error handling." version = { workspace = true } authors = ["coderipper "] license = { workspace = true } edition = { workspace = true } -publish = false +publish = true repository = { workspace = true } +homepage = { workspace = true } +keywords = ["soroban", "defindex", "strategy", "core", "stellar"] +categories = ["cryptography::cryptocurrencies", "no-std", "development-tools"] [dependencies] soroban-sdk = { workspace = true } diff --git a/apps/contracts/strategies/core/README.md b/apps/contracts/strategies/core/README.md new file mode 100644 index 00000000..0f59f1b9 --- /dev/null +++ b/apps/contracts/strategies/core/README.md @@ -0,0 +1,77 @@ +# DeFindex Strategy Core + +The defindex-strategy-core package is a foundational library designed to facilitate the development of strategies for DeFindex. It provides reusable abstractions and utilities that streamline the creation, management, and integration of strategies into the DeFindex ecosystem. + +### Features + +- **Reusable Events**: Predefined events to log actions such as deposits, withdrawals, and harvests. +- **Custom Errors**: A unified error handling system to ensure consistent and informative feedback across strategies. +- **Core Abstractions**: Base traits and utilities to define and implement strategies with minimal boilerplate. + +### Structure + +This package includes the following modules: +1. **Error**: Provides custom error types to handle various edge cases and ensure smooth execution. +2. **Event**: Includes predefined events for logging and monitoring strategy activity. +3. **Core Traits**: Defines the DeFindexStrategyTrait, which serves as the contract for developing new strategies. + +### Installation + +Add the defindex-strategy-core package to your Cargo.toml dependencies: + +```toml +[dependencies] +defindex-strategy-core = "0.1.0" +``` + +### Usage + +Here is a simple example of how to use this package to build a custom strategy: + +1. Import the Core Library +```rust +use defindex_strategy_core::{DeFindexStrategyTrait, StrategyError, event}; +``` + +2. Implement the Strategy Trait + +Define your custom strategy by implementing the DeFindexStrategyTrait: +```rust +#[contract] +struct MyCustomStrategy; + +#[contractimpl] +impl DeFindexStrategyTrait for MyCustomStrategy { + fn initialize(e: Env, asset: Address, init_args: Vec) -> Result<(), StrategyError> { + // Initialization logic + Ok(()) + } + + fn deposit(e: Env, amount: i128, from: Address) -> Result<(), StrategyError> { + // Deposit logic + Ok(()) + } + + fn withdraw(e: Env, amount: i128, from: Address) -> Result { + // Withdrawal logic + Ok(amount) + } + + fn balance(e: Env, from: Address) -> Result { + // Balance check logic + Ok(0) + } + + fn harvest(e: Env, from: Address) -> Result<(), StrategyError> { + // Harvest logic + Ok(()) + } +} +``` + +3. Emit Events + +Use the event module to log actions: +```rust +event::emit_deposit(&e, String::from("MyCustomStrategy"), amount, from.clone()); +``` \ No newline at end of file diff --git a/apps/contracts/vault/src/fee.rs b/apps/contracts/vault/src/fee.rs index 8a4dd847..5d5445c0 100644 --- a/apps/contracts/vault/src/fee.rs +++ b/apps/contracts/vault/src/fee.rs @@ -14,7 +14,7 @@ use crate::{ /// Fetches the current fee rate from the factory contract. /// The fee rate is expressed in basis points (BPS). -fn fetch_defindex_fee(e: &Env) -> u32 { +pub fn fetch_defindex_fee(e: &Env) -> u32 { let factory_address = get_factory(e); // Interacts with the factory contract to get the fee rate. e.invoke_contract( diff --git a/apps/contracts/vault/src/interface.rs b/apps/contracts/vault/src/interface.rs index 7dd40f1f..1beeef94 100644 --- a/apps/contracts/vault/src/interface.rs +++ b/apps/contracts/vault/src/interface.rs @@ -198,6 +198,8 @@ pub trait VaultTrait { // TODO: DELETE THIS, USED FOR TESTING /// Temporary method for testing purposes. fn get_asset_amounts_for_dftokens(e: Env, df_token: i128) -> Map; + + fn get_fees(e: Env) -> (u32, u32); } pub trait AdminInterfaceTrait { diff --git a/apps/contracts/vault/src/lib.rs b/apps/contracts/vault/src/lib.rs index ece2aec1..6dc5701c 100755 --- a/apps/contracts/vault/src/lib.rs +++ b/apps/contracts/vault/src/lib.rs @@ -24,7 +24,7 @@ mod utils; use access::{AccessControl, AccessControlTrait, RolesDataKey}; use aggregator::{internal_swap_exact_tokens_for_tokens, internal_swap_tokens_for_exact_tokens}; -use fee::collect_fees; +use fee::{collect_fees, fetch_defindex_fee}; use funds::{fetch_current_idle_funds, fetch_current_invested_funds, fetch_total_managed_funds}; //, fetch_idle_funds_for_asset}; use interface::{AdminInterfaceTrait, VaultManagementTrait, VaultTrait}; use investment::{check_and_execute_investments}; @@ -33,8 +33,7 @@ use models::{ OptionalSwapDetailsExactOut, }; use storage::{ - get_assets, set_asset, set_defindex_protocol_fee_receiver, set_factory, - set_total_assets, set_vault_fee, extend_instance_ttl + extend_instance_ttl, get_assets, get_vault_fee, set_asset, set_defindex_protocol_fee_receiver, set_factory, set_total_assets, set_vault_fee }; use strategies::{ get_asset_allocation_from_address, get_strategy_asset, get_strategy_client, @@ -568,6 +567,13 @@ impl VaultTrait for DeFindexVault { extend_instance_ttl(&e); calculate_asset_amounts_for_dftokens(&e, df_tokens) } + + fn get_fees(e: Env) -> (u32, u32) { + extend_instance_ttl(&e); + let defindex_protocol_fee = fetch_defindex_fee(&e); + let vault_fee = get_vault_fee(&e); + (defindex_protocol_fee, vault_fee) + } } #[contractimpl] diff --git a/apps/dapp/README.md b/apps/dapp/README.md index a98bfa81..fd343629 100644 --- a/apps/dapp/README.md +++ b/apps/dapp/README.md @@ -2,7 +2,17 @@ This is a [Next.js](https://nextjs.org) project bootstrapped with [`create-next- ## Getting Started -First, run the development server: +### Create a .env file + +Copy the .env.example file to .env and fill in the values. + +```bash +cp env.example .env +``` + + +## Running the app +Run the development server: ```bash npm run dev @@ -14,6 +24,11 @@ pnpm dev bun dev ``` +## Install dependencies +```bash +yarn install +``` + Open [http://localhost:3000](http://localhost:3000) with your browser to see the result. You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file. diff --git a/apps/dapp/app/layout.tsx b/apps/dapp/app/layout.tsx index 7ef5162d..81704878 100644 --- a/apps/dapp/app/layout.tsx +++ b/apps/dapp/app/layout.tsx @@ -6,7 +6,7 @@ export default function RootLayout({ children: ReactNode, }) { return ( - + {children} diff --git a/apps/dapp/app/page.tsx b/apps/dapp/app/page.tsx index 42d76f47..53954d92 100644 --- a/apps/dapp/app/page.tsx +++ b/apps/dapp/app/page.tsx @@ -1,8 +1,8 @@ "use client"; +import { HStack } from '@chakra-ui/react' +import { useSorobanReact } from '@soroban-react/core' import ManageVaults from '@/components/ManageVaults/ManageVaults'; import { TestTokens } from '@/components/TestTokens'; -import { Container } from '@chakra-ui/react'; -import { useSorobanReact } from '@soroban-react/core'; import { ArcElement, Chart as ChartJS, @@ -14,11 +14,8 @@ ChartJS.register(ArcElement); export default function Home() { const { address } = useSorobanReact() return ( - - - {address && ()} - - - + + + ); } diff --git a/apps/dapp/env.example b/apps/dapp/env.example new file mode 100644 index 00000000..06c166d9 --- /dev/null +++ b/apps/dapp/env.example @@ -0,0 +1 @@ +NEXT_PUBLIC_TEST_TOKENS_ADMIN= # Test tokens admin address \ No newline at end of file diff --git a/apps/dapp/src/components/DeployVault/AddNewStrategyButton.tsx b/apps/dapp/src/components/DeployVault/AddNewStrategyButton.tsx index 86ff3b42..e7da6c9f 100644 --- a/apps/dapp/src/components/DeployVault/AddNewStrategyButton.tsx +++ b/apps/dapp/src/components/DeployVault/AddNewStrategyButton.tsx @@ -9,8 +9,8 @@ import { } from '@/components/ui/dialog' import { getTokenSymbol } from '@/helpers/getTokenInfo' import { StrategyMethod, useStrategyCallback } from '@/hooks/useStrategy' -import { getDefaultStrategies, pushAmount, pushAsset } from '@/store/lib/features/vaultStore' -import { useAppDispatch } from '@/store/lib/storeHooks' +import { getDefaultStrategies, pushAmount, pushAsset, setAmountByAddress } from '@/store/lib/features/vaultStore' +import { useAppDispatch, useAppSelector } from '@/store/lib/storeHooks' import { Asset, Strategy } from '@/store/lib/types' import { Button, @@ -42,15 +42,36 @@ function AddNewStrategyButton() { const { activeChain } = useSorobanReact() const strategyCallback = useStrategyCallback(); const [open, setOpen] = useState(false) - const [isLoading, setIsLoading] = useState(false) - const [defaultStrategies, setDefaultStrategies] = useState([]) - const [asset, setAsset] = useState({ address: '', strategies: [] }) + const newVault = useAppSelector((state) => state.newVault) + const [defaultStrategies, setDefaultStrategies] = useState([]) + const [selectedAsset, setSelectedAsset] = useState({ address: '', strategies: [], symbol: '' }) + const [assets, setAssets] = useState([]) const [amountInput, setAmountInput] = useState({ amount: 0, enabled: false }) + const resetForm = () => { + setSelectedAsset({ address: '', strategies: [], symbol: '' }) + setAmountInput({ amount: 0, enabled: false }) + setOpen(false) + } + + const getSymbol = async (address: string) => { + const symbol = await getTokenSymbol(address, sorobanContext) + if (!symbol) return ''; + return symbol === 'native' ? 'XLM' : symbol + } + useEffect(() => { const fetchStrategies = async () => { const tempStrategies = await getDefaultStrategies(activeChain?.name?.toLowerCase() || 'testnet') - for (const strategy of tempStrategies) { + setDefaultStrategies(tempStrategies) + } + fetchStrategies(); + }, [activeChain?.networkPassphrase]) + + useEffect(() => { + const fetchStrategies = async () => { + const rawDefaultStrategies = await getDefaultStrategies(activeChain?.name?.toLowerCase() || 'testnet') + const defaultStrategiesWithAssets = await Promise.all(rawDefaultStrategies.map(async (strategy) => { const assetAddress = await strategyCallback( strategy.address, StrategyMethod.ASSET, @@ -62,76 +83,23 @@ function AddNewStrategyButton() { return asset; }) const assetSymbol = await getSymbol(assetAddress) - setAsset({ ...asset, address: assetAddress, symbol: assetSymbol! }) + const asset = { address: assetAddress, strategies: [strategy], symbol: assetSymbol! } + return asset } - setDefaultStrategies(tempStrategies) + )) + setAssets(defaultStrategiesWithAssets) } fetchStrategies(); }, [activeChain?.networkPassphrase]) - const resetForm = () => { - setAsset({ address: '', strategies: [] }) - setAmountInput({ amount: 0, enabled: false }) - setOpen(false) - } - - const getSymbol = async (address: string) => { - const symbol = await getTokenSymbol(address, sorobanContext) - if (!symbol) return ''; - return symbol === 'native' ? 'XLM' : symbol - } - const handleSelectStrategy = (value: boolean, strategy: Strategy) => { - setIsLoading(true) - switch (value) { - case true: - const fetchAssets = async () => { - try { - const asset = await strategyCallback( - strategy.address, - StrategyMethod.ASSET, - undefined, - false - ).then((result) => { - const resultScval = result as xdr.ScVal; - const asset = scValToNative(resultScval); - return asset; - }); - const symbol = await getSymbol(asset); - const newAsset = { address: asset, symbol: symbol!, strategies: [strategy] } - console.log(newAsset) - setAsset({ address: asset, symbol: symbol!, strategies: [strategy] }) - } catch (error) { - console.error(error); - } finally { - setIsLoading(false) - } - }; - fetchAssets(); - break - case false: - setAsset({ ...asset, strategies: asset.strategies.filter(str => str.address !== strategy.address) }) - setIsLoading(false) - break + const selectedAsset = assets.find((asset) => asset.strategies.some((str) => str.address === strategy.address)) + if (selectedAsset) { + setSelectedAsset(selectedAsset) } } - const addAsset = async () => { - const newAsset: Asset = { - address: asset.address, - strategies: asset.strategies, - symbol: asset.symbol - } - await dispatch(pushAsset(newAsset)) - if (amountInput.enabled && amountInput.amount! > 0) { - await dispatch(pushAmount(amountInput.amount!)) - } - resetForm() - } - - - const handleAmountInput = async (e: any) => { const input = e.target.value const decimalRegex = /^(\d+)?(\.\d{0,7})?$/ @@ -142,6 +110,31 @@ function AddNewStrategyButton() { } setAmountInput({ amount: input, enabled: true }); } + const strategyExists = (strategy: Strategy) => { + const exists = newVault.assets.some((asset) => asset.strategies.some((str) => str.address === strategy.address)) + return exists + } + const addAsset = async () => { + const newAsset: Asset = { + address: selectedAsset.address, + strategies: selectedAsset.strategies, + symbol: selectedAsset.symbol + } + const exists = strategyExists(selectedAsset.strategies[0]!) + if (exists) { + if (amountInput.enabled && amountInput.amount! > 0) { + await dispatch(setAmountByAddress({ address: selectedAsset.address, amount: amountInput.amount })) + } else if (amountInput.enabled == false || amountInput.amount! == 0) { + await dispatch(setAmountByAddress({ address: selectedAsset.address, amount: 0 })) + } + } + await dispatch(pushAsset(newAsset)) + if (!exists && amountInput.enabled && amountInput.amount! > 0) { + await dispatch(pushAmount(amountInput.amount!)) + } + resetForm() + } + return ( { setOpen(e.open) }} placement={'center'}> @@ -159,13 +152,12 @@ function AddNewStrategyButton() { {(strategy, index) => ( - {isLoading && } - {!isLoading && str.address === strategy.address)} + str.address === strategy.address)} onCheckedChange={(e) => handleSelectStrategy(!!e.checked, strategy)} label={strategy.name} - />} - {asset.strategies.some((str) => str.address === strategy.address) && + /> + {selectedAsset.strategies.some((str) => str.address === strategy.address) && Initial deposit: @@ -187,7 +179,7 @@ function AddNewStrategyButton() { @@ -204,7 +196,7 @@ function AddNewStrategyButton() { Close state.newVault); - //const strategies: Strategy[] = newVault.strategies; const indexName = useAppSelector(state => state.newVault.name) const indexSymbol = useAppSelector(state => state.newVault.symbol) const indexShare = useAppSelector(state => state.newVault.vaultShare) const managerString = useAppSelector(state => state.newVault.manager) const emergencyManagerString = useAppSelector(state => state.newVault.emergencyManager) const feeReceiverString = useAppSelector(state => state.newVault.feeReceiver) - const { transactionStatusModal: txModal } = useContext(ModalContext); + const { transactionStatusModal: txModal, deployVaultModal: deployModal } = useContext(ModalContext); const dispatch = useAppDispatch(); - const [assets, setAssets] = useState([]); - const [status, setStatus] = useState({ - isSuccess: false, - hasError: false, - network: undefined, - message: undefined, - txHash: undefined - }); + const { getIdleFunds, getInvestedFunds, getTVL, getUserBalance } = useVault() const [deployDisabled, setDeployDisabled] = useState(true); @@ -73,35 +67,6 @@ export const ConfirmDelpoyModal = ({ isOpen, onClose }: { isOpen: boolean, onClo } }, [managerString, emergencyManagerString, feeReceiverString]) - - - - /* useEffect(() => { - const newChartData: ChartData[] = strategies.map((strategy: Strategy, index: number) => { - return { - id: index, - label: strategy.name, - address: strategy.address, - value: strategy.share, - } - }); - const total = newChartData.reduce((acc: number, curr: ChartData) => acc + curr.value, 0) - if (total == 100) { - setChartData(newChartData); - return; - } else { - newChartData.push({ - id: newChartData.length, - label: 'Unassigned', - value: 100 - newChartData.reduce((acc: number, curr: ChartData) => acc + curr.value, 0), - address: undefined, - color: '#e0e0e0' - }) - setChartData(newChartData); - return; - } - }, [strategies]); */ - const autoCloseModal = async () => { await new Promise(resolve => setTimeout(resolve, 30000)) txModal.resetModal(); @@ -113,7 +78,6 @@ export const ConfirmDelpoyModal = ({ isOpen, onClose }: { isOpen: boolean, onClo autoCloseModal(); } }, [txModal.status]) - const activeStep: number = 0; const [buttonText, setButtonText] = useState('') const [accordionValue, setAccordionValue] = useState([AccordionItems.STRATEGY_DETAILS]) const [formControl, setFormControl] = useState({ @@ -144,6 +108,7 @@ export const ConfirmDelpoyModal = ({ isOpen, onClose }: { isOpen: boolean, onClo } }, [managerString, emergencyManagerString, feeReceiverString, indexShare]) + const deployDefindex = async () => { if (managerString === '' || emergencyManagerString === '') { console.log('please fill manager config') @@ -155,6 +120,7 @@ export const ConfirmDelpoyModal = ({ isOpen, onClose }: { isOpen: boolean, onClo setAccordionValue([AccordionItems.FEES_CONFIGS]) return } + deployModal.setIsOpen(false) txModal.initModal(); const vaultName = nativeToScVal(indexName, { type: "string" }) @@ -207,9 +173,16 @@ export const ConfirmDelpoyModal = ({ isOpen, onClose }: { isOpen: boolean, onClo ]); }); const assetParamsScValVec = xdr.ScVal.scvVec(assetParamsScVal); - const amountsScVal = newVault.amounts.map((amount) => { - return nativeToScVal((amount * Math.pow(10, 7)), { type: "i128" }); + const amountsScVal = newVault.assets.map((asset, index) => { + const parsedAmount = newVault.amounts[index] || 0; + const truncatedAmount = Math.floor(parsedAmount * 1e7) / 1e7; + const convertedAmount = Number(truncatedAmount) * Math.pow(10, 7) + if (newVault.amounts.length === 0) return nativeToScVal(0, { type: "i128" }); + return nativeToScVal(convertedAmount, { type: "i128" }); }); + /* const amountsScVal = newVault.amounts.map((amount) => { + return nativeToScVal((amount * Math.pow(10, 7)), { type: "i128" }); + }); */ const amountsScValVec = xdr.ScVal.scvVec(amountsScVal); /* fn create_defindex_vault( emergency_manager: address, @@ -222,7 +195,9 @@ export const ConfirmDelpoyModal = ({ isOpen, onClose }: { isOpen: boolean, onClo salt: bytesn<32>) -> result */ let result: any; - if (amountsScVal.length === 0) { + + + if (newVault.amounts.length === 0) { const createDefindexParams: xdr.ScVal[] = [ emergencyManager.toScVal(), feeReceiver.toScVal(), @@ -242,6 +217,7 @@ export const ConfirmDelpoyModal = ({ isOpen, onClose }: { isOpen: boolean, onClo } catch (e: any) { console.error(e) + dispatch(resetNewVault()); txModal.handleError(e.toString()); return } @@ -269,18 +245,33 @@ export const ConfirmDelpoyModal = ({ isOpen, onClose }: { isOpen: boolean, onClo } catch (e: any) { console.error(e) + dispatch(resetNewVault()); txModal.handleError(e.toString()); return } } const parsedResult: string = scValToNative(result.returnValue); if (parsedResult.length !== 56) throw new Error('Invalid result') - const tempVault: any = { + const idleFunds = newVault.assets.map((asset, index) => { + return { + address: asset.address, + amount: newVault.amounts[index] || 0 + } + }) + const tempVault: VaultData = { ...newVault, - address: parsedResult + address: parsedResult, + emergencyManager: emergencyManagerString, + feeReceiver: feeReceiverString, + manager: managerString, + TVL: 0, + totalSupply: 0, + idleFunds: idleFunds, + investedFunds: [{ address: '', amount: 0 }], } - txModal.handleSuccess(result.txHash); + await txModal.handleSuccess(result.txHash); dispatch(pushVault(tempVault)); + dispatch(resetNewVault()); return result; } @@ -305,14 +296,12 @@ export const ConfirmDelpoyModal = ({ isOpen, onClose }: { isOpen: boolean, onClo - {(activeStep == 0 && !status.hasError) && ( - )} + diff --git a/apps/dapp/src/components/DeployVault/DeployVault.tsx b/apps/dapp/src/components/DeployVault/DeployVault.tsx index 987f13aa..7432b96d 100644 --- a/apps/dapp/src/components/DeployVault/DeployVault.tsx +++ b/apps/dapp/src/components/DeployVault/DeployVault.tsx @@ -103,7 +103,7 @@ export const DeployVault = () => { Strategy Address: {shortenAddress(strategy.address)} - {amounts[j] &&
  • Initial deposit: ${amounts[j]} {asset.symbol}
  • } + {(amounts[j]! > 0) &&
  • Initial deposit: ${amounts[j]} {asset.symbol}
  • } )}
    diff --git a/apps/dapp/src/components/DeployVault/VaultPreview.tsx b/apps/dapp/src/components/DeployVault/VaultPreview.tsx index c0cbea33..d1927875 100644 --- a/apps/dapp/src/components/DeployVault/VaultPreview.tsx +++ b/apps/dapp/src/components/DeployVault/VaultPreview.tsx @@ -155,6 +155,29 @@ interface VaultPreviewProps { setFormControl: (args: FormControlInterface) => any; } + +export const dropdownData = { + strategies: { + title: 'Strategies', + description: 'A strategy is a set of steps to be followed to execute an investment in one or several protocols.', + href: 'https://docs.defindex.io/whitepaper/10-whitepaper/01-introduction#core-concepts' + }, + manager: { + title: 'Manager', + description: 'The Manager can rebalance the Vault, emergency withdraw and invest IDLE funds in strategies.', + href: 'https://docs.defindex.io/whitepaper/10-whitepaper/03-the-defindex-approach/02-contracts/01-vault-contract#management' + }, + emergencyManager: { + title: 'Emergency manager', + description: 'The Emergency Manager has the authority to withdraw assets from the DeFindex in case of an emergency.', + href: 'https://docs.defindex.io/whitepaper/10-whitepaper/03-the-defindex-approach/02-contracts/01-vault-contract#emergency-management' + }, + feeReceiver: { + title: 'Fee receiver', + description: ' Fee Receiver could be the manager using the same address, or it could be a different entity such as a streaming contract, a DAO, or another party.', + href: 'https://docs.defindex.io/whitepaper/10-whitepaper/03-the-defindex-approach/02-contracts/01-vault-contract#fee-collection' + } +} export const VaultPreview: React.FC = ({ data, accordionValue, setAccordionValue, formControl, setFormControl }) => { const dispatch = useAppDispatch() @@ -248,28 +271,7 @@ export const VaultPreview: React.FC = ({ data, accordionValue dispatch(setVaultShare(input * 100)) } - const dropdownData = { - strategies: { - title: 'Strategies', - description: 'A strategy is a set of steps to be followed to execute an investment in one or several protocols.', - href: 'https://docs.defindex.io/whitepaper/10-whitepaper/01-introduction#core-concepts' - }, - manager: { - title: 'Manager', - description: 'The Manager can rebalance the Vault, emergency withdraw and invest IDLE funds in strategies.', - href: 'https://docs.defindex.io/whitepaper/10-whitepaper/03-the-defindex-approach/02-contracts/01-vault-contract#management' - }, - emergencyManager: { - title: 'Emergency manager', - description: 'The Emergency Manager has the authority to withdraw assets from the DeFindex in case of an emergency.', - href: 'https://docs.defindex.io/whitepaper/10-whitepaper/03-the-defindex-approach/02-contracts/01-vault-contract#emergency-management' - }, - feeReceiver: { - title: 'Fee receiver', - description: ' Fee Receiver could be the manager using the same address, or it could be a different entity such as a streaming contract, a DAO, or another party.', - href: 'https://docs.defindex.io/whitepaper/10-whitepaper/03-the-defindex-approach/02-contracts/01-vault-contract#fee-collection' - } - } + return ( <> setAccordionValue(e.value)}> diff --git a/apps/dapp/src/components/InteractWithVault/EditVault.tsx b/apps/dapp/src/components/InteractWithVault/EditVault.tsx new file mode 100644 index 00000000..2be0c278 --- /dev/null +++ b/apps/dapp/src/components/InteractWithVault/EditVault.tsx @@ -0,0 +1,240 @@ +import React, { useContext, useEffect, useState } from 'react' +import { Address, xdr } from '@stellar/stellar-sdk' +import { useSorobanReact } from '@soroban-react/core' + +import { useAppDispatch, useAppSelector } from '@/store/lib/storeHooks' +import { setVaultFeeReceiver } from '@/store/lib/features/walletStore' +import { setFeeReceiver } from '@/store/lib/features/vaultStore' + +import { ModalContext } from '@/contexts' +import { VaultMethod, useVaultCallback, useVault } from '@/hooks/useVault' +import { isValidAddress } from '@/helpers/address' + +import { DialogBody, DialogContent, DialogHeader } from '../ui/dialog' +import { InputGroup } from '../ui/input-group' +import { + Input, + Text, + Stack, + HStack, + Fieldset, + Link, + IconButton, +} from '@chakra-ui/react' +import { InfoTip } from '../ui/toggle-tip' +import { Tooltip } from '../ui/tooltip' +import { FaRegPaste } from 'react-icons/fa6' +import { dropdownData } from '../DeployVault/VaultPreview' +import { Button } from '../ui/button' + +const CustomInputField = ({ + label, + value, + onChange, + handleClick, + placeholder, + invalid, + description, + href, +}: { + label: string, + value: string, + onChange: (e: any) => void, + handleClick: (address: string) => void, + placeholder: string, + invalid: boolean, + description?: string, + href?: string, +}) => { + const { address } = useSorobanReact() + return ( + + {label} + + {description} + + Learn more. + + + + } /> + + + + handleClick(address!)} + > + + + + } + > + + + + A valid Stellar / Soroban address is required. + + ) +} + +export const EditVaultModal = () => { + const selectedVault = useAppSelector(state => state.wallet.vaults.selectedVault) + const vaultMethod = selectedVault?.method + + const { address } = useSorobanReact(); + const vaultCB = useVaultCallback() + const vault = useVault() + const dispatch = useAppDispatch() + const { transactionStatusModal: statusModal, interactWithVaultModal: interactModal, inspectVaultModal: inspectModal } = useContext(ModalContext) + const [formControl, setFormControl] = useState({ + feeReceiver: { + value: selectedVault?.feeReceiver ?? '', + isValid: false, + needsUpdate: false, + isLoading: false, + } + }) + + + + + const handleFeeReceiverChange = (input: string) => { + const isValid = isValidAddress(input) + while (!isValid) { + setFormControl({ + ...formControl, + feeReceiver: { + ...formControl.feeReceiver, + value: input, + isValid: false, + } + }) + dispatch(setFeeReceiver('')) + return + } + setFormControl({ + ...formControl, + feeReceiver: { + ...formControl.feeReceiver, + value: input, + isValid: true, + } + }) + }; + + useEffect(() => { + setFormControl({ + ...formControl, + feeReceiver: { + isValid: isValidAddress(selectedVault?.feeReceiver ?? ''), + value: selectedVault?.feeReceiver ?? '', + needsUpdate: false, + isLoading: false, + } + }) + }, [selectedVault]) + enum Values { + FEERECIEVER = 'feeReceiver', + MANAGER = 'manager', + EMERGENCYMANAGER = 'emergencyManager' + } + + const updateValue = async (value: Values) => { + if (!address || !selectedVault) return; + let result: any; + if (value === Values.FEERECIEVER) { + setFormControl({ feeReceiver: { ...formControl.feeReceiver, isLoading: true } }) + statusModal.initModal() + console.log('Updating fee receiver') + const caller = new Address(address); + const feeReceiver = new Address(formControl.feeReceiver.value); + const createDefindexParams: xdr.ScVal[] = [ + caller.toScVal(), + feeReceiver.toScVal(), + ]; + try { + result = await vaultCB(VaultMethod.SETFEERECIEVER, selectedVault.address, createDefindexParams, true).then((res) => { + console.log(res) + statusModal.handleSuccess(res.txHash) + dispatch(setVaultFeeReceiver(formControl.feeReceiver.value)) + }) + } catch (error: any) { + console.error('Error:', error) + statusModal.handleError(error.toString()) + } finally { + setFormControl({ feeReceiver: { ...formControl.feeReceiver, isLoading: false } }) + } + + }; + } + + useEffect(() => { + if (!selectedVault?.feeReceiver) return + if (formControl.feeReceiver.value !== selectedVault.feeReceiver && formControl.feeReceiver.isValid) { + setFormControl({ + ...formControl, + feeReceiver: { + ...formControl.feeReceiver, + needsUpdate: true, + } + }) + } else if (formControl.feeReceiver.value === selectedVault.feeReceiver && formControl.feeReceiver.isValid) { + setFormControl({ + ...formControl, + feeReceiver: { + ...formControl.feeReceiver, + needsUpdate: false, + } + }) + } + }, [formControl.feeReceiver.value, formControl.feeReceiver.isValid]) + + if (!selectedVault) return null + return ( + <> + + + Manage {selectedVault.name} + + + + handleFeeReceiverChange(e.target.value)} + handleClick={(address) => setFormControl({ feeReceiver: { ...formControl.feeReceiver, isValid: true, value: address } })} + placeholder='GAFS3TLVM...' + invalid={!formControl.feeReceiver.isValid} + description={dropdownData.feeReceiver.description} + /> + + + {formControl.feeReceiver.needsUpdate && + + } + + + + + ) +} diff --git a/apps/dapp/src/components/InteractWithVault/InteractWithVault.tsx b/apps/dapp/src/components/InteractWithVault/InteractWithVault.tsx index 427033be..2fb0431d 100644 --- a/apps/dapp/src/components/InteractWithVault/InteractWithVault.tsx +++ b/apps/dapp/src/components/InteractWithVault/InteractWithVault.tsx @@ -3,8 +3,8 @@ import { Address, nativeToScVal, xdr } from '@stellar/stellar-sdk' import { useSorobanReact } from '@soroban-react/core' import { useAppDispatch, useAppSelector } from '@/store/lib/storeHooks' -import { setVaultTVL } from '@/store/lib/features/walletStore' -import { Strategy } from '@/store/lib/types' +import { setVaultTVL, setVaultUserBalance, updateVaultData } from '@/store/lib/features/walletStore' +import { Strategy, VaultData } from '@/store/lib/types' import { VaultMethod, useVaultCallback, useVault } from '@/hooks/useVault' import { ModalContext } from '@/contexts' @@ -23,6 +23,7 @@ import { NativeSelectField, HStack, } from '@chakra-ui/react' +import { ClipboardIconButton, ClipboardRoot } from '../ui/clipboard' export const InteractWithVault = () => { const [amount, set_amount] = useState(0) @@ -36,7 +37,7 @@ export const InteractWithVault = () => { const { transactionStatusModal: statusModal, interactWithVaultModal: interactModal, inspectVaultModal: inspectModal } = useContext(ModalContext) const vaultOperation = async () => { - if (!address || !vaultMethod) return; + if (!address || !vaultMethod || !selectedVault.address) return; if (!amount) throw new Error('Amount is required'); const parsedAmount = parseFloat(amount.toString()) const convertedAmount = parsedAmount * Math.pow(10, 7) @@ -52,38 +53,53 @@ export const InteractWithVault = () => { }; if (vaultMethod === VaultMethod.WITHDRAW) { const withdrawAmount = ((amount * selectedVault.totalSupply) / selectedVault.TVL) - const convertedWithdrawAmount = withdrawAmount * Math.pow(10, 7) + const truncatedWithdrawAmount = Math.floor(withdrawAmount * 1e7) / 1e7; + const convertedWithdrawAmount = Number(truncatedWithdrawAmount) * Math.pow(10, 7) const withdrawParams: xdr.ScVal[] = [ - nativeToScVal(convertedWithdrawAmount, { type: "i128" }), + nativeToScVal(Math.ceil(convertedWithdrawAmount), { type: "i128" }), new Address(address).toScVal(), ] params = withdrawParams }; - console.log('Vault method:', vaultMethod) try { const result = await vaultCB( vaultMethod!, selectedVault?.address!, params, true, - ).then((res) => - statusModal.handleSuccess(res.txHash) + ).then(async (res) => { + await statusModal.handleSuccess(res.txHash) + } ).finally(async () => { + const newBalance = await vault.getUserBalance(selectedVault.address, address) + const newIdleFunds = await vault.getIdleFunds(selectedVault.address!) + const newInvestedFunds = await vault.getInvestedFunds(selectedVault.address) const newTVL = await vault.getTVL(selectedVault?.address!) - const parsedNewTVL = Number(newTVL) / 10 ** 7 - dispatch(setVaultTVL(parsedNewTVL)) + const newVaultData: Partial = { + address: selectedVault.address, + userBalance: newBalance || 0, + idleFunds: newIdleFunds, + investedFunds: newInvestedFunds, + TVL: newTVL + } + dispatch(updateVaultData(newVaultData)) }); } catch (error: any) { console.error('Error:', error) - statusModal.handleError(error.toString()) + await statusModal.handleError(error.toString()) + } finally { + set_amount(0) + await setTimeout(() => { + interactModal.setIsOpen(false) + inspectModal.setIsOpen(false) + }, 3000) } } const setAmount = (input: any) => { if (input < 0 || !selectedVault) return; if (vaultMethod === VaultMethod.WITHDRAW) { - console.log(input, selectedVault?.userBalance) if (input > selectedVault.userBalance!) return; } const decimalRegex = /^(\d+)?(\.\d{0,7})?$/; @@ -106,19 +122,20 @@ export const InteractWithVault = () => {

    Vault address:

    -