Skip to content

Commit b084f17

Browse files
committed
Fix types in decode calldata function
1 parent 06e4e09 commit b084f17

File tree

2 files changed

+38
-24
lines changed

2 files changed

+38
-24
lines changed

.changeset/few-baboons-talk.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@3loop/transaction-decoder': patch
3+
---
4+
5+
Fix type error in recurive calldata decoding

packages/transaction-decoder/src/decoding/calldata-decode.ts

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,18 @@ const decodeBytesRecursively = (
2828
node.components.some((c) => callDataKeys.includes(c.name)) &&
2929
node.components.some((c) => addressKeys.includes(c.name))
3030
) {
31-
const toAddress = node.components.find((c) => addressKeys.includes(c.name))?.value as Address | undefined
32-
return {
33-
...node,
34-
components: yield* Effect.all(
35-
node.components.map((n) => decodeBytesRecursively(n, chainID, toAddress)),
36-
{
37-
concurrency: 'unbounded',
38-
},
39-
),
31+
const toAddress = node.components.find((c) => addressKeys.includes(c.name))?.value as string | undefined
32+
33+
if (toAddress && isAddress(toAddress)) {
34+
return {
35+
...node,
36+
components: yield* Effect.all(
37+
node.components.map((n) => decodeBytesRecursively(n, chainID, toAddress)),
38+
{
39+
concurrency: 'unbounded',
40+
},
41+
),
42+
}
4043
}
4144
}
4245

@@ -52,19 +55,23 @@ const decodeBytesRecursively = (
5255
}
5356
}
5457

55-
if (isCallDataNode && address) {
56-
const decoded = yield* decodeMethod({
57-
data: node.value as Hex,
58-
chainID,
59-
contractAddress: address,
60-
}).pipe(Effect.orElseSucceed(() => null))
61-
62-
return decoded
63-
? {
64-
...node,
65-
valueDecoded: decoded,
66-
}
67-
: node
58+
if (isCallDataNode && address && isAddress(address)) {
59+
const nodeValue = node.value as string
60+
61+
if (nodeValue.startsWith('0x')) {
62+
const decoded = yield* decodeMethod({
63+
data: nodeValue as Hex,
64+
chainID,
65+
contractAddress: address,
66+
}).pipe(Effect.orElseSucceed(() => null))
67+
68+
return decoded
69+
? {
70+
...node,
71+
valueDecoded: decoded,
72+
}
73+
: node
74+
}
6875
}
6976

7077
return node
@@ -191,10 +198,12 @@ export const decodeMethod = ({
191198

192199
if (hasCalldataParam || hasTuppleParams) {
193200
const targetAddressParam = decoded.params.find((p) => addressKeys.includes(p.name))
194-
const targetAddress = targetAddressParam?.value as Address | undefined
201+
const targetAddress = targetAddressParam?.value as string | undefined
195202

196203
const decodedParams = yield* Effect.all(
197-
decoded.params.map((p) => decodeBytesRecursively(p, chainID, targetAddress)),
204+
decoded.params.map((p) =>
205+
decodeBytesRecursively(p, chainID, targetAddress && isAddress(targetAddress) ? targetAddress : undefined),
206+
),
198207
{
199208
concurrency: 'unbounded',
200209
},

0 commit comments

Comments
 (0)