Skip to content

Instantly share code, notes, and snippets.

@BlockmanCodes
Created May 22, 2022 14:31
Show Gist options
  • Save BlockmanCodes/1cbefd0df6944d10140a5f5def01f9b3 to your computer and use it in GitHub Desktop.
Save BlockmanCodes/1cbefd0df6944d10140a5f5def01f9b3 to your computer and use it in GitHub Desktop.
Uniswap V3 AlphaRouter Example
WALLET_ADDRESS=
WALLET_SECRET=
INFURA_TEST_URL=
[
{
"inputs": [],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "owner",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "spender",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "value",
"type": "uint256"
}
],
"name": "Approval",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "from",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "to",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "value",
"type": "uint256"
}
],
"name": "Transfer",
"type": "event"
},
{
"inputs": [
{
"internalType": "address",
"name": "owner",
"type": "address"
},
{
"internalType": "address",
"name": "spender",
"type": "address"
}
],
"name": "allowance",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "spender",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "approve",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "account",
"type": "address"
}
],
"name": "balanceOf",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "decimals",
"outputs": [
{
"internalType": "uint8",
"name": "",
"type": "uint8"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "spender",
"type": "address"
},
{
"internalType": "uint256",
"name": "subtractedValue",
"type": "uint256"
}
],
"name": "decreaseAllowance",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "spender",
"type": "address"
},
{
"internalType": "uint256",
"name": "addedValue",
"type": "uint256"
}
],
"name": "increaseAllowance",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "name",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "symbol",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "totalSupply",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "to",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "transfer",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "from",
"type": "address"
},
{
"internalType": "address",
"name": "to",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "transferFrom",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "nonpayable",
"type": "function"
}
]
{
"name": "uniswap-alpha-router",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@uniswap/sdk": "^3.0.3",
"@uniswap/sdk-core": "^3.0.1",
"@uniswap/smart-order-router": "^2.5.30",
"dotenv": "^16.0.1",
"ethers": "^5.6.6",
"jsbi": "^3.2.5"
}
}
const { AlphaRouter } = require('@uniswap/smart-order-router')
const { Token, CurrencyAmount, TradeType, Percent } = require('@uniswap/sdk-core')
const { ethers, BigNumber } = require('ethers')
const JSBI = require('jsbi') // jsbi@3.2.5
const V3_SWAP_ROUTER_ADDRESS = '0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45'
require('dotenv').config()
const WALLET_ADDRESS = process.env.WALLET_ADDRESS
const WALLET_SECRET = process.env.WALLET_SECRET
const INFURA_TEST_URL = process.env.INFURA_TEST_URL
const web3Provider = new ethers.providers.JsonRpcProvider(INFURA_TEST_URL) // Ropsten
const chainId = 3
const router = new AlphaRouter({ chainId: chainId, provider: web3Provider})
const name0 = 'Wrapped Ether'
const symbol0 = 'WETH'
const decimals0 = 18
const address0 = '0xc778417e063141139fce010982780140aa0cd5ab'
const name1 = 'Uniswap Token'
const symbol1 = 'UNI'
const decimals1 = 18
const address1 = '0x1f9840a85d5af5bf1d1762f925bdaddc4201f984'
const WETH = new Token(chainId, address0, decimals0, symbol0, name0)
const UNI = new Token(chainId, address1, decimals1, symbol1, name1)
const wei = ethers.utils.parseUnits('0.01', 18)
const inputAmount = CurrencyAmount.fromRawAmount(WETH, JSBI.BigInt(wei))
async function main() {
const route = await router.route(
inputAmount,
UNI,
TradeType.EXACT_INPUT,
{
recipient: WALLET_ADDRESS,
slippageTolerance: new Percent(25, 100),
deadline: Math.floor(Date.now()/1000 + 1800)
}
)
console.log(`Quote Exact In: ${route.quote.toFixed(10)}`)
const transaction = {
data: route.methodParameters.calldata,
to: V3_SWAP_ROUTER_ADDRESS,
value: BigNumber.from(route.methodParameters.value),
from: WALLET_ADDRESS,
gasPrice: BigNumber.from(route.gasPriceWei),
gasLimit: ethers.utils.hexlify(1000000)
}
const wallet = new ethers.Wallet(WALLET_SECRET)
const connectedWallet = wallet.connect(web3Provider)
const approvalAmount = ethers.utils.parseUnits('1', 18).toString()
const ERC20ABI = require('./abi.json')
const contract0 = new ethers.Contract(address0, ERC20ABI, web3Provider)
await contract0.connect(connectedWallet).approve(
V3_SWAP_ROUTER_ADDRESS,
approvalAmount
)
const tradeTransaction = await connectedWallet.sendTransaction(transaction)
}
main()
@Shields10
Copy link

Hello Blockman, was trying to follow this tutorial that you made on youtube. I'm testing it on Sepolia Network, changed the addresses to match the ones in Sepolia. But I'm getting an error that the "execution reverted". Here's a transaction hash of one of the transaction:- 0x2efea2b5c729d85c757948c08ccc28205002b9bda4e196864f888bbb24aca263.

What could I be doing wrong?

@bitcazador
Copy link

Even with an exact mirror of the code, mirror of the dependencies and the whole project, the example simply does not work.

The libraries of uniswap and ethers changes constantly, so neither of the examples in the web works for a simple swap remotely via js, It's the curse of the js community.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment