-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6ba39ac
commit 3d3ed7c
Showing
9 changed files
with
210 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
--- | ||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
packages/apps/rwa-demo/src/app/(app)/(isAgent)/investors/[investorAccount]/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
'use client'; | ||
|
||
import { DistributionForm } from '@/components/DistributionForm/DistributionForm'; | ||
import { SideBarBreadcrumbs } from '@/components/SideBarBreadcrumbs/SideBarBreadcrumbs'; | ||
import { MonoAdd } from '@kadena/kode-icons'; | ||
import { Button, Heading, Stack } from '@kadena/kode-ui'; | ||
import { SideBarBreadcrumbsItem, useLayout } from '@kadena/kode-ui/patterns'; | ||
import { useParams } from 'next/navigation'; | ||
import { useState } from 'react'; | ||
|
||
const InvestorPage = () => { | ||
const { isRightAsideExpanded, setIsRightAsideExpanded } = useLayout(); | ||
const params = useParams(); | ||
const [hasOpenDistributeForm, setHasOpenDistributeForm] = useState(false); | ||
const investorAccount = decodeURIComponent(params.investorAccount as string); | ||
|
||
const handleDistributeTokens = () => { | ||
setIsRightAsideExpanded(true); | ||
setHasOpenDistributeForm(true); | ||
}; | ||
|
||
return ( | ||
<> | ||
<SideBarBreadcrumbs> | ||
<SideBarBreadcrumbsItem href={`/investors/${investorAccount}`}> | ||
Investor | ||
</SideBarBreadcrumbsItem> | ||
</SideBarBreadcrumbs> | ||
|
||
{isRightAsideExpanded && hasOpenDistributeForm && ( | ||
<DistributionForm | ||
investorAccount={investorAccount} | ||
onClose={() => { | ||
setIsRightAsideExpanded(false); | ||
setHasOpenDistributeForm(false); | ||
}} | ||
/> | ||
)} | ||
|
||
<Stack width="100%" flexDirection="column"> | ||
<Heading>Investor: {investorAccount}</Heading> | ||
<Stack gap="sm"> | ||
<Button startVisual={<MonoAdd />} onPress={handleDistributeTokens}> | ||
Distribute Tokens | ||
</Button> | ||
</Stack> | ||
</Stack> | ||
</> | ||
); | ||
}; | ||
|
||
export default InvestorPage; |
58 changes: 58 additions & 0 deletions
58
packages/apps/rwa-demo/src/components/DistributionForm/DistributionForm.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { useDistributeTokens } from '@/hooks/distributeTokens'; | ||
import type { IDistributeTokensProps } from '@/services/distributeTokens'; | ||
import { Button, TextField } from '@kadena/kode-ui'; | ||
import { | ||
RightAside, | ||
RightAsideContent, | ||
RightAsideFooter, | ||
RightAsideHeader, | ||
} from '@kadena/kode-ui/patterns'; | ||
import type { FC } from 'react'; | ||
import { useForm } from 'react-hook-form'; | ||
|
||
interface IProps { | ||
onClose: () => void; | ||
investorAccount: string; | ||
} | ||
|
||
export const DistributionForm: FC<IProps> = ({ onClose, investorAccount }) => { | ||
const { submit } = useDistributeTokens(); | ||
const { register, handleSubmit } = useForm<IDistributeTokensProps>({ | ||
defaultValues: { | ||
amount: '', | ||
investorAccount, | ||
}, | ||
values: { | ||
investorAccount, | ||
}, | ||
}); | ||
|
||
const onSubmit = async (data: IDistributeTokensProps) => { | ||
await submit(data); | ||
|
||
onClose(); | ||
}; | ||
|
||
return ( | ||
<> | ||
<RightAside isOpen onClose={onClose}> | ||
<form onSubmit={handleSubmit(onSubmit)}> | ||
<RightAsideHeader label="Distribute Tokens" /> | ||
<RightAsideContent> | ||
<TextField | ||
label="Amount" | ||
type="number" | ||
{...register('amount', { required: true })} | ||
/> | ||
</RightAsideContent> | ||
<RightAsideFooter> | ||
<Button onPress={onClose} variant="transparent"> | ||
Cancel | ||
</Button> | ||
<Button type="submit">Distribute</Button> | ||
</RightAsideFooter> | ||
</form> | ||
</RightAside> | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import type { IDistributeTokensProps } from '@/services/distributeTokens'; | ||
import { distributeTokens } from '@/services/distributeTokens'; | ||
import { getClient } from '@/utils/client'; | ||
import { useState } from 'react'; | ||
import { useAccount } from './account'; | ||
import { useTransactions } from './transactions'; | ||
|
||
export const useDistributeTokens = () => { | ||
const [error, setError] = useState<string | null>(null); | ||
const { account, sign } = useAccount(); | ||
const { addTransaction } = useTransactions(); | ||
|
||
const submit = async (data: IDistributeTokensProps) => { | ||
setError(null); | ||
try { | ||
const tx = await distributeTokens(data, account!); | ||
|
||
console.log(tx); | ||
|
||
const signedTransaction = await sign(tx); | ||
if (!signedTransaction) return; | ||
|
||
const client = getClient(); | ||
const res = await client.submit(signedTransaction); | ||
|
||
addTransaction({ | ||
...res, | ||
type: 'DISTRIBUTETOKENS', | ||
data: { ...res, ...data }, | ||
}); | ||
|
||
console.log({ res }); | ||
console.log('DONE'); | ||
} catch (e: any) { | ||
setError(e?.message || e); | ||
} | ||
}; | ||
|
||
return { submit, error }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import type { IWalletAccount } from '@/components/AccountProvider/utils'; | ||
import { getNetwork } from '@/utils/client'; | ||
import { Pact } from '@kadena/client'; | ||
import { PactNumber } from '@kadena/pactjs'; | ||
|
||
export interface IDistributeTokensProps { | ||
amount: number; | ||
investorAccount: string; | ||
} | ||
|
||
const createPubKeyFromAccount = (account: string): string => { | ||
return account.replace('k:', '').replace('r:', ''); | ||
}; | ||
|
||
export const distributeTokens = async ( | ||
data: IDistributeTokensProps, | ||
account: IWalletAccount, | ||
) => { | ||
console.log(new PactNumber(data.amount).toPrecision(2)); | ||
return Pact.builder | ||
.execution( | ||
` | ||
(RWA.mvp-token.mint (read-string 'investor) ${new PactNumber(data.amount).toPactDecimal().decimal} (read-string 'agent))`, | ||
) | ||
.addData('agent', account.address) | ||
.addData('investor', data.investorAccount) | ||
.addData('investor-keyset', { | ||
keys: [createPubKeyFromAccount(data.investorAccount)], | ||
pred: 'keys-all', | ||
}) | ||
.setMeta({ | ||
senderAccount: account.address, | ||
chainId: getNetwork().chainId, | ||
}) | ||
.addSigner(account.keyset.guard.keys[0], (withCap) => [ | ||
withCap(`RWA.agent-role.ONLY-AGENT`, account.address), | ||
withCap( | ||
`RWA.mvp-token.TRANSFER`, | ||
'c:soLScugf2M-6r5L-leMUCGvgddyB1qaKh9s7ka4upGU', | ||
data.investorAccount, | ||
|
||
{ decimal: data.amount }, | ||
), | ||
withCap(`coin.GAS`), | ||
]) | ||
.setNetworkId(getNetwork().networkId) | ||
.createTransaction(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters