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

Commit

Permalink
allowance work corrected
Browse files Browse the repository at this point in the history
  • Loading branch information
Hammad-Mubeen committed Jan 18, 2022
1 parent 901f4ae commit 7d9348d
Show file tree
Hide file tree
Showing 5 changed files with 277 additions and 301 deletions.
13 changes: 8 additions & 5 deletions JsClients/ERC20/src/erc20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,14 @@ class ERC20Client {
return maybeValue.value().toString();
}

public async allowance(owner: string, spender:string) {
public async allowance(owner:string, spender:string) {
try {

const keyOwner = createRecipientAddress(CLPublicKey.fromHex(owner));
const keySpender = createRecipientAddress(CLPublicKey.fromHex(spender));
const _spender = new CLByteArray(
Uint8Array.from(Buffer.from(spender, "hex"))
);

const keyOwner=new CLKey(new CLAccountHash(Uint8Array.from(Buffer.from(owner, "hex"))));
const keySpender = createRecipientAddress(_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");
Expand All @@ -211,7 +214,7 @@ class ERC20Client {
const maybeValue = result.value().unwrap();
return maybeValue.value().toString();
} catch (error) {
return "0";
return error;
}

}
Expand Down
11 changes: 5 additions & 6 deletions JsClients/ERC20/test/installed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
CLAccountHash,
CLPublicKeyType,
Contracts,
CLByteArray
} from "casper-js-sdk";

const { ERC20Events } = constants;
Expand Down Expand Up @@ -241,21 +242,19 @@ export const balanceOf = async (contractHash:string, key:string) => {

};

export const allowance = async (contractHash:string, ownerKey:string, spenderkey:string) => {
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);
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");

allowance("b761da7d5ef67f8825c30c40df8b72feca4724eb666dba556b0e3f67778143e0","8b217a09296d5ce360847a7d20f623476157c5f022333c4e988a464035cadd80","8a74e1ae230936013f3b544182b8011435f4a457d9444fa879ab483fdf829dc8");
Loading

0 comments on commit 7d9348d

Please sign in to comment.