generated from PolymeshAssociation/typescript-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Transfers.ts
159 lines (140 loc) · 4.43 KB
/
Transfers.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 { CanTransferResult } from 'polymesh-types/types';
import { Identity } from '~/api/entities/Identity';
import { toggleFreezeTransfers, transferToken, TransferTokenParams } from '~/api/procedures';
import { Namespace, TransactionQueue } from '~/base';
import { SubCallback, TransferStatus, UnsubCallback } from '~/types';
import {
boolToBoolean,
canTransferResultToTransferStatus,
numberToBalance,
stringToAccountId,
stringToIdentityId,
stringToTicker,
valueToDid,
} from '~/utils';
import { DUMMY_ACCOUNT_ID } from '~/utils/constants';
import { SecurityToken } from './';
/**
* Handles all Security Token Transfer related functionality
*/
export class Transfers extends Namespace<SecurityToken> {
/**
* Freezes transfers and minting of the Security Token
*/
public freeze(): Promise<TransactionQueue<SecurityToken>> {
const {
parent: { ticker },
context,
} = this;
return toggleFreezeTransfers.prepare({ ticker, freeze: true }, context);
}
/**
* Unfreeze transfers and minting of the Security Token
*/
public unfreeze(): Promise<TransactionQueue<SecurityToken>> {
const {
parent: { ticker },
context,
} = this;
return toggleFreezeTransfers.prepare({ ticker, freeze: false }, context);
}
/**
* Check whether transfers are frozen for the Security Token
*
* @note can be subscribed to
*/
public areFrozen(): Promise<boolean>;
public areFrozen(callback: SubCallback<boolean>): Promise<UnsubCallback>;
// eslint-disable-next-line require-jsdoc
public async areFrozen(callback?: SubCallback<boolean>): Promise<boolean | UnsubCallback> {
const {
parent: { ticker },
context: {
polymeshApi: {
query: { asset },
},
},
context,
} = this;
const rawTicker = stringToTicker(ticker, context);
if (callback) {
return asset.frozen(rawTicker, frozen => {
callback(boolToBoolean(frozen));
});
}
const result = await asset.frozen(rawTicker);
return boolToBoolean(result);
}
/**
* Check whether it is possible to transfer a certain amount of this asset between two identities
*
* @param args.from - sender identity (optional, defaults to the current identity)
* @param args.to - receiver identity
* @param args.amount - amount of tokens to transfer
*/
public canTransfer(args: {
from?: string | Identity;
to: string | Identity;
amount: BigNumber;
}): Promise<TransferStatus> {
const { from = this.context.getCurrentIdentity(), to, amount } = args;
return this._canTransfer({ from, to, amount });
}
/**
* Check whether it is possible to mint a certain amount of this asset
*
* @param args.to - receiver identity
* @param args.amount - amount of tokens to mint
*/
public canMint(args: { to: string | Identity; amount: BigNumber }): Promise<TransferStatus> {
const { to, amount } = args;
return this._canTransfer({ from: null, to, amount });
}
/**
* @hidden
*/
private async _canTransfer(args: {
from?: null | string | Identity;
to: string | Identity;
amount: BigNumber;
}): Promise<TransferStatus> {
const {
parent: { ticker },
context: {
polymeshApi: { rpc },
},
context,
} = this;
const { from, to, amount } = args;
let fromDid = null;
if (from) {
fromDid = stringToIdentityId(valueToDid(from), context);
}
const toDid = valueToDid(to);
/*
* The RPC requires a sender account ID (although it's not being used at the moment). We use the current account
* or a dummy account (Alice's in testnet) if the SDK was instanced without one
*/
const senderAddress = context.currentPair?.address || DUMMY_ACCOUNT_ID;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const res: CanTransferResult = await (rpc as any).asset.canTransfer(
stringToAccountId(senderAddress, context),
stringToTicker(ticker, context),
fromDid,
stringToIdentityId(toDid, context),
numberToBalance(amount, context)
);
return canTransferResultToTransferStatus(res);
}
/**
* Transfer an amount of the token to another identity.
*/
public async transfer(args: TransferTokenParams): Promise<TransactionQueue<SecurityToken>> {
const {
parent: { ticker },
context,
} = this;
return transferToken.prepare({ ticker, ...args }, context);
}
}