generated from PolymeshAssociation/typescript-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Settlements.ts
61 lines (55 loc) · 1.97 KB
/
Settlements.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
import BigNumber from 'bignumber.js';
import { CanTransferResult } from 'polymesh-types/types';
import { Identity, Namespace, SecurityToken } from '~/api/entities';
import { TransferStatus } from '~/types';
import {
canTransferResultToTransferStatus,
numberToBalance,
portfolioIdToMeshPortfolioId,
signerToString,
stringToAccountId,
stringToTicker,
} from '~/utils';
import { DUMMY_ACCOUNT_ID } from '~/utils/constants';
/**
* Handles all Security Token Settlements related functionality
*/
export class Settlements extends Namespace<SecurityToken> {
/**
* 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 async canSettle(args: {
from?: string | Identity;
to: string | Identity;
amount: BigNumber;
}): Promise<TransferStatus> {
const {
parent: { ticker },
context: {
polymeshApi: { rpc },
},
context,
} = this;
const { from = await this.context.getCurrentIdentity(), to, amount } = args;
/*
* 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),
null,
portfolioIdToMeshPortfolioId({ did: signerToString(from) }, context),
null,
portfolioIdToMeshPortfolioId({ did: signerToString(to) }, context),
stringToTicker(ticker, context),
numberToBalance(amount, context)
);
return canTransferResultToTransferStatus(res);
}
}