generated from PolymeshAssociation/typescript-boilerplate
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
27a14a8
commit f2817fa
Showing
8 changed files
with
240 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { ApolloQueryResult } from 'apollo-client'; | ||
|
||
import { Identity } from '~/api/entities/Identity'; | ||
import { Entity, PolymeshError } from '~/base'; | ||
import { Context } from '~/context'; | ||
import { eventByIndexedArgs } from '~/middleware/queries'; | ||
import { Query } from '~/middleware/types'; | ||
import { Ensured, ErrorCode, EventIdentifier } from '~/types'; | ||
import { padString } from '~/utils'; | ||
import { MAX_TICKER_LENGTH } from '~/utils/constants'; | ||
|
||
export interface UniqueIdentifiers { | ||
claimIssuerDid: string; | ||
} | ||
|
||
export interface Params { | ||
ticker: string; | ||
} | ||
|
||
/** | ||
* Represents a trusted claim issuer for a specific token in the Polymesh blockchain | ||
*/ | ||
export class TrustedClaimIssuer extends Entity<UniqueIdentifiers> { | ||
/** | ||
* @hidden | ||
* Check if a value is of type [[UniqueIdentifiers]] | ||
*/ | ||
public static isUniqueIdentifiers(identifier: unknown): identifier is UniqueIdentifiers { | ||
const { claimIssuerDid } = identifier as UniqueIdentifiers; | ||
|
||
return typeof claimIssuerDid === 'string'; | ||
} | ||
|
||
/** | ||
* identity of the trusted claim issuer | ||
*/ | ||
public identity: Identity; | ||
|
||
/** | ||
* ticker of the Security Token | ||
*/ | ||
public ticker: string; | ||
|
||
/** | ||
* @hidden | ||
*/ | ||
public constructor(args: UniqueIdentifiers & Params, context: Context) { | ||
const { ticker, ...identifiers } = args; | ||
|
||
super(identifiers, context); | ||
|
||
const { claimIssuerDid } = identifiers; | ||
|
||
this.identity = new Identity({ did: claimIssuerDid }, context); | ||
this.ticker = ticker; | ||
} | ||
|
||
/** | ||
* Retrieve the identifier data (block number and event index) of the event that was emitted when the token was created | ||
* | ||
* @note this data is harvested from the chain and stored in a database, so there is a possibility that the data is not ready by the time it is requested. In that case, `null` is returned | ||
*/ | ||
public async addedAt(): Promise<EventIdentifier | null> { | ||
const { | ||
context: { middlewareApi }, | ||
ticker, | ||
identity, | ||
} = this; | ||
|
||
let result: ApolloQueryResult<Ensured<Query, 'eventByIndexedArgs'>>; | ||
try { | ||
result = await middlewareApi.query<Ensured<Query, 'eventByIndexedArgs'>>( | ||
eventByIndexedArgs({ | ||
moduleId: 'complianceManager', | ||
eventId: 'TrustedDefaultClaimIssuerAdded', | ||
eventArg1: padString(ticker, MAX_TICKER_LENGTH), | ||
eventArg2: identity.did, | ||
}) | ||
); | ||
} catch (e) { | ||
throw new PolymeshError({ | ||
code: ErrorCode.MiddlewareError, | ||
message: `Error in middleware query: ${e.message}`, | ||
}); | ||
} | ||
|
||
if (result.data.eventByIndexedArgs) { | ||
// TODO remove null check once types fixed | ||
/* eslint-disable @typescript-eslint/no-non-null-assertion */ | ||
return { | ||
blockNumber: result.data.eventByIndexedArgs.block_id!, | ||
blockDatetime: result.data.eventByIndexedArgs.block!.datetime!, | ||
eventIndex: result.data.eventByIndexedArgs.event_idx!, | ||
}; | ||
/* eslint-enabled @typescript-eslint/no-non-null-assertion */ | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { Entity } from '~/base'; | ||
import { Context } from '~/context'; | ||
import { eventByIndexedArgs } from '~/middleware/queries'; | ||
import { dsMockUtils } from '~/testUtils/mocks'; | ||
import * as utilsModule from '~/utils'; | ||
import { MAX_TICKER_LENGTH } from '~/utils/constants'; | ||
|
||
import { Identity } from '../Identity'; | ||
import { TrustedClaimIssuer } from '../TrustedClaimIssuer'; | ||
|
||
describe('TrustedClaimIssuer class', () => { | ||
let context: Context; | ||
|
||
beforeAll(() => { | ||
dsMockUtils.initMocks(); | ||
}); | ||
|
||
beforeEach(() => { | ||
context = dsMockUtils.getContextInstance(); | ||
}); | ||
|
||
afterEach(() => { | ||
dsMockUtils.reset(); | ||
}); | ||
|
||
afterAll(() => { | ||
dsMockUtils.cleanup(); | ||
}); | ||
|
||
test('should extend entity', () => { | ||
expect(TrustedClaimIssuer.prototype instanceof Entity).toBe(true); | ||
}); | ||
|
||
describe('constructor', () => { | ||
test('should assign ticker and identity to instance', () => { | ||
const did = 'someDid'; | ||
const ticker = 'SOMETICKER'; | ||
const identity = new Identity({ did }, context); | ||
const trustedClaimIssuer = new TrustedClaimIssuer({ claimIssuerDid: did, ticker }, context); | ||
|
||
expect(trustedClaimIssuer.ticker).toBe(ticker); | ||
expect(trustedClaimIssuer.identity).toEqual(identity); | ||
}); | ||
}); | ||
|
||
describe('method: isUniqueIdentifiers', () => { | ||
test('should return true if the object conforms to the interface', () => { | ||
expect(TrustedClaimIssuer.isUniqueIdentifiers({ claimIssuerDid: 'someDid' })).toBe(true); | ||
expect(TrustedClaimIssuer.isUniqueIdentifiers({})).toBe(false); | ||
expect(TrustedClaimIssuer.isUniqueIdentifiers({ claimIssuerDid: 1 })).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('method: addedAt', () => { | ||
const claimIssuerDid = 'someDid'; | ||
const ticker = 'SOMETICKER'; | ||
const variables = { | ||
moduleId: 'complianceManager', | ||
eventId: 'TrustedDefaultClaimIssuerAdded', | ||
eventArg1: utilsModule.padString(ticker, MAX_TICKER_LENGTH), | ||
eventArg2: claimIssuerDid, | ||
}; | ||
|
||
test('should return the event identifier object of the trusted claim issuer creation', async () => { | ||
const blockId = 1234; | ||
const blockDatetime = new Date('4/14/2020'); | ||
const eventIdx = 1; | ||
const fakeResult = { blockNumber: blockId, blockDatetime, eventIndex: eventIdx }; | ||
const trustedClaimIssuer = new TrustedClaimIssuer({ claimIssuerDid, ticker }, context); | ||
|
||
dsMockUtils.createApolloQueryStub(eventByIndexedArgs(variables), { | ||
/* eslint-disable @typescript-eslint/camelcase */ | ||
eventByIndexedArgs: { | ||
block_id: blockId, | ||
block: { datetime: blockDatetime }, | ||
event_idx: eventIdx, | ||
}, | ||
/* eslint-enable @typescript-eslint/camelcase */ | ||
}); | ||
|
||
const result = await trustedClaimIssuer.addedAt(); | ||
|
||
expect(result).toEqual(fakeResult); | ||
}); | ||
|
||
test('should return null if the query result is empty', async () => { | ||
const trustedClaimIssuer = new TrustedClaimIssuer({ claimIssuerDid, ticker }, context); | ||
|
||
dsMockUtils.createApolloQueryStub(eventByIndexedArgs(variables), {}); | ||
const result = await trustedClaimIssuer.addedAt(); | ||
expect(result).toBeNull(); | ||
}); | ||
|
||
test('should throw if the middleware query fails', async () => { | ||
const trustedClaimIssuer = new TrustedClaimIssuer({ claimIssuerDid, ticker }, context); | ||
|
||
dsMockUtils.throwOnMiddlewareQuery(); | ||
|
||
return expect(trustedClaimIssuer.addedAt()).rejects.toThrow( | ||
'Error in middleware query: Error' | ||
); | ||
}); | ||
}); | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,6 +75,9 @@ export function eventByIndexedArgs( | |
block_id | ||
event_idx | ||
extrinsic_idx | ||
block { | ||
datetime | ||
} | ||
} | ||
} | ||
`; | ||
|
This file contains 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