diff --git a/sz-poc-offsite-2025/uniswap/nomiswap/src/ethereumFunctions.js b/sz-poc-offsite-2025/uniswap/nomiswap/src/ethereumFunctions.js index 2c664e3..252648c 100644 --- a/sz-poc-offsite-2025/uniswap/nomiswap/src/ethereumFunctions.js +++ b/sz-poc-offsite-2025/uniswap/nomiswap/src/ethereumFunctions.js @@ -26,7 +26,7 @@ export function getRouter(address, signer) { export async function checkNetwork(provider) { const chainId = getNetwork(provider); - if (chains.networks.includes(chainId)){ + if (chains.networks.includes(chainId)) { return true } return false; @@ -61,12 +61,12 @@ export function doesTokenExist(address, signer) { export async function getDecimals(token) { const decimals = await token.decimals().then((result) => { - return result; - }).catch((error) => { - console.log('No tokenDecimals function for this token, set to 0'); - return 0; - }); - return decimals; + return result; + }).catch((error) => { + console.log('No tokenDecimals function for this token, set to 0'); + return 0; + }); + return decimals; } // This function returns an object with 2 fields: `balance` which container's the account's balance in the particular token, @@ -98,13 +98,13 @@ export async function getBalanceAndSymbol( const symbol = await token.symbol(); return { - balance: balanceRaw*10**(-tokenDecimals), + balance: balanceRaw * 10 ** (-tokenDecimals), symbol: symbol, }; } } catch (error) { - console.log ('The getBalanceAndSymbol function had an error!'); - console.log (error) + console.log('The getBalanceAndSymbol function had an error!'); + console.log(error) return false; } } @@ -132,42 +132,47 @@ export async function swapTokens( const token1 = new Contract(address1, ERC20.abi, signer); const tokenDecimals = await getDecimals(token1); - + const amountIn = ethers.utils.parseUnits(amount, tokenDecimals); const amountOut = await routerContract.callStatic.getAmountsOut( amountIn, tokens ); - await token1.approve(routerContract.address, amountIn); + const tx_approve = await token1.approve(routerContract.address, amountIn); + await tx_approve.wait(); + const wethAddress = await routerContract.WETH(); if (address1 === wethAddress) { // Eth -> Token - await routerContract.swapExactETHForTokens( + const tx_swapExactETHForTokens = await routerContract.swapExactETHForTokens( amountOut[1], tokens, accountAddress, deadline, { value: amountIn } ); + await tx_swapExactETHForTokens.wait(); } else if (address2 === wethAddress) { // Token -> Eth - await routerContract.swapExactTokensForETH( + const tx_swapExactTokensForETH = await routerContract.swapExactTokensForETH( amountIn, amountOut[1], tokens, accountAddress, deadline ); + await tx_swapExactTokensForETH.wait(); } else { - await routerContract.swapExactTokensForTokens( + const tx_swapExactTokensForTokens = await routerContract.swapExactTokensForTokens( amountIn, amountOut[1], tokens, accountAddress, deadline ); + await tx_swapExactTokensForTokens.wait(); } } @@ -194,7 +199,7 @@ export async function getAmountOut( ethers.utils.parseUnits(String(amountIn), token1Decimals), [address1, address2] ); - const amount_out = values_out[1]*10**(-token2Decimals); + const amount_out = values_out[1] * 10 ** (-token2Decimals); console.log('amount out: ', amount_out) return Number(amount_out); } catch { @@ -222,15 +227,15 @@ export async function fetchReserves(address1, address2, pair, signer) { const reservesRaw = await pair.getReserves(); // Put the results in the right order - const results = [ + const results = [ (await pair.token0()) === address1 ? reservesRaw[0] : reservesRaw[1], (await pair.token1()) === address2 ? reservesRaw[1] : reservesRaw[0], ]; // Scale each to the right decimal place return [ - (results[0]*10**(-coin1Decimals)), - (results[1]*10**(-coin2Decimals)) + (results[0] * 10 ** (-coin1Decimals)), + (results[1] * 10 ** (-coin2Decimals)) ] } catch (err) { console.log("error!"); @@ -255,15 +260,15 @@ export async function getReserves( try { const pairAddress = await factory.getPair(address1, address2); const pair = new Contract(pairAddress, PAIR.abi, signer); - - if (pairAddress !== '0x0000000000000000000000000000000000000000'){ - + + if (pairAddress !== '0x0000000000000000000000000000000000000000') { + const reservesRaw = await fetchReserves(address1, address2, pair, signer); const liquidityTokens_BN = await pair.balanceOf(accountAddress); const liquidityTokens = Number( ethers.utils.formatEther(liquidityTokens_BN) ); - + return [ reservesRaw[0].toPrecision(6), reservesRaw[1].toPrecision(6), @@ -271,9 +276,9 @@ export async function getReserves( ]; } else { console.log("no reserves yet"); - return [0,0,0]; + return [0, 0, 0]; } - }catch (err) { + } catch (err) { console.log("error!"); console.log(err); return [0, 0, 0];