From 901f4ae6a4dd1cf8054cb82e3a083820552e8a82 Mon Sep 17 00:00:00 2001 From: Hammad-Mubeen Date: Mon, 17 Jan 2022 16:41:51 +0500 Subject: [PATCH] allowance blake2b work added and allowance endpoint aded --- JsClients/ERC20/src/erc20.ts | 37 +++++++++++++++++++--------- JsClients/ERC20/test/installed.ts | 31 ++++++++++++++++++----- package.json | 1 + routes/erc20routes.js | 41 +++++++++++++++++++++++++++++++ 4 files changed, 92 insertions(+), 18 deletions(-) diff --git a/JsClients/ERC20/src/erc20.ts b/JsClients/ERC20/src/erc20.ts index 05dce323..40975c17 100644 --- a/JsClients/ERC20/src/erc20.ts +++ b/JsClients/ERC20/src/erc20.ts @@ -17,9 +17,12 @@ import { RuntimeArgs, } from "casper-js-sdk"; import { Some, None } from "ts-results"; +import * as blake from "blakejs"; +import { concat } from "@ethersproject/bytes"; import { ERC20Events } from "./constants"; import * as utils from "./utils"; import { RecipientType, IPendingDeploy } from "./types"; +import {createRecipientAddress } from "./utils"; class ERC20Client { private contractName: string = "erc20"; @@ -190,19 +193,29 @@ class ERC20Client { return maybeValue.value().toString(); } - public async allowance(owner: CLPublicKey, spender: CLPublicKey) { - const ownerAccountHash = Buffer.from(owner.toAccountHash()).toString("hex"); - const spenderAccountHash = Buffer.from(spender.toAccountHash()).toString("hex"); - const accountHash: string = `${ownerAccountHash}_${spenderAccountHash}`; - const result = await utils.contractDictionaryGetter( - this.nodeAddress, - accountHash, - this.namedKeys.allowances - ); - const maybeValue = result.value().unwrap(); - return maybeValue.value().toString(); - } + public async allowance(owner: string, spender:string) { + try { + + const keyOwner = createRecipientAddress(CLPublicKey.fromHex(owner)); + const keySpender = createRecipientAddress(CLPublicKey.fromHex(spender)); + const finalBytes = concat([CLValueParsers.toBytes(keyOwner).unwrap(), CLValueParsers.toBytes(keySpender).unwrap()]); + const blaked = blake.blake2b(finalBytes, undefined, 32); + const encodedBytes = Buffer.from(blaked).toString("hex"); + + const result = await utils.contractDictionaryGetter( + this.nodeAddress, + encodedBytes, + this.namedKeys.allowances + ); + + const maybeValue = result.value().unwrap(); + return maybeValue.value().toString(); + } catch (error) { + return "0"; + } + } + public async totalSupply() { const result = await contractSimpleGetter( this.nodeAddress, diff --git a/JsClients/ERC20/test/installed.ts b/JsClients/ERC20/test/installed.ts index e2fccca4..a35adb07 100644 --- a/JsClients/ERC20/test/installed.ts +++ b/JsClients/ERC20/test/installed.ts @@ -88,17 +88,17 @@ const test = async () => { // // console.log(`... Total supply: ${totalSupply}`); // // // //balanceof - let balance = await erc20.balanceOf("7b217a09296d5ce360847a7d20f623476157c5f022333c4e988a464035cadd80"); - console.log(`... Balance: ${balance}`); + // let balance = await erc20.balanceOf("7b217a09296d5ce360847a7d20f623476157c5f022333c4e988a464035cadd80"); + // console.log(`... Balance: ${balance}`); // // //nonce // // let nonce = await erc20.nonce(KEYS.publicKey); // // console.log(`... Nonce: ${nonce}`); // // // //allowance - // // // let allowance = await erc20.allowance(KEYS.publicKey,KEYS.publicKey); - // // // console.log(`... Allowance: ${allowance}`); - + //let allowance = await erc20.allowance(KEYS.publicKey,KEYS.publicKey); + // console.log(`... Allowance: ${allowance}`); + //mint // const mintDeployHash = await erc20.mint( // ROUTERKEYS, @@ -239,4 +239,23 @@ export const balanceOf = async (contractHash:string, key:string) => { return balance; -}; \ No newline at end of file +}; + +export const allowance = async (contractHash:string, ownerKey:string, spenderkey:string) => { + + console.log(`... Contract Hash: ${contractHash}`); + + // We don't need hash- prefix so i'm removing it + await erc20.setContractHash(contractHash); + + //balanceof + let allowance = await erc20.allowance(ownerKey,spenderkey); + + console.log(`... Allowance: ${allowance}`); + + return allowance; + +}; +//console.log(KEYS.publicKey); +//console.log(CLPublicKey.fromHex("015a5b4ae1e1ff10fd610c7d6323d3c331438a81eef7bcd3aa4783f6f264fa3aa4")); +//allowance("6ab4a5bf100fb9f444a12e92c663e3cf65a8c3ef4523cb2f80bed4fd41f85706","8b217a09296d5ce360847a7d20f623476157c5f022333c4e988a464035cadd80","8a74e1ae230936013f3b544182b8011435f4a457d9444fa879ab483fdf829dc8"); \ No newline at end of file diff --git a/package.json b/package.json index 06e6c8a6..761edfcb 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ }, "dependencies": { "axios": "^0.24.0", + "blakejs": "^1.1.1", "casper-js-sdk": "^2.7.2", "cookie-parser": "~1.4.4", "debug": "~2.6.9", diff --git a/routes/erc20routes.js b/routes/erc20routes.js index bb8e33fb..5f95ede5 100644 --- a/routes/erc20routes.js +++ b/routes/erc20routes.js @@ -38,4 +38,45 @@ router } }); +router + .route("/allowanceagainstownerandspender") + .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.owner) { + return res.status(400).json({ + success: false, + message: "owner not found in request body", + }); + } + + if (!req.body.spender) { + return res.status(400).json({ + success: false, + message: "spender not found in request body", + }); + } + + let allowance = await erc20.allowance(req.body.contractHash,req.body.onwer,req.body.spender); + return res.status(200).json({ + success: true, + message: + "Allowance has been found against this owner and spender ", + allowance:allowance, + }); + } catch (error) { + console.log("error (try-catch) : " + error); + return res.status(500).json({ + success: false, + err: error, + }); + } + }); module.exports = router;