This repository has been archived by the owner on Jan 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from oxalys-dev/main
jest and mock testing added to project for unit testing
- Loading branch information
Showing
12 changed files
with
3,407 additions
and
436 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
node_modules | ||
.env | ||
__pycache__ | ||
__pycache__ | ||
yarn-error.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
} | ||
} | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.