Skip to content

Commit 2d5065a

Browse files
committed
listen to transfer event
1 parent 2e520f2 commit 2d5065a

File tree

3 files changed

+50
-14
lines changed

3 files changed

+50
-14
lines changed

packages/apps/rwa-demo/src/components/AccountProvider/AccountProvider.tsx

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use client';
2+
import { useGetAccountKDABalance } from '@/hooks/getAccountKDABalance';
23
import type { IAgentHookProps } from '@/hooks/getAgentRoles';
34
import { useGetAgentRoles } from '@/hooks/getAgentRoles';
45
import { useGetInvestorBalance } from '@/hooks/getInvestorBalance';
5-
import { accountKDABalance } from '@/services/accountKDABalance';
66
import { isAgent } from '@/services/isAgent';
77
import { isComplianceOwner } from '@/services/isComplianceOwner';
88
import { isFrozen } from '@/services/isFrozen';
@@ -75,7 +75,10 @@ export const AccountProvider: FC<PropsWithChildren> = ({ children }) => {
7575
const [isAgentState, setIsAgentState] = useState(false);
7676
const [isInvestorState, setIsInvestorState] = useState(false);
7777
const [isFrozenState, setIsFrozenState] = useState(false);
78-
const [kdaBalance, setKdaBalance] = useState(-1);
78+
const { data: kdaBalance } = useGetAccountKDABalance({
79+
accountAddress: account?.address,
80+
});
81+
// const [kdaBalance, setKdaBalance] = useState(-1);
7982
const { ...accountRoles } = useGetAgentRoles({
8083
agent: account?.address,
8184
});
@@ -89,12 +92,11 @@ export const AccountProvider: FC<PropsWithChildren> = ({ children }) => {
8992
setIsAgentState(!!resIsAgent);
9093
};
9194
const checkIsGasPayable = async (account: IWalletAccount) => {
92-
const res = await accountKDABalance(
93-
{ accountName: account.address },
94-
account,
95-
);
96-
97-
setKdaBalance(res);
95+
// const res = await accountKDABalance(
96+
// { accountName: account.address },
97+
// account,
98+
// );
99+
// setKdaBalance(res);
98100
};
99101
const checkIsOwner = async (account: IWalletAccount) => {
100102
const resIsOwner = await isOwner({ owner: account.address });
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { useEventSubscriptionSubscription } from '@/__generated__/sdk';
2+
import { accountKDABalance } from '@/services/accountKDABalance';
3+
import { useEffect, useState } from 'react';
4+
5+
export const useGetAccountKDABalance = ({
6+
accountAddress,
7+
}: {
8+
accountAddress?: string;
9+
}) => {
10+
const [innerData, setInnerData] = useState(0);
11+
12+
const { data: subscriptionData } = useEventSubscriptionSubscription({
13+
variables: {
14+
qualifiedName: `coin.TRANSFER`,
15+
},
16+
});
17+
18+
const init = async () => {
19+
if (!accountAddress) return;
20+
const res = await accountKDABalance({ accountName: accountAddress });
21+
setInnerData(res);
22+
};
23+
24+
useEffect(() => {
25+
if (!accountAddress) return;
26+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
27+
init();
28+
}, [accountAddress]);
29+
30+
useEffect(() => {
31+
subscriptionData?.events?.map((event) => {
32+
const params = JSON.parse(event.parameters ?? '[]');
33+
console.log({ params }, event);
34+
});
35+
}, [subscriptionData]);
36+
37+
return { data: innerData };
38+
};

packages/apps/rwa-demo/src/services/accountKDABalance.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
1-
import type { IWalletAccount } from '@/components/AccountProvider/AccountType';
21
import { getClient, getNetwork } from '@/utils/client';
32
import { Pact } from '@kadena/client';
43

54
export interface IAccountKDABalanceProps {
65
accountName: string;
76
}
87

9-
export const accountKDABalance = async (
10-
data: IAccountKDABalanceProps,
11-
account: IWalletAccount,
12-
) => {
8+
export const accountKDABalance = async (data: IAccountKDABalanceProps) => {
139
const client = getClient();
1410
const transaction = Pact.builder
1511
.execution(
1612
`(let ((details (coin.details "${data.accountName}")))(let ((principal (create-principal (at "guard" details)))){"details":details, "principal":principal}))`,
1713
)
1814
.setMeta({
19-
senderAccount: account.address,
15+
senderAccount: data.accountName,
2016
chainId: getNetwork().chainId,
2117
})
2218
.addData('account', data.accountName)

0 commit comments

Comments
 (0)