Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ tests/target/

# Legacy local config directory (now uses config-local Docker volume)
config/local/

# js
node_modules/
50 changes: 0 additions & 50 deletions scripts/add-subgraph.sh

This file was deleted.

51 changes: 51 additions & 0 deletions scripts/generate-mock-rav.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env bash
#
# Generate a mock signed RAV and insert it into the database.
#
# Usage: ./generate-mock-rav.sh <allocation-id> [--value 0.1] [--db postgres://...]
#
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"

# shellcheck source=../.env
. "$REPO_ROOT/.env"
[ -f "$REPO_ROOT/.env.local" ] && . "$REPO_ROOT/.env.local"

# shellcheck source=../shared/lib.sh
. "$REPO_ROOT/shared/lib.sh"

ALLOC_ID="${1:-}"
if [ -z "$ALLOC_ID" ]; then
echo "Usage: $0 <allocation-id> [--value 0.1] [--db postgres://...]"
exit 1
fi
shift

CHAIN_HOST="${CHAIN_HOST:-localhost}"
RPC_URL="http://${CHAIN_HOST}:${CHAIN_RPC_PORT}"

echo "Fetching contract addresses..."
COLLECTOR=$(contract_addr GraphTallyCollector.address horizon)
echo " GraphTallyCollector: $COLLECTOR"

DATA_SERVICE=$(contract_addr SubgraphService.address subgraph-service)
echo " SubgraphService: $DATA_SERVICE"

echo " Allocation ID: $ALLOC_ID"

echo "Fetching current block timestamp..."
BLOCK_TIMESTAMP=$(cast block latest --field timestamp --rpc-url="$RPC_URL")
# Convert to nanoseconds and subtract 10 seconds to be behind chainhead
TIMESTAMP_NS=$(( (BLOCK_TIMESTAMP - 10) * 1000000000 ))
echo " Timestamp (ns): $TIMESTAMP_NS"

echo ""
echo "Generating mock RAV..."
node "$SCRIPT_DIR/js/generate-mock-rav.mjs" \
--allocation-id "$ALLOC_ID" \
--data-service "$DATA_SERVICE" \
--collector "$COLLECTOR" \
--timestamp-ns "$TIMESTAMP_NS" \
"$@"
162 changes: 162 additions & 0 deletions scripts/js/generate-mock-rav.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
#!/usr/bin/env node
/**
* Generate a mock signed RAV and insert it into the database.
*
* Usage:
* node generate-mock-rav.mjs \
* --allocation-id 0x... \
* --data-service 0x... \
* --collector 0x... \
* --timestamp-ns 1234567890000000000 \
* [--value 0.1] \
* [--db postgres://postgres@localhost:5432/indexer_components_1]
*/

import { parseArgs } from "node:util";
import { resolve, dirname } from "node:path";
import { fileURLToPath } from "node:url";
import { config } from "dotenv";
import pg from "pg";
import { generateSignedRAV } from "@graphprotocol/toolshed";

const __dirname = dirname(fileURLToPath(import.meta.url));
config({ path: resolve(__dirname, "../../.env") });

function parseCliArgs() {
const { values } = parseArgs({
options: {
"allocation-id": { type: "string" },
"data-service": { type: "string" },
collector: { type: "string" },
"timestamp-ns": { type: "string" },
value: { type: "string", default: "0.1" },
db: { type: "string", default: "postgres://postgres@localhost:5432/indexer_components_1" },
},
});

const required = ["allocation-id", "data-service", "collector", "timestamp-ns"];
for (const key of required) {
if (!values[key]) {
console.error(`Missing required argument: --${key}`);
process.exit(1);
}
}

return values;
}

function getEnvVar(name) {
const value = process.env[name];
if (!value) {
console.error(`Missing environment variable: ${name}`);
process.exit(1);
}
return value;
}

function grtToWei(grt) {
const [whole, decimal = ""] = grt.split(".");
const paddedDecimal = decimal.padEnd(18, "0").slice(0, 18);
return BigInt(whole + paddedDecimal);
}

function stripHexPrefix(hex) {
return hex.startsWith("0x") ? hex.slice(2) : hex;
}

async function main() {
const args = parseCliArgs();

const payer = getEnvVar("ACCOUNT0_ADDRESS");
const serviceProvider = getEnvVar("RECEIVER_ADDRESS");
const signerPrivateKey = getEnvVar("ACCOUNT1_SECRET");
const chainId = parseInt(getEnvVar("CHAIN_ID"), 10);

const allocationId = args["allocation-id"];
const dataService = args["data-service"];
const graphTallyCollectorAddress = args["collector"];
const timestampNs = BigInt(args["timestamp-ns"]);
const valueAggregate = grtToWei(args["value"]);
const metadata = "0x";

console.log("Generating signed RAV...");
console.log(" allocationId:", allocationId);
console.log(" payer:", payer);
console.log(" serviceProvider:", serviceProvider);
console.log(" dataService:", dataService);
console.log(" timestampNs:", timestampNs.toString());
console.log(" valueAggregate:", valueAggregate.toString(), "(wei)");

const { signature } = await generateSignedRAV(
allocationId,
payer,
serviceProvider,
dataService,
timestampNs,
valueAggregate,
metadata,
signerPrivateKey,
graphTallyCollectorAddress,
chainId
);

console.log("RAV generated successfully");
console.log(" signature:", signature);

console.log("Connecting to database...");
const client = new pg.Client({ connectionString: args["db"] });
await client.connect();

try {
const collectionId = stripHexPrefix(allocationId).toLowerCase().padStart(64, "0");

const insertQuery = `
INSERT INTO tap_horizon_ravs (
signature,
collection_id,
payer,
data_service,
service_provider,
timestamp_ns,
value_aggregate,
metadata,
last,
final,
redeemed_at,
created_at,
updated_at
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13
)
`;

const now = new Date();
const values = [
Buffer.from(stripHexPrefix(signature), "hex"),
collectionId,
stripHexPrefix(payer).toLowerCase(),
stripHexPrefix(dataService).toLowerCase(),
stripHexPrefix(serviceProvider).toLowerCase(),
timestampNs.toString(),
valueAggregate.toString(),
Buffer.from(stripHexPrefix(metadata) || "", "hex"),
true, // last
false, // final
null, // redeemed_at
now, // created_at
now, // updated_at
];

await client.query(insertQuery, values);
console.log("RAV inserted into database successfully");
} finally {
await client.end();
}

console.log("Done!");
}

main().catch((error) => {
console.error("Script failed:", error);
process.exit(1);
});
8 changes: 8 additions & 0 deletions scripts/js/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"private": true,
"dependencies": {
"@graphprotocol/toolshed": "^0.6.1",
"dotenv": "^16.4.7",
"pg": "^8.13.3"
}
}
Loading