Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Audit fixes #19

Merged
merged 3 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading