Created
April 21, 2025 07:58
-
-
Save xstelea/8d814fb0dfe25ce8a0336fb1a4ad5075 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import { describe, it, expect } from 'vitest'; | |
| import { createHookahSdk } from 'hookah-sdk'; | |
| import { | |
| createEd25519KeyPair, | |
| createRadixWeb3Client, | |
| deriveAccountAddressFromPublicKey, | |
| manifests, | |
| } from 'radix-web3.js'; | |
| import { PrivateKey } from '@radixdlt/radix-engine-toolkit'; | |
| const e2eTestAccountPrivateKey = process.env.E2E_TEST_ACCOUNT_PRIVATE_KEY; | |
| const mainnetE2eTestAccountKeyPair = createEd25519KeyPair( | |
| e2eTestAccountPrivateKey, | |
| ); | |
| const stokenetE2eTestAccountKeyPair = createEd25519KeyPair( | |
| e2eTestAccountPrivateKey, | |
| ); | |
| const mainnetWeb3Client = createRadixWeb3Client({ | |
| networkId: 'Mainnet', | |
| notaryPublicKey: mainnetE2eTestAccountKeyPair.publicKey(), | |
| notarizer: (hash) => mainnetE2eTestAccountKeyPair.signToSignature(hash), | |
| }); | |
| const stokenetWeb3Client = createRadixWeb3Client({ | |
| networkId: 'Stokenet', | |
| notaryPublicKey: stokenetE2eTestAccountKeyPair.publicKey(), | |
| notarizer: (hash) => stokenetE2eTestAccountKeyPair.signToSignature(hash), | |
| }); | |
| const userId = | |
| 'identity_rdx122e04s09fgwhkgrnacqkw7x4yq745fc0vy58zanhm7tz5lnhgelrk0'; | |
| const mainnetDAppDefinitionAddress = | |
| 'account_rdx12xgr6cx4w85nc7655p6mcgtu8h03qukyz8gymlhe8vg9zddf52y5qp'; | |
| const stokenetDAppDefinitionAddress = | |
| 'account_tdx_2_128ahm47qv2znp9ecugudx34gte535uxgr843hfe46t0p43r3edmy6d'; | |
| const mainnetKeypair = new PrivateKey.Ed25519(mainnetPrivateKey); | |
| const stokenetKeypair = new PrivateKey.Ed25519(stokenetPrivateKey); | |
| const sdk = createHookahSdk({ | |
| publicKey: mainnetKeypair.publicKey(), | |
| personaLabel: 'Test', | |
| signer: async (message) => mainnetKeypair.signToSignature(message).hex(), | |
| }); | |
| let webhookId: string; | |
| const webhookName = 'test-webhook'; | |
| describe('E2E', () => { | |
| describe('Auth', () => { | |
| it('should create a session', async () => { | |
| await sdk.auth(); | |
| const client = await sdk.getTrpcClient(); | |
| const { session } = await client.auth.getSession.query(); | |
| expect(session.userId).toBe(userId); | |
| }); | |
| }, 10_000); | |
| describe('Create', () => { | |
| it('should create a webhook', async () => { | |
| const client = await sdk.getTrpcClient(); | |
| webhookId = await client.webhook.getByUserId | |
| .query() | |
| .then(([item]) => item?.id); | |
| if (!webhookId) { | |
| console.log('Creating webhook'); | |
| await client.webhook.create.mutate({ | |
| url: 'https://8eb4-2001-8a0-57f4-c600-696c-84c7-245c-3813.ngrok-free.app/webhook', | |
| name: webhookName, | |
| }); | |
| webhookId = await client.webhook.getByUserId | |
| .query() | |
| .then(([item]) => item.id); | |
| } else { | |
| console.log('Webhook already exists'); | |
| } | |
| expect( | |
| await client.webhook.getById.query({ id: webhookId }), | |
| ).toBeDefined(); | |
| }); | |
| it('should create a trigger', async () => { | |
| const client = await sdk.getTrpcClient(); | |
| const triggers = await client.trigger.getByUserId.query(); | |
| if (triggers.length === 0) { | |
| console.log('Creating trigger'); | |
| await client.trigger.create.mutate({ | |
| webhookId, | |
| emitterAddress: mainnetDAppDefinitionAddress, | |
| eventName: 'DepositEvent', | |
| }); | |
| await client.trigger.create.mutate({ | |
| webhookId, | |
| emitterAddress: stokenetE2eTestAccountAddress, | |
| eventName: 'DepositEvent', | |
| }); | |
| } else { | |
| console.log('Trigger already exists'); | |
| } | |
| }); | |
| }); | |
| describe('Listen', () => { | |
| it('should get an incoming webhook on mainnet', async () => { | |
| const client = await sdk.getTrpcClient(); | |
| console.log( | |
| `mainnet address: ${await deriveAccountAddressFromPublicKey( | |
| mainnetE2eTestAccountKeyPair.publicKey(), | |
| 2, | |
| )}`, | |
| ); | |
| console.log( | |
| `stokenet address: ${await deriveAccountAddressFromPublicKey( | |
| stokenetE2eTestAccountKeyPair.publicKey(), | |
| 2, | |
| )}`, | |
| ); | |
| const sendEarlyManifest = manifests.sendResourceManifest({ | |
| amount: '1', | |
| fromAddress: mainnetE2eTestAccountAddress, | |
| toAddress: mainnetDAppDefinitionAddress, | |
| resourceAddress: | |
| 'resource_rdx1t5xv44c0u99z096q00mv74emwmxwjw26m98lwlzq6ddlpe9f5cuc7s', | |
| }); | |
| const stokenetGetXrdManifest = manifests.getXrdFromFaucetManifest( | |
| stokenetE2eTestAccountAddress, | |
| ); | |
| const mainnetResponse = await mainnetWeb3Client.submitTransaction( | |
| sendEarlyManifest, | |
| { message: 'yeehaa' }, | |
| ); | |
| const stokenetResponse = await stokenetWeb3Client.submitTransaction( | |
| stokenetGetXrdManifest, | |
| { message: 'yeehaa' }, | |
| ); | |
| console.log('mainnet txId', mainnetResponse.transactionId); | |
| console.log('stokenet txId', stokenetResponse.transactionId); | |
| let logItems = await client.log.getByWebhookId.query({ webhookId }); | |
| while (logItems.length <= 1) { | |
| console.log('Waiting for transaction to be processed'); | |
| await new Promise((resolve) => setTimeout(resolve, 1000)); | |
| logItems = await client.log.getByWebhookId.query({ webhookId }); | |
| } | |
| console.log('Webhook log items'); | |
| console.log(JSON.stringify(logItems, null, 2)); | |
| }); | |
| }, 30_000); | |
| describe('Cleanup', () => { | |
| it('should delete triggers', async () => { | |
| const client = await sdk.getTrpcClient(); | |
| const triggers = await client.trigger.getByUserId.query(); | |
| for (const trigger of triggers) { | |
| console.log('Deleting trigger', trigger.id); | |
| await client.trigger.remove.mutate({ | |
| id: trigger.id, | |
| }); | |
| } | |
| expect(await client.trigger.getByUserId.query()).toEqual([]); | |
| }); | |
| it('should delete webhook', async () => { | |
| const client = await sdk.getTrpcClient(); | |
| const webhooks = await client.webhook.getByUserId.query(); | |
| for (const webhook of webhooks) { | |
| console.log('Deleting webhook', webhook.id); | |
| await client.webhook.remove.mutate({ | |
| id: webhook.id, | |
| }); | |
| } | |
| expect(await client.webhook.getByUserId.query()).toEqual([]); | |
| }); | |
| it('should delete logs', async () => { | |
| const client = await sdk.getTrpcClient(); | |
| const logs = await client.log.getByUserId.query(); | |
| expect(logs).toEqual([]); | |
| }); | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment