generated from PolymeshAssociation/typescript-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.ts
159 lines (135 loc) · 3.92 KB
/
index.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import BigNumber from 'bignumber.js';
import { PortfolioId as MeshPortfolioId } from 'polymesh-types/types';
import { Entity, Identity, SecurityToken } from '~/api/entities';
import { Context } from '~/base';
import {
balanceToBigNumber,
identityIdToString,
meshAuthorizationStatusToAuthorizationStatus,
meshInstructionStatusToInstructionStatus,
momentToDate,
numberToU64,
tickerToString,
u32ToBigNumber,
} from '~/utils';
import {
AuthorizerWithAuthorizationStatus,
InstructionDetails,
InstructionType,
Leg,
} from './types';
export interface UniqueIdentifiers {
id: BigNumber;
}
/**
* Represents a settlement Instruction to be executed on a certain Venue
*/
export class Instruction extends Entity<UniqueIdentifiers> {
/**
* @hidden
* Check if a value is of type [[UniqueIdentifiers]]
*/
public static isUniqueIdentifiers(identifier: unknown): identifier is UniqueIdentifiers {
const { id } = identifier as UniqueIdentifiers;
return id instanceof BigNumber;
}
/**
* Identifier number of the venue
*/
public id: BigNumber;
/**
* @hidden
*/
public constructor(identifiers: UniqueIdentifiers, context: Context) {
super(identifiers, context);
const { id } = identifiers;
this.id = id;
}
/**
* Retrieve information specific to this Instruction
*/
public async details(): Promise<InstructionDetails> {
const {
context: {
polymeshApi: {
query: { settlement },
},
},
id,
context,
} = this;
const {
status,
created_at: createdAt,
valid_from: validFrom,
settlement_type: type,
} = await settlement.instructionDetails(numberToU64(id, context));
const details = {
status: meshInstructionStatusToInstructionStatus(status),
createdAt: momentToDate(createdAt.unwrap()), // NOTE @monitz87: I'm pretty sure this can't be null, but I'll ask
validFrom: validFrom.isSome ? momentToDate(validFrom.unwrap()) : null,
};
if (type.isSettleOnAuthorization) {
return {
...details,
type: InstructionType.SettleOnAuthorization,
};
}
return {
...details,
type: InstructionType.SettleOnBlock,
endBlock: u32ToBigNumber(type.asSettleOnBlock),
};
}
/**
* Retrieve all authorization statuses with authorizer identity of this Instruction
*/
public async getAuthorizationStatuses(): Promise<AuthorizerWithAuthorizationStatus[]> {
const {
context: {
polymeshApi: {
query: { settlement },
},
},
id,
context,
} = this;
const entries = await settlement.authsReceived.entries(numberToU64(id, context));
const authorizerWithAuthorizationStatus: AuthorizerWithAuthorizationStatus[] = [];
entries.forEach(([key, meshAuthorizationStatus]) => {
const { did } = key.args[1] as MeshPortfolioId;
authorizerWithAuthorizationStatus.push({
identity: new Identity({ did: identityIdToString(did) }, context),
authorizarionStatus: meshAuthorizationStatusToAuthorizationStatus(meshAuthorizationStatus),
});
});
return authorizerWithAuthorizationStatus;
}
/**
* Retrieve all legs of this Instruction
*/
public async getLegs(): Promise<Leg[]> {
const {
context: {
polymeshApi: {
query: { settlement },
},
},
id,
context,
} = this;
const legs = await settlement.instructionLegs.entries(numberToU64(id, context));
return legs.map(([, leg]) => {
const { from, to, amount, asset } = leg;
const ticker = tickerToString(asset);
const fromDid = identityIdToString(from.did);
const toDid = identityIdToString(to.did);
return {
from: new Identity({ did: fromDid }, context),
to: new Identity({ did: toDid }, context),
amount: balanceToBigNumber(amount),
token: new SecurityToken({ ticker }, context),
};
});
}
}