|
| 1 | +import { ASSET_TYPE } from "../types"; |
| 2 | +import { |
| 3 | + Approval, |
| 4 | + AssetsTransfersAndApprovalsRes, |
| 5 | + DebugCallTracerWithLogs, |
| 6 | + DebugProvider, |
| 7 | + SimulationParams, |
| 8 | + Transfer, |
| 9 | +} from "../types/simulator"; |
| 10 | +import { |
| 11 | + ERC1155_TRANSFER_BATCH_TOPIC, |
| 12 | + ERC1155_TRANSFER_SINGLE_TOPIC, |
| 13 | + ERC20_ERC721_APPROVE_TOPIC, |
| 14 | + ERC20_ERC721_TRANSFER_TOPIC, |
| 15 | + ERC721_ERC1155_APPROVE_ALL_TOPIC, |
| 16 | + TRUE, |
| 17 | + ZERO_ADDRESS, |
| 18 | + abiCoder, |
| 19 | + addressFrom32bytesTo20bytes, |
| 20 | + decodeErrorMessage, |
| 21 | +} from "../utils"; |
| 22 | +import { nodeRealFactory } from "./providers"; |
| 23 | + |
| 24 | +const { ERC1155, ERC20, ERC721, NATIVE } = ASSET_TYPE; |
| 25 | +export default class Simulator { |
| 26 | + private providers: { |
| 27 | + [chainId: number]: DebugProvider["debugTrace"]; |
| 28 | + } = {}; |
| 29 | + |
| 30 | + constructor(providerInstances: DebugProvider[]) { |
| 31 | + providerInstances.forEach((p) => { |
| 32 | + this.providers[p.chainId] = p.debugTrace; |
| 33 | + }); |
| 34 | + } |
| 35 | + |
| 36 | + async simulate(params: SimulationParams): Promise<AssetsTransfersAndApprovalsRes> { |
| 37 | + if (!params.chainId) throw new Error("undefined chainId param"); |
| 38 | + |
| 39 | + if (!params.from) throw new Error("undefined from param"); |
| 40 | + |
| 41 | + if (!params.to) throw new Error("undefined to param"); |
| 42 | + |
| 43 | + if (!params.calldata) throw new Error("undefined to calldata"); |
| 44 | + |
| 45 | + const debugTraceCall = this.providers[params.chainId]; |
| 46 | + |
| 47 | + if (!debugTraceCall) { |
| 48 | + throw new Error(`Simulator: no debugProvider found for chainId: ${params.chainId}`); |
| 49 | + } |
| 50 | + |
| 51 | + const trace = await debugTraceCall(params); |
| 52 | + const transfers: Transfer[] = []; |
| 53 | + const approvals: Approval[] = []; |
| 54 | + |
| 55 | + this.processTraceCallData(trace, transfers, approvals); |
| 56 | + return { |
| 57 | + transfers, |
| 58 | + approvals, |
| 59 | + }; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @dev recursive function to precess top and internal calls |
| 64 | + * and mutably append records to both `transfers` and `approvals` arrays |
| 65 | + * the function classify and decode data according to EVM specification and Ethereum EIPs |
| 66 | + * @see https://eips.ethereum.org/EIPS/eip-721 |
| 67 | + * @see https://eips.ethereum.org/EIPS/eip-20 |
| 68 | + * @see https://eips.ethereum.org/EIPS/eip-1155 |
| 69 | + * @see https://docs.soliditylang.org/en/v0.8.13/abi-spec.html |
| 70 | + * @param {DebugCallTracerWithLogs} data data to be processed recursively |
| 71 | + * @param {Transfer[]} transfersReference mutable array to append transfer records |
| 72 | + * @param {Approval[]} approvalsReference mutable array to append approval records |
| 73 | + */ |
| 74 | + private processTraceCallData = ( |
| 75 | + data: DebugCallTracerWithLogs, |
| 76 | + transfersReference: Transfer[], |
| 77 | + approvalsReference: Approval[] |
| 78 | + ) => { |
| 79 | + data?.forEach((call) => { |
| 80 | + if (call.error) { |
| 81 | + throw new Error( |
| 82 | + `Transaction will fail with error: '${decodeErrorMessage(call.output)}'` |
| 83 | + ); |
| 84 | + } |
| 85 | + // handle native ETH transfers |
| 86 | + if (call.value && call.value !== "0x0") { |
| 87 | + transfersReference.push({ |
| 88 | + address: ZERO_ADDRESS, |
| 89 | + type: NATIVE, |
| 90 | + from: call.from, |
| 91 | + to: call.to, |
| 92 | + amount: call.value, |
| 93 | + id: "", |
| 94 | + }); |
| 95 | + } |
| 96 | + |
| 97 | + // handle logs |
| 98 | + call.logs?.forEach((event) => { |
| 99 | + const eventHash = event.topics[0]; |
| 100 | + |
| 101 | + // ERC20 and ERC721 Transfer events |
| 102 | + // keccak256(Transfer(address,address,uint256)) |
| 103 | + if (eventHash === ERC20_ERC721_TRANSFER_TOPIC) { |
| 104 | + // ERC20 Transfer event have a topics array of 3 elements (eventHash, and 2 indexed addresses) |
| 105 | + if (event.topics.length === 3) { |
| 106 | + const [, _from, _to] = event.topics; |
| 107 | + const from = addressFrom32bytesTo20bytes(_from); |
| 108 | + const to = addressFrom32bytesTo20bytes(_to); |
| 109 | + const amount = BigInt(event.data).toString(); |
| 110 | + transfersReference.push({ |
| 111 | + address: event.address, |
| 112 | + type: ERC20, |
| 113 | + from, |
| 114 | + to, |
| 115 | + amount, |
| 116 | + }); |
| 117 | + } |
| 118 | + // ERC721 Transfer event have a topics array of 4 elements (eventHash, and 2 indexed addresses and an indexed tokenId) |
| 119 | + else { |
| 120 | + const [, _from, _to, _tokenId] = event.topics; |
| 121 | + const from = addressFrom32bytesTo20bytes(_from); |
| 122 | + const to = addressFrom32bytesTo20bytes(_to); |
| 123 | + const tokenId = BigInt(_tokenId).toString(); |
| 124 | + transfersReference.push({ |
| 125 | + address: event.address, |
| 126 | + type: ERC721, |
| 127 | + from, |
| 128 | + to, |
| 129 | + id: tokenId, |
| 130 | + }); |
| 131 | + } |
| 132 | + } |
| 133 | + // ERC20 and ERC721 Approval events |
| 134 | + // keccak256(Approval(address,address,uint256)) |
| 135 | + else if (eventHash === ERC20_ERC721_APPROVE_TOPIC) { |
| 136 | + // ERC20 Approval event have a topics array of 3 elements (eventHash, and 2 indexed addresses) |
| 137 | + if (event.topics.length === 3) { |
| 138 | + const [, _owner, _operator] = event.topics; |
| 139 | + const owner = addressFrom32bytesTo20bytes(_owner); |
| 140 | + const operator = addressFrom32bytesTo20bytes(_operator); |
| 141 | + |
| 142 | + const amount = BigInt(event.data).toString(); |
| 143 | + approvalsReference.push({ |
| 144 | + address: event.address, |
| 145 | + type: ERC20, |
| 146 | + owner, |
| 147 | + operator, |
| 148 | + amount, |
| 149 | + }); |
| 150 | + } |
| 151 | + // ERC721 Approval event have a topics array of 4 elements (eventHash, and 2 indexed addresses and an indexed tokenId) |
| 152 | + else if (event.topics.length === 4) { |
| 153 | + const [, _owner, _operator, _tokenId] = event.topics; |
| 154 | + const owner = addressFrom32bytesTo20bytes(_owner); |
| 155 | + const operator = addressFrom32bytesTo20bytes(_operator); |
| 156 | + const tokenId = BigInt(_tokenId).toString(); |
| 157 | + approvalsReference.push({ |
| 158 | + address: event.address, |
| 159 | + type: ERC721, |
| 160 | + owner, |
| 161 | + operator, |
| 162 | + id: tokenId, |
| 163 | + }); |
| 164 | + } |
| 165 | + } |
| 166 | + // ERC721 and ERC1155 ApprovalForAll event |
| 167 | + // keccak256(ApprovalForAll(address,address,bool)) |
| 168 | + else if (eventHash === ERC721_ERC1155_APPROVE_ALL_TOPIC) { |
| 169 | + const [, _owner, _operator] = event.topics; |
| 170 | + const owner = addressFrom32bytesTo20bytes(_owner); |
| 171 | + const operator = addressFrom32bytesTo20bytes(_operator); |
| 172 | + const isApprove = event.data === TRUE; |
| 173 | + approvalsReference.push({ |
| 174 | + address: event.address, |
| 175 | + type: ERC721, |
| 176 | + owner, |
| 177 | + operator, |
| 178 | + approveForAllStatus: isApprove, |
| 179 | + }); |
| 180 | + } |
| 181 | + |
| 182 | + // ERC1155 TransferSingle event |
| 183 | + // keccak256(TransferSingle(address,address,address,uint256,uint256)) |
| 184 | + else if (eventHash === ERC1155_TRANSFER_SINGLE_TOPIC) { |
| 185 | + const [, _operator, _from, _to] = event.topics; |
| 186 | + const operator = addressFrom32bytesTo20bytes(_operator); |
| 187 | + const from = addressFrom32bytesTo20bytes(_from); |
| 188 | + const to = addressFrom32bytesTo20bytes(_to); |
| 189 | + const [id, amount] = abiCoder.decode(["uint256", "uint256"], event.data); |
| 190 | + transfersReference.push({ |
| 191 | + address: event.address, |
| 192 | + type: ERC1155, |
| 193 | + from, |
| 194 | + to, |
| 195 | + operator, |
| 196 | + amount: BigInt(amount).toString(), |
| 197 | + id: BigInt(id).toString(), |
| 198 | + }); |
| 199 | + } |
| 200 | + |
| 201 | + // ERC1155 TransferBatch event |
| 202 | + // keccak256(TransferBatch(address,address,address,uint256[],uint256[])) |
| 203 | + else if (eventHash === ERC1155_TRANSFER_BATCH_TOPIC) { |
| 204 | + const [, _operator, _from, _to] = event.topics; |
| 205 | + const operator = addressFrom32bytesTo20bytes(_operator); |
| 206 | + const from = addressFrom32bytesTo20bytes(_from); |
| 207 | + const to = addressFrom32bytesTo20bytes(_to); |
| 208 | + const [ids, amounts] = abiCoder.decode(["uint256[]", "uint256[]"], event.data); |
| 209 | + |
| 210 | + (ids as bigint[]).forEach((id, index) => { |
| 211 | + const amount = amounts[index]; |
| 212 | + transfersReference.push({ |
| 213 | + address: event.address, |
| 214 | + type: ERC1155, |
| 215 | + from, |
| 216 | + to, |
| 217 | + operator, |
| 218 | + amount: BigInt(amount).toString(), |
| 219 | + id: BigInt(id).toString(), |
| 220 | + }); |
| 221 | + }); |
| 222 | + } |
| 223 | + }); |
| 224 | + |
| 225 | + // recursive call to process internal calls, since the EVM use gas and the call stack deterministic there's no risk of infinite looping |
| 226 | + this.processTraceCallData(call.calls, transfersReference, approvalsReference); |
| 227 | + }); |
| 228 | + }; |
| 229 | + |
| 230 | + static nodeRealFactory = nodeRealFactory; |
| 231 | +} |
0 commit comments