mirror of
https://github.com/KeyZox71/knl_meowscendence.git
synced 2026-01-01 06:06:41 +01:00
「🏗️」 wip: seems to be working
This commit is contained in:
26
src/api/scoreStore/addTx.js
Normal file
26
src/api/scoreStore/addTx.js
Normal 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" });
|
||||
}
|
||||
}
|
||||
@ -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);
|
||||
});
|
||||
}
|
||||
|
||||
@ -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" });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user