Skip to content

Commit

Permalink
calculate network
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben-Rey committed Jul 12, 2024
1 parent a84a44d commit 0bb10db
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions src/lib/massa-react/hooks/useWriteSmartContract.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,24 @@ export function useWriteSmartContract(
): Promise<bigint> {
const minimalFee = await publicClient.getMinimalFee();

if (!fee) {
fee = minimalFee;
} else if (fee < minimalFee) {
throw new Error('Fee is too low');
}

if (!fee || fee < minimalFee) return minimalFee;
return fee;
}

async function clientFromNetwork() {
const network = await provider.getNetwork();
switch (network) {
case 'buildnet':
return JsonRPCClient.buildnet();
case 'mainnet':
return JsonRPCClient.mainnet();
case 'testnet':
return JsonRPCClient.testnet();
default:
throw new Error('Unsupported network');
}
}

async function callSmartContract(
targetFunction: string,
targetAddress: string,
Expand All @@ -100,11 +109,13 @@ export function useWriteSmartContract(
}

resetState();
setIsOpPending(true);

let loadingToastId: string | undefined;

try {
const publicClient = new JsonRPCClient(await provider.getNetwork());
const publicClient = await clientFromNetwork();

fee = await getMinimalFee(publicClient, fee);

let maxGas = await gasEstimation(
Expand All @@ -125,7 +136,6 @@ export function useWriteSmartContract(
)) as ITransactionDetails;

setOpId(operationId);
setIsOpPending(true);

loadingToastId = showToast(
'loading',
Expand Down

0 comments on commit 0bb10db

Please sign in to comment.