🏗️」 wip: started doing the api for the contract

This commit is contained in:
2025-07-28 21:49:48 +02:00
parent 443a5a4099
commit 0449ad7ad2
7 changed files with 151 additions and 3 deletions

View File

@ -0,0 +1,13 @@
import { evm } from '@avalabs/avalanchejs';
import scoreDB from '../../utils/scoreDB.js';
import { getTx } from './getTx.js';
/**
* @param {import('fastify').FastifyInstance} fastify
* @param {import('fastify').FastifyPluginOptions} options
*/
export default async function(fastify, options) {
fastify.get("/:id", async (request, reply) => {
return getTx(request, reply, fastify);
});
}

View File

@ -0,0 +1,19 @@
import scoreDB from "../../utils/scoreDB.js";
/**
* @async
* @param {import("fastify".FastifyRequest)} request
* @param {import("fastify").FastifyReply} reply
* @param {import("fastify").FastifyInstance} fastify
*
* @returns {import('fastify').FastifyReply}
*/
export async function getTx(request, reply, fastify) {
try {
} catch (err) {
fastify.log.error(err);
return reply.code(500).send({ error: "Internal server error" });
}
}

52
src/utils/scoreDB.js Normal file
View File

@ -0,0 +1,52 @@
import { Int } from "@avalabs/avalanchejs";
import Database from "better-sqlite3";
var env = process.env.NODE_ENV || 'development';
let database;
if (!env || env === 'development') {
database = new Database(":memory:", { verbose: console.log });
} else {
var dbPath = process.env.DB_PATH || '/db/db.sqlite';
database = new Database(dbPath);
}
/**
* @description Can be used to prepare the database
*/
function prepareDB() {
database.exec(`
CREATE TABLE IF NOT EXISTS scoresTx (
id INTEGER PRIMARY KEY,
txHash TEXT
) STRICT
`);
}
/**
* @description Can be used to add a score hash to the DB
* @param {Int} The id of the score
* @param {String} The hash of the score
*/
function addTx(id, txHash) {
const txAdd = database.prepare('INSERT INTO scoresTx (id, txHash) VALUES (?, ?)');
txAdd.run(id, txHash);
}
/**
* @description Can be used to get a tx hash from an id
* @param {Int} The id to get
* @returns {String} The tx hash
*/
function getTx(id) {
const txGet = database.prepare('SELECT txHash FROM credentials WHERE id = ?;')
return txGet.get(id);
}
const scoreDB = {
prepareDB,
addTx,
getTx
};
export default scoreDB;