Skip to content

Commit ae7b1ba

Browse files
committed
Fix sql stores syntax
1 parent fed374f commit ae7b1ba

File tree

3 files changed

+114
-37
lines changed

3 files changed

+114
-37
lines changed

.changeset/wet-mirrors-work.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@3loop/transaction-decoder': minor
3+
---
4+
5+
Fix sql stores syntax

packages/transaction-decoder/src/sql/abi-store.ts

Lines changed: 60 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ export const make = (strategies: AbiStore['strategies']) =>
88
Effect.gen(function* () {
99
const sql = yield* SqlClient.SqlClient
1010

11+
const table = sql('loop_decoder_contract_abi__')
12+
13+
// TODO; add timestamp to the table
1114
yield* sql`
12-
CREATE TABLE IF NOT EXISTS contractAbi (
15+
CREATE TABLE IF NOT EXISTS ${table} (
1316
type TEXT NOT NULL,
1417
address TEXT,
1518
event TEXT,
@@ -18,7 +21,10 @@ export const make = (strategies: AbiStore['strategies']) =>
1821
abi TEXT,
1922
status TEXT NOT NULL
2023
)
21-
`.pipe(Effect.catchAll(() => Effect.dieMessage('Failed to create contractAbi table')))
24+
`.pipe(
25+
Effect.tapError(Effect.logError),
26+
Effect.catchAll(() => Effect.dieMessage('Failed to create contractAbi table')),
27+
)
2228

2329
return AbiStore.of({
2430
strategies,
@@ -28,33 +34,70 @@ export const make = (strategies: AbiStore['strategies']) =>
2834
if (value.status === 'success' && value.result.type === 'address') {
2935
const result = value.result
3036
yield* sql`
31-
INSERT INTO contractAbi (type, address, chain, abi, status)
32-
VALUES (${result.type}, ${normalizedAddress}, ${result.chainID}, ${result.abi}, "success")
37+
INSERT INTO ${table}
38+
${sql.insert([
39+
{
40+
type: result.type,
41+
address: normalizedAddress,
42+
chain: key.chainID,
43+
abi: result.abi,
44+
status: 'success',
45+
},
46+
])}
3347
`
3448
} else if (value.status === 'success') {
3549
const result = value.result
3650
yield* sql`
37-
INSERT INTO contractAbi (type, event, signature, abi, status)
38-
VALUES (${result.type}, ${'event' in result ? result.event : null}, ${
39-
'signature' in result ? result.signature : null
40-
}, ${result.abi}, "success")
51+
INSERT INTO ${table}
52+
${sql.insert([
53+
{
54+
type: result.type,
55+
event: 'event' in result ? result.event : null,
56+
signature: 'signature' in result ? result.signature : null,
57+
abi: result.abi,
58+
status: 'success',
59+
},
60+
])}
4161
`
4262
} else {
4363
yield* sql`
44-
INSERT INTO contractAbi (type, address, chain, status)
45-
VALUES ("address", ${normalizedAddress}, ${key.chainID}, "not-found")
64+
INSERT INTO ${table}
65+
${sql.insert([
66+
{
67+
type: 'address',
68+
address: normalizedAddress,
69+
chain: key.chainID,
70+
status: 'not-found',
71+
},
72+
])}
4673
`
4774
}
48-
}).pipe(Effect.catchAll(() => Effect.succeed(null))),
75+
}).pipe(
76+
Effect.tapError(Effect.logError),
77+
Effect.catchAll(() => {
78+
return Effect.succeed(null)
79+
}),
80+
),
4981

5082
get: ({ address, signature, event, chainID }) =>
5183
Effect.gen(function* () {
52-
const items = yield* sql`
53-
SELECT * FROM contractAbi
54-
WHERE (address = ${address.toLowerCase()} AND chain = ${chainID} AND type = "address")
55-
${signature ? `OR (signature = ${signature} AND type = "func")` : ''}
56-
${event ? `OR (event = ${event} AND type = "event")` : ''}
57-
`.pipe(Effect.catchAll(() => Effect.succeed([])))
84+
const addressQuery = sql.and([
85+
sql`address = ${address.toLowerCase()}`,
86+
sql`chain = ${chainID}`,
87+
sql`type = 'address'`,
88+
])
89+
90+
const signatureQuery = signature ? sql.and([sql`signature = ${signature}`, sql`type = 'func'`]) : undefined
91+
const eventQuery = event ? sql.and([sql`event = ${event}`, sql`type = 'event'`]) : undefined
92+
const query =
93+
signature == null && event == null
94+
? addressQuery
95+
: sql.or([addressQuery, signatureQuery, eventQuery].filter(Boolean))
96+
97+
const items = yield* sql` SELECT * FROM ${table} WHERE ${query}`.pipe(
98+
Effect.tapError(Effect.logError),
99+
Effect.catchAll(() => Effect.succeed([])),
100+
)
58101

59102
const item =
60103
items.find((item) => {

packages/transaction-decoder/src/sql/contract-meta-store.ts

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,23 @@ export const make = () =>
1616
const sql = yield* SqlClient.SqlClient
1717
const publicClient = yield* PublicClient
1818

19+
const table = sql('loop_decoder_contract_meta__')
20+
21+
// TODO; add timestamp to the table
1922
yield* sql`
20-
CREATE TABLE IF NOT EXISTS contractMeta (
23+
CREATE TABLE IF NOT EXISTS ${table} (
2124
address TEXT NOT NULL,
2225
chain INTEGER NOT NULL,
23-
contractName TEXT,
24-
tokenSymbol TEXT,
26+
contract_name TEXT,
27+
token_symbol TEXT,
2528
decimals INTEGER,
2629
type TEXT,
2730
status TEXT NOT NULL
2831
)
29-
`.pipe(Effect.catchAll(() => Effect.dieMessage('Failed to create contractMeta table')))
32+
`.pipe(
33+
Effect.tapError(Effect.logError),
34+
Effect.catchAll(() => Effect.dieMessage('Failed to create contractMeta table')),
35+
)
3036

3137
return ContractMetaStore.of({
3238
strategies: {
@@ -39,28 +45,51 @@ export const make = () =>
3945
set: (key, value) =>
4046
Effect.gen(function* () {
4147
if (value.status === 'success') {
42-
const name = value.result.contractName ?? null
43-
const symbol = value.result.tokenSymbol ?? null
44-
const decimals = value.result.decimals ?? null
48+
const name = value.result.contractName ?? ''
49+
const symbol = value.result.tokenSymbol ?? ''
50+
const decimals = value.result.decimals ?? undefined
51+
52+
const clear = Object.fromEntries(
53+
Object.entries({
54+
address: key.address.toLowerCase(),
55+
chain: key.chainID,
56+
contract_name: name,
57+
token_symbol: symbol,
58+
decimals,
59+
type: value.result.type,
60+
status: 'success',
61+
}).filter(([_, v]) => v !== undefined),
62+
)
4563

4664
yield* sql`
47-
INSERT INTO contractMeta (address, chain, contractName, tokenSymbol, decimals, type, status)
48-
VALUES (${key.address.toLowerCase()}, ${key.chainID}, ${name}, ${symbol}, ${decimals},
49-
${value.result.type}, "success")
50-
`
65+
INSERT INTO ${table}
66+
${sql.insert([clear])}
67+
`
5168
} else {
5269
yield* sql`
53-
INSERT INTO contractMeta (address, chain, contractName, tokenSymbol, decimals, type, status)
54-
VALUES (${key.address.toLowerCase()}, ${key.chainID}, null, null, null, null, "not-found")
55-
`
70+
INSERT INTO ${table}
71+
${sql.insert([
72+
{
73+
address: key.address.toLowerCase(),
74+
chain: key.chainID,
75+
status: 'not-found',
76+
},
77+
])}
78+
`
5679
}
57-
}).pipe(Effect.catchAll(() => Effect.succeed(null))),
80+
}).pipe(
81+
Effect.tapError(Effect.logError),
82+
Effect.catchAll(() => Effect.succeed(null)),
83+
),
5884
get: ({ address, chainID }) =>
5985
Effect.gen(function* () {
6086
const items = yield* sql`
61-
SELECT * FROM contractMeta
62-
WHERE address = ${address.toLowerCase()} AND chain = ${chainID}
63-
`.pipe(Effect.catchAll(() => Effect.succeed([])))
87+
SELECT * FROM ${table}
88+
WHERE ${sql.and([sql`address = ${address.toLowerCase()}`, sql`chain = ${chainID}`])}
89+
`.pipe(
90+
Effect.tapError(Effect.logError),
91+
Effect.catchAll(() => Effect.succeed([])),
92+
)
6493

6594
const item = items[0]
6695

@@ -69,8 +98,8 @@ export const make = () =>
6998
status: 'success',
7099
result: {
71100
contractAddress: address,
72-
contractName: item.contractName,
73-
tokenSymbol: item.tokenSymbol,
101+
contractName: item.contract_name,
102+
tokenSymbol: item.token_symbol,
74103
decimals: item.decimals,
75104
type: item.type,
76105
address,

0 commit comments

Comments
 (0)