From 783f1c43a08ae6a56f9530a3e2a54b18068038ba Mon Sep 17 00:00:00 2001 From: Dawid Sowa Date: Fri, 13 Dec 2024 15:16:48 +0100 Subject: [PATCH] fix: pass intent hash correctly --- .../request-items/request-item.module.ts | 3 +- .../wallet-request/wallet-request.spec.ts | 114 +++++++++++++++++- 2 files changed, 111 insertions(+), 6 deletions(-) diff --git a/packages/dapp-toolkit/src/modules/wallet-request/request-items/request-item.module.ts b/packages/dapp-toolkit/src/modules/wallet-request/request-items/request-item.module.ts index ec3a3c4..713dc6b 100644 --- a/packages/dapp-toolkit/src/modules/wallet-request/request-items/request-item.module.ts +++ b/packages/dapp-toolkit/src/modules/wallet-request/request-items/request-item.module.ts @@ -118,7 +118,8 @@ export const RequestItemModule = (input: RequestItemModuleInput) => { signals.delete(id) } if (status === RequestStatus.success) { - getAndRemoveSignal(id)?.(item.metadata?.parentTransactionIntentHash as string) + const signal = getAndRemoveSignal(id) + signal?.(metadata?.parentTransactionIntentHash as string) } const updated = { ...item, diff --git a/packages/dapp-toolkit/src/modules/wallet-request/wallet-request.spec.ts b/packages/dapp-toolkit/src/modules/wallet-request/wallet-request.spec.ts index 466bbfa..3422a94 100644 --- a/packages/dapp-toolkit/src/modules/wallet-request/wallet-request.spec.ts +++ b/packages/dapp-toolkit/src/modules/wallet-request/wallet-request.spec.ts @@ -3,21 +3,19 @@ import { WalletRequestModule } from './wallet-request' import { RadixNetwork, TransactionStatus } from '../gateway' import { LocalStorageModule } from '../storage' import { ok, okAsync, ResultAsync } from 'neverthrow' -import { - WalletInteractionFailureResponse, - WalletInteractionItems, -} from '../../schemas' +import { WalletInteractionItems } from '../../schemas' import { failedResponseResolver, + preAuthorizationResponseResolver, RequestResolverModule, sendTransactionResponseResolver, } from './request-resolver' import { RequestItemModule } from './request-items' import { delayAsync } from '../../test-helpers/delay-async' import { WalletRequestSdk } from './wallet-request-sdk' -import { TransportProvider } from '../../_types' import { TestingTransportModule } from './transport/testing-transport/transport.testing-module' import { EnvironmentModule } from '../environment' +import { SubintentRequestBuilder } from './pre-authorization-request' const createMockEnvironment = () => { const storageModule = LocalStorageModule(`rdt:${crypto.randomUUID()}:1`, { @@ -30,6 +28,16 @@ const createMockEnvironment = () => { ResultAsync.fromSafePromise(delayAsync(2000)).map(() => ok({ status: 'success' as TransactionStatus }), ), + + pollSubintentStatus: () => { + return { + stop: () => undefined, + result: ResultAsync.fromSafePromise(delayAsync(100)).map(() => ({ + subintentStatus: 'CommittedSuccess' as TransactionStatus, + transactionIntentHash: 'transactionIntentHash', + })), + } + }, } as any const requestItemModule = RequestItemModule({ providers: { @@ -207,4 +215,100 @@ describe('WalletRequestModule', () => { ) }) }) + + describe('GIVEN subintent is submitted to the network', () => { + describe('AND onSubmittedSuccess callback is provided', () => { + it('should call the callback with transaction intent hash', async () => { + // Arange + const { + storageModule, + requestItemModule, + gatewayModule, + updateConnectButtonStatus, + } = createMockEnvironment() + + const requestResolverModule = RequestResolverModule({ + providers: { + storageModule, + requestItemModule, + resolvers: [ + preAuthorizationResponseResolver({ + requestItemModule, + updateConnectButtonStatus, + }), + ], + }, + }) + + const interactionId = '8cefec84' + + const testingTransport = TestingTransportModule({ + requestResolverModule, + }) + testingTransport.setNextWalletResponse({ + discriminator: 'success', + items: { + discriminator: 'preAuthorizationResponse', + response: { + subintentHash: + 'subtxid_tdx_2_17nhcfn9njxlrvgl8afk5dwcaj2peydrtzty0rppdm5dqnwqxs6sq0u59fe', + expirationTimestamp: Math.floor(Date.now() / 1000) + 3600, + signedPartialTransaction: + '4d220e03210221012105210607020a32ef0000000000000a40ef00000000000022000', + }, + }, + interactionId, + }) + + const walletRequestModule = WalletRequestModule({ + useCache: false, + networkId: RadixNetwork.Stokenet, + dAppDefinitionAddress: '', + providers: { + stateModule: {} as any, + storageModule, + requestItemModule, + requestResolverModule, + environmentModule: EnvironmentModule(), + gatewayModule, + walletRequestSdk: WalletRequestSdk({ + networkId: 2, + dAppDefinitionAddress: '', + providers: { + environmentModule: EnvironmentModule(), + interactionIdFactory: () => interactionId, + transports: [testingTransport], + }, + }), + }, + }) + + const onSubmittedSpy = vi.fn() + // Act + const result = await walletRequestModule.sendPreAuthorizationRequest( + SubintentRequestBuilder() + .manifest(``) + .setExpiration('afterDelay', 4600) + .onSubmittedSuccess((a) => { + console.log('onSubmittedSuccess', a) + onSubmittedSpy(a) + }), + ) + + await delayAsync(2000) + + // Assert + + expect(onSubmittedSpy).toHaveBeenCalledWith('transactionIntentHash') + expect(result.isOk() && result.value).toEqual( + expect.objectContaining({ + signedPartialTransaction: + '4d220e03210221012105210607020a32ef0000000000000a40ef00000000000022000', + subintentHash: + 'subtxid_tdx_2_17nhcfn9njxlrvgl8afk5dwcaj2peydrtzty0rppdm5dqnwqxs6sq0u59fe', + }), + ) + }) + }) + }) })