Skip to content

Commit c51d35c

Browse files
authored
fix(rwa): update the KDA balance realtime (#2803)
1 parent 71d6c1f commit c51d35c

File tree

6 files changed

+121
-22
lines changed

6 files changed

+121
-22
lines changed

packages/apps/rwa-demo/src/__generated__/sdk.ts

+39
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

+4-11
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,9 @@ 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+
});
7981
const { ...accountRoles } = useGetAgentRoles({
8082
agent: account?.address,
8183
});
@@ -88,14 +90,6 @@ export const AccountProvider: FC<PropsWithChildren> = ({ children }) => {
8890
const resIsAgent = await isAgent({ agent: account.address });
8991
setIsAgentState(!!resIsAgent);
9092
};
91-
const checkIsGasPayable = async (account: IWalletAccount) => {
92-
const res = await accountKDABalance(
93-
{ accountName: account.address },
94-
account,
95-
);
96-
97-
setKdaBalance(res);
98-
};
9993
const checkIsOwner = async (account: IWalletAccount) => {
10094
const resIsOwner = await isOwner({ owner: account.address });
10195
setIsOwnerState(!!resIsOwner);
@@ -185,7 +179,6 @@ export const AccountProvider: FC<PropsWithChildren> = ({ children }) => {
185179

186180
// eslint-disable-next-line @typescript-eslint/no-floating-promises
187181
Promise.allSettled([
188-
checkIsGasPayable(accountProp),
189182
checkIsOwner(accountProp),
190183
checkIsComplianceOwner(accountProp),
191184
checkIsInvestor(accountProp),

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

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import { useAccount } from '@/hooks/account';
12
import { useFaucet } from '@/hooks/faucet';
3+
import { useGetAccountKDABalance } from '@/hooks/getAccountKDABalance';
24
import { env } from '@/utils/env';
35
import { MonoMonetizationOn } from '@kadena/kode-icons';
46
import {
@@ -9,13 +11,15 @@ import {
911
} from '@kadena/kode-ui';
1012
import { useNotifications } from '@kadena/kode-ui/patterns';
1113
import type { FC } from 'react';
12-
import { useState } from 'react';
1314
import { TransactionTypeSpinner } from '../TransactionTypeSpinner/TransactionTypeSpinner';
1415
import { TXTYPES } from '../TransactionsProvider/TransactionsProvider';
1516

1617
export const GasPayableBanner: FC = () => {
1718
const { submit, isAllowed } = useFaucet();
18-
const [isDone, setIsDone] = useState(false);
19+
const { account } = useAccount();
20+
const { data } = useGetAccountKDABalance({
21+
accountAddress: account?.address,
22+
});
1923
const { addNotification } = useNotifications();
2024

2125
const handleAddKda = async () => {
@@ -29,13 +33,11 @@ export const GasPayableBanner: FC = () => {
2933
label: 'KDA added to account',
3034
message: `We added ${env.FAUCETAMOUNT} KDA to the account`,
3135
});
32-
33-
setIsDone(true);
3436
},
3537
);
3638
};
3739

38-
if (!isAllowed || isDone) return null;
40+
if (!isAllowed || data) return null;
3941

4042
return (
4143
<Notification intent="warning" role="status" type="stacked">
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { useEventSubscriptionFilteredSubscription } 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+
const { data: toData } = useEventSubscriptionFilteredSubscription({
12+
variables: {
13+
qualifiedName: `coin.TRANSFER`,
14+
parametersFilter: `{\"array_contains\":\"${accountAddress}\"}`,
15+
},
16+
});
17+
18+
const init = async () => {
19+
if (!accountAddress) return;
20+
const res = await accountKDABalance({ accountName: accountAddress });
21+
setInnerData(res);
22+
};
23+
24+
const formatData = (data: any) => {
25+
console.log({ data });
26+
data?.events?.map(({ parameters }: any) => {
27+
const params = JSON.parse(parameters);
28+
const fromAccount = params.length > 1 && params[0];
29+
const toAccount = params.length > 2 && params[1];
30+
const amount = params.length >= 3 && params[2];
31+
32+
if (!amount) return;
33+
if (fromAccount === accountAddress) {
34+
setInnerData((oldValue) => oldValue - amount);
35+
}
36+
if (toAccount === accountAddress) {
37+
setInnerData((oldValue) => oldValue + amount);
38+
}
39+
});
40+
};
41+
42+
useEffect(() => {
43+
if (!accountAddress) return;
44+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
45+
init();
46+
}, [accountAddress]);
47+
48+
useEffect(() => {
49+
formatData(toData);
50+
}, [toData]);
51+
52+
return { data: innerData };
53+
};

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)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { gql } from '@apollo/client';
2+
import type { DocumentNode } from 'graphql';
3+
4+
export const coreEvents: DocumentNode = gql`
5+
subscription eventSubscriptionFiltered(
6+
$qualifiedName: String!
7+
$parametersFilter: String!
8+
) {
9+
events(
10+
qualifiedEventName: $qualifiedName
11+
parametersFilter: $parametersFilter
12+
) {
13+
parameters
14+
}
15+
}
16+
`;

0 commit comments

Comments
 (0)