-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from astraly-labs/feat/handle-pragma-devnet
feat: Scripts for Pragma Devnet
- Loading branch information
Showing
3 changed files
with
248 additions
and
78 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
examples/pragma/pragma-devnet/devnet-script-spot-checkpoint.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { hash, shortString } from "https://esm.run/starknet@5.14"; | ||
|
||
const filter = { | ||
// Only request header if any event matches. | ||
header: { | ||
weak: true, | ||
}, | ||
events: [ | ||
{ | ||
fromAddress: | ||
"0x753034396b7e59f3579e2beba2f408dd2ed12974e054a1348234bea4b10bd30", | ||
keys: [hash.getSelectorFromName("CheckpointSpotEntry")], | ||
includeTransaction: true, | ||
includeReceipt: false, | ||
}, | ||
], | ||
}; | ||
|
||
function escapeInvalidCharacters(str) { | ||
return str.replace(/^[\x00-\x1F]+/, ""); | ||
} | ||
|
||
function decodeTransfersInBlock({ header, events }) { | ||
const { blockNumber, blockHash, timestamp } = header; | ||
return events.flatMap(({ event, transaction }) => { | ||
const transactionHash = transaction.meta.hash; | ||
|
||
const invoke_tx = transaction.invokeV1 ?? transaction.invokeV3; | ||
const senderAddress = invoke_tx.senderAddress; | ||
|
||
const dataId = `${transactionHash}_${event.index ?? 0}`; | ||
|
||
const [ | ||
pairId, | ||
checkpointTimestamp, | ||
price, | ||
aggregationMode, | ||
nbSourcesAggregated, | ||
] = event.data; | ||
|
||
// Convert felts to string | ||
const pairIdName = escapeInvalidCharacters( | ||
shortString.decodeShortString(pairId), | ||
); | ||
|
||
// Convert to snake_case because it works better with postgres. | ||
return { | ||
network: "pragma-devnet", | ||
pair_id: pairIdName, | ||
data_id: dataId, | ||
block_hash: blockHash, | ||
block_number: +blockNumber, | ||
block_timestamp: timestamp, | ||
transaction_hash: transactionHash, | ||
price: +price, | ||
timestamp: new Date(Number(checkpointTimestamp) * 1000).toISOString(), | ||
aggregation_mode: +aggregationMode, | ||
nb_sources_aggregated: +nbSourcesAggregated, | ||
sender_address: senderAddress, | ||
}; | ||
}); | ||
} | ||
|
||
// Configure indexer for streaming Starknet Goerli data starting at the specified block. | ||
export const config = { | ||
streamUrl: "https://devnet.pragma.a5a.ch", | ||
startingBlock: Number(0), | ||
network: "starknet", | ||
filter, | ||
batchSize: 1, | ||
finality: "DATA_STATUS_PENDING", | ||
sinkType: "postgres", | ||
sinkOptions: { | ||
// Send data as returned by `transform`. | ||
// When `raw = false`, the data is sent together with the starting and end cursor. | ||
raw: true, | ||
}, | ||
}; | ||
|
||
// Transform each block using the function defined in starknet.js. | ||
export default decodeTransfersInBlock; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { hash, shortString } from "https://esm.run/starknet@5.14"; | ||
|
||
const filter = { | ||
// Only request header if any event matches. | ||
header: { | ||
weak: true, | ||
}, | ||
events: [ | ||
{ | ||
fromAddress: | ||
"0x753034396b7e59f3579e2beba2f408dd2ed12974e054a1348234bea4b10bd30", | ||
keys: [hash.getSelectorFromName("SubmittedSpotEntry")], | ||
includeTransaction: true, | ||
includeReceipt: false, | ||
}, | ||
], | ||
}; | ||
|
||
function escapeInvalidCharacters(str) { | ||
return str.replace(/^[\x00-\x1F]+/, ""); | ||
} | ||
|
||
function decodeTransfersInBlock({ header, events }) { | ||
const { blockNumber, blockHash, timestamp } = header; | ||
|
||
return events.flatMap(({ event, transaction }) => { | ||
const transactionHash = transaction.meta.hash; | ||
|
||
const dataId = `${transactionHash}_${event.index ?? 0}`; | ||
|
||
const [entryTimestamp, source, publisher, price, pairId, volume] = | ||
event.data; | ||
|
||
// Convert felts to string | ||
const publisherName = escapeInvalidCharacters( | ||
shortString.decodeShortString(publisher), | ||
); | ||
const sourceName = escapeInvalidCharacters( | ||
shortString.decodeShortString(source), | ||
); | ||
const pairIdName = escapeInvalidCharacters( | ||
shortString.decodeShortString(pairId), | ||
); | ||
|
||
// Convert to snake_case because it works better with postgres. | ||
return { | ||
network: "pragma-devnet", | ||
pair_id: pairIdName, | ||
data_id: dataId, | ||
block_hash: blockHash, | ||
block_number: +blockNumber, | ||
block_timestamp: timestamp, | ||
transaction_hash: transactionHash, | ||
price: +price, | ||
timestamp: new Date(Number(entryTimestamp) * 1000).toISOString(), | ||
publisher: publisherName, | ||
source: sourceName, | ||
volume: +volume, | ||
}; | ||
}); | ||
} | ||
|
||
// Configure indexer for streaming Starknet Goerli data starting at the specified block. | ||
export const config = { | ||
streamUrl: "https://devnet.pragma.a5a.ch", | ||
startingBlock: Number(0), | ||
network: "starknet", | ||
filter, | ||
batchSize: 1, | ||
finality: "DATA_STATUS_PENDING", | ||
sinkType: "postgres", | ||
sinkOptions: { | ||
// Send data as returned by `transform`. | ||
// When `raw = false`, the data is sent together with the starting and end cursor. | ||
raw: true, | ||
}, | ||
}; | ||
|
||
// Transform each block using the function defined in starknet.js. | ||
export default decodeTransfersInBlock; |
Oops, something went wrong.