Skip to content
This repository has been archived by the owner on Jan 10, 2023. It is now read-only.

Commit

Permalink
Merge pull request #5 from oxalys-dev/main
Browse files Browse the repository at this point in the history
jest and mock testing added to project for unit testing
  • Loading branch information
amin-nejad authored Oct 5, 2022
2 parents ef77c74 + 0dc3f27 commit 40af6e3
Show file tree
Hide file tree
Showing 12 changed files with 3,407 additions and 436 deletions.
11 changes: 11 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
env:
browser: true
commonjs: true
es2021: true
jest/globals: true
extends: [standard, prettier]
overrides: []
parserOptions:
ecmaVersion: latest
rules: {}
plugins: ["jest"]
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
.env
__pycache__
__pycache__
yarn-error.log
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ pip install -r requirements.txt

First you need to create a `.env` file in the root directory with the following keys:

* `NODE_ENV`: Used by the node server. If this is set to 'development', middleware logging will be done.
* `API_KEY`: XUMM Developer Application API Key. Used by the node server to communicate with XUMM.
* `API_SECRET`: XUMM Developer Application API Secret. Used by the node server to communicate with XUMM.
* `WALLET_ADDRESS`: Used by the python code to know which wallet to transact under.
- `NODE_ENV`: Used by the node server. If this is set to 'development', middleware logging will be done.
- `API_KEY`: XUMM Developer Application API Key. Used by the node server to communicate with XUMM.
- `API_SECRET`: XUMM Developer Application API Secret. Used by the node server to communicate with XUMM.
- `WALLET_ADDRESS`: Used by the python code to know which wallet to transact under.

Then, open two terminals. In the first terminal run:

Expand Down
22 changes: 10 additions & 12 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const express = require("express");
const morgan = require("morgan");
const env = require('dotenv');
const env = require("dotenv");
const cors = require("cors");
const xss = require("xss-clean");
env.config({path: './.env'})
env.config({ path: "./.env" });

// init express
const app = express();
Expand All @@ -23,22 +23,20 @@ const xumm = require("./routes/v1/xumm.js");

// dev logging middleware
if (process.env.NODE_ENV === "development") {
app.use(morgan("dev"));
}
app.use(morgan("dev"));
}

// mount v1 routers
app.use("/api/v1/xrp", xrp);
app.use("/api/v1/xumm", xumm);

// init port for server to run
const PORT = process.env.PORT || 4000;

// process server and listening message
app.listen(
PORT,
console.log(
`Server is running in ${process.env.NODE_ENV} MODE on port ${PORT}`
)
);


PORT,
console.log(
`Server is running in ${process.env.NODE_ENV} MODE on port ${PORT}`
)
);
128 changes: 64 additions & 64 deletions controllers/v1/xrpController.js
Original file line number Diff line number Diff line change
@@ -1,83 +1,83 @@
const express = require("express");
const {XummSdk} = require('xumm-sdk') //xumm sdk input
const xrpl = require('xrpl')
const env = require('dotenv');
env.config({path: './.env'})
const { XummSdk } = require("xumm-sdk"); // xumm sdk input
const { Client, xrpToDrops } = require("xrpl");
const env = require("dotenv");
env.config({ path: "./.env" });

// post /api/v1/xrp/test
exports.test = async(req, res, next) => {
console.log('testing' )
exports.test = async (req, res, next) => {
console.log("testing");
res.status(201).json({
success: true,
data: 'hello world',
data: "hello world",
});
}

};

// @desc create offer on the dex (xrp ledger)
// @route post /api/v1/xrp/createOffer
// @access Private(public for now)
exports.createOffer = async (req, res, next) => {
exports.createOffer = async (req, res, next) => {
// Connect to XRP ledger
const client = new Client("wss://xrplcluster.com");
console.log("Connecting to production XRPL server...");
await client.connect();

// Connect to XRP ledger
const client = new xrpl.Client('wss://xrplcluster.com')
console.log("Connecting to production XRPL server...")
await client.connect()

// Parse request body

const body=req.body
const weWant = {
currency: body.weWant.currency,
issuer: body.address,
value: body.weWant.value
}
const weSpend = {
currency: body.weSpend.currency,
value: xrpl.xrpToDrops(body.weSpend.value)
}

// Create offer transaction
// Parse request body

const offer = {
"TransactionType": "OfferCreate",
"Account": body.address,
"TakerPays": weWant,
"TakerGets": weSpend.value // since it's XRP
}
const prepared = await client.autofill(offer)
console.log("Prepared transaction:", JSON.stringify(prepared, null, 2))
const body = req.body;
const weWant = {
currency: body.weWant.currency,
issuer: body.address,
value: body.weWant.value,
};
const weSpend = {
currency: body.weSpend.currency,
value: xrpToDrops(body.weSpend.value),
};

// Create offer transaction

const offer = {
TransactionType: "OfferCreate",
Account: body.address,
TakerPays: weWant,
TakerGets: weSpend.value, // since it's XRP
};
const prepared = await client.autofill(offer);
console.log("Prepared transaction:", JSON.stringify(prepared, null, 2));

// Send the offerCreate transaction to be signed and sent to the ledged
// Send the offerCreate transaction to be signed and sent to the ledged

const sdk = new XummSdk(process.env.API_KEY, process.env.API_SECRET)
const request = {
"txjson": prepared,
"user_token": body.userToken
const sdk = new XummSdk(process.env.API_KEY, process.env.API_SECRET);
const request = {
txjson: prepared,
user_token: body.userToken,
};
console.log("Sending OfferCreate transaction...");

const subscription = await sdk.payload.createAndSubscribe(
request,
(event) => {
if (Object.keys(event.data).indexOf("signed") > -1) {
return event.data;
}
}
console.log("Sending OfferCreate transaction...")
);

const subscription = await sdk.payload.createAndSubscribe(request, event => {
if(Object.keys(event.data).indexOf('signed') > -1){
return event.data
}
})
// Get response regarding the transaction

// Get response regarding the transaction
const resolveData = await subscription.resolved;
const result = await sdk.payload.get(resolveData.payload_uuidv4);

const resolveData = await subscription.resolved
const result = await sdk.payload.get(resolveData.payload_uuidv4)
if (result.response.dispatched_result === "tesSUCCESS") {
console.log("Transaction succeeded");
} else {
throw Error(`Error sending transaction: ${result}`);
}

if (result.response.dispatched_result == "tesSUCCESS") {
console.log("Transaction succeeded")
} else {
throw Error(`Error sending transaction: ${result}`)
}

// Return success code and disconnect from XRPL server
// Return success code and disconnect from XRPL server

res.status(201).json({
success: true,
});
client.disconnect()
}
res.status(201).json({
success: true,
});
client.disconnect();
};
55 changes: 29 additions & 26 deletions controllers/v1/xummController.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,50 @@
const express = require("express");
const open = require('open');
const open = require("open");
const { XummSdk } = require("xumm-sdk");

const { XummSdk } = require('xumm-sdk')
// accessing xumm account via xumm sdk
const sdk = new XummSdk(process.env.API_KEY, process.env.API_SECRET)
const sdk = new XummSdk(process.env.API_KEY, process.env.API_SECRET);

// get /api/v1/xumm/getUserToken
exports.getUserToken = async (req, res) => {
const request = {
"options": {
"submit": false,
"expire": 240,
options: {
submit: false,
expire: 240,
},
"txjson": {
"TransactionType": "SignIn" // Dummy transaction type to trigger login
}
}

const subscription = await sdk.payload.createAndSubscribe(request, event => {
if (Object.keys(event.data).indexOf('signed') > -1) {
return event.data
txjson: {
TransactionType: "SignIn", // Dummy transaction type to trigger login
},
};

const subscription = await sdk.payload.createAndSubscribe(
request,
(event) => {
if (Object.keys(event.data).indexOf("signed") > -1) {
return event.data;
}
}
})
);
console.log(
"If the webpage does not open automatically, open this URL and scan the QR code: ",
subscription.created.next.always
);
await open(subscription.created.next.always);

console.log('Open this URL and scan the QR code: ', subscription.created.next.always)
await open(subscription.created.next.always)
const resolveData = await subscription.resolved;

const resolveData = await subscription.resolved
if (!resolveData.signed) {
console.log('The request was rejected.')
console.log("The request was rejected.");
res.status(400).json({
success: false,
});

} else {
console.log('The request was signed.')
const result = await sdk.payload.get(resolveData.payload_uuidv4)
console.log('User_token: ', result.application.issued_user_token)
console.log("The request was signed.");
const result = await sdk.payload.get(resolveData.payload_uuidv4);
console.log("User_token: ", result.application.issued_user_token);

res.status(201).json({
success: true,
userToken: result.application.issued_user_token,
});
}
}

};
20 changes: 16 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,40 @@
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "nodemon app"
"dev": "nodemon app",
"test": "jest"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"open": "^8.0.0",
"axios": "^0.27.2",
"bignumber.js": "^9.1.0",
"cors": "^2.8.5",
"express": "^4.18.1",
"jsonwebtoken": "^8.5.1",
"mockjs": "^1.1.0",
"mongoose": "^6.3.4",
"morgan": "^1.10.0",
"open": "^8.0.0",
"qs": "^6.11.0",
"ripple-lib": "^1.10.1",
"xrpl": "^2.3.1",
"xss-clean": "^0.1.1",
"xumm-sdk": "^1.3.4"
},
"devDependencies": {
"nodemon": "^2.0.19"
"eslint": "^8.0.1",
"eslint-config-prettier": "^8.5.0",
"eslint-config-standard": "^17.0.0",
"eslint-plugin-import": "^2.25.2",
"eslint-plugin-jest": "^27.0.4",
"eslint-plugin-n": "^15.0.0",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-promise": "^6.0.0",
"jest": "^29.0.3",
"nodemon": "^2.0.19",
"prettier": "^2.7.1",
"supertest": "^6.2.4"
}
}
22 changes: 11 additions & 11 deletions routes/v1/xrp.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
const bodyParser = require('body-parser');
const express = require("express");
const {
createOffer,
test
} = require("../../controllers/v1/xrpController");
const router = express.Router();
const { createOffer, test } = require("../../controllers/v1/xrpController");
const app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));

// initalize xrp routes
// api/v1/xrp/createOffer
router.route("/createOffer").post(createOffer);
// api/v1/xrp/createOffer
app.route("/createOffer").post(createOffer);

// for testing
// api/v1/xrp/test
router.route("/test").post(test);

// api/v1/xrp/test
app.route("/test").get(test);

module.exports = router;
module.exports = app;
12 changes: 5 additions & 7 deletions routes/v1/xumm.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
const express = require("express");
const {
getUserToken
} = require("../../controllers/v1/xummController");
const router = express.Router();
const { getUserToken } = require("../../controllers/v1/xummController");
const app = express();

// initalize xumm routes
// localhost:4000/api/v1/xumm/getUserToken
router.route("/getUserToken").get(getUserToken);
// localhost:4000/api/v1/xumm/getUserToken
app.route("/getUserToken").get(getUserToken);

module.exports = router;
module.exports = app;
Loading

0 comments on commit 40af6e3

Please sign in to comment.