-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathtransactionHelper.ts
More file actions
188 lines (162 loc) · 5.59 KB
/
transactionHelper.ts
File metadata and controls
188 lines (162 loc) · 5.59 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import { BigNumber, BigNumberish, Transaction } from 'ethers';
import { Deferrable, resolveProperties } from '@ethersproject/properties';
import { TransactionRequest } from '@ethersproject/abstract-provider';
import { accessListify } from '@ethersproject/transactions';
import { hexlify } from '@ethersproject/bytes';
import { GAS_LIMIT_CHUNK, GAS_MASK, MAX_GAS_LIMIT_CC, ONE_HUNDRED_GWEI, STORAGE_MASK, TEN_GWEI, U32_MAX } from '../consts';
import { ethToNativeDecimal } from './utils';
import { formatter } from './receiptHelper';
type TxConsts = {
storageByteDeposit: BigNumberish;
txFeePerGas: BigNumberish;
};
const divRoundUp = (x: BigNumber, y: BigNumber): BigNumber => {
const mod = x.mod(y);
const div = x.div(y);
return div.add(mod.gt(0) ? 1 : 0);
};
export const calcEthereumTransactionParams = (
data: {
gasLimit: BigNumberish;
storageLimit: BigNumberish;
validUntil: BigNumberish;
} & TxConsts
): {
txGasPrice: BigNumber;
txGasLimit: BigNumber;
} => {
const gasLimit = BigNumber.from(data.gasLimit);
const storageLimit = BigNumber.from(data.storageLimit);
const validUntil = BigNumber.from(data.validUntil);
const storageByteDeposit = BigNumber.from(data.storageByteDeposit);
const txFeePerGas = BigNumber.from(data.txFeePerGas);
const blockPeriod = divRoundUp(validUntil, BigNumber.from(30));
const storageEntryLimit = divRoundUp(storageLimit, BigNumber.from(64));
const storageEntryDeposit = storageByteDeposit.mul(64);
const txGasPrice = txFeePerGas.add(blockPeriod.shl(16)).add(storageEntryLimit);
const txGasLimit = storageEntryDeposit.div(txFeePerGas).mul(storageEntryLimit).add(gasLimit);
return {
txGasPrice,
txGasLimit,
};
};
export const calcSubstrateTransactionParams = (
data: {
txGasPrice: BigNumberish;
txGasLimit: BigNumberish;
} & TxConsts
): {
gasLimit: BigNumber;
storageLimit: BigNumber;
validUntil: BigNumber;
} => {
const txGasPrice = BigNumber.from(data.txGasPrice);
const txGasLimit = BigNumber.from(data.txGasLimit);
const storageByteDeposit = BigNumber.from(data.storageByteDeposit);
const txFeePerGas = BigNumber.from(data.txFeePerGas);
const storageEntryLimit = txGasPrice.and(0xffff);
const blockPeriod = txGasPrice.sub(storageEntryLimit).sub(txFeePerGas).shr(16);
const storageLimit = storageEntryLimit.mul(64);
const validUntil = blockPeriod.mul(30);
const storageEntryDeposit = storageByteDeposit.mul(64);
const gasLimit = txGasLimit.sub(storageEntryDeposit.div(txFeePerGas).mul(storageEntryLimit));
return {
gasLimit,
storageLimit,
validUntil,
};
};
export const getTransactionRequest = async (
transaction: Deferrable<TransactionRequest>
): Promise<Partial<Transaction>> => {
const values: any = await transaction;
const tx: any = {};
['from', 'to'].forEach(key => {
if (values[key] === null || values[key] === undefined) {
return;
}
tx[key] = Promise.resolve(values[key]).then(v => (v ? v : null));
});
['gasLimit', 'gasPrice', 'maxFeePerGas', 'maxPriorityFeePerGas', 'value'].forEach(key => {
if (values[key] === null || values[key] === undefined) {
return;
}
tx[key] = Promise.resolve(values[key]).then(v => (v ? BigNumber.from(v) : null));
});
['type'].forEach(key => {
if (values[key] === null || values[key] === undefined) {
return;
}
tx[key] = Promise.resolve(values[key]).then(v => (v !== null || v !== undefined ? v : null));
});
if (values.accessList) {
tx.accessList = accessListify(values.accessList);
}
['data'].forEach(key => {
if (values[key] === null || values[key] === undefined) {
return;
}
tx[key] = Promise.resolve(values[key]).then(v => (v ? hexlify(v) : null));
});
return formatter.transactionRequest(await resolveProperties(tx));
};
export const encodeGasLimit = (
txFee: BigNumber,
gasPrice: BigNumber,
gasLimit: BigNumber,
usedStorage: BigNumber,
isTokenTransfer = false,
): BigNumber => {
const rawEthGasLimit = txFee.div(gasPrice);
const encodedStorageLimit = usedStorage.gt(0)
? Math.ceil(Math.log2(usedStorage.toNumber()))
: 0;
let encodedGasLimit = gasLimit.div(GAS_LIMIT_CHUNK).add(1);
if (isTokenTransfer) {
// for token transfer, need to make sure when metamask 1.5X gasLimit, it won't affect cc
// bbb => b(b+1)0
encodedGasLimit = encodedGasLimit.div(10).add(1).mul(10);
}
const aaaa00000 = rawEthGasLimit.gt(GAS_MASK)
? rawEthGasLimit.div(GAS_MASK).mul(GAS_MASK)
: BigNumber.from(GAS_MASK);
const bbb00 = encodedGasLimit.mul(STORAGE_MASK);
const cc = encodedStorageLimit;
return aaaa00000.add(bbb00).add(cc); // aaaabbbcc
};
export const decodeEthGas = ({
gasPrice,
gasLimit,
}: {
gasPrice: BigNumber;
gasLimit: BigNumber;
}) => {
const bbbcc = gasLimit.mod(GAS_MASK);
const encodedGasLimit = bbbcc.div(STORAGE_MASK); // bbb
const encodedStorageLimit = bbbcc.mod(STORAGE_MASK); // cc
let tip = 0n;
const tipNumber = gasPrice.div(TEN_GWEI).sub(10);
if (tipNumber.gt(0)) {
gasPrice = gasPrice.sub(tipNumber.mul(TEN_GWEI));
const ethTip = gasPrice.mul(gasLimit).mul(tipNumber).div(10);
tip = ethToNativeDecimal(ethTip).toBigInt();
}
let validUntil = gasPrice.sub(ONE_HUNDRED_GWEI).toBigInt();
if (validUntil > U32_MAX) {
validUntil = U32_MAX;
}
const substrateGasLimit = encodedGasLimit.mul(GAS_LIMIT_CHUNK).toBigInt();
const storageLimit = BigNumber.from(2)
.pow(
encodedStorageLimit.gt(MAX_GAS_LIMIT_CC)
? MAX_GAS_LIMIT_CC
: encodedStorageLimit
)
.toBigInt();
return {
gasLimit: substrateGasLimit,
storageLimit,
tip,
validUntil,
};
};