-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rwa): forced transfer functionality (#2775)
- Loading branch information
1 parent
869bb18
commit 6b163f3
Showing
10 changed files
with
187 additions
and
60 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
42 changes: 0 additions & 42 deletions
42
packages/apps/rwa-demo/src/app/(app)/assets/[uuid]/page.tsx
This file was deleted.
Oops, something went wrong.
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
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,54 @@ | ||
import { | ||
interpretErrorMessage, | ||
TXTYPES, | ||
} from '@/components/TransactionsProvider/TransactionsProvider'; | ||
import { forcedTransferTokens } from '@/services/forcedTransferTokens'; | ||
import type { ITransferTokensProps } from '@/services/transferTokens'; | ||
import { getClient } from '@/utils/client'; | ||
import { useNotifications } from '@kadena/kode-ui/patterns'; | ||
import { useEffect, useState } from 'react'; | ||
import { useAccount } from './account'; | ||
import { useAsset } from './asset'; | ||
import { useTransactions } from './transactions'; | ||
|
||
export const useForcedTransferTokens = () => { | ||
const { account, sign, isMounted, accountRoles } = useAccount(); | ||
const { paused } = useAsset(); | ||
const { addTransaction } = useTransactions(); | ||
const { addNotification } = useNotifications(); | ||
const [isAllowed, setIsAllowed] = useState(false); | ||
|
||
const submit = async (data: ITransferTokensProps) => { | ||
try { | ||
const tx = await forcedTransferTokens(data, account!); | ||
const signedTransaction = await sign(tx); | ||
if (!signedTransaction) return; | ||
|
||
const client = getClient(); | ||
const res = await client.submit(signedTransaction); | ||
|
||
return addTransaction({ | ||
...res, | ||
type: TXTYPES.TRANSFERTOKENS, | ||
accounts: [ | ||
account?.address!, | ||
data.investorFromAccount, | ||
data.investorToAccount, | ||
], | ||
}); | ||
} catch (e: any) { | ||
addNotification({ | ||
intent: 'negative', | ||
label: 'there was an error', | ||
message: interpretErrorMessage(e.message), | ||
}); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
if (!isMounted) return; | ||
setIsAllowed(!paused && accountRoles.isTransferManager()); | ||
}, [paused, isMounted, accountRoles]); | ||
|
||
return { submit, isAllowed }; | ||
}; |
45 changes: 45 additions & 0 deletions
45
packages/apps/rwa-demo/src/services/forcedTransferTokens.ts
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,45 @@ | ||
import type { IWalletAccount } from '@/components/AccountProvider/AccountType'; | ||
import { getNetwork } from '@/utils/client'; | ||
import { getAsset } from '@/utils/getAsset'; | ||
import { getPubkeyFromAccount } from '@/utils/getPubKey'; | ||
import { Pact } from '@kadena/client'; | ||
import { PactNumber } from '@kadena/pactjs'; | ||
import type { ITransferTokensProps } from './transferTokens'; | ||
|
||
const createPubKeyFromAccount = (account: string): string => { | ||
return account.replace('k:', '').replace('r:', ''); | ||
}; | ||
|
||
export const forcedTransferTokens = async ( | ||
data: ITransferTokensProps, | ||
account: IWalletAccount, | ||
) => { | ||
return Pact.builder | ||
.execution( | ||
` | ||
(${getAsset()}.forced-transfer (read-string 'investorFrom) (read-string 'investorTo) ${new PactNumber(data.amount).toDecimal()})`, | ||
) | ||
.addData('agent', account.address) | ||
.addData('investorFrom', data.investorFromAccount) | ||
.addData('investorTo', data.investorToAccount) | ||
.setMeta({ | ||
senderAccount: account.address, | ||
chainId: getNetwork().chainId, | ||
}) | ||
.addSigner(createPubKeyFromAccount(account.address), (withCap) => [ | ||
withCap(`${getAsset()}.ONLY-AGENT`, 'transfer-manager'), | ||
withCap( | ||
`${getAsset()}.TRANSFER`, | ||
data.investorFromAccount, | ||
data.investorToAccount, | ||
{ | ||
decimal: data.amount, | ||
}, | ||
), | ||
]) | ||
.addSigner(getPubkeyFromAccount(account), (withCap) => [ | ||
withCap(`coin.GAS`), | ||
]) | ||
.setNetworkId(getNetwork().networkId) | ||
.createTransaction(); | ||
}; |
Oops, something went wrong.