generated from PolymeshAssociation/typescript-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.ts
116 lines (97 loc) · 3.11 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
import BigNumber from 'bignumber.js';
import P from 'bluebird';
import { Entity, Identity, Instruction } from '~/api/entities';
import { addInstruction, AddInstructionParams } from '~/api/procedures/addInstruction';
import { Context, TransactionQueue } from '~/base';
import { InstructionStatus } from '~/types';
import {
identityIdToString,
meshVenueTypeToVenueType,
numberToU64,
u64ToBigNumber,
venueDetailsToString,
} from '~/utils';
import { VenueDetails } from './types';
export interface UniqueIdentifiers {
id: BigNumber;
}
/**
* Represents a Venue through which settlements are handled
*/
export class Venue 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 venue
*/
public async details(): Promise<VenueDetails> {
const {
context: {
polymeshApi: {
query: { settlement },
},
},
id,
context,
} = this;
const { creator, details, venue_type: type } = await settlement.venueInfo(
numberToU64(id, context)
);
return {
owner: new Identity({ did: identityIdToString(creator) }, context),
description: venueDetailsToString(details),
type: meshVenueTypeToVenueType(type),
};
}
/**
* Retrieve all pending Instructions in this Venue
*/
public async getPendingInstructions(): Promise<Instruction[]> {
const {
context: {
polymeshApi: {
query: { settlement },
},
},
id,
context,
} = this;
const { instructions: rawInstructions } = await settlement.venueInfo(numberToU64(id, context));
const instructions = rawInstructions.map(
instructionId => new Instruction({ id: u64ToBigNumber(instructionId) }, context)
);
return P.filter(instructions, async instruction => {
const { status } = await instruction.details();
return status === InstructionStatus.Pending;
});
}
/**
* Creates a settlement instruction in this Venue
*
* @param args.legs - array of token movements (amount, from, to, token)
* @param args.validFrom - date from which the instruction is valid and can be authorized by the participants (optional, instruction will be valid from the start if not supplied)
* @param args.endBlock - block at which the instruction will be executed automatically (optional, the instruction will be executed when all participants have authorized it if not supplied)
*/
public async addInstruction(args: AddInstructionParams): Promise<TransactionQueue<Instruction>> {
const { id, context } = this;
return addInstruction.prepare({ ...args, venueId: id }, context);
}
}