generated from PolymeshAssociation/typescript-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 11
/
issueTokens.ts
66 lines (54 loc) · 1.65 KB
/
issueTokens.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
import BigNumber from 'bignumber.js';
import { SecurityToken } from '~/api/entities';
import { PolymeshError, Procedure } from '~/base';
import { ErrorCode, Role, RoleType } from '~/types';
import { numberToBalance, stringToTicker } from '~/utils';
import { MAX_TOKEN_AMOUNT } from '~/utils/constants';
export interface IssueTokensParams {
amount: BigNumber;
ticker: string;
}
/**
* @hidden
*/
export async function prepareIssueTokens(
this: Procedure<IssueTokensParams, SecurityToken>,
args: IssueTokensParams
): Promise<SecurityToken> {
const {
context: {
polymeshApi: {
tx: { asset },
},
},
context,
} = this;
const { ticker, amount } = args;
const securityToken = new SecurityToken({ ticker }, context);
const { isDivisible, totalSupply } = await securityToken.details();
const supplyAfterMint = amount.plus(totalSupply);
if (supplyAfterMint.isGreaterThan(MAX_TOKEN_AMOUNT)) {
throw new PolymeshError({
code: ErrorCode.ValidationError,
message: `This issuance operation will cause the total supply of "${ticker}" to exceed the supply limit`,
data: {
currentSupply: totalSupply,
supplyLimit: MAX_TOKEN_AMOUNT,
},
});
}
const rawTicker = stringToTicker(ticker, context);
const rawValue = numberToBalance(amount, context, isDivisible);
this.addTransaction(asset.issue, {}, rawTicker, rawValue);
return securityToken;
}
/**
* @hidden
*/
export function getRequiredRoles({ ticker }: IssueTokensParams): Role[] {
return [{ type: RoleType.TokenOwner, ticker }];
}
/**
* @hidden
*/
export const issueTokens = new Procedure(prepareIssueTokens, getRequiredRoles);