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: adds token mint endpoint and token_txn messaging queue workflow #536

Merged
merged 9 commits into from
May 11, 2024
104 changes: 104 additions & 0 deletions backend/api/src/routes/token.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import {
MintTokenSchema,
Network,
TokenCreateSchema,
TopicTypes,
TxnTopic,
dbResStatus,
responseStatus,
} from "@paybox/common";
Expand All @@ -10,6 +13,7 @@ import { checkPassword, getNetworkPrivateKey } from "@paybox/backend-common";
import { insertToken } from "../db/token";
import { insertAta } from "../db/ata";
import { decryptWithPassword } from "../auth";
import { Worker } from "../workers/txn";

export const tokenRouter = Router();

Expand Down Expand Up @@ -108,3 +112,103 @@ tokenRouter.post("/create", checkPassword, async (req, res) => {
});
}
});

tokenRouter.post("/mint", checkPassword, async (req, res) => {
try {
//@ts-ignore
const id = req.id;
//@ts-ignore
const hashPassword = req.hashPassword;

if (id && hashPassword) {
const { mint, authority, network, tokens, ata, username } =
MintTokenSchema.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().mintToken(
privateKey,
mint,
ata,
tokens,
);
break;

case Network.Eth:
break;

default:
return res.status(404).json({
msg: `${network} is not yet Supported...`,
status: responseStatus.Error,
});
}

if (!instance) {
return res.status(404).json({
msg: "Sorry, We don't yet Support tokens on that chain",
status: responseStatus.Error,
});
}

console.log(instance);

try {
//publishing to push the txn
await Worker.getInstance().publishOne({
topic: TopicTypes.Txn,
message: [
{
partition: 0,
key: instance,
value: JSON.stringify({
type: TxnTopic.Finalized,
chain: network,
from: id,
to: username,
hash: instance,
isTokenTxn: true,
isMint: true,
}),
},
],
});
} catch (e) {
console.log(e);
return res.status(500).json({
msg: "Error publishing the txn",
status: responseStatus.Error,
});
}

return res.status(200).json({
msg: `${tokens} minted successfully...`,
status: responseStatus.Ok,
});
}

return res.status(401).json({
msg: "Auth Error",
status: responseStatus.Error,
});
} catch (e) {
console.log(e);
return res.status(500).json({
msg: "Internal Server Error",
status: responseStatus.Error,
});
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,18 @@ object_relationships:
- name: tokenByToken
using:
foreign_key_constraint_on: token
array_relationships:
- name: tokenTxnsByToata
using:
foreign_key_constraint_on:
column: toAta
table:
name: token_txn
schema: public
- name: token_txns
using:
foreign_key_constraint_on:
column: fromAta
table:
name: token_txn
schema: public
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ array_relationships:
table:
name: notification
schema: public
- name: token_txns
using:
foreign_key_constraint_on:
column: clientId
table:
name: token_txn
schema: public
- name: tokens
using:
foreign_key_constraint_on:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,10 @@ array_relationships:
table:
name: ata
schema: public
- name: token_txns
using:
foreign_key_constraint_on:
column: token
table:
name: token_txn
schema: public
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
table:
name: token_txn
schema: public
object_relationships:
- name: token_txn_to_client
using:
foreign_key_constraint_on: clientId
- name: token_txn_to_token
using:
foreign_key_constraint_on: token
- name: txn_fromAta_to_ata
using:
foreign_key_constraint_on: fromAta
- name: txn_toAta_to_ata
using:
foreign_key_constraint_on: toAta
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
- "!include public_notification_subscription.yaml"
- "!include public_sol.yaml"
- "!include public_token.yaml"
- "!include public_token_txn.yaml"
- "!include public_transactions.yaml"
- "!include public_wallet.yaml"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE "public"."token_txn";
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
CREATE TABLE "public"."token_txn" ("id" uuid NOT NULL DEFAULT gen_random_uuid(), "blockHash" text NOT NULL, "fee" float8 NOT NULL, "hash" text NOT NULL, "amount" float8 NOT NULL, "fromAta" text NOT NULL, "toAta" text NOT NULL, "isMint" boolean NOT NULL DEFAULT false, "token" text NOT NULL, "network" text NOT NULL, "slot" int8 NOT NULL, "status" text NOT NULL DEFAULT 'pending', "time" time NOT NULL, "created_at" timestamptz NOT NULL DEFAULT now(), "updated_at" timestamptz NOT NULL DEFAULT now(), "clientId" uuid NOT NULL, PRIMARY KEY ("id") , FOREIGN KEY ("clientId") REFERENCES "public"."client"("id") ON UPDATE no action ON DELETE no action, FOREIGN KEY ("token") REFERENCES "public"."token"("pubKey") ON UPDATE no action ON DELETE no action, FOREIGN KEY ("fromAta") REFERENCES "public"."ata"("pubKey") ON UPDATE no action ON DELETE no action, FOREIGN KEY ("toAta") REFERENCES "public"."ata"("pubKey") ON UPDATE no action ON DELETE no action, UNIQUE ("id"));COMMENT ON TABLE "public"."token_txn" IS E'all the token transactions';
CREATE OR REPLACE FUNCTION "public"."set_current_timestamp_updated_at"()
RETURNS TRIGGER AS $$
DECLARE
_new record;
BEGIN
_new := NEW;
_new."updated_at" = NOW();
RETURN _new;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER "set_public_token_txn_updated_at"
BEFORE UPDATE ON "public"."token_txn"
FOR EACH ROW
EXECUTE PROCEDURE "public"."set_current_timestamp_updated_at"();
COMMENT ON TRIGGER "set_public_token_txn_updated_at" ON "public"."token_txn"
IS 'trigger to set value of column "updated_at" to current timestamp on row update';
CREATE EXTENSION IF NOT EXISTS pgcrypto;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "public"."token_txn" ALTER COLUMN "time" TYPE time without time zone;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "public"."token_txn" ALTER COLUMN "time" TYPE timetz;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
comment on column "public"."token_txn"."time" is E'all the token transactions';
alter table "public"."token_txn" alter column "time" drop not null;
alter table "public"."token_txn" add column "time" timetz;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter table "public"."token_txn" drop column "time" cascade;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- Could not auto-generate a down migration.
-- Please write an appropriate down migration for the SQL below:
-- alter table "public"."token_txn" add column "time" timestamptz
-- not null;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
alter table "public"."token_txn" add column "time" timestamptz
not null;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter table "public"."token_txn" rename column "fromAtaOwner" to "fromAta";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter table "public"."token_txn" rename column "fromAta" to "fromAtaOwner";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter table "public"."token_txn" rename column "toAtaOwner" to "toAta";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter table "public"."token_txn" rename column "toAta" to "toAtaOwner";
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
alter table "public"."token_txn"
add constraint "token_txn_fromAta_fkey"
foreign key ("fromAtaOwner")
references "public"."ata"
("pubKey") on update no action on delete no action;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter table "public"."token_txn" drop constraint "token_txn_fromAta_fkey";
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
alter table "public"."token_txn"
add constraint "token_txn_toAta_fkey"
foreign key ("toAtaOwner")
references "public"."ata"
("pubKey") on update no action on delete no action;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter table "public"."token_txn" drop constraint "token_txn_toAta_fkey";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter table "public"."token_txn" rename column "fromAta" to "fromAtaOwner";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter table "public"."token_txn" rename column "fromAtaOwner" to "fromAta";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter table "public"."token_txn" rename column "toAta" to "toAtaOwner";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter table "public"."token_txn" rename column "toAtaOwner" to "toAta";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter table "public"."token_txn" drop constraint "token_txn_fromAta_fkey";
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
alter table "public"."token_txn"
add constraint "token_txn_fromAta_fkey"
foreign key ("fromAta")
references "public"."ata"
("pubKey") on update no action on delete no action;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter table "public"."token_txn" drop constraint "token_txn_toAta_fkey";
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
alter table "public"."token_txn"
add constraint "token_txn_toAta_fkey"
foreign key ("toAta")
references "public"."ata"
("pubKey") on update no action on delete no action;
Loading
Loading