generated from PolymeshAssociation/typescript-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 11
/
TrustedClaimIssuer.ts
83 lines (70 loc) · 2.53 KB
/
TrustedClaimIssuer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import BigNumber from 'bignumber.js';
import { Entity, Identity } from '~/api/entities';
import { Context } from '~/base';
import { eventByIndexedArgs } from '~/middleware/queries';
import { EventIdEnum, ModuleIdEnum, Query } from '~/middleware/types';
import { Ensured, EventIdentifier } from '~/types';
import { MAX_TICKER_LENGTH } from '~/utils/constants';
import { padString } from '~/utils/internal';
export interface UniqueIdentifiers {
did: string;
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 { did, ticker } = identifier as UniqueIdentifiers;
return typeof did === 'string' && typeof ticker === 'string';
}
/**
* identity of the Trusted Claim Issuer
*/
public identity: Identity;
/**
* ticker of the Security Token
*/
public ticker: string;
/**
* @hidden
*/
public constructor(identifiers: UniqueIdentifiers, context: Context) {
super(identifiers, context);
const { did, ticker } = identifiers;
this.identity = new Identity({ did }, context);
this.ticker = ticker;
}
/**
* Retrieve the identifier data (block number, date and event index) of the event that was emitted when the token was created
*
* @note uses the middleware
* @note 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 { ticker, identity, context } = this;
const result = await context.queryMiddleware<Ensured<Query, 'eventByIndexedArgs'>>(
eventByIndexedArgs({
moduleId: ModuleIdEnum.Compliancemanager,
eventId: EventIdEnum.TrustedDefaultClaimIssuerAdded,
eventArg1: padString(ticker, MAX_TICKER_LENGTH),
eventArg2: identity.did,
})
);
if (result.data.eventByIndexedArgs) {
// TODO remove null check once types fixed
/* eslint-disable @typescript-eslint/no-non-null-assertion */
return {
blockNumber: new BigNumber(result.data.eventByIndexedArgs.block_id),
blockDate: result.data.eventByIndexedArgs.block!.datetime,
eventIndex: result.data.eventByIndexedArgs.event_idx,
};
/* eslint-enabled @typescript-eslint/no-non-null-assertion */
}
return null;
}
}