Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(rwa): listen to transactions via the graph #2761

Merged
merged 2 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .changeset/silly-bobcats-wink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
49 changes: 45 additions & 4 deletions packages/apps/rwa-demo/src/__generated__/sdk.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 18 additions & 11 deletions packages/apps/rwa-demo/src/components/AssetInfo/AssetInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { MonoPause, MonoPlayArrow, MonoVpnLock } from '@kadena/kode-icons';
import { Button, Heading, Stack } from '@kadena/kode-ui';
import type { FC } from 'react';
import { CopyButton } from '../CopyButton/CopyButton';
import { TransactionTypeSpinner } from '../TransactionTypeSpinner/TransactionTypeSpinner';
import { TXTYPES } from '../TransactionsProvider/TransactionsProvider';

export const AssetInfo: FC = () => {
const { paused, asset } = useAsset();
Expand All @@ -18,17 +20,22 @@ export const AssetInfo: FC = () => {
value={`${env.URL}/assets/create/${asset?.namespace}/${asset?.contractName}`}
/>
<Button isCompact variant="transparent" isDisabled>
{paused ? (
<Stack gap="sm" alignItems="center">
<MonoPause />
paused
</Stack>
) : (
<Stack gap="sm" alignItems="center">
<MonoPlayArrow />
active
</Stack>
)}
<TransactionTypeSpinner
type={TXTYPES.PAUSECONTRACT}
fallbackIcon={
paused ? (
<Stack gap="sm" alignItems="center">
<MonoPause />
paused
</Stack>
) : (
<Stack gap="sm" alignItems="center">
<MonoPlayArrow />
active
</Stack>
)
}
/>
</Button>
</Stack>
</Stack>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
import type { IWalletAccount } from '@/components/AccountProvider/AccountType';
import { useAccount } from '@/hooks/account';
import { useNetwork } from '@/hooks/networks';
import { getClient } from '@/utils/client';
import { transactionsQuery } from '@/services/graph/transactionSubscription.graph';
import { store } from '@/utils/store';
import { useApolloClient } from '@apollo/client';
import type { ICommandResult } from '@kadena/client';
import { useNotifications } from '@kadena/kode-ui/patterns';
import type { FC, PropsWithChildren } from 'react';
Expand Down Expand Up @@ -51,7 +52,7 @@ export interface ITransaction {
uuid: string;
requestKey: string;
type: ITxType;
listener?: Promise<void | ICommandResult>;
listener?: any;
accounts: string[];
result?: ICommandResult['result'];
}
Expand Down Expand Up @@ -103,6 +104,7 @@ export const interpretErrorMessage = (
};

export const TransactionsProvider: FC<PropsWithChildren> = ({ children }) => {
const client = useApolloClient();
const { addNotification } = useNotifications();
const { account } = useAccount();
const [transactions, setTransactions] = useState<ITransaction[]>([]);
Expand All @@ -115,34 +117,38 @@ export const TransactionsProvider: FC<PropsWithChildren> = ({ children }) => {

const addListener = useCallback(
(data: ITransaction, account: IWalletAccount) => {
return getClient()
.listen({
requestKey: data.requestKey,
chainId: activeNetwork.chainId,
networkId: activeNetwork.networkId,
})
.then((result) => {
if (result.result.status === 'failure') {
const r = client.subscribe({
query: transactionsQuery,
variables: { requestKey: data.requestKey },
});

r.subscribe(
(nextData: any) => {
if (nextData?.errors?.length !== undefined) {
addNotification({
intent: 'negative',
label: 'there was an error',
message: interpretErrorMessage(result, data),
message: JSON.stringify(nextData?.errors),
url: `https://explorer.kadena.io/${activeNetwork.networkId}/transaction/${data.requestKey}`,
});
return;
}
})
.catch((e) => {
},
(errorData) => {
addNotification({
intent: 'negative',
label: 'there was an error',
message: JSON.stringify(e),
message: JSON.stringify(errorData),
url: `https://explorer.kadena.io/${activeNetwork.networkId}/transaction/${data.requestKey}`,
});
})
.finally(() => {
},
() => {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
store.removeTransaction(data);
});
},
);

return true;
},
[],
);
Expand Down
1 change: 0 additions & 1 deletion packages/apps/rwa-demo/src/services/graph/agent.graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { CORE_EVENTS_FIELDS } from './fragments/events.graph';

export const coreEvents: DocumentNode = gql`
${CORE_EVENTS_FIELDS}

query events($qualifiedName: String!) {
events(qualifiedEventName: $qualifiedName) {
edges {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { gql } from '@apollo/client';
import type { DocumentNode } from 'graphql';
import { CORE_EVENTS_FIELDS } from './fragments/events.graph';

export const coreEvents: DocumentNode = gql`
${CORE_EVENTS_FIELDS}

subscription eventSubscription($qualifiedName: String!) {
events(qualifiedEventName: $qualifiedName) {
parameters
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { gql } from '@apollo/client';
import type { DocumentNode } from 'graphql';

export const transactionsQuery: DocumentNode = gql`
subscription transaction($requestKey: String!) {
transaction(requestKey: $requestKey) {
result {
... on TransactionResult {
badResult
goodResult
}
}
}
}
`;
Loading