generated from PolymeshAssociation/typescript-boilerplate
/
createCheckpointSchedule.ts
105 lines (92 loc) · 2.56 KB
/
createCheckpointSchedule.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
import { ISubmittableResult } from '@polkadot/types/types';
import {
CheckpointSchedule,
Context,
PolymeshError,
PostTransactionValue,
Procedure,
SecurityToken,
} from '~/internal';
import { CalendarPeriod, ErrorCode, RoleType, TxTags } from '~/types';
import { ProcedureAuthorization } from '~/types/internal';
import {
scheduleSpecToMeshScheduleSpec,
storedScheduleToCheckpointScheduleParams,
stringToTicker,
} from '~/utils/conversion';
import { filterEventRecords } from '~/utils/internal';
export interface CreateCheckpointScheduleParams {
start: Date | null;
period: CalendarPeriod | null;
repetitions: number | null;
}
/**
* @hidden
*/
export type Params = CreateCheckpointScheduleParams & {
ticker: string;
};
/**
* @hidden
*/
export const createCheckpointScheduleResolver = (ticker: string, context: Context) => (
receipt: ISubmittableResult
): CheckpointSchedule => {
const [{ data }] = filterEventRecords(receipt, 'checkpoint', 'ScheduleCreated');
const scheduleParams = storedScheduleToCheckpointScheduleParams(data[2]);
return new CheckpointSchedule(
{
ticker,
...scheduleParams,
},
context
);
};
/**
* @hidden
*/
export async function prepareCreateCheckpointSchedule(
this: Procedure<Params, CheckpointSchedule>,
args: Params
): Promise<PostTransactionValue<CheckpointSchedule>> {
const { context } = this;
const { ticker, start, period, repetitions } = args;
const now = new Date();
if (start && start < now) {
throw new PolymeshError({
code: ErrorCode.ValidationError,
message: 'Schedule start date must be in the future',
});
}
const rawTicker = stringToTicker(ticker, context);
const rawSchedule = scheduleSpecToMeshScheduleSpec({ start, period, repetitions }, context);
const [schedule] = this.addTransaction(
context.polymeshApi.tx.checkpoint.createSchedule,
{ resolvers: [createCheckpointScheduleResolver(ticker, context)] },
rawTicker,
rawSchedule
);
return schedule;
}
/**
* @hidden
*/
export function getAuthorization(
this: Procedure<Params, CheckpointSchedule>,
{ ticker }: Params
): ProcedureAuthorization {
const { context } = this;
return {
identityRoles: [{ type: RoleType.TokenOwner, ticker }],
signerPermissions: {
transactions: [TxTags.checkpoint.CreateSchedule],
tokens: [new SecurityToken({ ticker }, context)],
portfolios: [],
},
};
}
/**
* @hidden
*/
export const createCheckpointSchedule = (): Procedure<Params, CheckpointSchedule> =>
new Procedure(prepareCreateCheckpointSchedule, getAuthorization);