Skip to content

Commit

Permalink
refactor: 💡 replace all SQ syn awaits with newer function
Browse files Browse the repository at this point in the history
  • Loading branch information
polymath-eric committed Jan 29, 2025
1 parent 840ab95 commit c7f9c9c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 43 deletions.
42 changes: 20 additions & 22 deletions src/__tests__/sdk/assets/manageNft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {

import { TestFactory } from '~/helpers';
import { createNftCollection } from '~/sdk/assets/createNftCollection';
import { awaitMiddlewareSynced } from '~/util';

let factory: TestFactory;

Expand Down Expand Up @@ -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())!;
Expand Down Expand Up @@ -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' },
{
Expand All @@ -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: [
{
Expand All @@ -123,7 +124,7 @@ describe('manageNft', () => {
],
});

await moveProc.run();
await moveTx.run();

const [defaultCollections, portfolioCollections] = await Promise.all([
defaultPortfolio.getCollections(),
Expand Down Expand Up @@ -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: [
{
Expand All @@ -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 () => {
Expand All @@ -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();

Expand All @@ -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' },
{
Expand All @@ -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();
});
});
12 changes: 3 additions & 9 deletions src/sdk/identities/customClaims.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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();

Expand Down Expand Up @@ -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] },
Expand Down
7 changes: 3 additions & 4 deletions src/sdk/identities/portfolioCustody.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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);

Expand All @@ -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();
Expand Down
11 changes: 3 additions & 8 deletions src/sdk/settlements/tradeOffChainAssets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
import assert from 'node:assert';

import { createVenue } from '~/sdk/settlements/createVenue';
import { awaitMiddlewareSynced } from '~/util';

interface OffChainLegInfo {
ticker: string;
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit c7f9c9c

Please sign in to comment.