Skip to content

Commit 956dab3

Browse files
Add polymarket interpreation (#253)
1 parent 60187ea commit 956dab3

File tree

5 files changed

+319
-37
lines changed

5 files changed

+319
-37
lines changed

.changeset/proud-suns-tan.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@3loop/transaction-interpreter': patch
3+
---
4+
5+
Add polymarket interpretation

apps/web/src/app/data.ts

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,22 @@ export const EXAMPLE_TXS = {
2727
interpreter: 'aave',
2828
},
2929
],
30+
'Polymarket Predictions': [
31+
{
32+
name: 'Buy Outcome',
33+
hash: '0xe1429321d4d89f85126d346b01797e7b20b86ba6ea1d53e6c2b782ea46df4247',
34+
chainID: 137,
35+
interpreter: 'polymarket',
36+
interpretAsUserAddress: '0x5f5be567196fe36552f79dca5dac8d7a2132b598',
37+
},
38+
{
39+
name: 'Sell Outcome',
40+
hash: '0x5b646ff588feafa9b17d4a181b249cb9d9cd92d468a4ce8efbbb79fe1ab17567',
41+
chainID: 137,
42+
interpreter: 'polymarket',
43+
interpretAsUserAddress: '0xdbade4c82fb72780a0db9a38f821d8671aba9c95',
44+
},
45+
],
3046
'Gnosis Safe': [
3147
{
3248
name: 'MultiTransfer',
@@ -121,26 +137,6 @@ export const EXAMPLE_TXS = {
121137
interpreter: 'hop-protocol',
122138
},
123139
],
124-
'Moxie (Base mainnet)': [
125-
{
126-
name: 'Sell',
127-
hash: '0x0f2540f5936228704cf94348085fb16fde87bfb554a76f0234dc8d5a804b0a7b',
128-
chainID: 8453,
129-
interpreter: 'moxie',
130-
},
131-
{
132-
name: 'Buy',
133-
hash: '0xc355f63566a9407d9a610b13f5e4e7fc64ce526f34503af18666904b63e0556f',
134-
chainID: 8453,
135-
interpreter: 'moxie',
136-
},
137-
{
138-
name: 'Burn',
139-
hash: '0x88833e8e873c09b3def62c2fe82f5ac3a20cdb936acce5ba27a5e4ab20417831',
140-
chainID: 8453,
141-
interpreter: 'moxie',
142-
},
143-
],
144140
}
145141

146142
export const supportedChains: {

apps/web/src/app/interpret/[chainID]/[hash]/form.tsx

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ const PATH = 'interpret'
2626
export default function DecodingForm({ decoded, currentHash, chainID, error }: FormProps) {
2727
const [result, setResult] = React.useState<Interpretation>()
2828
const [persistedSchema, setSchema] = useLocalStorage(decoded?.toAddress ?? 'unknown', '')
29+
const [isInterpreting, setIsInterpreting] = React.useState(false)
30+
const [userAddress, setUserAddress] = React.useState<string>('')
31+
32+
const matchingExample = React.useMemo(() => {
33+
return Object.values(EXAMPLE_TXS)
34+
.flatMap((categoryTxs) => categoryTxs)
35+
.find((tx) => tx.hash.toLowerCase() === currentHash?.toLowerCase())
36+
}, [currentHash])
2937

3038
const schema = React.useMemo(() => {
3139
if (persistedSchema !== '') return persistedSchema
@@ -54,11 +62,25 @@ export default function DecodingForm({ decoded, currentHash, chainID, error }: F
5462
schema: schema,
5563
}
5664

57-
applyInterpreter(decoded, newInterpreter).then((res) => {
58-
setResult(res)
59-
})
65+
setIsInterpreting(true)
66+
setResult(undefined)
67+
68+
applyInterpreter(decoded, newInterpreter, userAddress || undefined)
69+
.then((res) => {
70+
setResult(res)
71+
})
72+
.finally(() => {
73+
setIsInterpreting(false)
74+
})
6075
}
61-
}, [schema, decoded])
76+
}, [schema, decoded, userAddress])
77+
78+
// Set user address from example if available
79+
React.useEffect(() => {
80+
if (matchingExample && (matchingExample as any).interpretAsUserAddress) {
81+
setUserAddress((matchingExample as any).interpretAsUserAddress)
82+
}
83+
}, [matchingExample])
6284

6385
// Run the interpreter on page load
6486
React.useEffect(() => {
@@ -68,14 +90,10 @@ export default function DecodingForm({ decoded, currentHash, chainID, error }: F
6890
}, [schema, decoded, result, onRun])
6991

7092
const interpreterSourceLink = React.useMemo(() => {
71-
const matchingExample = Object.values(EXAMPLE_TXS)
72-
.flatMap((categoryTxs) => categoryTxs)
73-
.find((tx) => tx.hash.toLowerCase() === currentHash?.toLowerCase())
74-
7593
return matchingExample?.interpreter
7694
? `${INTERPRETER_REPO}/interpreters/${matchingExample?.interpreter}.ts`
7795
: INTERPRETER_REPO
78-
}, [currentHash])
96+
}, [matchingExample])
7997

8098
return (
8199
<div className="grid h-full items-stretch gap-6 grid-cols-1 lg:grid-cols-[1fr_200px]">
@@ -102,12 +120,22 @@ export default function DecodingForm({ decoded, currentHash, chainID, error }: F
102120
<Button type="submit" className="flex-1">
103121
Decode
104122
</Button>
105-
<Button variant={'outline'} onClick={onRun} type="button" className="flex-1">
123+
<Button variant={'outline'} onClick={onRun} type="button" className="flex-1" disabled={isInterpreting}>
106124
<PlayIcon className="mr-2 h-4 w-4" />
107-
Interpret
125+
{isInterpreting ? 'Interpreting...' : 'Interpret'}
108126
</Button>
109127
</div>
110128
</div>
129+
<div className="flex w-full lg:items-center gap-2 flex-col lg:flex-row mt-2">
130+
<Input
131+
className="flex-1"
132+
id="userAddress"
133+
name="userAddress"
134+
placeholder="Optional: interpret as user address"
135+
value={userAddress}
136+
onChange={(e) => setUserAddress(e.target.value)}
137+
/>
138+
</div>
111139
</form>
112140

113141
{error ? (
@@ -146,12 +174,21 @@ export default function DecodingForm({ decoded, currentHash, chainID, error }: F
146174
<Label>Result:</Label>
147175
</div>
148176

149-
<CodeBlock
150-
language="json"
151-
value={result?.error ? result?.error : JSON.stringify(result?.interpretation, null, 2)}
152-
readonly={true}
153-
lineNumbers={false}
154-
/>
177+
{isInterpreting ? (
178+
<div className="flex items-center justify-center p-8 border rounded-md">
179+
<div className="text-center">
180+
<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-gray-900 mx-auto mb-2"></div>
181+
<p className="text-sm text-gray-600">Interpreting transaction...</p>
182+
</div>
183+
</div>
184+
) : (
185+
<CodeBlock
186+
language="json"
187+
value={result?.error ? result?.error : JSON.stringify(result?.interpretation, null, 2)}
188+
readonly={true}
189+
lineNumbers={false}
190+
/>
191+
)}
155192
</div>
156193
</div>
157194
)}

0 commit comments

Comments
 (0)