Skip to content

Commit

Permalink
Audit fixes (#19)
Browse files Browse the repository at this point in the history
Audit fixes
  • Loading branch information
Tanya-atatakai authored Mar 26, 2024
1 parent 91a535c commit 24542c4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export const PreRegistration = ({
account,
chainId,
serviceId,
// any amount, if not ETH token substitute with 1
amountToApprove: 1
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useState, useEffect } from 'react';
import PropTypes from 'prop-types';
import { ethers } from 'ethers';
import { Divider, Typography } from 'antd';
import {
convertToEth,
Expand Down Expand Up @@ -132,7 +133,12 @@ export const ActiveRegistration = ({
// if not eth, check if the user has sufficient token balance
// and if not, approve the token
if (!isEthToken && !isSvm) {
await checkAndApproveToken({ account, chainId, serviceId });
await checkAndApproveToken({
account,
chainId,
serviceId,
amountToApprove: ethers.utils.parseUnits(`${totalBonds}`, 'ether'),
});
}

// check if the agent instances are valid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export const getServiceOwner = async (id) => {
return response;
};

const hasSufficientTokenRequest = async ({ account, chainId, serviceId }) => {
const hasSufficientTokenRequest = async ({ account, chainId, serviceId, amountToApprove }) => {
/**
* - fetch the token address from the serviceId
* - fetch the allowance of the token using the token address
Expand All @@ -78,35 +78,36 @@ const hasSufficientTokenRequest = async ({ account, chainId, serviceId }) => {
const response = await contract.methods
.allowance(account, ADDRESSES[chainId].serviceRegistryTokenUtility)
.call();
return !(ethers.BigNumber.from(response) < ethers.constants.MaxUint256);
return !(ethers.BigNumber.from(response) < amountToApprove);
};

/**
* Approves token
*/
const approveToken = async ({ account, chainId, serviceId }) => {
const approveToken = async ({ account, chainId, serviceId, amountToApprove }) => {
const { token } = await getTokenDetailsRequest(serviceId);
const contract = getGenericErc20Contract(token);
const fn = contract.methods
.approve(
ADDRESSES[chainId].serviceRegistryTokenUtility,
ethers.constants.MaxUint256,
amountToApprove,
)
.send({ from: account });

const response = await sendTransaction(fn, account);
return response;
};

export const checkAndApproveToken = async ({ account, chainId, serviceId }) => {
export const checkAndApproveToken = async ({ account, chainId, serviceId, amountToApprove }) => {
const hasTokenBalance = await hasSufficientTokenRequest({
account,
chainId,
serviceId,
amountToApprove
});

if (!hasTokenBalance) {
const response = await approveToken({ account, chainId, serviceId });
const response = await approveToken({ account, chainId, serviceId, amountToApprove });
return response;
}

Expand Down

0 comments on commit 24542c4

Please sign in to comment.