diff --git a/apps/cartesify/backend/js-template/src/example.js b/apps/cartesify/backend/js-template/src/example.js deleted file mode 100644 index afb0a53..0000000 --- a/apps/cartesify/backend/js-template/src/example.js +++ /dev/null @@ -1,295 +0,0 @@ -const express = require("express"); -const axios = require("axios"); -const { createWallet } = require("@deroll/wallet"); -const { CartesifyBackend } = require("@calindra/cartesify-backend") - -console.log('starting app.js...'); - -const app = express(); -const port = 8383; -app.use(express.json()); - -let greetings = []; -let nextId = 1; -let dapp -let wallet - -CartesifyBackend.createDapp().then(initDapp => { - console.log('Dapp started'); - initDapp.start().catch(e => { - console.error(e); - process.exit(1); - }); - - dapp = initDapp; - wallet = createWallet(); - - dapp.addAdvanceHandler(() => { - console.log('before wallet handler'); - return "reject"; - }); - - dapp.addAdvanceHandler(wallet.handler); - - dapp.addAdvanceHandler(() => { - console.log('final handler'); - return "reject"; - }); -}); - -app.post('/greetings', (req, res) => { - const { message, sender } = req.body; - const newGreeting = { id: nextId++, message, sender }; - greetings.push(newGreeting); - if (message) { - res.status(200).json(newGreeting); - } else { - res.status(400).json({ error: 'Please provide a greeting' }); - } -}); - -app.get('/greetings/:id', (req, res) => { - try { - const id = parseInt(req.params.id, 10); - const greet = greetings.find(greet => greet.id === id); - if (greet) { - res.status(200).json({ message: greet }); - } else { - res.status(404).json({ error: 'Greeting not found' }); - } - } catch (e) { - res.status(400).json({ error: 'No greeting provided' }); - } -}); - -app.get('/greetings', (req, res) => { - try { - if (greetings) { - res.status(200).json({ greetings }); - } - } catch (e) { - res.status(400).json({ error: 'No greeting provided' }); - } -}); - -app.put('/greetings/:id', (req, res) => { - const id = parseInt(req.params.id, 10); - const index = greetings.findIndex(greet => greet.id === id); - - if (index === -1) { - return res.status(404).json({ error: 'Item not found' }); - } - - const updatedGreeting = { id: id, message: req.body.message }; - greetings[index] = updatedGreeting; - - res.json(updatedGreeting); -}); - -app.delete('/greetings/:id', (req, res) => { - const id = parseInt(req.params.id, 10); - const index = greetings.findIndex(greet => greet.id === id); - if (index === -1) { - return res.status(404).json({ error: 'Item not found' }); - } - greetings = greetings.filter(greet => greet.id !== id); - res.json({ message: 'Item deleted successfully' }); -}); - -app.delete('/greetings', (req, res) => { - const deletedCount = greetings.length; - greetings.length = 0; - res.status(200).send({ message: `${deletedCount} greetings deleted` }); -}); - -app.get("/wallet/:address", async (req, res) => { - try{ - if(!wallet || !wallet.getWallet) throw new Error("Wallet not accessible!"); - console.log(`Checking balance ${req.params.address}`); - const userWallet = await wallet.getWallet(req.params.address); - console.log("UserWallet", userWallet) - const json = JSON.stringify(userWallet, (_key, value) => { - if (typeof value === 'bigint') { - return value.toString(); - } else if (typeof value === 'object' && value instanceof Map) { - return Object.fromEntries(value); - } else if (typeof value === 'object' && value instanceof Set) { - return [...value]; - } else { - return value; - } - }); - res.setHeader('Content-Type', 'application/json; charset=utf-8'); - res.send(json); - }catch(e){ - res.status(400).send({ message: e.message }); - } - -}); - -app.post("/wallet/ether/transfer", async (req, res) => { - try { - wallet.transferEther( - req.get('x-msg_sender'), - req.body.to, - BigInt(req.body.amount), - ); - res.send({ ok: 1 }); - } catch (e) { - res.status(400).send({ message: e.message }); - } -}); - -app.post("/wallet/ether/withdraw", async (req, res) => { - try { - if(!wallet || !wallet.withdrawEther) throw new Error("Wallet not accessible!"); - const voucher = wallet.withdrawEther( - req.get('x-msg_sender'), - BigInt(req.body.amount) - ); - const voucherResult = await dapp.createVoucher(voucher); - res.send({ - ok: 1, voucherResult, inputIndex: req.get('x-input_index') - }); - } catch (e) { - res.status(400).send({ message: e.message }); - } -}); - -app.post("/wallet/erc-20/withdraw", async (req, res) => { - try { - if(!wallet || !wallet.withdrawERC20) throw new Error("Wallet not accessible!"); - const voucher = wallet.withdrawERC20( - req.body.token, - req.get('x-msg_sender'), - BigInt(req.body.amount) - ); - const voucherResult = await dapp.createVoucher(voucher); - res.send({ - ok: 1, voucherResult, inputIndex: req.get('x-input_index') - }); - } catch (e) { - res.status(400).send({ message: e.message }); - } -}); - -app.post("/wallet/erc-20/transfer", async (req, res) => { - try { - wallet.transferERC20( - req.body.token, - req.get('x-msg_sender'), - req.body.to, - BigInt(req.body.amount) - ); - res.send({ ok: 1 }); - } catch (e) { - res.status(400).send({ message: e.message }); - } -}); - -app.post("/wallet/erc-721/transfer", async (req, res) => { - try { - wallet.transferERC721( - req.body.token, - req.get('x-msg_sender'), - req.body.to, - BigInt(req.body.tokenId) - ); - res.send({ ok: 1 }); - } catch (e) { - res.status(400).send({ message: e.message }); - } -}); - -app.post("/wallet/erc-1155/transfer", async (req, res) => { - try { - wallet.transferERC1155( - req.body.token, - req.get('x-msg_sender'), - req.body.to, - req.body.tokenIds.map(id => { - if (typeof id !== 'number') { - throw new Error(`BadRequest id ${id} is not a number`); - } - return BigInt(id); - }), - req.body.values.map(value => { - if (typeof value !== 'number') { - throw new Error(`BadRequest value ${value} is not a number`); - } - return BigInt(value); - }) - ); - res.send({ ok: 1 }); - } catch (e) { - res.status(400).send({ message: e.message }); - } -}); - -app.post("/wallet/erc-721/withdraw", async (req, res) => { - try { - const voucher = wallet.withdrawERC721( - req.body.token, - req.get('x-msg_sender'), - BigInt(req.body.tokenId) - ); - const voucherResult = await dapp.createVoucher(voucher); - res.send({ - ok: 1, voucherResult, inputIndex: req.get('x-input_index') - }); - } catch (e) { - res.status(400).send({ message: e.message }); - } -}); - -app.post("/wallet/erc-1155/withdraw", async (req, res) => { - try { - const voucher = wallet.withdrawERC1155( - req.body.token, - req.get('x-msg_sender'), - req.body.tokenIds.map(id => { - if (typeof id !== 'number') { - throw new Error('BadRequest'); - } - return BigInt(id); - }), - req.body.values.map(value => { - if (typeof value !== 'number') { - throw new Error('BadRequest'); - } - return BigInt(value); - }), - "0x" - ); - const voucherResult = await dapp.createVoucher(voucher); - res.send({ - ok: 1, voucherResult, inputIndex: req.get('x-input_index') - }); - } catch (e) { - res.status(400).send({ message: e.message }); - } -}); - -app.post('/deposit', (req, res) => { - axios.post('http://deroll/voucher'); -}); - -app.get("/health", (req, res) => { - res.send({ some: "response" }); -}); - -app.post('/echo', (req, res) => { - res.send({ myPost: req.body }); -}); - -app.post('/echo/headers', (req, res) => { - res.send({ headers: req.headers }); -}); - -app.get('/echo/headers', (req, res) => { - res.send({ headers: req.headers }); -}); - -app.listen(port, () => { - console.log(`[server]: Server is running at http://localhost:${port}`); -}); diff --git a/apps/cartesify/backend/js-template/src/index.js b/apps/cartesify/backend/js-template/src/index.js index 9dc9612..afb0a53 100644 --- a/apps/cartesify/backend/js-template/src/index.js +++ b/apps/cartesify/backend/js-template/src/index.js @@ -1,37 +1,294 @@ -// Cartesify REST bridge +const express = require("express"); +const axios = require("axios"); +const { createWallet } = require("@deroll/wallet"); const { CartesifyBackend } = require("@calindra/cartesify-backend") +console.log('starting app.js...'); -CartesifyBackend.createDapp().then((dapp) => { - dapp.start().catch((e) => { +const app = express(); +const port = 8383; +app.use(express.json()); + +let greetings = []; +let nextId = 1; +let dapp +let wallet + +CartesifyBackend.createDapp().then(initDapp => { + console.log('Dapp started'); + initDapp.start().catch(e => { console.error(e); process.exit(1); }); + + dapp = initDapp; + wallet = createWallet(); + + dapp.addAdvanceHandler(() => { + console.log('before wallet handler'); + return "reject"; + }); + + dapp.addAdvanceHandler(wallet.handler); + + dapp.addAdvanceHandler(() => { + console.log('final handler'); + return "reject"; + }); }); -// Normal nodejs application using express -const express = require("express"); +app.post('/greetings', (req, res) => { + const { message, sender } = req.body; + const newGreeting = { id: nextId++, message, sender }; + greetings.push(newGreeting); + if (message) { + res.status(200).json(newGreeting); + } else { + res.status(400).json({ error: 'Please provide a greeting' }); + } +}); -const app = express(); -// Middleware to parse JSON bodies -app.use(bodyParser.json()); +app.get('/greetings/:id', (req, res) => { + try { + const id = parseInt(req.params.id, 10); + const greet = greetings.find(greet => greet.id === id); + if (greet) { + res.status(200).json({ message: greet }); + } else { + res.status(404).json({ error: 'Greeting not found' }); + } + } catch (e) { + res.status(400).json({ error: 'No greeting provided' }); + } +}); -const port = 8383; +app.get('/greetings', (req, res) => { + try { + if (greetings) { + res.status(200).json({ greetings }); + } + } catch (e) { + res.status(400).json({ error: 'No greeting provided' }); + } +}); -app.use(express.json()); +app.put('/greetings/:id', (req, res) => { + const id = parseInt(req.params.id, 10); + const index = greetings.findIndex(greet => greet.id === id); + + if (index === -1) { + return res.status(404).json({ error: 'Item not found' }); + } + const updatedGreeting = { id: id, message: req.body.message }; + greetings[index] = updatedGreeting; + + res.json(updatedGreeting); +}); + +app.delete('/greetings/:id', (req, res) => { + const id = parseInt(req.params.id, 10); + const index = greetings.findIndex(greet => greet.id === id); + if (index === -1) { + return res.status(404).json({ error: 'Item not found' }); + } + greetings = greetings.filter(greet => greet.id !== id); + res.json({ message: 'Item deleted successfully' }); +}); + +app.delete('/greetings', (req, res) => { + const deletedCount = greetings.length; + greetings.length = 0; + res.status(200).send({ message: `${deletedCount} greetings deleted` }); +}); -app.post("/your-endpoint", (req, res) => { +app.get("/wallet/:address", async (req, res) => { try{ - console.log("Request received on your endpoint") - const senderAddress = req.header("x-msg_sender"); - res.send({ some: "response", senderAddress }); - }catch(error){ - return error + if(!wallet || !wallet.getWallet) throw new Error("Wallet not accessible!"); + console.log(`Checking balance ${req.params.address}`); + const userWallet = await wallet.getWallet(req.params.address); + console.log("UserWallet", userWallet) + const json = JSON.stringify(userWallet, (_key, value) => { + if (typeof value === 'bigint') { + return value.toString(); + } else if (typeof value === 'object' && value instanceof Map) { + return Object.fromEntries(value); + } else if (typeof value === 'object' && value instanceof Set) { + return [...value]; + } else { + return value; + } + }); + res.setHeader('Content-Type', 'application/json; charset=utf-8'); + res.send(json); + }catch(e){ + res.status(400).send({ message: e.message }); } + +}); + +app.post("/wallet/ether/transfer", async (req, res) => { + try { + wallet.transferEther( + req.get('x-msg_sender'), + req.body.to, + BigInt(req.body.amount), + ); + res.send({ ok: 1 }); + } catch (e) { + res.status(400).send({ message: e.message }); + } }); +app.post("/wallet/ether/withdraw", async (req, res) => { + try { + if(!wallet || !wallet.withdrawEther) throw new Error("Wallet not accessible!"); + const voucher = wallet.withdrawEther( + req.get('x-msg_sender'), + BigInt(req.body.amount) + ); + const voucherResult = await dapp.createVoucher(voucher); + res.send({ + ok: 1, voucherResult, inputIndex: req.get('x-input_index') + }); + } catch (e) { + res.status(400).send({ message: e.message }); + } +}); +app.post("/wallet/erc-20/withdraw", async (req, res) => { + try { + if(!wallet || !wallet.withdrawERC20) throw new Error("Wallet not accessible!"); + const voucher = wallet.withdrawERC20( + req.body.token, + req.get('x-msg_sender'), + BigInt(req.body.amount) + ); + const voucherResult = await dapp.createVoucher(voucher); + res.send({ + ok: 1, voucherResult, inputIndex: req.get('x-input_index') + }); + } catch (e) { + res.status(400).send({ message: e.message }); + } +}); + +app.post("/wallet/erc-20/transfer", async (req, res) => { + try { + wallet.transferERC20( + req.body.token, + req.get('x-msg_sender'), + req.body.to, + BigInt(req.body.amount) + ); + res.send({ ok: 1 }); + } catch (e) { + res.status(400).send({ message: e.message }); + } +}); + +app.post("/wallet/erc-721/transfer", async (req, res) => { + try { + wallet.transferERC721( + req.body.token, + req.get('x-msg_sender'), + req.body.to, + BigInt(req.body.tokenId) + ); + res.send({ ok: 1 }); + } catch (e) { + res.status(400).send({ message: e.message }); + } +}); + +app.post("/wallet/erc-1155/transfer", async (req, res) => { + try { + wallet.transferERC1155( + req.body.token, + req.get('x-msg_sender'), + req.body.to, + req.body.tokenIds.map(id => { + if (typeof id !== 'number') { + throw new Error(`BadRequest id ${id} is not a number`); + } + return BigInt(id); + }), + req.body.values.map(value => { + if (typeof value !== 'number') { + throw new Error(`BadRequest value ${value} is not a number`); + } + return BigInt(value); + }) + ); + res.send({ ok: 1 }); + } catch (e) { + res.status(400).send({ message: e.message }); + } +}); + +app.post("/wallet/erc-721/withdraw", async (req, res) => { + try { + const voucher = wallet.withdrawERC721( + req.body.token, + req.get('x-msg_sender'), + BigInt(req.body.tokenId) + ); + const voucherResult = await dapp.createVoucher(voucher); + res.send({ + ok: 1, voucherResult, inputIndex: req.get('x-input_index') + }); + } catch (e) { + res.status(400).send({ message: e.message }); + } +}); + +app.post("/wallet/erc-1155/withdraw", async (req, res) => { + try { + const voucher = wallet.withdrawERC1155( + req.body.token, + req.get('x-msg_sender'), + req.body.tokenIds.map(id => { + if (typeof id !== 'number') { + throw new Error('BadRequest'); + } + return BigInt(id); + }), + req.body.values.map(value => { + if (typeof value !== 'number') { + throw new Error('BadRequest'); + } + return BigInt(value); + }), + "0x" + ); + const voucherResult = await dapp.createVoucher(voucher); + res.send({ + ok: 1, voucherResult, inputIndex: req.get('x-input_index') + }); + } catch (e) { + res.status(400).send({ message: e.message }); + } +}); + +app.post('/deposit', (req, res) => { + axios.post('http://deroll/voucher'); +}); + +app.get("/health", (req, res) => { + res.send({ some: "response" }); +}); + +app.post('/echo', (req, res) => { + res.send({ myPost: req.body }); +}); + +app.post('/echo/headers', (req, res) => { + res.send({ headers: req.headers }); +}); + +app.get('/echo/headers', (req, res) => { + res.send({ headers: req.headers }); +}); app.listen(port, () => { console.log(`[server]: Server is running at http://localhost:${port}`); diff --git a/apps/cartesify/backend/ts-template/src/examples.ts b/apps/cartesify/backend/ts-template/src/examples.ts deleted file mode 100644 index 681b7b9..0000000 --- a/apps/cartesify/backend/ts-template/src/examples.ts +++ /dev/null @@ -1,314 +0,0 @@ - -import { Request, Response } from 'express'; -import axios from "axios" -import { createWallet, WalletApp } from "@deroll/wallet"; -import CartesifyBackend from '@calindra/cartesify-backend'; - -console.log('starting app.js...'); - -const express = require("express"); -const app = express(); -const port = 8383; -app.use(express.json()); - -interface Greeting { - id: number; - message: string; - sender: string -} - -let greetings: Greeting[] = []; -let nextId = 1; - - -let dapp: any; -let wallet: WalletApp - -CartesifyBackend.createDapp().then(initDapp => { - console.log('Dapp started'); - initDapp.start().catch((e: Error) => { - console.error(e); - process.exit(1); - }); - - dapp = initDapp; - wallet = createWallet(); - - dapp.addAdvanceHandler((): string => { - console.log('before wallet handler'); - return "reject"; - }); - - dapp.addAdvanceHandler(wallet.handler); - - dapp.addAdvanceHandler((): string => { - console.log('final handler'); - return "reject"; - }); -}); - - app.post('/greetings', (req: Request, res: Response) => { - const { message, sender } = req.body; - const newGreeting: Greeting = { id: nextId++, message, sender }; - greetings.push(newGreeting); - if (message) { - res.status(200).json(newGreeting); - } else { - res.status(400).json({ error: 'Please provide a greeting' }); - } - }); - - // GET request to the /greeting/:id endpoint - app.get('/greetings/:id', (req: Request, res: Response) => { - try { - const id = parseInt(req.params.id!, 10); - const greet = greetings.find(greet => greet.id === id); - if (greet) { - res.status(200).json({ message: greet }); - } else { - res.status(404).json({ error: 'Greeting not found' }); - } - } catch (e) { - res.status(400).json({ error: 'No greeting provided' }); - } - }); - - // GET request to the /greetings endpoint - app.get('/greetings', (req: Request, res: Response) => { - try { - if (greetings) { - res.status(200).json({ greetings }); - } - } catch (e) { - res.status(400).json({ error: 'No greeting provided' }); - } - }); - - app.put('/greetings/:id', (req: Request, res: Response) => { - const id = parseInt(req.params.id!, 10); - const index = greetings.findIndex(greet => greet.id === id); - - if (index === -1) { - return res.status(404).json({ error: 'Item not found' }); - } - - const updatedGreeting = { id: id, message: req.body.message } as Greeting; // Assuming request body contains the updated item data - greetings[index] = updatedGreeting; - - res.json(updatedGreeting); - }); - - app.delete('/greetings/:id', (req: Request, res: Response) => { - const id = parseInt(req.params.id!, 10); - const index = greetings.findIndex(greet => greet.id === id); - if (index === -1) { - return res.status(404).json({ error: 'Item not found' }); - } - greetings = greetings.filter(greet => greet.id !== id); - res.json({ message: 'Item deleted successfully' }); - }); - - app.delete('/greetings', (req: Request, res: Response) => { - const deletedCount = greetings.length; - greetings.length = 0; - res.status(200).send({ message: `${deletedCount} greetings deleted` }); - }); - - - app.get("/wallet/:address", async (req: Request, res: Response) => { - try{ - wallet = createWallet() - console.log(`Checking balance ${req.params.address}`); - const userWallet = wallet.getWallet(req.params.address!); - console.log("UserWallet", userWallet) - const json = JSON.stringify(userWallet, (_key, value) => { - if (typeof value === 'bigint') { - return value.toString(); - } else if (typeof value === 'object' && value instanceof Map) { - return Object.fromEntries(value); - } else if (typeof value === 'object' && value instanceof Set) { - return [...value]; - } else { - return value; - } - }); - res.setHeader('Content-Type', 'application/json; charset=utf-8'); - res.send(json); - }catch(e: any){ - res.status(400).send({ message: e.message }); - } - - }); - - app.post("/wallet/ether/transfer", async (req: Request, res: Response) => { - try { - wallet.transferEther( - req.get('x-msg_sender') as string, - req.body.to, - BigInt(req.body.amount), - ); - res.send({ ok: 1 }); - } catch (e: any) { - res.status(400).send({ message: e.message }); - } - }); - - app.post("/wallet/ether/withdraw", async (req: Request, res: Response) => { - try { - if(!wallet || !wallet.withdrawEther) throw new Error("Wallet not accessible!") - const voucher = wallet.withdrawEther( - req.get('x-msg_sender') as `0x${string}`, - BigInt(req.body.amount) - ); - const voucherResult = await dapp.createVoucher(voucher); - res.send({ - ok: 1, voucherResult, inputIndex: req.get('x-input_index') - }); - } catch (e: any) { - res.status(400).send({ message: e.message }); - } - }); - - app.post("/wallet/erc-20/withdraw", async (req: Request, res: Response) => { - try { - if(!wallet || !wallet.withdrawERC20) throw new Error("Wallet not accessible!") - const voucher = wallet.withdrawERC20( - req.body.token, - req.get('x-msg_sender') as `0x${string}`, - BigInt(req.body.amount) - ); - const voucherResult = await dapp.createVoucher(voucher); - res.send({ - ok: 1, voucherResult, inputIndex: req.get('x-input_index') - }); - } catch (e: any) { - res.status(400).send({ message: e.message }); - } - }); - - app.post("/wallet/erc-20/transfer", async (req: Request, res: Response) => { - try { - wallet.transferERC20( - req.body.token, - req.get('x-msg_sender') as string, - req.body.to, - BigInt(req.body.amount) - ); - res.send({ ok: 1 }); - } catch (e: any) { - res.status(400).send({ message: e.message }); - } - }); - - app.post("/wallet/erc-721/transfer", async (req: Request, res: Response) => { - try { - wallet.transferERC721( - req.body.token, - req.get('x-msg_sender') as string, - req.body.to, - BigInt(req.body.tokenId) - ); - res.send({ ok: 1 }); - } catch (e: any) { - res.status(400).send({ message: e.message }); - } - }); - - app.post("/wallet/erc-1155/transfer", async (req: Request, res: Response) => { - try { - wallet.transferERC1155( - req.body.token, - req.get('x-msg_sender') as string, - req.body.to, - req.body.tokenIds.map((id: number) => { - if (typeof id !== 'number') { - throw new Error(`BadRequest id ${id} is not a number`); - } - return BigInt(id); - }), - req.body.values.map((value: number) => { - if (typeof value !== 'number') { - throw new Error(`BadRequest value ${value} is not a number`); - } - return BigInt(value); - }) - ); - res.send({ ok: 1 }); - } catch (e: any) { - res.status(400).send({ message: e.message }); - } - }); - - app.post("/wallet/erc-721/withdraw", async (req: Request, res: Response) => { - try { - const voucher = wallet.withdrawERC721( - req.body.token, - req.get('x-msg_sender') as `0x${string}`, - BigInt(req.body.tokenId) - ); - const voucherResult = await dapp.createVoucher(voucher); - res.send({ - ok: 1, voucherResult, inputIndex: req.get('x-input_index') - }); - } catch (e: any) { - res.status(400).send({ message: e.message }); - } - }); - - app.post("/wallet/erc-1155/withdraw", async (req: Request, res: Response) => { - try { - const voucher = wallet.withdrawERC1155( - req.body.token, - req.get('x-msg_sender') as `0x${string}`, - req.body.tokenIds.map((id: number) => { - if (typeof id !== 'number') { - throw new Error('BadRequest'); - } - return BigInt(id); - }), - req.body.values.map((value: number) => { - if (typeof value !== 'number') { - throw new Error('BadRequest'); - } - return BigInt(value); - }), - "0x" - ); - const voucherResult = await dapp.createVoucher(voucher); - res.send({ - ok: 1, voucherResult, inputIndex: req.get('x-input_index') - }); - } catch (e: any) { - res.status(400).send({ message: e.message }); - } - }); - - app.post('/deposit', (req: Request, res: Response) => { - axios.post('http://deroll/voucher'); - }); - - app.get("/health", (req: Request, res: Response) => { - res.send({ some: "response" }); - }); - - app.post('/echo', (req: Request, res: Response) => { - res.send({ myPost: req.body }); - }); - - app.post('/echo', (req: Request, res: Response) => { - res.send({ myPost: req.body }); - }); - - app.post('/echo/headers', (req: Request, res: Response) => { - res.send({ headers: req.headers }); - }); - - app.get('/echo/headers', (req: Request, res: Response) => { - res.send({ headers: req.headers }); - }); - -app.listen(port, () => { -console.log(`[server]: Server is running at http://localhost:${port}`); -}); - - diff --git a/apps/cartesify/backend/ts-template/src/index.ts b/apps/cartesify/backend/ts-template/src/index.ts index 90c270c..681b7b9 100644 --- a/apps/cartesify/backend/ts-template/src/index.ts +++ b/apps/cartesify/backend/ts-template/src/index.ts @@ -1,30 +1,314 @@ -import { Request, Response } from 'express' -// Cartesify REST bridge -const { CartesifyBackend } = require("@calindra/cartesify-backend") +import { Request, Response } from 'express'; +import axios from "axios" +import { createWallet, WalletApp } from "@deroll/wallet"; +import CartesifyBackend from '@calindra/cartesify-backend'; +console.log('starting app.js...'); -CartesifyBackend.createDapp().then((dapp:any) => { - dapp.start().catch((e: any) => { +const express = require("express"); +const app = express(); +const port = 8383; +app.use(express.json()); + +interface Greeting { + id: number; + message: string; + sender: string +} + +let greetings: Greeting[] = []; +let nextId = 1; + + +let dapp: any; +let wallet: WalletApp + +CartesifyBackend.createDapp().then(initDapp => { + console.log('Dapp started'); + initDapp.start().catch((e: Error) => { console.error(e); process.exit(1); }); -}); - -// Normal nodejs application using express -const express = require("express"); -const app = express(); -const port = 8383; + dapp = initDapp; + wallet = createWallet(); + + dapp.addAdvanceHandler((): string => { + console.log('before wallet handler'); + return "reject"; + }); -app.use(express.json()); + dapp.addAdvanceHandler(wallet.handler); -app.post("/your-endpoint", (req:Request, res:Response) => { - console.log("Request received on your endpoint") - const senderAddress = req.header("x-msg_sender"); - res.send({ some: "response", senderAddress }); + dapp.addAdvanceHandler((): string => { + console.log('final handler'); + return "reject"; + }); }); + + app.post('/greetings', (req: Request, res: Response) => { + const { message, sender } = req.body; + const newGreeting: Greeting = { id: nextId++, message, sender }; + greetings.push(newGreeting); + if (message) { + res.status(200).json(newGreeting); + } else { + res.status(400).json({ error: 'Please provide a greeting' }); + } + }); + + // GET request to the /greeting/:id endpoint + app.get('/greetings/:id', (req: Request, res: Response) => { + try { + const id = parseInt(req.params.id!, 10); + const greet = greetings.find(greet => greet.id === id); + if (greet) { + res.status(200).json({ message: greet }); + } else { + res.status(404).json({ error: 'Greeting not found' }); + } + } catch (e) { + res.status(400).json({ error: 'No greeting provided' }); + } + }); + + // GET request to the /greetings endpoint + app.get('/greetings', (req: Request, res: Response) => { + try { + if (greetings) { + res.status(200).json({ greetings }); + } + } catch (e) { + res.status(400).json({ error: 'No greeting provided' }); + } + }); + + app.put('/greetings/:id', (req: Request, res: Response) => { + const id = parseInt(req.params.id!, 10); + const index = greetings.findIndex(greet => greet.id === id); + + if (index === -1) { + return res.status(404).json({ error: 'Item not found' }); + } + + const updatedGreeting = { id: id, message: req.body.message } as Greeting; // Assuming request body contains the updated item data + greetings[index] = updatedGreeting; + + res.json(updatedGreeting); + }); + + app.delete('/greetings/:id', (req: Request, res: Response) => { + const id = parseInt(req.params.id!, 10); + const index = greetings.findIndex(greet => greet.id === id); + if (index === -1) { + return res.status(404).json({ error: 'Item not found' }); + } + greetings = greetings.filter(greet => greet.id !== id); + res.json({ message: 'Item deleted successfully' }); + }); + + app.delete('/greetings', (req: Request, res: Response) => { + const deletedCount = greetings.length; + greetings.length = 0; + res.status(200).send({ message: `${deletedCount} greetings deleted` }); + }); + + + app.get("/wallet/:address", async (req: Request, res: Response) => { + try{ + wallet = createWallet() + console.log(`Checking balance ${req.params.address}`); + const userWallet = wallet.getWallet(req.params.address!); + console.log("UserWallet", userWallet) + const json = JSON.stringify(userWallet, (_key, value) => { + if (typeof value === 'bigint') { + return value.toString(); + } else if (typeof value === 'object' && value instanceof Map) { + return Object.fromEntries(value); + } else if (typeof value === 'object' && value instanceof Set) { + return [...value]; + } else { + return value; + } + }); + res.setHeader('Content-Type', 'application/json; charset=utf-8'); + res.send(json); + }catch(e: any){ + res.status(400).send({ message: e.message }); + } + + }); + + app.post("/wallet/ether/transfer", async (req: Request, res: Response) => { + try { + wallet.transferEther( + req.get('x-msg_sender') as string, + req.body.to, + BigInt(req.body.amount), + ); + res.send({ ok: 1 }); + } catch (e: any) { + res.status(400).send({ message: e.message }); + } + }); + + app.post("/wallet/ether/withdraw", async (req: Request, res: Response) => { + try { + if(!wallet || !wallet.withdrawEther) throw new Error("Wallet not accessible!") + const voucher = wallet.withdrawEther( + req.get('x-msg_sender') as `0x${string}`, + BigInt(req.body.amount) + ); + const voucherResult = await dapp.createVoucher(voucher); + res.send({ + ok: 1, voucherResult, inputIndex: req.get('x-input_index') + }); + } catch (e: any) { + res.status(400).send({ message: e.message }); + } + }); + + app.post("/wallet/erc-20/withdraw", async (req: Request, res: Response) => { + try { + if(!wallet || !wallet.withdrawERC20) throw new Error("Wallet not accessible!") + const voucher = wallet.withdrawERC20( + req.body.token, + req.get('x-msg_sender') as `0x${string}`, + BigInt(req.body.amount) + ); + const voucherResult = await dapp.createVoucher(voucher); + res.send({ + ok: 1, voucherResult, inputIndex: req.get('x-input_index') + }); + } catch (e: any) { + res.status(400).send({ message: e.message }); + } + }); + + app.post("/wallet/erc-20/transfer", async (req: Request, res: Response) => { + try { + wallet.transferERC20( + req.body.token, + req.get('x-msg_sender') as string, + req.body.to, + BigInt(req.body.amount) + ); + res.send({ ok: 1 }); + } catch (e: any) { + res.status(400).send({ message: e.message }); + } + }); + + app.post("/wallet/erc-721/transfer", async (req: Request, res: Response) => { + try { + wallet.transferERC721( + req.body.token, + req.get('x-msg_sender') as string, + req.body.to, + BigInt(req.body.tokenId) + ); + res.send({ ok: 1 }); + } catch (e: any) { + res.status(400).send({ message: e.message }); + } + }); + + app.post("/wallet/erc-1155/transfer", async (req: Request, res: Response) => { + try { + wallet.transferERC1155( + req.body.token, + req.get('x-msg_sender') as string, + req.body.to, + req.body.tokenIds.map((id: number) => { + if (typeof id !== 'number') { + throw new Error(`BadRequest id ${id} is not a number`); + } + return BigInt(id); + }), + req.body.values.map((value: number) => { + if (typeof value !== 'number') { + throw new Error(`BadRequest value ${value} is not a number`); + } + return BigInt(value); + }) + ); + res.send({ ok: 1 }); + } catch (e: any) { + res.status(400).send({ message: e.message }); + } + }); + + app.post("/wallet/erc-721/withdraw", async (req: Request, res: Response) => { + try { + const voucher = wallet.withdrawERC721( + req.body.token, + req.get('x-msg_sender') as `0x${string}`, + BigInt(req.body.tokenId) + ); + const voucherResult = await dapp.createVoucher(voucher); + res.send({ + ok: 1, voucherResult, inputIndex: req.get('x-input_index') + }); + } catch (e: any) { + res.status(400).send({ message: e.message }); + } + }); + + app.post("/wallet/erc-1155/withdraw", async (req: Request, res: Response) => { + try { + const voucher = wallet.withdrawERC1155( + req.body.token, + req.get('x-msg_sender') as `0x${string}`, + req.body.tokenIds.map((id: number) => { + if (typeof id !== 'number') { + throw new Error('BadRequest'); + } + return BigInt(id); + }), + req.body.values.map((value: number) => { + if (typeof value !== 'number') { + throw new Error('BadRequest'); + } + return BigInt(value); + }), + "0x" + ); + const voucherResult = await dapp.createVoucher(voucher); + res.send({ + ok: 1, voucherResult, inputIndex: req.get('x-input_index') + }); + } catch (e: any) { + res.status(400).send({ message: e.message }); + } + }); + + app.post('/deposit', (req: Request, res: Response) => { + axios.post('http://deroll/voucher'); + }); + + app.get("/health", (req: Request, res: Response) => { + res.send({ some: "response" }); + }); + + app.post('/echo', (req: Request, res: Response) => { + res.send({ myPost: req.body }); + }); + + app.post('/echo', (req: Request, res: Response) => { + res.send({ myPost: req.body }); + }); + + app.post('/echo/headers', (req: Request, res: Response) => { + res.send({ headers: req.headers }); + }); + + app.get('/echo/headers', (req: Request, res: Response) => { + res.send({ headers: req.headers }); + }); app.listen(port, () => { - console.log(`[server]: Server is running at http://localhost:${port}`); -}); \ No newline at end of file +console.log(`[server]: Server is running at http://localhost:${port}`); +}); + + diff --git a/apps/cartesify/frontend/next-app/app/components/examples/Transfers.tsx b/apps/cartesify/frontend/next-app/app/components/examples/Transfers.tsx index d88e4e1..7441969 100644 --- a/apps/cartesify/frontend/next-app/app/components/examples/Transfers.tsx +++ b/apps/cartesify/frontend/next-app/app/components/examples/Transfers.tsx @@ -291,7 +291,7 @@ const clear1155Batch = () => { onClick={ async () => { if(!erc20Token || !erc20Amount) return errorAlert("Field required!") setLoadERC20(true) - await depositERC20(DAPP_ADDRESS, erc20Token, erc20Amount, signer!, chain!) + await depositERC20(DAPP_ADDRESS, erc20Token, parseEther(erc20Amount).toString(), signer!, chain!) setLoadERC20(false) } } diff --git a/apps/cartesify/frontend/react-app/src/components/examples/Transfers.tsx b/apps/cartesify/frontend/react-app/src/components/examples/Transfers.tsx index effb7e1..ad81fdb 100644 --- a/apps/cartesify/frontend/react-app/src/components/examples/Transfers.tsx +++ b/apps/cartesify/frontend/react-app/src/components/examples/Transfers.tsx @@ -290,7 +290,7 @@ const clear1155Batch = () => { onClick={ async () => { if(!erc20Token || !erc20Amount) return errorAlert("Field required!") setLoadERC20(true) - await depositERC20(DAPP_ADDRESS, erc20Token, erc20Amount, signer!, chain!) + await depositERC20(DAPP_ADDRESS, erc20Token, parseEther(erc20Amount).toString(), signer!, chain!) setLoadERC20(false) } } diff --git a/apps/frontend/next-app/app/component/examples/Vouchers.tsx b/apps/frontend/next-app/app/component/examples/Vouchers.tsx index 5549c76..4e29092 100644 --- a/apps/frontend/next-app/app/component/examples/Vouchers.tsx +++ b/apps/frontend/next-app/app/component/examples/Vouchers.tsx @@ -39,8 +39,6 @@ export const Vouchers: React.FC = (props) => { } setVoucherToExecute(voucher); console.log(voucherToExecute) - console.log(voucherToExecute.executed) - },[rollups, voucherToExecute]) useEffect(() => { diff --git a/package.json b/package.json index e3bd8ef..4c8f63e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cartdevkit", - "version": "1.0.7", + "version": "1.0.8", "description": "Custom template", "author": "gconnect", "license": "Apache-2.0",