Skip to content

Commit

Permalink
Merge pull request #172 from paltalabs/feat/newFactoryFields
Browse files Browse the repository at this point in the history
New factory
  • Loading branch information
chopan123 authored Nov 7, 2024
2 parents f82138e + 9fa063f commit 9aa5887
Show file tree
Hide file tree
Showing 11 changed files with 327 additions and 181 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ To deploy the factory contract run:
```sh
bash run.sh
cd apps/contracts
yarn deploy-factory
yarn deploy-factory <network>
```
### Publish addresses
Once you have deployed an instance of the factory contract. You can publish the addresses to be used by anyone on the network.
Expand Down
63 changes: 58 additions & 5 deletions apps/dapp/src/components/DeployVault/ConfirmDelpoyModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ export const ConfirmDelpoyModal = ({ isOpen, onClose }: { isOpen: boolean, onClo
const newVault: NewVaultState = useAppSelector(state => 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)
Expand Down Expand Up @@ -80,6 +82,7 @@ export const ConfirmDelpoyModal = ({ isOpen, onClose }: { isOpen: boolean, onClo
&& emergencyManagerString !== ""
&& feeReceiverString !== ""
&& assets.length > 0
&& !indexShare
&& loadingAssets === false
) {
setDeployDisabled(false);
Expand All @@ -105,6 +108,7 @@ export const ConfirmDelpoyModal = ({ isOpen, onClose }: { isOpen: boolean, onClo
})
);
const assetsArray = await Promise.all(assetsPromises);
console.log(assetsArray)
setAssets(assetsArray);
setLoadingAssets(false);
} catch (error) {
Expand All @@ -119,6 +123,14 @@ export const ConfirmDelpoyModal = ({ isOpen, onClose }: { isOpen: boolean, onClo

const deployDefindex = async () => {

if (indexName === "" || indexName === undefined) {
console.log("Set index name please")
return
}
if (indexSymbol === "" || indexSymbol === undefined) {
console.log("Set index symbol please")
return
}
if (managerString === "" || managerString === undefined) {
console.log("Set manager please")
return
Expand All @@ -131,14 +143,26 @@ export const ConfirmDelpoyModal = ({ isOpen, onClose }: { isOpen: boolean, onClo
console.log("Set fee receiver please")
return
}

const vaultName = nativeToScVal(indexName, { type: "string" })
const vaultSymbol = nativeToScVal(indexSymbol, { type: "string" })
const vaultShare = nativeToScVal(indexShare, { type: "u32" })
const emergencyManager = new Address(emergencyManagerString)
const feeReceiver = new Address(feeReceiverString)
const manager = new Address(managerString)
const salt = randomBytes(32)

const ratios = [1];

/*
pub struct AssetAllocation {
pub address: Address,
pub strategies: Vec<Strategy>,
}
pub struct Strategy {
pub address: Address,
pub name: String,
pub paused: bool,
}
*/
const strategyParamsScVal = strategies.map((param) => {
return xdr.ScVal.scvMap([
new xdr.ScMapEntry({
Expand All @@ -149,18 +173,47 @@ export const ConfirmDelpoyModal = ({ isOpen, onClose }: { isOpen: boolean, onClo
key: xdr.ScVal.scvSymbol('name'),
val: nativeToScVal(param.name, { type: "string" }),
}),
new xdr.ScMapEntry({
key: xdr.ScVal.scvSymbol('paused'),
val: nativeToScVal(false, { type: "bool" }),
}),
]);
});

const strategyParamsScValVec = xdr.ScVal.scvVec(strategyParamsScVal);

const assetParamsScVal = assets.map((asset) => {
return xdr.ScVal.scvMap([
new xdr.ScMapEntry({
key: xdr.ScVal.scvSymbol('address'),
val: new Address(asset).toScVal(),
}),
new xdr.ScMapEntry({
key: xdr.ScVal.scvSymbol('strategies'),
val: strategyParamsScValVec,
}),
]);
});


/* fn create_defindex_vault(
emergency_manager: address,
fee_receiver: address,
vault_share: u32,
vault_name: string,
vault_symbol: string,
manager: address,
assets: vec<AssetAllocation>,
salt: bytesn<32>) -> result<address,FactoryError>
*/
const createDefindexParams: xdr.ScVal[] = [
emergencyManager.toScVal(),
feeReceiver.toScVal(),
vaultShare,
vaultName,
vaultSymbol,
manager.toScVal(),
xdr.ScVal.scvVec(assets.map((token) => new Address(token).toScVal())),
xdr.ScVal.scvVec(ratios.map((ratio) => nativeToScVal(ratio, { type: "u32" }))),
strategyParamsScValVec,
xdr.ScVal.scvVec(assetParamsScVal),
nativeToScVal(salt),
];

Expand Down
33 changes: 21 additions & 12 deletions apps/dapp/src/components/DeployVault/DeployVault.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ import ItemSlider from './Slider'
import AddNewStrategyButton from './AddNewStrategyButton'
import { useAppDispatch, useAppSelector } from '@/store/lib/storeHooks'
import { ConfirmDelpoyModal } from './ConfirmDelpoyModal'
import { setName } from '@/store/lib/features/vaultStore'
import { setName, setSymbol } from '@/store/lib/features/vaultStore'
import { Strategy } from '@/store/lib/features/walletStore'
import { DialogBody, DialogCloseTrigger, DialogContent, DialogFooter, DialogHeader, DialogRoot, DialogTitle } from '../ui/dialog'

export const DeployVault = () => {
const dispatch = useAppDispatch()
const strategies: Strategy[] = useAppSelector(state => state.newVault.strategies)
const totalValues = useAppSelector(state => state.newVault.totalValues)
const vaultName = useAppSelector(state => state.newVault.name)
const vaultSymbol = useAppSelector(state => state.newVault.symbol)
const [openConfirm, setOpenConfirm] = useState<boolean>(false)

const handleClose = () => {
Expand All @@ -31,6 +33,10 @@ export const DeployVault = () => {
await dispatch(setName(e.target.value))
}

const setVaultSymbol = async (e: any) => {
await dispatch(setSymbol(e.target.value))
}

return (
<DialogContent>
<DialogBody>
Expand All @@ -39,9 +45,13 @@ export const DeployVault = () => {
alignSelf={'end'}
alignContent={'center'}
mb={4}
gap={6}
>
<GridItem colStart={1} colSpan={[12, null, 3]} mb={{ base: 4, md: 0 }}>
<Input onChange={setVaultName} placeholder='Defindex name...'></Input>
<GridItem colStart={1} colSpan={[12, null, 5]} mb={{ base: 4, md: 0 }}>
<Input onChange={setVaultName} value={vaultName} w={'full'} placeholder='Defindex name...'></Input>
</GridItem>
<GridItem colStart={1} colSpan={[12, null, 4]} mb={{ base: 4, md: 0 }}>
<Input onChange={setVaultSymbol} value={vaultSymbol} w={'full'} placeholder='Defindex symbol...' maxLength={6} minLength={1}></Input>
</GridItem>
<GridItem colStart={[1, null, 12]} colSpan={[12, null, 1]} textAlign={['center', null, 'end']}>
<AddNewStrategyButton />
Expand All @@ -60,17 +70,16 @@ export const DeployVault = () => {
</DialogBody>
<DialogFooter>
<DialogRoot open={openConfirm} onOpenChange={(e) => setOpenConfirm(e.open)}>
<DialogTrigger>
<Button
disabled={totalValues! > 100 || strategies.length == 0 || totalValues == 0}
colorScheme="green"
size="lg"
mt={4}
w={['100%', null, 'auto']}
<Button
onClick={() => setOpenConfirm(true)}
disabled={totalValues! > 100 || strategies.length == 0 || totalValues == 0 || vaultName == '' || vaultName.length < 4}
colorScheme="green"
size="lg"
mt={4}
w={['100%', null, 'auto']}
>
Deploy Defindex
</Button>
</DialogTrigger>
</Button>
<DialogContent>
<DialogHeader>
<DialogCloseTrigger />
Expand Down
Loading

0 comments on commit 9aa5887

Please sign in to comment.