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

Commit

Permalink
token balance_of endpoint implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
Hammad-Mubeen committed Jan 17, 2022
1 parent 379f38b commit 56e41e4
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 21 deletions.
23 changes: 14 additions & 9 deletions JsClients/ERC20/src/erc20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down
39 changes: 27 additions & 12 deletions JsClients/ERC20/test/installed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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;

};
2 changes: 2 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -76,6 +77,7 @@ app.use("/", deploypairRouter);
app.use("/", pairsListRouter);
app.use("/", swapRouter);
app.use("/", liquidityRouter);
app.use("/", erc20Router);
app.use("/", coinsmarketcapapiRouter);

app.use(
Expand Down
41 changes: 41 additions & 0 deletions routes/erc20routes.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 56e41e4

Please sign in to comment.