Skip to content

Commit

Permalink
chore: add method to check allowance
Browse files Browse the repository at this point in the history
  • Loading branch information
njokuScript committed Jan 11, 2024
1 parent d512ed9 commit 9ef96ef
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@descent-protocol/sdk",
"version": "0.1.20",
"version": "0.1.21",
"description": "A Typescript library for interacting with the Descent Protocol",
"keywords": [
"xNGN",
Expand All @@ -18,7 +18,7 @@
"main": "dist/index.js",
"module": "dist/index.es.js",
"browser": "dist/index.bundle.js",
"types": "./dist/types/index.d.ts",
"types": "./dist/types.d.ts",
"exports": {
".": {
"import": "./dist/esm/index.mjs",
Expand Down
17 changes: 17 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
approveCurrencyToken,
getCollateralTokenBalance,
getCurrencyTokenBalance,
getCollateralTokenAllowance,
} from './services/utility';

export class DescentClass {
Expand Down Expand Up @@ -212,6 +213,22 @@ export class DescentClass {
return result;
}

/**
* @dev approve the vault to take a certain amount of collateral
* @param amount amount of allowance
* @returns tx object
*/
public async collateralTokenAllowance(approver: string) {
const result = await getCollateralTokenAllowance(
this.collateral,
approver,
this.chainId,
this.signer,
);

return result;
}

/**
* @dev gets the xNGN balalnce of an address
* @param owner address of the owner
Expand Down
4 changes: 2 additions & 2 deletions src/services/utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,14 @@ const getCollateralTokenBalance = async (
const getCollateralTokenAllowance = async (
collateral: ICollateral,
owner: string,
spender: string,
chainId: string,
signer: Signer,
) => {
const collateralAddress: any = getContractAddress(collateral)[chainId];
const vaultAddress: any = getContractAddress('Vault')[chainId];

const collateralContract = Contract(collateralAddress, USDC__factory.abi, signer);
const allowance = await collateralContract.allowance(owner, spender);
const allowance = await collateralContract.allowance(owner, vaultAddress);
const formattedAllowance = formatUnits(allowance, 6);
return formattedAllowance;
};
Expand Down
7 changes: 7 additions & 0 deletions test/utility.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,11 @@ describe('Descent Protocol Utility Test', () => {
console.log(response);
expect(response).toBeDefined();
}, 2000000);

it('should check for token allowance on vault', async () => {
const response = await descent.collateralTokenAllowance(owner);
console.log(response);

expect(response).toBeDefined();
});
});

0 comments on commit 9ef96ef

Please sign in to comment.