Skip to content

Commit

Permalink
Merge pull request #64 from astraly-labs/feat/handle-pragma-devnet
Browse files Browse the repository at this point in the history
feat: Scripts for Pragma Devnet
  • Loading branch information
akhercha authored Oct 8, 2024
2 parents e8daf34 + 27687f2 commit c15f723
Show file tree
Hide file tree
Showing 3 changed files with 248 additions and 78 deletions.
81 changes: 81 additions & 0 deletions examples/pragma/pragma-devnet/devnet-script-spot-checkpoint.js
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;
80 changes: 80 additions & 0 deletions examples/pragma/pragma-devnet/devnet-script-spot.js
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;
Loading

0 comments on commit c15f723

Please sign in to comment.