-
Notifications
You must be signed in to change notification settings - Fork 27
/
generateInterface.ts
160 lines (140 loc) · 4.24 KB
/
generateInterface.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
160
import { writeFile } from 'fs-extra';
import { join } from 'path';
const config = {
genericNames: ['Q', 'W', 'E', 'R', 'T', 'Y'],
};
const capitalize = (str: string): string =>
`${str.charAt(0).toUpperCase()}${str.slice(1)}`;
const numToNth = (nthApiCall: number): string => {
return nthApiCall === 1
? '1st'
: nthApiCall === 2
? '2nd'
: nthApiCall === 3
? '3rd'
: `${nthApiCall}th`;
};
type HelperArgs = {
isFirstCall: boolean;
isPenultimateCall: boolean;
isLastCall: boolean;
generics: string[];
};
const generateReturnType = ({
isFirstCall,
isPenultimateCall,
isLastCall,
generics,
}: HelperArgs) => (methodName: string): string => {
const signerTxType = `Signer${capitalize(methodName)}Tx`;
if (isFirstCall) {
// первый вызов
return `${generics[0]}, ${signerTxType}`;
} else if (isPenultimateCall) {
// предпоследний вызов
return 'SignerTx[]';
} else if (isLastCall) {
// последний вызов
return generics[0];
} else {
// вызовы 2..max-1
return `${generics.join(', ')}, ${signerTxType}`;
}
};
const compile = (currCall: number, max: number): string => {
const isLastCall = currCall === max;
const isFirstCall = currCall === 1;
const generics = config.genericNames.slice(0, currCall);
const curr = `ChainApi${numToNth(currCall)}Call`;
const next = isLastCall
? `ChainApi${numToNth(currCall)}Call`
: `ChainApi${numToNth(currCall + 1)}Call`;
const params = generics
.slice(0, isLastCall ? 1 : currCall)
.map((g) => `${g} extends SignerTx${isLastCall ? '[]' : ''}`)
.join(', ');
let signBroadcastGenerics = generics[0];
if (!isLastCall) {
const genericsStr = generics.join(', ');
signBroadcastGenerics = `[${genericsStr}]`;
}
const returns = generateReturnType({
isFirstCall,
isLastCall,
isPenultimateCall: currCall === max - 1,
generics,
});
return `
export type ${curr}<${params}> = {
issue(data: IssueArgs): ${next}<${returns('issue')}>;
transfer(data: TransferArgs): ${next}<${returns('transfer')}>;
reissue(data: ReissueArgs): ${next}<${returns('reissue')}>;
burn(data: BurnArgs): ${next}<${returns('burn')}>;
lease(data: LeaseArgs): ${next}<${returns('lease')}>;
exchange(data: ExchangeArgs): ${next}<${returns('exchange')}>;
cancelLease(data: CancelLeaseArgs): ${next}<${returns('cancelLease')}>;
alias(data: AliasArgs): ${next}<${returns('alias')}>;
massTransfer(data: MassTransferArgs): ${next}<${returns('massTransfer')}>;
data(data: DataArgs): ${next}<${returns('data')}>;
sponsorship(data: SponsorshipArgs): ${next}<${returns('sponsorship')}>;
setScript(data: SetScriptArgs): ${next}<${returns('setScript')}>;
setAssetScript(data: SetAssetScriptArgs): ${next}<${returns(
'setAssetScript'
)}>;
invoke(data: InvokeArgs): ${next}<${returns('invoke')}>;
sign(): Promise<SignedTx<${signBroadcastGenerics}>>;
broadcast(options?: BroadcastOptions): Promise<BroadcastedTx<SignedTx<${signBroadcastGenerics}>>>;
};
`.trim();
};
const generateApiTypes = (): string => {
let apiTypes = '';
const max = config.genericNames.length + 1;
for (let currCall = 1; currCall <= max; currCall += 1) {
apiTypes = `${apiTypes}\n${compile(currCall, max)}`;
}
return apiTypes;
};
writeFile(
join(__dirname, 'src/types/api.ts'),
`
/* prettier-ignore */
import {
BroadcastOptions,
SignerTx,
SignedTx,
BroadcastedTx,
SignerIssueTx,
SignerTransferTx,
SignerReissueTx,
SignerBurnTx,
SignerLeaseTx,
SignerExchangeTx,
SignerCancelLeaseTx,
SignerAliasTx,
SignerMassTransferTx,
SignerDataTx,
SignerSponsorshipTx,
SignerSetScriptTx,
SignerSetAssetScriptTx,
SignerInvokeTx,
IssueArgs,
TransferArgs,
ReissueArgs,
BurnArgs,
LeaseArgs,
ExchangeArgs,
CancelLeaseArgs,
AliasArgs,
MassTransferArgs,
DataArgs,
SponsorshipArgs,
SetScriptArgs,
SetAssetScriptArgs,
InvokeArgs
} from '.';
${generateApiTypes()}
`
).catch((e) => {
console.error(e);
});