Skip to content

Commit

Permalink
Fix ERC20
Browse files Browse the repository at this point in the history
  • Loading branch information
ezynda3 committed Dec 31, 2024
1 parent 684cdc1 commit fe7ecdb
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 23 deletions.
2 changes: 1 addition & 1 deletion script/demoScripts/demoAcrossV3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ const createDestCallPayload = (
}

// ########################################## CONFIGURE SCRIPT HERE ##########################################
const TRANSACTION_TYPE = TX_TYPE.ERC20_WITH_SRC as TX_TYPE // define which type of transaction you want to send
const TRANSACTION_TYPE = TX_TYPE.NATIVE_WITH_SRC as TX_TYPE // define which type of transaction you want to send
const SEND_TX = true // allows you to the script run without actually sending a transaction (=false)
const DEBUG = false // set to true for higher verbosity in console output

Expand Down
65 changes: 43 additions & 22 deletions script/demoScripts/utils/demoScriptHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,33 +279,54 @@ export const getUniswapDataERC20toExactERC20 = async (
requiresDeposit = true,
deadline = Math.floor(Date.now() / 1000) + 60 * 60
) => {
const uniswap = new Contract(uniswapAddress, [
'function swapTokensForExactTokens(uint amountOut, uint amountInMax, address[] calldata path, address to, uint deadline) external returns (uint[] memory amounts)',
])
// Get provider for the chain
const provider = getProviderForChainId(chainId)

const uniswap = new Contract(
uniswapAddress,
[
'function getAmountsIn(uint amountOut, address[] calldata path) external view returns (uint[] memory amounts)',
'function swapTokensForExactTokens(uint amountOut, uint amountInMax, address[] calldata path, address to, uint deadline) external returns (uint[] memory amounts)',
],
provider
)

const path = [sendingAssetId, receivingAssetId]
const maxAmountIn = exactAmountOut.mul(105).div(100) // 5% max slippage

const uniswapCalldata = (
await uniswap.populateTransaction.swapTokensForExactTokens(
exactAmountOut,
maxAmountIn,
path,
receiverAddress,
deadline
)
).data
try {
// Get the required input amount for the exact output
const amounts = await uniswap.getAmountsIn(exactAmountOut, path)
const requiredInputAmount = amounts[0]
const maxAmountIn = BigNumber.from(requiredInputAmount).mul(105).div(100) // 5% max slippage

if (!uniswapCalldata) throw Error('Could not create Uniswap calldata')
console.log('Required input amount:', requiredInputAmount.toString())
console.log('Max input with slippage:', maxAmountIn.toString())
console.log('Exact output amount:', exactAmountOut.toString())

return {
callTo: uniswapAddress,
approveTo: uniswapAddress,
sendingAssetId,
receivingAssetId,
fromAmount: maxAmountIn,
callData: uniswapCalldata,
requiresDeposit,
const uniswapCalldata = (
await uniswap.populateTransaction.swapTokensForExactTokens(
exactAmountOut,
maxAmountIn,
path,
receiverAddress,
deadline
)
).data

if (!uniswapCalldata) throw Error('Could not create Uniswap calldata')

return {
callTo: uniswapAddress,
approveTo: uniswapAddress,
sendingAssetId,
receivingAssetId,
fromAmount: maxAmountIn,
callData: uniswapCalldata,
requiresDeposit,
}
} catch (error) {
console.error('Error in Uniswap contract interaction:', error)
throw error
}
}

Expand Down

0 comments on commit fe7ecdb

Please sign in to comment.