generated from PolymeshAssociation/typescript-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 11
/
PolymeshTransaction.ts
128 lines (108 loc) · 3.58 KB
/
PolymeshTransaction.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
import { SubmittableExtrinsic } from '@polkadot/api/types';
import { ISubmittableResult } from '@polkadot/types/types';
import BigNumber from 'bignumber.js';
import { Context, PolymeshTransactionBase } from '~/internal';
import { TxTag, TxTags } from '~/types';
import { PolymeshTx, TransactionConstructionData, TransactionSpec } from '~/types/internal';
import { transactionToTxTag } from '~/utils/conversion';
/**
* Wrapper class for a Polymesh Transaction
*/
export class PolymeshTransaction<
ReturnValue,
TransformedReturnValue = ReturnValue,
Args extends unknown[] | [] = unknown[]
> extends PolymeshTransactionBase<ReturnValue, TransformedReturnValue> {
/**
* @hidden
*/
public static override toTransactionSpec<R, T, A extends unknown[] | [] = unknown[]>(
inputTransaction: PolymeshTransaction<R, T, A>
): TransactionSpec<R, A, T> {
const spec = PolymeshTransactionBase.toTransactionSpec(inputTransaction);
const { transaction, args, protocolFee: fee, feeMultiplier } = inputTransaction;
return {
...spec,
transaction,
args,
fee,
feeMultiplier,
} as unknown as TransactionSpec<R, A, T>;
}
/**
* arguments for the transaction in SCALE format (polkadot.js Codec)
*/
public args: Args;
/**
* type of transaction represented by this instance (mostly for display purposes)
*/
public tag: TxTag;
/**
* @hidden
*
* underlying transaction to be executed
*/
private transaction: PolymeshTx<Args>;
/**
* @hidden
*
* amount by which the protocol fees are multiplied. The total fees of some transactions depend on the size of the input.
* For example, when adding documents to an Asset, the fees are proportional to the amount of documents being added
*
* @note defaults to 1
*/
protected feeMultiplier?: BigNumber;
/**
* @hidden
*
* used by procedures to set the protocol fee manually in case the protocol op can't be
* dynamically generated from the transaction name, or a specific procedure has
* special rules for calculating them
*/
private protocolFee?: BigNumber;
/**
* @hidden
*/
constructor(
transactionSpec: TransactionSpec<ReturnValue, Args, TransformedReturnValue> &
TransactionConstructionData,
context: Context
) {
const { args = [], feeMultiplier, transaction, fee, paidForBy, ...rest } = transactionSpec;
super(rest, context);
this.args = args as Args;
this.transaction = transaction;
this.tag = transactionToTxTag(transaction);
this.feeMultiplier = feeMultiplier;
this.protocolFee = fee;
this.paidForBy = paidForBy;
}
// eslint-disable-next-line require-jsdoc
protected composeTx(): SubmittableExtrinsic<'promise', ISubmittableResult> {
const { transaction, args } = this;
return transaction(...args);
}
// eslint-disable-next-line require-jsdoc
public async getProtocolFees(): Promise<BigNumber> {
const { protocolFee, feeMultiplier = new BigNumber(1) } = this;
let fees = protocolFee;
if (!fees) {
const { tag } = this;
[{ fees }] = await this.context.getProtocolFees({ tags: [tag] });
}
return fees.multipliedBy(feeMultiplier);
}
// eslint-disable-next-line require-jsdoc
protected override ignoresSubsidy(): boolean {
/*
* this is the only extrinsic so far that always has to be
* paid by the caller
*/
return this.tag === TxTags.relayer.RemovePayingKey;
}
// eslint-disable-next-line require-jsdoc
public supportsSubsidy(): boolean {
const { tag, context } = this;
return context.supportsSubsidy({ tag });
}
}