-
Notifications
You must be signed in to change notification settings - Fork 7
/
InputAdded.ts
230 lines (201 loc) · 8.15 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
import { BlockData, DataHandlerContext, Log } 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,
Erc1155Deposit,
Erc1155Transfer,
Erc20Deposit,
Erc721Deposit,
Input,
MultiToken,
NFT,
Token,
} from '../model';
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 async prepareErc20Deposit(
input: Input,
block: BlockData,
ctx: DataHandlerContext<Store>,
opts: {
inputId: string;
},
) {
if (input.msgSender !== ERC20PortalAddress) return undefined;
// 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
let token = this.tokenStorage.get(tokenAddress) 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: tokenAddress, name, symbol, decimals });
this.tokenStorage.set(tokenAddress, token);
ctx.log.info(`${tokenAddress} (Token) stored`);
}
const deposit = new Erc20Deposit({
id: input.id,
amount,
from,
token,
});
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;
},
) {
if (input.msgSender !== ERC721PortalAddress) return undefined;
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
let nft = this.nftStorage.get(tokenAddress);
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: tokenAddress, name, symbol });
this.nftStorage.set(tokenAddress, nft);
ctx.log.info(`${tokenAddress} (NFT) stored`);
}
const deposit = new Erc721Deposit({
id: input.id,
from,
token: nft,
tokenIndex,
});
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;
},
) {
if (
input.msgSender !== ERC1155BatchPortalAddress &&
input.msgSender !== ERC1155SinglePortalAddress
)
return undefined;
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
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(tokenAddress);
if (!token) {
token = new MultiToken({ id: tokenAddress });
this.multiTokenStorage.set(tokenAddress, token);
ctx.log.info(`${tokenAddress} (ERC-1155) contract stored.`);
}
const deposit = new Erc1155Deposit({
id: input.id,
from,
token,
transfers,
});
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
) {
const timestamp = BigInt(log.block.timestamp);
const event = events.InputAdded.decode(log);
const dappId = event.dapp.toLowerCase();
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,
});
this.applicationStorage.set(dappId, application);
ctx.log.info(`${dappId} (Application) stored`);
}
const inputId = `${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,
});
const params = [input, block, ctx, { inputId }] 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;
}
}