-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtransaction.service.ts
144 lines (130 loc) · 4.09 KB
/
transaction.service.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
import { Injectable } from '@nestjs/common';
import { ethers, BigNumber, Transaction } from 'ethers';
import {
EthEstimateGasParams,
JsonrpcRequestBody,
JsonrpcId,
JsonrpcVersion,
JsonrpcError,
WebhookTransferData,
} from 'src/entities';
import {
TransactionAllow,
getTxAllowList,
} from 'src/config/transactionAllowList';
import { VerseService } from './verse.service';
import { AllowCheckService } from './allowCheck.service';
import { RateLimitService } from './rateLimit.service';
import { WebhookService } from './webhook.service';
@Injectable()
export class TransactionService {
private txAllowList: Array<TransactionAllow>;
constructor(
private verseService: VerseService,
private allowCheckService: AllowCheckService,
private readonly rateLimitService: RateLimitService,
private readonly webhookService: WebhookService,
) {
this.txAllowList = getTxAllowList();
this.txAllowList.forEach((txAllow) => {
this.allowCheckService.checkAddressList(txAllow.fromList);
this.allowCheckService.checkAddressList(txAllow.toList);
});
}
checkContractDeploy(from: string) {
if (this.allowCheckService.isAllowedDeploy(from)) {
return;
} else {
throw new JsonrpcError('deploy transaction is not allowed', -32602);
}
}
async getMatchedTxAllowRule(
from: string,
to: string,
methodId: string,
value: BigNumber,
webhookArg: WebhookTransferData,
): Promise<TransactionAllow> {
let matchedTxAllowRule;
for (const condition of this.txAllowList) {
const fromCheck = this.allowCheckService.isIncludedAddress(
condition.fromList,
from,
);
const toCheck = this.allowCheckService.isIncludedAddress(
condition.toList,
to,
);
const contractCheck = this.allowCheckService.isAllowedContractMethod(
condition.contractMethodList,
methodId,
);
const valueCondition = condition.value;
const valueCheck = this.allowCheckService.isAllowedValue(
valueCondition,
value,
);
if (fromCheck && toCheck && contractCheck && valueCheck) {
if (condition.rateLimit)
await this.rateLimitService.checkRateLimit(
from,
to,
methodId,
condition.rateLimit,
);
if (condition.webhooks) {
await this.webhookService.checkWebhook(
webhookArg,
condition.webhooks,
);
}
matchedTxAllowRule = condition;
break;
}
}
if (!matchedTxAllowRule)
throw new JsonrpcError('transaction is not allowed', -32602);
return matchedTxAllowRule;
}
async checkAllowedGas(
tx: Transaction,
jsonrpc: JsonrpcVersion,
id: JsonrpcId,
): Promise<void> {
const ethCallParams: EthEstimateGasParams = {
nonce: ethers.utils.hexValue(BigNumber.from(tx.nonce)),
gas: ethers.utils.hexValue(tx.gasLimit),
value: ethers.utils.hexValue(tx.value),
data: tx.data,
chainId: ethers.utils.hexValue(BigNumber.from(tx.chainId)),
};
if (tx.type)
ethCallParams['type'] = ethers.utils.hexValue(BigNumber.from(tx.type));
if (tx.from) ethCallParams['from'] = tx.from;
if (tx.to) ethCallParams['to'] = tx.to;
if (tx.gasPrice)
ethCallParams['gasPrice'] = ethers.utils.hexValue(tx.gasPrice);
if (tx.maxPriorityFeePerGas)
ethCallParams['maxPriorityFeePerGas'] = ethers.utils.hexValue(
tx.maxPriorityFeePerGas,
);
if (tx.maxFeePerGas)
ethCallParams['maxFeePerGas'] = ethers.utils.hexValue(tx.maxFeePerGas);
if (tx.accessList) ethCallParams['accessList'] = tx.accessList;
const params = [ethCallParams];
const headers = {};
const body: JsonrpcRequestBody = {
jsonrpc: jsonrpc,
id: id,
method: 'eth_estimateGas',
params: params,
};
const { data } = await this.verseService.post(headers, body);
if (data.error) {
throw new JsonrpcError(data.error.message, -32602);
}
}
parseRawTx(rawTx: string): ethers.Transaction {
return ethers.utils.parseTransaction(rawTx);
}
}