-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathInputAdded.ts
284 lines (247 loc) · 9.58 KB
/
InputAdded.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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import { DataHandlerContext } from '@subsquid/evm-processor';
import { Store } from '@subsquid/typeorm-store';
import { AbiCoder, dataSlice, getUint } from 'ethers';
import { Contract as ERC20 } from '../abi/ERC20';
import { Contract as ERC721 } from '../abi/ERC721';
import { events } from '../abi/InputBox';
import {
ERC1155BatchPortalAddress,
ERC1155SinglePortalAddress,
ERC20PortalAddress,
ERC721PortalAddress,
InputBoxAddress,
} from '../config';
import {
Application,
Chain,
Erc1155Deposit,
Erc1155Transfer,
Erc20Deposit,
Erc721Deposit,
Input,
MultiToken,
NFT,
Token,
} from '../model';
import { BlockData, Log } from '../processor';
import { generateIDFrom } from '../utils';
import Handler from './Handler';
const logErrorAndReturnNull =
(ctx: DataHandlerContext<Store>) => (reason: any) => {
ctx.log.error(reason);
return null;
};
export default class InputAdded implements Handler {
constructor(
private tokenStorage: Map<string, Token>,
private depositStorage: Map<string, Erc20Deposit>,
private applicationStorage: Map<string, Application>,
private inputStorage: Map<string, Input>,
private nftStorage: Map<string, NFT>,
private erc721DepositStorage: Map<string, Erc721Deposit>,
private multiTokenStorage: Map<string, MultiToken>,
private erc1155DepositStorage: Map<string, Erc1155Deposit>,
private chainStorage: Map<string, Chain>,
) {}
private async prepareErc20Deposit(
input: Input,
block: BlockData,
ctx: DataHandlerContext<Store>,
opts: {
inputId: string;
chain: Chain;
},
) {
if (input.msgSender !== ERC20PortalAddress) return undefined;
const chain = opts.chain;
// first byte is a boolean and it is not used here at the moment
const tokenAddress = dataSlice(input.payload, 1, 21).toLowerCase(); // 20 bytes for address
const from = dataSlice(input.payload, 21, 41).toLowerCase(); // 20 bytes for address
const amount = getUint(dataSlice(input.payload, 41, 73)); // 32 bytes for uint256
const tokenId = generateIDFrom([chain.id, tokenAddress]);
let token = this.tokenStorage.get(tokenId) as Token;
if (!token) {
const contract = new ERC20(ctx, block.header, tokenAddress);
const name = await contract.name();
const symbol = await contract.symbol();
const decimals = await contract.decimals();
token = new Token({
id: tokenId,
name,
symbol,
decimals,
chain: chain,
address: tokenAddress,
});
this.tokenStorage.set(tokenId, token);
ctx.log.info(`${tokenId} (Token) stored`);
}
const deposit = new Erc20Deposit({
id: input.id,
amount,
from,
token,
chain: opts.chain,
});
this.depositStorage.set(opts.inputId, deposit);
ctx.log.info(`${opts.inputId} (Erc20Deposit) stored`);
return deposit;
}
private async prepareErc721Deposit(
input: Input,
block: BlockData,
ctx: DataHandlerContext<Store>,
opts: {
inputId: string;
chain: Chain;
},
) {
if (input.msgSender !== ERC721PortalAddress) return undefined;
const chain = opts.chain;
const tokenAddress = dataSlice(input.payload, 0, 20).toLowerCase(); // 20 bytes for address
const from = dataSlice(input.payload, 20, 40).toLowerCase(); // 20 bytes for address
const tokenIndex = getUint(dataSlice(input.payload, 40, 72)); // 32 bytes for uint256
const tokenId = generateIDFrom([chain.id, tokenAddress]);
let nft = this.nftStorage.get(tokenId);
if (!nft) {
const contract = new ERC721(ctx, block.header, tokenAddress);
const name = await contract
.name()
.catch(logErrorAndReturnNull(ctx));
const symbol = await contract
.symbol()
.catch(logErrorAndReturnNull(ctx));
nft = new NFT({
id: tokenId,
name,
symbol,
chain,
address: tokenAddress,
});
this.nftStorage.set(tokenId, nft);
ctx.log.info(`${tokenId} (NFT) stored`);
}
const deposit = new Erc721Deposit({
id: input.id,
from,
token: nft,
tokenIndex,
chain,
});
this.erc721DepositStorage.set(opts.inputId, deposit);
ctx.log.info(`${opts.inputId} (Erc721Deposit) stored`);
return deposit;
}
private async prepareErc1155Deposit(
input: Input,
_block: BlockData,
ctx: DataHandlerContext<Store>,
opts: {
inputId: string;
chain: Chain;
},
) {
if (
input.msgSender !== ERC1155BatchPortalAddress &&
input.msgSender !== ERC1155SinglePortalAddress
)
return undefined;
const chain = opts.chain;
const tokenAddress = dataSlice(input.payload, 0, 20).toLowerCase(); // 20 bytes for token address
const from = dataSlice(input.payload, 20, 40).toLowerCase(); // 20 bytes for from address
const tokenId = generateIDFrom([chain.id, tokenAddress]);
let transfers: Erc1155Transfer[] = [];
if (input.msgSender === ERC1155BatchPortalAddress) {
ctx.log.info(`${input.id} (ERC-1155) batch deposit`);
const data = dataSlice(input.payload, 40); // Data arbitrary size
const [tokenIds, amounts] = new AbiCoder().decode(
['uint256[]', 'uint256[]'],
data,
);
transfers = tokenIds.map(
(tokenIndex: bigint, idx: number) =>
new Erc1155Transfer({ tokenIndex, amount: amounts[idx] }),
);
} else if (input.msgSender === ERC1155SinglePortalAddress) {
ctx.log.info(`${input.id} (ERC-1155) single deposit`);
const tokenIndex = getUint(dataSlice(input.payload, 40, 72)); // 32 bytes for tokenId
const amount = getUint(dataSlice(input.payload, 72, 104)); // 32 bytes for value a.k.a amount
transfers = [new Erc1155Transfer({ tokenIndex, amount })];
}
let token = this.multiTokenStorage.get(tokenId);
if (!token) {
token = new MultiToken({
id: tokenId,
chain,
address: tokenAddress,
});
this.multiTokenStorage.set(tokenId, token);
ctx.log.info(`${tokenId} (ERC-1155) contract stored.`);
}
const deposit = new Erc1155Deposit({
id: input.id,
from,
token,
transfers,
chain,
});
this.erc1155DepositStorage.set(input.id, deposit);
ctx.log.info(`${input.id} (Erc1155Deposit) stored`);
return deposit;
}
async handle(log: Log, block: BlockData, ctx: DataHandlerContext<Store>) {
if (
log.address === InputBoxAddress &&
log.topics[0] === events.InputAdded.topic
) {
if (!log.transaction?.chainId)
throw new Error(
'Chain id is required to save InputAdded events and related data!',
);
const chain = new Chain({
id: log.transaction.chainId.toString(),
});
// Storing Chain
this.chainStorage.set(chain.id, chain);
const timestamp = BigInt(log.block.timestamp);
const event = events.InputAdded.decode(log);
const dappAddress = event.dapp.toLowerCase();
const dappId = generateIDFrom([chain.id, dappAddress]);
const timestampInSeconds = timestamp / 1000n;
let application =
this.applicationStorage.get(dappId) ??
(await ctx.store.get(Application, dappId));
if (!application) {
ctx.log.warn(`${dappId} (Application) not found`);
application = new Application({
id: dappId,
timestamp: timestampInSeconds,
chain: chain,
address: dappAddress,
});
this.applicationStorage.set(dappId, application);
ctx.log.info(`${dappId} (Application) stored`);
}
const inputId = generateIDFrom([dappId, event.inboxInputIndex]);
const input = new Input({
id: inputId,
application,
index: Number(event.inboxInputIndex),
msgSender: event.sender.toLowerCase(),
payload: event.input,
timestamp: timestampInSeconds,
blockNumber: BigInt(log.block.height),
blockHash: log.block.hash,
transactionHash: log.transaction?.hash,
chain: chain,
});
const params = [input, block, ctx, { inputId, chain }] as const;
input.erc20Deposit = await this.prepareErc20Deposit(...params);
input.erc721Deposit = await this.prepareErc721Deposit(...params);
input.erc1155Deposit = await this.prepareErc1155Deposit(...params);
this.inputStorage.set(inputId, input);
ctx.log.info(`${inputId} (Input) stored`);
}
return true;
}
}