@instadapp/interop-x 0.0.0-dev.80722d6 → 0.0.0-dev.8a0297a
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/interop-x +1 -1
- package/dist/package.json +72 -0
- package/dist/{abi → src/abi}/erc20.json +0 -0
- package/dist/{abi → src/abi}/gnosisSafe.json +0 -0
- package/dist/{abi → src/abi}/index.js +0 -0
- package/dist/{abi → src/abi}/interopBridgeToken.json +0 -0
- package/dist/{abi → src/abi}/interopXGateway.json +0 -0
- package/dist/src/api/index.js +33 -0
- package/dist/{config → src/config}/index.js +0 -0
- package/dist/{constants → src/constants}/addresses.js +0 -8
- package/dist/{constants → src/constants}/index.js +1 -0
- package/dist/src/constants/itokens.js +13 -0
- package/dist/{constants → src/constants}/tokens.js +0 -0
- package/dist/{db → src/db}/index.js +0 -0
- package/dist/{db → src/db}/models/index.js +0 -0
- package/dist/{db → src/db}/models/transaction.js +3 -1
- package/dist/{db → src/db}/sequelize.js +0 -0
- package/dist/{index.js → src/index.js} +5 -0
- package/dist/{logger → src/logger}/index.js +0 -0
- package/dist/{net → src/net}/index.js +0 -0
- package/dist/{net → src/net}/peer/index.js +6 -2
- package/dist/{net → src/net}/pool/index.js +9 -2
- package/dist/{net → src/net}/protocol/dial/BaseDialProtocol.js +0 -0
- package/dist/{net → src/net}/protocol/dial/SignatureDialProtocol.js +15 -12
- package/dist/{net → src/net}/protocol/index.js +0 -0
- package/dist/{tasks → src/tasks}/BaseTask.js +1 -1
- package/dist/src/tasks/InteropXGateway/ProcessDepositEvents.js +151 -0
- package/dist/{tasks → src/tasks}/InteropXGateway/SyncDepositEvents.js +14 -18
- package/dist/{tasks → src/tasks}/index.js +4 -0
- package/dist/{typechain → src/typechain}/Erc20.js +0 -0
- package/dist/{typechain → src/typechain}/GnosisSafe.js +0 -0
- package/dist/{typechain → src/typechain}/InteropBridgeToken.js +0 -0
- package/dist/{typechain → src/typechain}/InteropXGateway.js +0 -0
- package/dist/{typechain → src/typechain}/common.js +0 -0
- package/dist/{typechain → src/typechain}/factories/Erc20__factory.js +0 -0
- package/dist/{typechain → src/typechain}/factories/GnosisSafe__factory.js +0 -0
- package/dist/{typechain → src/typechain}/factories/InteropBridgeToken__factory.js +0 -0
- package/dist/{typechain → src/typechain}/factories/InteropXGateway__factory.js +0 -0
- package/dist/{typechain → src/typechain}/factories/index.js +0 -0
- package/dist/{typechain → src/typechain}/index.js +0 -0
- package/dist/{types.js → src/types.js} +0 -0
- package/dist/src/utils/index.js +178 -0
- package/package.json +9 -3
- package/patches/@ethersproject+properties+5.6.0.patch +13 -0
- package/src/api/index.ts +33 -0
- package/src/constants/addresses.ts +0 -8
- package/src/constants/index.ts +1 -0
- package/src/constants/itokens.ts +10 -0
- package/src/db/models/transaction.ts +8 -4
- package/src/index.ts +7 -0
- package/src/net/peer/index.ts +7 -6
- package/src/net/pool/index.ts +12 -2
- package/src/net/protocol/dial/SignatureDialProtocol.ts +17 -13
- package/src/tasks/BaseTask.ts +1 -1
- package/src/tasks/InteropXGateway/ProcessDepositEvents.ts +245 -0
- package/src/tasks/InteropXGateway/SyncDepositEvents.ts +21 -9
- package/src/tasks/index.ts +7 -2
- package/src/utils/index.ts +113 -4
- package/dist/utils/index.js +0 -101
@@ -0,0 +1,245 @@
|
|
1
|
+
import { BaseTask } from "../BaseTask";
|
2
|
+
import Logger from '@/logger';
|
3
|
+
import { BigNumber, ethers } from "ethers";
|
4
|
+
import abi from "@/abi";
|
5
|
+
import { Transaction } from "@/db";
|
6
|
+
import { buildDataForTransaction, buildSignatureBytes, getContract, getRpcProviderUrl, Signature } from "@/utils";
|
7
|
+
import { addresses } from "@/constants";
|
8
|
+
import { ChainId } from "@/types";
|
9
|
+
import config from "@/config";
|
10
|
+
import { GnosisSafe, InteropXGateway } from "@/typechain";
|
11
|
+
import { Op } from "sequelize";
|
12
|
+
import wait from "waait";
|
13
|
+
import { peerPool, protocol } from "@/net";
|
14
|
+
import { LogDescription } from "ethers/lib/utils";
|
15
|
+
|
16
|
+
const generateGnosisTransaction = async (transactionData: any, safeContract: GnosisSafe) => {
|
17
|
+
console.log(transactionData);
|
18
|
+
|
19
|
+
let isExecuted = await safeContract.dataHashes(
|
20
|
+
await safeContract.getTransactionHash(
|
21
|
+
transactionData.to,
|
22
|
+
transactionData.value,
|
23
|
+
transactionData.data,
|
24
|
+
transactionData.operation,
|
25
|
+
transactionData.safeTxGas,
|
26
|
+
transactionData.baseGas,
|
27
|
+
transactionData.gasPrice,
|
28
|
+
transactionData.gasToken,
|
29
|
+
transactionData.refundReceiver,
|
30
|
+
transactionData.nonce
|
31
|
+
)
|
32
|
+
)
|
33
|
+
|
34
|
+
while (isExecuted == 1) {
|
35
|
+
transactionData.safeTxGas = BigNumber.from(String(transactionData.safeTxGas)).add(1).toString()
|
36
|
+
|
37
|
+
isExecuted = await safeContract.dataHashes(
|
38
|
+
await safeContract.getTransactionHash(
|
39
|
+
transactionData.to,
|
40
|
+
transactionData.value,
|
41
|
+
transactionData.data,
|
42
|
+
transactionData.operation,
|
43
|
+
transactionData.safeTxGas,
|
44
|
+
transactionData.baseGas,
|
45
|
+
transactionData.gasPrice,
|
46
|
+
transactionData.gasToken,
|
47
|
+
transactionData.refundReceiver,
|
48
|
+
transactionData.nonce
|
49
|
+
)
|
50
|
+
)
|
51
|
+
}
|
52
|
+
|
53
|
+
return transactionData
|
54
|
+
}
|
55
|
+
|
56
|
+
class ProcessDepositEvents extends BaseTask {
|
57
|
+
contractAddress: string;
|
58
|
+
provider: ethers.providers.JsonRpcProvider;
|
59
|
+
contract: InteropXGateway;
|
60
|
+
chainId: ChainId;
|
61
|
+
leadNodeOnly = true
|
62
|
+
|
63
|
+
constructor({ chainId }: { chainId: ChainId }) {
|
64
|
+
super({
|
65
|
+
logger: new Logger("InteropXGateway::ProcessDepositEvents"),
|
66
|
+
})
|
67
|
+
this.chainId = chainId;
|
68
|
+
}
|
69
|
+
|
70
|
+
async pollHandler() {
|
71
|
+
const blockNumber = await this.provider.getBlockNumber()
|
72
|
+
|
73
|
+
const transaction = await Transaction.findOne({
|
74
|
+
where: {
|
75
|
+
status: 'pending',
|
76
|
+
sourceStatus: 'success',
|
77
|
+
targetStatus: 'uninitialised',
|
78
|
+
action: 'deposit',
|
79
|
+
sourceCreatedAt: {
|
80
|
+
[Op.gte]: new Date(Date.now() - 12 * 60 * 60 * 1000),
|
81
|
+
},
|
82
|
+
sourceBlockNumber: {
|
83
|
+
[Op.lt]: blockNumber - 12,
|
84
|
+
},
|
85
|
+
sourceChainId: this.chainId,
|
86
|
+
}
|
87
|
+
})
|
88
|
+
|
89
|
+
if (!transaction) {
|
90
|
+
return;
|
91
|
+
}
|
92
|
+
|
93
|
+
|
94
|
+
transaction.targetStatus = 'pending';
|
95
|
+
await transaction.save();
|
96
|
+
|
97
|
+
|
98
|
+
// refresh event data?
|
99
|
+
|
100
|
+
const targetChainProvider = new ethers.providers.JsonRpcProvider(
|
101
|
+
getRpcProviderUrl(transaction.targetChainId as ChainId)
|
102
|
+
);
|
103
|
+
|
104
|
+
const targetWallet = new ethers.Wallet(config.privateKey!, targetChainProvider);
|
105
|
+
|
106
|
+
const safeAddress = addresses[transaction.targetChainId].gnosisSafe;
|
107
|
+
|
108
|
+
|
109
|
+
const safeContract = getContract<GnosisSafe>(
|
110
|
+
safeAddress,
|
111
|
+
abi.gnosisSafe,
|
112
|
+
targetWallet
|
113
|
+
)
|
114
|
+
|
115
|
+
const ownersThreshold = await safeContract.getThreshold();
|
116
|
+
await wait(10000);
|
117
|
+
|
118
|
+
let gnosisTx = await generateGnosisTransaction({
|
119
|
+
baseGas: "0",
|
120
|
+
data: await buildDataForTransaction(transaction),
|
121
|
+
gasPrice: "0",
|
122
|
+
gasToken: "0x0000000000000000000000000000000000000000",
|
123
|
+
nonce: '0',
|
124
|
+
operation: "1",
|
125
|
+
refundReceiver: "0x0000000000000000000000000000000000000000",
|
126
|
+
safeAddress: safeAddress,
|
127
|
+
safeTxGas: "79668",
|
128
|
+
to: addresses[transaction.targetChainId].multisend,
|
129
|
+
value: "0",
|
130
|
+
}, safeContract);
|
131
|
+
|
132
|
+
const owners = await safeContract.getOwners().then(owners => owners.map(owner => owner.toLowerCase()));
|
133
|
+
|
134
|
+
const ownerPeerIds = peerPool.activePeers.filter(peer => owners.includes(peer.publicAddress.toLowerCase())).map(peer => peer.id)
|
135
|
+
|
136
|
+
console.log(`Collecting signatures for execution ${transaction.transactionHash}`)
|
137
|
+
|
138
|
+
const signatures = await protocol.requestSignatures({
|
139
|
+
type: 'source',
|
140
|
+
transactionHash: transaction.transactionHash,
|
141
|
+
safeTxGas: gnosisTx.safeTxGas,
|
142
|
+
safeNonce: gnosisTx.nonce
|
143
|
+
}, ownerPeerIds)
|
144
|
+
|
145
|
+
|
146
|
+
const validSignatures = signatures.filter(s => !!s.data && s.data !== '0x') as Signature[];
|
147
|
+
|
148
|
+
console.log({ signatures, validSignatures, ownersThreshold: ownersThreshold.toString() });
|
149
|
+
|
150
|
+
if (validSignatures.length === 0 || ownersThreshold.gt(validSignatures.length)) {
|
151
|
+
await transaction.save();
|
152
|
+
transaction.targetDelayUntil = new Date(Date.now() + 30 * 1000);
|
153
|
+
transaction.targetStatus = 'pending'
|
154
|
+
|
155
|
+
await transaction.save();
|
156
|
+
const errorMessage = signatures.find(s => !!s.error)?.error;
|
157
|
+
throw new Error(`Not enough signatures` + (errorMessage ? `: ${errorMessage}` : ''));
|
158
|
+
}
|
159
|
+
|
160
|
+
const execTransactionParams = [
|
161
|
+
gnosisTx.to,
|
162
|
+
gnosisTx.value,
|
163
|
+
gnosisTx.data,
|
164
|
+
gnosisTx.operation,
|
165
|
+
gnosisTx.safeTxGas,
|
166
|
+
gnosisTx.baseGas,
|
167
|
+
gnosisTx.gasPrice,
|
168
|
+
gnosisTx.gasToken,
|
169
|
+
gnosisTx.refundReceiver,
|
170
|
+
buildSignatureBytes(validSignatures),
|
171
|
+
];
|
172
|
+
|
173
|
+
console.log(`Executing transaction for execution ${transaction.transactionHash}`)
|
174
|
+
|
175
|
+
console.log({
|
176
|
+
execTransactionParams
|
177
|
+
})
|
178
|
+
|
179
|
+
const { data: txData } = await safeContract.populateTransaction.execTransaction(
|
180
|
+
gnosisTx.to,
|
181
|
+
gnosisTx.value,
|
182
|
+
gnosisTx.data,
|
183
|
+
gnosisTx.operation,
|
184
|
+
gnosisTx.safeTxGas,
|
185
|
+
gnosisTx.baseGas,
|
186
|
+
gnosisTx.gasPrice,
|
187
|
+
gnosisTx.gasToken,
|
188
|
+
gnosisTx.refundReceiver,
|
189
|
+
buildSignatureBytes(validSignatures)
|
190
|
+
);
|
191
|
+
|
192
|
+
console.log({
|
193
|
+
from: targetWallet.address,
|
194
|
+
gasPrice: BigNumber.from(120 * 10 ** 9).toString(),
|
195
|
+
gasLimit: BigNumber.from(6_000_000).toString(),
|
196
|
+
to: safeAddress,
|
197
|
+
data: txData,
|
198
|
+
})
|
199
|
+
return;
|
200
|
+
|
201
|
+
const txSent = await targetWallet.sendTransaction({
|
202
|
+
from: targetWallet.address,
|
203
|
+
gasPrice: BigNumber.from(120 * 10 ** 9),
|
204
|
+
gasLimit: BigNumber.from(6_000_000),
|
205
|
+
to: safeAddress,
|
206
|
+
data: txData,
|
207
|
+
})
|
208
|
+
|
209
|
+
const receipt = await txSent.wait();
|
210
|
+
|
211
|
+
const parsedLogs: LogDescription[] = [];
|
212
|
+
|
213
|
+
receipt.logs.forEach((log) => {
|
214
|
+
try {
|
215
|
+
parsedLogs.push(safeContract.interface.parseLog(log));
|
216
|
+
} catch (e) { }
|
217
|
+
});
|
218
|
+
|
219
|
+
if (parsedLogs.find(e => e.name === 'ExecutionSuccess')) {
|
220
|
+
console.log('ExecutionSuccess')
|
221
|
+
} else {
|
222
|
+
console.log('ExecutionFailure')
|
223
|
+
}
|
224
|
+
}
|
225
|
+
|
226
|
+
async start(): Promise<void> {
|
227
|
+
this.logger.info(`Starting execution watcher on interop chain`);
|
228
|
+
|
229
|
+
this.contractAddress = addresses[this.chainId].interopXGateway;
|
230
|
+
|
231
|
+
this.provider = new ethers.providers.JsonRpcProvider(
|
232
|
+
getRpcProviderUrl(this.chainId)
|
233
|
+
);
|
234
|
+
|
235
|
+
this.contract = getContract<InteropXGateway>(
|
236
|
+
this.contractAddress,
|
237
|
+
abi.interopXGateway,
|
238
|
+
new ethers.Wallet(config.privateKey!, this.provider)
|
239
|
+
);
|
240
|
+
|
241
|
+
await super.start()
|
242
|
+
}
|
243
|
+
}
|
244
|
+
|
245
|
+
export default ProcessDepositEvents;
|
@@ -3,7 +3,7 @@ import Logger from '@/logger';
|
|
3
3
|
import { ethers } from "ethers";
|
4
4
|
import abi from "@/abi";
|
5
5
|
import { Transaction } from "@/db";
|
6
|
-
import { generateInteropTransactionHash, getRpcProviderUrl } from "@/utils";
|
6
|
+
import { generateInteropTransactionHash, getContract, getRpcProviderUrl } from "@/utils";
|
7
7
|
import { addresses } from "@/constants";
|
8
8
|
import { ChainId } from "@/types";
|
9
9
|
import config from "@/config";
|
@@ -43,8 +43,8 @@ class SyncDepositEvents extends BaseTask {
|
|
43
43
|
const { sourceChainId, targetChainId, user, vnonce, amount, token } = event.args;
|
44
44
|
|
45
45
|
const uniqueIdentifier = {
|
46
|
-
|
47
|
-
|
46
|
+
action: 'deposit',
|
47
|
+
submitTransactionHash: event.transactionHash,
|
48
48
|
sourceChainId: sourceChainId.toNumber(),
|
49
49
|
targetChainId: targetChainId.toNumber(),
|
50
50
|
}
|
@@ -56,17 +56,20 @@ class SyncDepositEvents extends BaseTask {
|
|
56
56
|
const tx = await event.getTransaction()
|
57
57
|
|
58
58
|
await Transaction.create({
|
59
|
+
...uniqueIdentifier,
|
59
60
|
transactionHash: generateInteropTransactionHash(uniqueIdentifier),
|
60
|
-
type: 'deposit',
|
61
61
|
from: tx.from,
|
62
62
|
to: user,
|
63
63
|
|
64
|
-
|
64
|
+
|
65
|
+
submitTransactionHash: event.transactionHash,
|
66
|
+
submitBlockNumber: event.blockNumber,
|
67
|
+
|
68
|
+
// submit & source are the same
|
65
69
|
sourceTransactionHash: event.transactionHash,
|
66
70
|
sourceBlockNumber: event.blockNumber,
|
67
|
-
sourceStatus: "
|
71
|
+
sourceStatus: "success",
|
68
72
|
|
69
|
-
targetChainId: targetChainId.toNumber(),
|
70
73
|
targetStatus: "uninitialised",
|
71
74
|
|
72
75
|
submitEvent: {
|
@@ -77,6 +80,15 @@ class SyncDepositEvents extends BaseTask {
|
|
77
80
|
ammout: amount.toString(),
|
78
81
|
vnonce: vnonce.toString(),
|
79
82
|
},
|
83
|
+
|
84
|
+
sourceEvent: {
|
85
|
+
user,
|
86
|
+
sourceChainId: sourceChainId.toString(),
|
87
|
+
targetChainId: targetChainId.toString(),
|
88
|
+
token: token,
|
89
|
+
ammout: amount.toString(),
|
90
|
+
vnonce: vnonce.toString(),
|
91
|
+
},
|
80
92
|
status: "pending",
|
81
93
|
})
|
82
94
|
|
@@ -101,11 +113,11 @@ class SyncDepositEvents extends BaseTask {
|
|
101
113
|
getRpcProviderUrl(this.chainId)
|
102
114
|
);
|
103
115
|
|
104
|
-
this.contract =
|
116
|
+
this.contract = getContract<InteropXGateway>(
|
105
117
|
this.contractAddress,
|
106
118
|
abi.interopXGateway,
|
107
119
|
new ethers.Wallet(config.privateKey!, this.provider)
|
108
|
-
)
|
120
|
+
);
|
109
121
|
|
110
122
|
await super.start()
|
111
123
|
}
|
package/src/tasks/index.ts
CHANGED
@@ -1,10 +1,15 @@
|
|
1
1
|
import { BaseTask } from "./BaseTask";
|
2
|
-
import
|
2
|
+
import InteropXGatewayProcessDepositEvents from "./InteropXGateway/ProcessDepositEvents";
|
3
|
+
import InteropXGatewaySyncDepositEvents from "./InteropXGateway/SyncDepositEvents";
|
3
4
|
|
4
5
|
export class Tasks {
|
5
6
|
|
6
7
|
tasks: BaseTask[] = [
|
7
|
-
new
|
8
|
+
new InteropXGatewaySyncDepositEvents({
|
9
|
+
chainId: 43114
|
10
|
+
}),
|
11
|
+
|
12
|
+
new InteropXGatewayProcessDepositEvents({
|
8
13
|
chainId: 43114
|
9
14
|
})
|
10
15
|
];
|
package/src/utils/index.ts
CHANGED
@@ -3,9 +3,14 @@
|
|
3
3
|
*/
|
4
4
|
import axios from 'axios'
|
5
5
|
import axiosRetry from "axios-retry";
|
6
|
-
import { addresses } from '@/constants';
|
6
|
+
import { addresses, itokens, tokens } from '@/constants';
|
7
7
|
import { ChainId } from '@/types'
|
8
8
|
import { ethers } from 'ethers';
|
9
|
+
import { encodeMulti, MetaTransaction, OperationType } from 'ethers-multisend';
|
10
|
+
import { Transaction } from '@/db';
|
11
|
+
import config from '@/config';
|
12
|
+
import abi from '@/abi';
|
13
|
+
import { InteropBridgeToken } from '@/typechain';
|
9
14
|
|
10
15
|
export const http = axios.create();
|
11
16
|
|
@@ -119,11 +124,115 @@ export const asyncCallWithTimeout = async <T>(asyncPromise: Promise<T>, timeout:
|
|
119
124
|
}
|
120
125
|
|
121
126
|
|
122
|
-
export const generateInteropTransactionHash = (data: {
|
127
|
+
export const generateInteropTransactionHash = (data: { action: string, submitTransactionHash: string, sourceChainId: string | number, targetChainId: string | number }) => {
|
123
128
|
return ethers.utils.solidityKeccak256(['string', 'string', 'string', 'string'], [
|
124
|
-
String(data.
|
125
|
-
String(data.
|
129
|
+
String(data.action),
|
130
|
+
String(data.submitTransactionHash),
|
126
131
|
String(data.sourceChainId),
|
127
132
|
String(data.targetChainId),
|
128
133
|
]);
|
134
|
+
}
|
135
|
+
|
136
|
+
export const buildDataForTransaction = async (transaction: Transaction, type?: 'source' | 'target') => {
|
137
|
+
type = type || transaction.sourceStatus === 'pending' ? 'source' : 'target';
|
138
|
+
|
139
|
+
const transactions: MetaTransaction[] = [];
|
140
|
+
|
141
|
+
if (transaction.action != 'deposit') {
|
142
|
+
throw new Error('Invalid action');
|
143
|
+
}
|
144
|
+
|
145
|
+
if (transaction.action === 'deposit' && transaction.sourceStatus === 'pending') {
|
146
|
+
throw Error('Cannot build data for pending deposit transaction');
|
147
|
+
}
|
148
|
+
|
149
|
+
if (!transaction.submitEvent) {
|
150
|
+
throw Error('Cannot build data for transaction without submitEvent');
|
151
|
+
}
|
152
|
+
|
153
|
+
|
154
|
+
const token = tokens[transaction.sourceChainId].find(token => token.address.toLowerCase() === transaction.submitEvent.token.toLowerCase());
|
155
|
+
|
156
|
+
if (!token) {
|
157
|
+
throw Error('Cannot build data for transaction without token');
|
158
|
+
}
|
159
|
+
|
160
|
+
const itoken = itokens[transaction.targetChainId].find(itoken => itoken.symbol.toLowerCase() === token.symbol.toLowerCase());
|
161
|
+
|
162
|
+
if (!itoken) {
|
163
|
+
throw Error('Cannot build data for transaction without itoken');
|
164
|
+
}
|
165
|
+
|
166
|
+
const targetChainProvider = new ethers.providers.JsonRpcProvider(getRpcProviderUrl(transaction.targetChainId as ChainId));
|
167
|
+
const targetWallet = new ethers.Wallet(config.privateKey, targetChainProvider);
|
168
|
+
const interopBridgeContract = getContract<InteropBridgeToken>(itoken.address, abi.interopBridgeToken, targetWallet);
|
169
|
+
|
170
|
+
const { data } = await interopBridgeContract.populateTransaction.mint(
|
171
|
+
transaction.submitEvent.user,
|
172
|
+
ethers.BigNumber.from(transaction.submitEvent.amount),
|
173
|
+
transaction.sourceChainId,
|
174
|
+
transaction.sourceTransactionHash,
|
175
|
+
);
|
176
|
+
|
177
|
+
transactions.push({
|
178
|
+
to: itoken.address,
|
179
|
+
data: data!,
|
180
|
+
value: '0',
|
181
|
+
operation: OperationType.Call,
|
182
|
+
});
|
183
|
+
|
184
|
+
return encodeMulti(transactions).data
|
185
|
+
}
|
186
|
+
|
187
|
+
|
188
|
+
export function getContract<TContract extends ethers.Contract>(address: string, contractInterface: ethers.ContractInterface | any, signerOrProvider?: ethers.Signer | ethers.providers.Provider) {
|
189
|
+
if (!ethers.utils.getAddress(address) || address === ethers.constants.AddressZero) {
|
190
|
+
throw Error(`Invalid 'address' parameter '${address}'.`)
|
191
|
+
}
|
192
|
+
|
193
|
+
const contract = new ethers.Contract(
|
194
|
+
address,
|
195
|
+
contractInterface,
|
196
|
+
signerOrProvider
|
197
|
+
) as TContract
|
198
|
+
|
199
|
+
|
200
|
+
return new Proxy(contract, {
|
201
|
+
get(target, prop, receiver) {
|
202
|
+
const value = Reflect.get(target, prop, receiver);
|
203
|
+
|
204
|
+
if (typeof value === 'function' && contract.functions.hasOwnProperty(prop)) {
|
205
|
+
return async (...args: any[]) => {
|
206
|
+
try {
|
207
|
+
return await value(...args);
|
208
|
+
} catch (error) {
|
209
|
+
throw new Error(`Error calling "${String(prop)}" on "${address}": ${error.reason || error.message}`)
|
210
|
+
}
|
211
|
+
}
|
212
|
+
}
|
213
|
+
|
214
|
+
|
215
|
+
if (typeof value === 'object' && ['populateTransaction', 'estimateGas', 'functions', 'callStatic'].includes(String(prop))) {
|
216
|
+
const parentProp = String(prop);
|
217
|
+
|
218
|
+
return new Proxy(value, {
|
219
|
+
get(target, prop, receiver) {
|
220
|
+
const value = Reflect.get(target, prop, receiver);
|
221
|
+
|
222
|
+
if (typeof value === 'function') {
|
223
|
+
return async (...args: any[]) => {
|
224
|
+
try {
|
225
|
+
return await value(...args);
|
226
|
+
} catch (error) {
|
227
|
+
throw new Error(`Error calling "${String(prop)}" using "${parentProp}" on "${address}": ${error.reason || error.message}`)
|
228
|
+
}
|
229
|
+
}
|
230
|
+
}
|
231
|
+
}
|
232
|
+
})
|
233
|
+
}
|
234
|
+
|
235
|
+
return value;
|
236
|
+
},
|
237
|
+
});
|
129
238
|
}
|
package/dist/utils/index.js
DELETED
@@ -1,101 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
-
};
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
-
exports.generateInteropTransactionHash = exports.asyncCallWithTimeout = exports.buildSignatureBytes = exports.getRpcProviderUrl = exports.signGnosisSafeTx = exports.short = exports.http = void 0;
|
7
|
-
/**
|
8
|
-
* @module util
|
9
|
-
*/
|
10
|
-
const axios_1 = __importDefault(require("axios"));
|
11
|
-
const axios_retry_1 = __importDefault(require("axios-retry"));
|
12
|
-
const constants_1 = require("@/constants");
|
13
|
-
const ethers_1 = require("ethers");
|
14
|
-
exports.http = axios_1.default.create();
|
15
|
-
(0, axios_retry_1.default)(exports.http, { retries: 3, retryDelay: axios_retry_1.default.exponentialDelay });
|
16
|
-
function short(buffer) {
|
17
|
-
return buffer.toString('hex').slice(0, 8) + '...';
|
18
|
-
}
|
19
|
-
exports.short = short;
|
20
|
-
const signGnosisSafeTx = async ({ to, data = null, value = '0', operation = '1', baseGas = '0', gasPrice = "0", gasToken = "0x0000000000000000000000000000000000000000", refundReceiver = "0x0000000000000000000000000000000000000000", safeTxGas = "79668", nonce = "0", chainId = 137, }, { signer }) => {
|
21
|
-
const gnosisSafe = constants_1.addresses[chainId].gnosisSafe;
|
22
|
-
const domain = {
|
23
|
-
verifyingContract: gnosisSafe,
|
24
|
-
chainId,
|
25
|
-
};
|
26
|
-
const types = {
|
27
|
-
SafeTx: [
|
28
|
-
{ type: 'address', name: 'to' },
|
29
|
-
{ type: 'uint256', name: 'value' },
|
30
|
-
{ type: 'bytes', name: 'data' },
|
31
|
-
{ type: 'uint8', name: 'operation' },
|
32
|
-
{ type: 'uint256', name: 'safeTxGas' },
|
33
|
-
{ type: 'uint256', name: 'baseGas' },
|
34
|
-
{ type: 'uint256', name: 'gasPrice' },
|
35
|
-
{ type: 'address', name: 'gasToken' },
|
36
|
-
{ type: 'address', name: 'refundReceiver' },
|
37
|
-
{ type: 'uint256', name: 'nonce' },
|
38
|
-
],
|
39
|
-
};
|
40
|
-
const message = {
|
41
|
-
baseGas,
|
42
|
-
data,
|
43
|
-
gasPrice,
|
44
|
-
gasToken,
|
45
|
-
nonce: Number(nonce),
|
46
|
-
operation,
|
47
|
-
refundReceiver,
|
48
|
-
safeAddress: gnosisSafe,
|
49
|
-
safeTxGas: String(safeTxGas),
|
50
|
-
to,
|
51
|
-
value,
|
52
|
-
};
|
53
|
-
return await signer._signTypedData(domain, types, message);
|
54
|
-
};
|
55
|
-
exports.signGnosisSafeTx = signGnosisSafeTx;
|
56
|
-
const getRpcProviderUrl = (chainId) => {
|
57
|
-
switch (chainId) {
|
58
|
-
case 1:
|
59
|
-
return 'https://rpc.instadapp.io/mainnet';
|
60
|
-
case 137:
|
61
|
-
return 'https://rpc.instadapp.io/polygon';
|
62
|
-
case 43114:
|
63
|
-
return 'https://rpc.instadapp.io/avalanche';
|
64
|
-
default:
|
65
|
-
throw new Error(`Unknown chainId: ${chainId}`);
|
66
|
-
}
|
67
|
-
};
|
68
|
-
exports.getRpcProviderUrl = getRpcProviderUrl;
|
69
|
-
const buildSignatureBytes = (signatures) => {
|
70
|
-
signatures.sort((left, right) => left.signer.toLowerCase().localeCompare(right.signer.toLowerCase()));
|
71
|
-
let signatureBytes = "0x";
|
72
|
-
for (const sig of signatures) {
|
73
|
-
signatureBytes += sig.data.slice(2);
|
74
|
-
}
|
75
|
-
return signatureBytes;
|
76
|
-
};
|
77
|
-
exports.buildSignatureBytes = buildSignatureBytes;
|
78
|
-
/**
|
79
|
-
* Call an async function with a maximum time limit (in milliseconds) for the timeout
|
80
|
-
* Resolved promise for async function call, or an error if time limit reached
|
81
|
-
*/
|
82
|
-
const asyncCallWithTimeout = async (asyncPromise, timeout) => {
|
83
|
-
let timeoutHandle;
|
84
|
-
const timeoutPromise = new Promise((_resolve, reject) => {
|
85
|
-
timeoutHandle = setTimeout(() => reject(new Error('Async call timeout limit reached')), timeout);
|
86
|
-
});
|
87
|
-
return Promise.race([asyncPromise, timeoutPromise]).then(result => {
|
88
|
-
clearTimeout(timeoutHandle);
|
89
|
-
return result;
|
90
|
-
});
|
91
|
-
};
|
92
|
-
exports.asyncCallWithTimeout = asyncCallWithTimeout;
|
93
|
-
const generateInteropTransactionHash = (data) => {
|
94
|
-
return ethers_1.ethers.utils.solidityKeccak256(['string', 'string', 'string', 'string'], [
|
95
|
-
String(data.type),
|
96
|
-
String(data.sourceTransactionHash),
|
97
|
-
String(data.sourceChainId),
|
98
|
-
String(data.targetChainId),
|
99
|
-
]);
|
100
|
-
};
|
101
|
-
exports.generateInteropTransactionHash = generateInteropTransactionHash;
|