Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: sign eip712 #13

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
49 changes: 49 additions & 0 deletions src/sync/sign.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { createWalletClient, http } from "viem";
import { privateKeyToAccount } from "viem/accounts";
import { mainnet } from "viem/chains";
import getPointsByTokenId from "./utils.cjs";
import { Signature } from "ethers";

const KEY = process.env.ORACLE_PRIVATE_KEY || "";

async function sign(tokenId, wallet, chainId) {
const account = privateKeyToAccount(KEY);

const client = createWalletClient({
account,
chain: mainnet,
transport: http(),
});

const points = await getPointsByTokenId(tokenId, chainId);

const domain = {
name: "RewardsController",
version: "1",
chainId,
verifyingContract: "0x",
};

const types = {
Data: [
{ name: "tokenId", type: "uint256" },
{ name: "points", type: "uint256" },
{ name: "nonce", type: "uint256" },
{ name: "user", type: "uint256" },
],
};

const value = {
tokenId: tokenId,
points,
// Get the nonce
nonce: 0,
user: wallet,
};

const signature = await client.signTypedData(domain, types, value);

const { r, s, v } = Signature.from(signature);

return { r, s, v };
}
34 changes: 0 additions & 34 deletions src/sync/sync.js

This file was deleted.

21 changes: 20 additions & 1 deletion src/sync/utils.cjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { get } from "http";

require("dotenv").config();
const postgres = require("postgres");
const sql = postgres(process.env.DATABASE_URL);
Expand Down Expand Up @@ -27,6 +29,23 @@ async function getTokenIdByUserId(userId) {
return tokenId;
}

async function getPointsByTokenId(tokenId, chainId) {
const pointsData = await sql`
SELECT "pointsEarned", "pointsClaimed"
FROM public."TokenIdData"
WHERE "tokenId" = ${tokenId} AND "chainId" = ${chainId}
;`;

if (pointsData.length === 0) {
return 0; // or handle as appropriate for no data found
}

const { pointsEarned, pointsClaimed } = pointsData[0];
const totalPoints = pointsEarned - pointsClaimed;

return totalPoints;
}

async function getClaimedPoints(chainId) {
console.log("Fetching claimed points from the database");
const claimedPoints = await sql`
Expand Down Expand Up @@ -76,4 +95,4 @@ async function getCombinedPoints(chainId) {
return combinedPointByTokenId;
}

module.exports = getCombinedPoints;
module.exports = { getCombinedPoints, getPointsByTokenId };