🏗️」 wip: seems to be working

This commit is contained in:
2025-07-30 16:04:18 +02:00
parent 0449ad7ad2
commit 041bb2deb5
10 changed files with 252 additions and 12 deletions

View File

@ -0,0 +1,26 @@
import { ContractTransactionResponse } from "ethers";
import scoreDB from "../../utils/scoreDB.js";
import { callAddScore, callLastId } from "../../utils/scoreStore_contract.js";
/**
* @async
* @param {import("fastify").FastifyRequest} request
* @param {import("fastify").FastifyReply} reply
* @param {import("fastify").FastifyInstance} fastify
*/
export async function addTx(request, reply, fastify) {
try {
const id = await callLastId();
/** @type ContractTransactionResponse */
const tx = await callAddScore(request.body.p1, request.body.p2, request.body.p1Score, request.body.p2Score);
scoreDB.addTx(id, tx.hash);
return reply.code(200).send({
tx: tx.hash
});
} catch (err) {
fastify.log.error(err);
return reply.code(500).send({ error: "Internal server error" });
}
}

View File

@ -1,6 +1,8 @@
import { evm } from '@avalabs/avalanchejs';
import scoreDB from '../../utils/scoreDB.js';
import { getTx } from './getTx.js';
import { addTx } from './addTx.js';
import scoreDB from '../../utils/scoreDB.js';
scoreDB.prepareDB();
/**
* @param {import('fastify').FastifyInstance} fastify
@ -10,4 +12,21 @@ export default async function(fastify, options) {
fastify.get("/:id", async (request, reply) => {
return getTx(request, reply, fastify);
});
fastify.post("/", {
schema: {
body: {
type: 'object',
required: ['p1', 'p2', 'p1Score', 'p2Score'],
properties: {
p1: { type: 'string', minLength: 1 },
p2: { type: 'string', minLength: 1 },
p1Score: { type: 'integer', minimum: 0 },
p2Score: { type: 'integer', minimum: 0 },
}
}
}
}, async (request, reply) => {
return addTx(request, reply, fastify);
});
}

View File

@ -1,4 +1,5 @@
import scoreDB from "../../utils/scoreDB.js";
import { callGetScore } from "../../utils/scoreStore_contract.js";
/**
* @async
@ -10,10 +11,16 @@ import scoreDB from "../../utils/scoreDB.js";
*/
export async function getTx(request, reply, fastify) {
try {
const tx = scoreDB.getTx(request.params.id);
const score = callGetScore(request.params.id);
return reply.code(200).send({
score: score,
tx: tx
});
} catch (err) {
fastify.log.error(err);
return reply.code(500).send({ error: "Internal server error" });
}
}