diff --git a/src/__tests__/sdk/assets/manageNft.ts b/src/__tests__/sdk/assets/manageNft.ts index 748cc60..fa3db49 100644 --- a/src/__tests__/sdk/assets/manageNft.ts +++ b/src/__tests__/sdk/assets/manageNft.ts @@ -15,6 +15,7 @@ import { import { TestFactory } from '~/helpers'; import { createNftCollection } from '~/sdk/assets/createNftCollection'; +import { awaitMiddlewareSynced } from '~/util'; let factory: TestFactory; @@ -54,20 +55,20 @@ describe('manageNft', () => { receiver = await sdk.identities.getIdentity({ did: receiverDid }); - const portfolioProc = await sdk.identities.createPortfolio({ name: 'NFT portfolio' }); + const portfolioTx = await sdk.identities.createPortfolio({ name: 'NFT portfolio' }); - const venueProc = await sdk.settlements.createVenue({ + const venueTx = await sdk.settlements.createVenue({ description: 'test exchange', type: VenueType.Exchange, }); - const pauseProc = await collection.compliance.requirements.pause(); + const pauseTx = await collection.compliance.requirements.pause(); - const batchProc = await sdk.createTransactionBatch({ - transactions: [portfolioProc, venueProc, pauseProc] as const, + const batchTx = await sdk.createTransactionBatch({ + transactions: [portfolioTx, venueTx, pauseTx] as const, }); - [portfolio, venue] = await batchProc.run(); + [portfolio, venue] = await batchTx.run(); // eslint-disable-next-line @typescript-eslint/no-non-null-assertion holder = (await sdk.getSigningIdentity())!; @@ -96,7 +97,7 @@ describe('manageNft', () => { }); it('should issue an Nft', async () => { - const issueProc = await collection.issue({ + const issueTx = await collection.issue({ metadata: [ { type: MetadataType.Local, id: new BigNumber(1), value: 'https://example.com/nft/1' }, { @@ -107,13 +108,13 @@ describe('manageNft', () => { ], }); - nft = await issueProc.run(); + nft = await issueTx.run(); expect(nft.id).toEqual(new BigNumber(1)); }); it('should allow holder to transfer NFTs between portfolios', async () => { - const moveProc = await defaultPortfolio.moveFunds({ + const moveTx = await defaultPortfolio.moveFunds({ to: portfolio, items: [ { @@ -123,7 +124,7 @@ describe('manageNft', () => { ], }); - await moveProc.run(); + await moveTx.run(); const [defaultCollections, portfolioCollections] = await Promise.all([ defaultPortfolio.getCollections(), @@ -152,7 +153,7 @@ describe('manageNft', () => { }); it('should let the holder send instructions with an NFT', async () => { - const instructionProc = await sdk.settlements.addInstruction({ + const instructionTx = await sdk.settlements.addInstruction({ venueId: venue.id, legs: [ { @@ -164,12 +165,9 @@ describe('manageNft', () => { ], }); - const middlewareSynced = () => - new Promise((resolve) => instructionProc.onProcessedByMiddleware(resolve)); + instruction = await instructionTx.run(); - instruction = await instructionProc.run(); - - await middlewareSynced(); + await awaitMiddlewareSynced(instructionTx, sdk); }); it('should return legs for an instruction when they contain an NFT', async () => { @@ -188,8 +186,8 @@ describe('manageNft', () => { }); it('should allow the receiver to accept an NFT settlement', async () => { - const affirmProc = await instruction.affirm({}, { signingAccount: receiverAddress }); - await affirmProc.run(); + const affirmTx = await instruction.affirm({}, { signingAccount: receiverAddress }); + await affirmTx.run(); const receiverPortfolio = await receiver.portfolios.getPortfolio(); @@ -209,7 +207,7 @@ describe('manageNft', () => { }); it('should allow the issuer to redeem an NFT', async () => { - const createNftProc = await collection.issue({ + const createNftTx = await collection.issue({ metadata: [ { type: MetadataType.Local, id: new BigNumber(1), value: 'https://example.com/nft/1' }, { @@ -220,10 +218,10 @@ describe('manageNft', () => { ], }); - const redeemNft = await createNftProc.run(); + const redeemNft = await createNftTx.run(); - const redeemProc = await redeemNft.redeem(); + const redeemTx = await redeemNft.redeem(); - expect(redeemProc.run()).resolves.not.toThrow(); + expect(redeemTx.run()).resolves.not.toThrow(); }); }); diff --git a/src/sdk/identities/customClaims.ts b/src/sdk/identities/customClaims.ts index 07f5572..f45db59 100644 --- a/src/sdk/identities/customClaims.ts +++ b/src/sdk/identities/customClaims.ts @@ -7,7 +7,7 @@ import { } from '@polymeshassociation/polymesh-sdk/types'; import assert from 'node:assert'; -import { randomString } from '~/util'; +import { awaitMiddlewareSynced, randomString } from '~/util'; export const manageCustomClaims = async ( sdk: Polymesh, @@ -28,12 +28,9 @@ export const manageCustomClaims = async ( { signingAccount } ); - const middlewareSyncedOnClaimType = () => - new Promise((resolve) => registerCustomClaimTypeTx.onProcessedByMiddleware(resolve)); - const customClaimTypeId = await registerCustomClaimTypeTx.run(); - await middlewareSyncedOnClaimType(); + await awaitMiddlewareSynced(registerCustomClaimTypeTx, sdk); const registeredCustomClaimTypes = await sdk.claims.getAllCustomClaimTypes(); @@ -71,14 +68,11 @@ export const manageCustomClaims = async ( { signingAccount } ); - const middlewareSyncedOnAddClaim = () => - new Promise((resolve) => addClaimTx.onProcessedByMiddleware(resolve)); - await addClaimTx.run(); assert(addClaimTx.isSuccess, 'Should be able to add a custom claim'); - await middlewareSyncedOnAddClaim(); + await awaitMiddlewareSynced(addClaimTx, sdk); const revokeClaimTx = await sdk.claims.revokeClaims( { claims: [customClaim] }, diff --git a/src/sdk/identities/portfolioCustody.ts b/src/sdk/identities/portfolioCustody.ts index 31d8560..c32413e 100644 --- a/src/sdk/identities/portfolioCustody.ts +++ b/src/sdk/identities/portfolioCustody.ts @@ -1,6 +1,8 @@ import { Polymesh } from '@polymeshassociation/polymesh-sdk'; import assert from 'node:assert'; +import { awaitMiddlewareSynced } from '~/util'; + /* This script showcases Portfolio's Custodian related functionality. It: - Creates a Portfolio @@ -45,9 +47,6 @@ export const portfolioCustody = async (sdk: Polymesh, custodianDid: string): Pro // The custodian needs to accept the created authorization const acceptTx = await authRequest.accept({ signingAccount: custodianAccount }); - const middlewareSynced = () => - new Promise((resolve) => acceptTx.onProcessedByMiddleware(resolve)); - await acceptTx.run(); assert(acceptTx.isSuccess); @@ -62,7 +61,7 @@ export const portfolioCustody = async (sdk: Polymesh, custodianDid: string): Pro ); assert(!isCustodiedByOwner, `DID ${identity.did} should no longer be be the custodian`); - await middlewareSynced(); + await awaitMiddlewareSynced(acceptTx, sdk); // The custodian can get all non owned portfolios where they are the custodian - note there are pagination options const custodiedPortfolios = await custodian.portfolios.getCustodiedPortfolios(); diff --git a/src/sdk/settlements/tradeOffChainAssets.ts b/src/sdk/settlements/tradeOffChainAssets.ts index 3c6336c..fa9f4cd 100644 --- a/src/sdk/settlements/tradeOffChainAssets.ts +++ b/src/sdk/settlements/tradeOffChainAssets.ts @@ -7,6 +7,7 @@ import { import assert from 'node:assert'; import { createVenue } from '~/sdk/settlements/createVenue'; +import { awaitMiddlewareSynced } from '~/util'; interface OffChainLegInfo { ticker: string; @@ -71,13 +72,10 @@ export const tradeOffChainAssets = async ( memo: 'Some message', }); - const middlewareSynced = () => - new Promise((resolve) => addInstructionTx.onProcessedByMiddleware(resolve)); - const instruction = await addInstructionTx.run(); assert(addInstructionTx.isSuccess, 'add instruction should succeed'); - await middlewareSynced(); + await awaitMiddlewareSynced(addInstructionTx, sdk); const details = await instruction.details(); assert(details.memo, 'the instruction should have a memo'); @@ -109,13 +107,10 @@ export const tradeOffChainAssets = async ( receipts: offChainReceipts, }); - const middlewareAffirmationsSynced = () => - new Promise((resolve) => affirmWithReceiptTx.onProcessedByMiddleware(resolve)); - await affirmWithReceiptTx.run(); assert(affirmWithReceiptTx.isSuccess); - await middlewareAffirmationsSynced(); + await awaitMiddlewareSynced(affirmWithReceiptTx, sdk); // Fetch and verify off chain affirmations const offChainAffirmations = await instruction.getOffChainAffirmations();