Skip to content

Commit

Permalink
Add proxies functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
kattylucy committed Nov 29, 2024
1 parent 4c08770 commit 92af4ab
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useWallet } from '@centrifuge/centrifuge-react'
import { Button } from '@centrifuge/fabric'
import { useFormikContext } from 'formik'
import { CreatePoolValues } from '.'
import { PageSection } from '../../components/PageSection'
import { MultisigForm } from '../IssuerPool/Access/MultisigForm'
import { CreatePoolValues } from './types'

export function AdminMultisigSection() {
const form = useFormikContext<CreatePoolValues>()
Expand Down
61 changes: 49 additions & 12 deletions centrifuge-app/src/pages/IssuerCreatePool/PoolSetupSection.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { PoolMetadataInput } from '@centrifuge/centrifuge-js'
import { addressToHex, evmToSubstrateAddress, PoolMetadataInput } from '@centrifuge/centrifuge-js'
import { useCentEvmChainId } from '@centrifuge/centrifuge-react'
import {
Box,
Checkbox,
Expand All @@ -18,6 +19,7 @@ import { useTheme } from 'styled-components'
import { FieldWithErrorMessage } from '../../../src/components/FieldWithErrorMessage'
import { Tooltips } from '../../../src/components/Tooltips'
import { feeCategories } from '../../../src/config'
import { isEvmAddress } from '../../../src/utils/address'
import { AddButton } from './PoolDetailsSection'
import { CheckboxOption, Line, StyledGrid } from './PoolStructureSection'

Expand Down Expand Up @@ -51,9 +53,12 @@ const TaxDocument = () => {

export const PoolSetupSection = () => {
const theme = useTheme()
const chainId = useCentEvmChainId()
const form = useFormikContext<PoolMetadataInput>()
const { values } = form

console.log(values)

return (
<Box>
<Text variant="heading2" fontWeight={700}>
Expand Down Expand Up @@ -94,7 +99,24 @@ export const PoolSetupSection = () => {
values.adminMultisig?.signers?.map((_, index) => (
<Box key={index} mt={2}>
<Field name={`adminMultisig.signers.${index}`}>
{({ field }: FieldProps) => <TextInput placeholder="Type here..." {...field} />}
{({ field, form }: FieldProps) => (
<TextInput
placeholder="Type address..."
{...field}
onChange={(val) => {
form.setFieldValue(`adminMultisig.signers.${index}`, val.target.value)
}}
onBlur={() => {
const value = form.values.adminMultisig.signers[index]
if (value) {
const transformedValue = isEvmAddress(value)
? evmToSubstrateAddress(value, chainId ?? 0)
: value
form.setFieldValue(`adminMultisig.signers.${index}`, transformedValue)
}
}}
/>
)}
</Field>
</Box>
))
Expand Down Expand Up @@ -124,19 +146,19 @@ export const PoolSetupSection = () => {
</Box>
<Box mt={2} mb={2}>
<StyledGrid gridTemplateColumns={['1fr', '1fr 1fr']} gap={3} mt={3}>
<Field name="subAssetClass">
<Field name="adminMultisig.threshold">
{({ field, meta, form }: FieldProps) => (
<Select
name="subAssetClass"
name="adminMultisig.threshold"
label={`Configuration change threshold (1 out of ${Math.max(
values?.adminMultisig?.signers?.length ?? 0,
1
)} managers)`}
onChange={(event) => form.setFieldValue('subAssetClass', event.target.value)}
onChange={(event) => form.setFieldValue('adminMultisig.threshold', event.target.value)}
onBlur={field.onBlur}
errorMessage={meta.touched && meta.error ? meta.error : undefined}
value={field.value}
options={values.adminMultisig.signers.map((_: string, i: number) => ({
options={values.adminMultisig?.signers.map((_: string, i: number) => ({
label: i + 1,
value: i + 1,
}))}
Expand Down Expand Up @@ -166,7 +188,24 @@ export const PoolSetupSection = () => {
{values.assetOriginators?.map((_: string, index: number) => (
<Box key={index} mt={2}>
<Field name={`assetOriginators.${index}`}>
{({ field }: FieldProps) => <TextInput placeholder="Type address..." {...field} />}
{({ field, form }: FieldProps) => (
<TextInput
placeholder="Type address..."
{...field}
onChange={(val) => {
form.setFieldValue(`assetOriginators.${index}`, val.target.value)
}}
onBlur={() => {
const value = form.values.assetOriginators[index]
if (value) {
const transformedValue = isEvmAddress(value)
? evmToSubstrateAddress(value, chainId ?? 0)
: addressToHex(value)
form.setFieldValue(`assetOriginators.${index}`, transformedValue)
}
}}
/>
)}
</Field>
</Box>
))}
Expand Down Expand Up @@ -233,11 +272,9 @@ export const PoolSetupSection = () => {
<StyledGrid mt={3} gap={1}>
<Box display="flex" justifyContent="space-between" alignItems="center">
<Text variant="heading3">Pool fees {index + 1}</Text>
{values.poolFees.length > 1 && (
<IconButton onClick={() => remove(index)}>
<IconTrash color="textSecondary" />
</IconButton>
)}
<IconButton onClick={() => remove(index)}>
<IconTrash color="textSecondary" />
</IconButton>
</Box>
<Line />
<Grid gridTemplateColumns={['1fr', '1fr 1fr']} gap={3}>
Expand Down
77 changes: 44 additions & 33 deletions centrifuge-app/src/pages/IssuerCreatePool/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
AddFee,
CurrencyBalance,
CurrencyKey,
FileType,
isSameAddress,
Expand Down Expand Up @@ -130,17 +131,14 @@ const IssuerCreatePoolPage = () => {
],
options
) => {
const [values, transferToMultisig, aoProxy, adminProxy, , , , , { adminMultisig }] = args
const [values, transferToMultisig, aoProxy, adminProxy, , , , , { adminMultisig, assetOriginators }] = args
const multisigAddr = adminMultisig && createKeyMulti(adminMultisig.signers, adminMultisig.threshold)
const poolArgs = args.slice(3) as any
return combineLatest([
cent.getApi(),
cent.pools.createPool(poolArgs, { createType: options?.createType, batch: true }),
]).pipe(
switchMap(([api, poolSubmittable]) => {
// BATCH https://polkadot.js.org/docs/kusama/extrinsics/#batchcalls-veccall
api.tx.utlity.batch()

const adminProxyDelegates = multisigAddr
? [multisigAddr]
: (adminMultisig && values.adminMultisig?.signers?.filter((addr) => addr !== address)) ?? []
Expand All @@ -152,7 +150,9 @@ const IssuerCreatePoolPage = () => {
api.tx.balances.transferKeepAlive(adminProxy, consts.proxy.proxyDepositFactor.add(transferToMultisig)),
api.tx.balances.transferKeepAlive(
aoProxy,
consts.proxy.proxyDepositFactor.add(consts.uniques.collectionDeposit)
consts.proxy.proxyDepositFactor
.add(consts.uniques.collectionDeposit)
.add(consts.proxy.proxyDepositFactor.mul(new BN(assetOriginators.length * 4)))
),
adminProxyDelegates.length > 0 &&
api.tx.proxy.proxy(
Expand All @@ -168,18 +168,26 @@ const IssuerCreatePoolPage = () => {
api.tx.proxy.proxy(
aoProxy,
undefined,
api.tx.utility.batchAll([
api.tx.proxy.addProxy(adminProxy, 'Any', 0),
api.tx.proxy.removeProxy(address, 'Any', 0),
])
api.tx.utility.batchAll(
[
api.tx.proxy.addProxy(adminProxy, 'Any', 0),
...assetOriginators.map((addr) => [
api.tx.proxy.addProxy(addr, 'Borrow', 0),
api.tx.proxy.addProxy(addr, 'Invest', 0),
api.tx.proxy.addProxy(addr, 'Transfer', 0),
api.tx.proxy.addProxy(addr, 'PodOperation', 0),
]),
api.tx.proxy.removeProxy(address, 'Any', 0),
].flat()
)
),
multisigAddr
? api.tx.multisig.asMulti(adminMultisig.threshold, otherMultisigSigners, null, proxiedPoolCreate, 0)
: proxiedPoolCreate,
].filter(Boolean)
)
setMultisigData({ callData: proxiedPoolCreate.method.toHex(), hash: proxiedPoolCreate.method.hash.toHex() })
return cent.wrapSignAndSend(api, submittable, { ...options, multisig: undefined, proxies: undefined })
return cent.wrapSignAndSend(api, submittable, { ...options })
})
)
},
Expand All @@ -206,7 +214,7 @@ const IssuerCreatePoolPage = () => {
const metadataValues: PoolMetadataInput = { ...values } as any

// Find the currency (asset denomination in UI)
const currency = currencies.find((c) => c.symbol.toLowerCase() === values.assetDenomination)!
const currency = currencies.find((c) => c.symbol.toLowerCase() === values.assetDenomination.toLowerCase())!

// Handle pining files for ipfs
if (!values.poolIcon) return
Expand Down Expand Up @@ -322,27 +330,25 @@ const IssuerCreatePoolPage = () => {
}
}

console.log(metadataValues)

// createProxies([
// (aoProxy, adminProxy) => {
// createPoolTx(
// [
// values,
// CurrencyBalance.fromFloat(createDeposit, chainDecimals),
// aoProxy,
// adminProxy,
// poolId,
// tranches,
// currency.key,
// CurrencyBalance.fromFloat(values.maxReserve, currency.decimals),
// metadataValues,
// poolFees,
// ],
// { createType }
// )
// },
// ])
createProxies([
(aoProxy, adminProxy) => {
createPoolTx(
[
values,
CurrencyBalance.fromFloat(createDeposit, chainDecimals),
aoProxy,
adminProxy,
poolId,
tranches,
currency.key,
CurrencyBalance.fromFloat(values.maxReserve, currency.decimals),
metadataValues,
feeInput,
],
{ createType }
)
},
])
},
})

Expand Down Expand Up @@ -424,7 +430,12 @@ const IssuerCreatePoolPage = () => {
Previous
</Button>
)}
<Button style={{ width: 163 }} small onClick={handleNextStep}>
<Button
style={{ width: 163 }}
small
onClick={handleNextStep}
loading={createProxiesIsPending || transactionIsPending}
>
{step === 3 ? 'Create pool' : 'Next'}
</Button>
</Box>
Expand Down
7 changes: 6 additions & 1 deletion centrifuge-app/src/pages/IssuerCreatePool/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,15 @@ export const initialValues: CreatePoolValues = {

assetOriginators: [''],
adminMultisig: {
signers: ['', ''],
signers: [''],
threshold: 1,
},
adminMultisigEnabled: false,
poolFees: [createPoolFee()],
poolType: 'open',

onboarding: {
tranches: {},
taxInfoRequired: false,
},
}
36 changes: 34 additions & 2 deletions centrifuge-js/src/modules/pools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,38 @@ export type AddFee = {
poolId: string
}

export type PoolFeesCreatePool = Array<
[
string,
{
destination: string
editor: any
feeType: {
[key: string]: {
limit: {
ShareOfPortfolioValuation: Rate
}
}
}
}
]
>

export type TrancheCreatePool = {
trancheType:
| 'Residual'
| {
NonResidual: {
interestRatePerSec: string
minRiskBuffer: string
}
}
metadata: {
tokenName: string
tokenSymbol: string
}
}

const formatPoolKey = (keys: StorageKey<[u32]>) => (keys.toHuman() as string[])[0].replace(/\D/g, '')
const formatLoanKey = (keys: StorageKey<[u32, u32]>) => (keys.toHuman() as string[])[1].replace(/\D/g, '')

Expand All @@ -1031,11 +1063,11 @@ export function getPoolsModule(inst: Centrifuge) {
args: [
admin: string,
poolId: string,
tranches: TrancheInput[],
tranches: TrancheCreatePool[],
currency: CurrencyKey,
maxReserve: BN,
metadata: PoolMetadataInput,
fees: AddFee['fee'][]
fees: PoolFeesCreatePool[]
],
options?: TransactionOptions
) {
Expand Down
2 changes: 1 addition & 1 deletion fabric/src/components/Toast/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export function Toast({ status = 'info', label, sublabel, onDismiss, onStatusCha
</Stack>
<Shelf ml="auto">
{action}
{onDismiss && <Button variant="tertiary" icon={IconX} onClick={onDismiss} />}
{onDismiss && <Button variant="tertiary" icon={<IconX color={statusColors[status]} />} onClick={onDismiss} />}
</Shelf>
</Shelf>
</Card>
Expand Down

0 comments on commit 92af4ab

Please sign in to comment.