From 56e41e4009ecf7205552d921e2e4ecd40876fe19 Mon Sep 17 00:00:00 2001 From: Hammad-Mubeen Date: Mon, 17 Jan 2022 15:23:33 +0500 Subject: [PATCH] token balance_of endpoint implemented --- JsClients/ERC20/src/erc20.ts | 23 ++++++++++------- JsClients/ERC20/test/installed.ts | 39 ++++++++++++++++++++--------- app.js | 2 ++ routes/erc20routes.js | 41 +++++++++++++++++++++++++++++++ 4 files changed, 84 insertions(+), 21 deletions(-) create mode 100644 routes/erc20routes.js diff --git a/JsClients/ERC20/src/erc20.ts b/JsClients/ERC20/src/erc20.ts index d52f3012..05dce323 100644 --- a/JsClients/ERC20/src/erc20.ts +++ b/JsClients/ERC20/src/erc20.ts @@ -151,16 +151,21 @@ class ERC20Client { ); return result.value(); } - public async balanceOf(account: CLPublicKey) { + public async balanceOf(account: string) { + try { + + const result = await utils.contractDictionaryGetter( + this.nodeAddress, + account, + this.namedKeys.balances + ); + const maybeValue = result.value().unwrap(); + return maybeValue.value().toString(); - const accountHash = Buffer.from(account.toAccountHash()).toString("hex"); - const result = await utils.contractDictionaryGetter( - this.nodeAddress, - accountHash, - this.namedKeys.balances - ); - const maybeValue = result.value().unwrap(); - return maybeValue.value().toString(); + } catch (error) { + return "0"; + } + } public async balanceOfcontract(accountHash: string) { diff --git a/JsClients/ERC20/test/installed.ts b/JsClients/ERC20/test/installed.ts index 5a7ce8c7..e2fccca4 100644 --- a/JsClients/ERC20/test/installed.ts +++ b/JsClients/ERC20/test/installed.ts @@ -88,9 +88,8 @@ const test = async () => { // // console.log(`... Total supply: ${totalSupply}`); // // // //balanceof - // // let balance = await erc20.balanceOf(KEYS.publicKey); - // // console.log(`... Balance of account ${KEYS.publicKey.toAccountHashStr()}`); - // // console.log(`... Balance: ${balance}`); + let balance = await erc20.balanceOf("7b217a09296d5ce360847a7d20f623476157c5f022333c4e988a464035cadd80"); + console.log(`... Balance: ${balance}`); // // //nonce // // let nonce = await erc20.nonce(KEYS.publicKey); @@ -133,16 +132,16 @@ const test = async () => { // // console.log(`... Total supply: ${totalSupply}`); //approve - const approveDeployHash = await erc20.approve( - ROUTERKEYS, - PACKAGE_HASH!, - AMOUNT_B_DESIRED!, - APPROVE_PAYMENT_AMOUNT! - ); - console.log("... Approve deploy hash: ", approveDeployHash); + // const approveDeployHash = await erc20.approve( + // ROUTERKEYS, + // PACKAGE_HASH!, + // AMOUNT_B_DESIRED!, + // APPROVE_PAYMENT_AMOUNT! + // ); + // console.log("... Approve deploy hash: ", approveDeployHash); - await getDeploy(NODE_ADDRESS!, approveDeployHash); - console.log("... Token approved successfully"); + // await getDeploy(NODE_ADDRESS!, approveDeployHash); + // console.log("... Token approved successfully"); // // //transfer // // const transferDeployHash = await erc20.transfer( @@ -225,3 +224,19 @@ export const getTotalSupply = async (contractHash:string) => { return totalSupply; }; + +export const balanceOf = async (contractHash:string, key:string) => { + + console.log(`... Contract Hash: ${contractHash}`); + + // We don't need hash- prefix so i'm removing it + await erc20.setContractHash(contractHash); + + //balanceof + let balance = await erc20.balanceOf(key); + + console.log(`... Balance: ${balance}`); + + return balance; + +}; \ No newline at end of file diff --git a/app.js b/app.js index 21937875..66612b10 100644 --- a/app.js +++ b/app.js @@ -15,6 +15,7 @@ var pairsListRouter = require("./routes/pairslist"); var deploypairRouter = require("./routes/deploypair"); var swapRouter = require("./routes/swaproutes"); var liquidityRouter = require("./routes/liquidityroutes"); +var erc20Router = require("./routes/erc20routes"); var coinsmarketcapapiRouter = require("./routes/coinsmarketcapapi"); // view engine setup @@ -76,6 +77,7 @@ app.use("/", deploypairRouter); app.use("/", pairsListRouter); app.use("/", swapRouter); app.use("/", liquidityRouter); +app.use("/", erc20Router); app.use("/", coinsmarketcapapiRouter); app.use( diff --git a/routes/erc20routes.js b/routes/erc20routes.js new file mode 100644 index 00000000..bb8e33fb --- /dev/null +++ b/routes/erc20routes.js @@ -0,0 +1,41 @@ +require("dotenv").config(); +var express = require("express"); +var router = express.Router(); +var erc20 = require("../JsClients/ERC20/test/installed.ts"); + +router + .route("/balanceagainstuser") + .post(async function (req, res, next) { + try { + + if (!req.body.contractHash) { + return res.status(400).json({ + success: false, + message: "contractHash not found in request body", + }); + } + + if (!req.body.user) { + return res.status(400).json({ + success: false, + message: "user not found in request body", + }); + } + + let balance = await erc20.balanceOf(req.body.contractHash,req.body.user); + return res.status(200).json({ + success: true, + message: + "Balance has been found against this user against passed token ", + balance:balance, + }); + } catch (error) { + console.log("error (try-catch) : " + error); + return res.status(500).json({ + success: false, + err: error, + }); + } + }); + +module.exports = router;