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

Commit

Permalink
allowance blake2b work added and allowance endpoint aded
Browse files Browse the repository at this point in the history
  • Loading branch information
Hammad-Mubeen committed Jan 17, 2022
1 parent 56e41e4 commit 901f4ae
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 18 deletions.
37 changes: 25 additions & 12 deletions JsClients/ERC20/src/erc20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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,
Expand Down
31 changes: 25 additions & 6 deletions JsClients/ERC20/test/installed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -239,4 +239,23 @@ export const balanceOf = async (contractHash:string, key:string) => {

return balance;

};
};

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");
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
41 changes: 41 additions & 0 deletions routes/erc20routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 901f4ae

Please sign in to comment.