From cb5a713f72761b53476af9ec82bca08a56dcb599 Mon Sep 17 00:00:00 2001 From: Marcel Ebert Date: Tue, 23 Apr 2024 19:28:52 +0200 Subject: [PATCH] Amend test --- .../polkadot/__tests__/spacewalk.test.tsx | 109 ++++++++++-------- 1 file changed, 62 insertions(+), 47 deletions(-) diff --git a/src/services/polkadot/__tests__/spacewalk.test.tsx b/src/services/polkadot/__tests__/spacewalk.test.tsx index a10d06aa..9ecac8a7 100644 --- a/src/services/polkadot/__tests__/spacewalk.test.tsx +++ b/src/services/polkadot/__tests__/spacewalk.test.tsx @@ -1,4 +1,3 @@ -import { SpacewalkPrimitivesVaultId } from '@polkadot/types/lookup'; import { WalletAccount } from '@talismn/connect-wallets'; import { Keypair } from 'stellar-sdk'; import { Keyring } from '@polkadot/api'; @@ -6,55 +5,71 @@ import { getApiManagerInstance } from '../polkadotApi'; import { VaultService } from '../spacewalk'; const TEST_ACCOUNT_SECRET_PHRASE = process.env.TEST_ACCOUNT_SECRET_PHRASE || ''; -const TEST_STELLAR_DESTINATION_ACCOUNT = process.env.TEST_STELLAR_DESTINATION_ACCOUNT || ''; +const TEST_STELLAR_DESTINATION_ADDRESS = process.env.TEST_STELLAR_DESTINATION_ADDRESS || ''; const TEST_CURRENCY_SYMBOL = process.env.TEST_CURRENCY_SYMBOL || 'EURC'; +// Set timeout to five minutes +const TIMEOUT = 5 * 60 * 1000; + describe('VaultService', () => { describe('redeem', () => { - it('should successfully execute and await a redeem', async () => { - if (!TEST_ACCOUNT_SECRET_PHRASE) { - console.log('Skipping tests because TEST_ACCOUNT_SECRET_PHRASE is not set.'); - return; - } - if (!TEST_STELLAR_DESTINATION_ACCOUNT) { - console.log('Skipping tests because TEST_STELLAR_DESTINATION_ACCOUNT is not set.'); - return; - } - - // Create a new instance of the PolkadotApi - const apiManager = await getApiManagerInstance(); - const apiComponents = await apiManager.getApi(); - const api = apiComponents.api; - - const vaults: SpacewalkPrimitivesVaultId[] = await api.query.vaultRegistry.vaults().then((res) => { - console.log(res); - return res.toJSON() as unknown as SpacewalkPrimitivesVaultId[]; - }); - - const vaultsForCurrency = vaults.filter((vault) => { - vault.currencies.wrapped.isStellar && - vault.currencies.wrapped.asStellar.isAlphaNum4 && - vault.currencies.wrapped.asStellar.asAlphaNum4.code.toString() === TEST_CURRENCY_SYMBOL; - }); - - if (!vaultsForCurrency.length) { - console.log(`Skipping tests because no vaults found for ${TEST_CURRENCY_SYMBOL}`); - return; - } - - const testingVault = vaultsForCurrency[0]; - - // Create polkadot.js keypair from secret phrase - const keyring = new Keyring({ type: 'sr25519' }); - const keypair = keyring.addFromUri(TEST_ACCOUNT_SECRET_PHRASE); - // Create a new VaultService instance - const vaultService = new VaultService(testingVault, apiComponents); - const walletAccount: WalletAccount = { address: keypair.address, signer: keypair, source: 'polkadot' }; - const stellarPkBytes = Keypair.fromPublicKey(TEST_STELLAR_DESTINATION_ACCOUNT).rawPublicKey(); - const amount = '100000'; - - const redeem = vaultService.requestRedeem(walletAccount, amount, stellarPkBytes); - expect(redeem).toBeInstanceOf(Promise); - }); + it( + 'should successfully execute and await a redeem', + async () => { + if (!TEST_ACCOUNT_SECRET_PHRASE) { + console.log('Skipping tests because TEST_ACCOUNT_SECRET_PHRASE is not set.'); + return; + } + if (!TEST_STELLAR_DESTINATION_ADDRESS) { + console.log('Skipping tests because TEST_STELLAR_DESTINATION_ADDRESS is not set.'); + return; + } + + // Create a new instance of the PolkadotApi + const apiManager = await getApiManagerInstance(); + const apiComponents = await apiManager.getApi(); + const api = apiComponents.api; + + const vaultEntries = await api.query.vaultRegistry.vaults.entries(); + const vaults = vaultEntries.map(([key, value]) => value.unwrap()); + + const vaultsForCurrency = vaults.filter((vault) => { + return ( + vault.id.currencies.wrapped.isStellar && + vault.id.currencies.wrapped.asStellar.isAlphaNum4 && + vault.id.currencies.wrapped.asStellar.asAlphaNum4.code.toHuman() === TEST_CURRENCY_SYMBOL + ); + }); + + if (!vaultsForCurrency.length) { + console.log(`Skipping tests because no vaults found for ${TEST_CURRENCY_SYMBOL}`); + return; + } + + const testingVault = vaultsForCurrency[0]; + + // Create polkadot.js keypair from secret phrase + const keyring = new Keyring({ type: 'sr25519' }); + const keypair = keyring.addFromUri(TEST_ACCOUNT_SECRET_PHRASE); + // Create a new VaultService instance + const vaultService = new VaultService(testingVault.id, apiComponents); + const walletAccount: WalletAccount = { address: keypair.address, signer: keypair, source: 'polkadot' }; + const stellarPk = Keypair.fromPublicKey(TEST_STELLAR_DESTINATION_ADDRESS); + const stellarPkBytes = stellarPk.rawPublicKey(); + + const amount = '100000'; + + const redeem = vaultService.requestRedeem(walletAccount, amount, stellarPkBytes); + expect(redeem).toBeInstanceOf(Promise); + + const redeemRequestEvent = await redeem; + expect(redeemRequestEvent).toBeDefined(); + expect(redeemRequestEvent.redeemId).toBeDefined(); + expect(redeemRequestEvent.redeemer).toBeDefined(); + expect(redeemRequestEvent.vaultId).toBeDefined(); + expect(redeemRequestEvent.amount).toBeDefined(); + }, + TIMEOUT, + ); }); });