Skip to content

Commit

Permalink
Merge pull request #545 from shawakash/token_ata
Browse files Browse the repository at this point in the history
feat: adds associated token account create endpoint
  • Loading branch information
shawakash authored May 13, 2024
2 parents a2de20d + 8c1ee56 commit 811243d
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changeset/feat: silver-baboons-hope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@paybox/common": patch
"@paybox/api": patch
---

feat: adds associated token account create endpoint
41 changes: 41 additions & 0 deletions backend/api/src/db/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,44 @@ export const getToken = async (
status: dbResStatus.Error,
};
};

// insert one ata object to db
export const insertAtaOne = async (
owner: string,
clientId: string,
isMinter: boolean,
pubKey: string,
token: string,
): Promise<{
status: dbResStatus;
ataId?: string;
}> => {
const response = await chain("mutation")(
{
insert_ata_one: [
{
object: {
owner,
clientId,
isMinter,
pubKey,
token,
},
},
{
id: true,
},
],
},
{ operationName: "insertAtaOne" },
);
if (response.insert_ata_one?.id) {
return {
status: dbResStatus.Ok,
ataId: response.insert_ata_one.id as string,
};
}
return {
status: dbResStatus.Error,
};
};
74 changes: 73 additions & 1 deletion backend/api/src/routes/token.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
GenAtaSchema,
GetTokenSchema,
MintTokenSchema,
Network,
Expand All @@ -11,7 +12,7 @@ import {
import { Router } from "express";
import { SolTokenOps } from "@paybox/blockchain";
import { checkPassword, getNetworkPrivateKey } from "@paybox/backend-common";
import { getToken, getTokens, insertToken } from "../db/token";
import { getToken, getTokens, insertAtaOne, insertToken } from "../db/token";
import { insertAta } from "../db/ata";
import { decryptWithPassword } from "../auth";
import { Worker } from "../workers/txn";
Expand Down Expand Up @@ -295,3 +296,74 @@ tokenRouter.get("/", async (req, res) => {
});
}
});

tokenRouter.post("/ata", checkPassword, async (req, res) => {
try {
//@ts-ignore;
const id = req.id;
//@ts-ignore;
const hashPassword = req.hashPassword;
if (id) {
const { token, authority, network } = GenAtaSchema.parse(req.body);

let { status, privateKey } = await getNetworkPrivateKey(
authority,
network,
);
if (status == dbResStatus.Error || !privateKey) {
return res.status(404).json({
msg: "That account is not Found in Database...",
status: responseStatus.Error,
});
}
privateKey = await decryptWithPassword(privateKey, hashPassword);

let instance;
switch (network) {
case Network.Sol:
instance = await SolTokenOps.getInstance().genAta(token, privateKey);
break;
case Network.Eth:
break;
default:
break;
}

if (!instance) {
return res.status(404).json({
status: responseStatus.Error,
msg: `Sorry, ${network} is not yet supported...`,
});
}

const { status: insertAtaStatus, ataId } = await insertAtaOne(
authority,
id,
false,
instance,
token,
);
if (insertAtaStatus == dbResStatus.Error || !ataId) {
return res.status(500).json({
msg: "Insert Db Error",
status: responseStatus.Error,
});
}

return res.status(200).json({
status: responseStatus.Ok,
ataId,
});
}
return res.status(401).json({
status: responseStatus.Error,
msg: "Auth Error",
});
} catch (e) {
console.log(e);
return res.status(500).json({
msg: "Internal Server Error",
status: responseStatus.Error,
});
}
});
6 changes: 6 additions & 0 deletions packages/common/src/validations/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,9 @@ export const GetTokenSchema = z.object({
"should be a valid UUID.",
),
});

export const GenAtaSchema = z.object({
token: z.string(),
authority: publicKeyType,
network: z.nativeEnum(Network),
});

0 comments on commit 811243d

Please sign in to comment.