Last active
September 6, 2022 11:55
-
-
Save HenryHengZJ/a8b994fe277aeeea933b32d8177e49a9 to your computer and use it in GitHub Desktop.
Uniswap SDK implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
const ethers = require('ethers'); | |
const IWETH = require('@uniswap/v2-periphery/build/IWETH.json'); | |
const uniswapsdk = require('simple-uniswap-sdk'); | |
class Uniswap { | |
provider; | |
wallet; | |
constructor(rpcUrl, privateKey) { | |
this.provider = new ethers.providers.JsonRpcProvider(rpcUrl) | |
this.wallet = new ethers.Wallet(privateKey, this.provider); | |
} | |
async wrapNativeToken(toTokenContractAddress, amountToSwap) { | |
const wrapEthContract = new ethers.Contract( | |
toTokenContractAddress, | |
IWETH['abi'], | |
this.wallet | |
); | |
const tx = await wrapEthContract.deposit({value: ethers.utils.parseUnits(amountToSwap, 18)}); | |
const approveReceipt = await tx.wait(); | |
if (approveReceipt.status === 0) throw new Error(`Failed to swap ETH to WETH`); | |
console.log(`Succesfully swap ETH to WETH!`); | |
console.log(`Transaction Hash: ${approveReceipt.hash}`); | |
console.log(JSON.stringify(approveReceipt)); | |
} | |
async unwrapNativeToken(fromTokenContractAddress, amountToSwap) { | |
const wrapEthContract = new ethers.Contract( | |
fromTokenContractAddress, | |
IWETH['abi'], | |
this.wallet | |
); | |
const tx = await wrapEthContract.withdraw(ethers.utils.parseUnits(amountToSwap, 18)); | |
const approveReceipt = await tx.wait(); | |
if (approveReceipt.status === 0) throw new Error(`Failed to swap WETH to ETH`); | |
console.log(`Succesfully swap WETH to ETH!`); | |
console.log(`Transaction Hash: ${approveReceipt.hash}`); | |
console.log(JSON.stringify(approveReceipt)); | |
} | |
async swapToken(fromTokenContractAddress, toTokenContractAddress, chainId, amountToSwap) { | |
const uniswapPair = new uniswapsdk.UniswapPair({ | |
fromTokenContractAddress, | |
toTokenContractAddress, | |
ethereumAddress: this.wallet.address, | |
ethereumProvider: this.provider, | |
chainId, | |
settings: new uniswapsdk.UniswapPairSettings({ | |
slippage: 0.0005, | |
deadlineMinutes: 20, | |
}), | |
}); | |
const uniswapPairFactory = await uniswapPair.createFactory(); | |
const trade = await uniswapPairFactory.trade(amountToSwap); | |
if (!trade.fromBalance.hasEnough) { | |
throw new Error('You do not have enough from balance to execute this swap'); | |
} | |
// Why we need two transactions: https://github.com/joshstevens19/simple-uniswap-sdk#ethers-example | |
if (trade.approvalTransaction) { | |
const approved = await wallet.sendTransaction(trade.approvalTransaction); | |
await approved.wait(); | |
} | |
const tradeTransaction = await wallet.sendTransaction(trade.transaction); | |
await tradeTransaction.wait(); | |
trade.destroy(); | |
console.log(`Succesfully swap ${fromTokenContractAddress} to ${toTokenContractAddress}!`); | |
console.log(`Transaction Hash: ${tradeTransaction.hash}`); | |
console.log(JSON.stringify(tradeTransaction)); | |
} | |
} | |
const privateKey = "<PRIVATE_KEY>"; | |
const rpcUrl = "<RPC_URL>"; | |
const uniswap = new Uniswap(rpcUrl, privateKey); | |
/** WETH address | |
*[Mainnet]: 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2 | |
*[Ropsten Testnet]: 0xc778417E063141139Fce010982780140Aa0cD5Ab | |
*[Rinkeby Testnet]: 0xc778417E063141139Fce010982780140Aa0cD5Ab | |
*[Kovan Testnet]: 0xd0A1E359811322d97991E03f863a0C30C2cF029C | |
*/ | |
// Swapping ETH to WETH | |
uniswap.wrapNativeToken("0xc778417E063141139Fce010982780140Aa0cD5Ab", "0.01"); | |
// Swapping WETH to ETH | |
uniswap.unwrapNativeToken("0xc778417E063141139Fce010982780140Aa0cD5Ab", "0.01"); | |
/** Swapping WETH to DAI on Rinkeby (ERC20-ERC20) | |
* WETH Rinkeby - 0xc778417E063141139Fce010982780140Aa0cD5Ab | |
* DAI Rinkeby - 0xc7AD46e0b8a400Bb3C915120d284AafbA8fc4735 | |
* Rinkby ChainId - 4 | |
*/ | |
uniswap.swapToken( | |
"0xc778417E063141139Fce010982780140Aa0cD5Ab", | |
"0xc7AD46e0b8a400Bb3C915120d284AafbA8fc4735", | |
4, | |
"0.01" | |
); | |
/** Swapping Native ETH to DAI on Rinkeby (ETH-ERC20) | |
* ETH Rinkeby - 0xc778417E063141139Fce010982780140Aa0cD5Ab_ETH | |
* DAI Rinkeby - 0xc7AD46e0b8a400Bb3C915120d284AafbA8fc4735 | |
* Rinkby ChainId - 4 | |
*/ | |
uniswap.swapToken( | |
"0xc778417E063141139Fce010982780140Aa0cD5Ab_ETH", | |
"0xc7AD46e0b8a400Bb3C915120d284AafbA8fc4735", | |
4, | |
"0.01" | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "uniswap-example", | |
"version": "1.0.0", | |
"bin": "./index.js", | |
"scripts": { | |
"start": "node index.js" | |
}, | |
"type": "module", | |
"dependencies": { | |
"@uniswap/v2-periphery": "^1.1.0-beta.0", | |
"ethers": "^5.6.8", | |
"simple-uniswap-sdk": "^3.7.0" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async swapToken(fromTokenContractAddress, toTokenContractAddress, chainId, amountToSwap) { | |
const uniswapPair = new uniswapsdk.UniswapPair({ | |
fromTokenContractAddress, | |
toTokenContractAddress, | |
ethereumAddress: this.wallet.address, | |
ethereumProvider: this.provider, | |
chainId, | |
settings: new uniswapsdk.UniswapPairSettings({ | |
slippage: 0.0005, | |
deadlineMinutes: 20, | |
}), | |
}); | |
const uniswapPairFactory = await uniswapPair.createFactory(); | |
const trade = await uniswapPairFactory.trade(amountToSwap); | |
if (!trade.fromBalance.hasEnough) { | |
throw new Error('You do not have enough from balance to execute this swap'); | |
} | |
// Why we need two transactions: https://github.com/joshstevens19/simple-uniswap-sdk#ethers-example | |
if (trade.approvalTransaction) { | |
const approved = await wallet.sendTransaction(trade.approvalTransaction); | |
await approved.wait(); | |
} | |
const tradeTransaction = await wallet.sendTransaction(trade.transaction); | |
await tradeTransaction.wait(); | |
trade.destroy(); | |
console.log(`Succesfully swap ${fromTokenContractAddress} to ${toTokenContractAddress}!`); | |
console.log(`Transaction Hash: ${tradeTransaction.hash}`); | |
console.log(JSON.stringify(tradeTransaction)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ethers = require('ethers'); | |
const IWETH = require('@uniswap/v2-periphery/build/IWETH.json'); | |
const uniswapsdk = require('simple-uniswap-sdk'); | |
class Uniswap { | |
provider; | |
wallet; | |
constructor(rpcUrl, privateKey) { | |
this.provider = new ethers.providers.JsonRpcProvider(rpcUrl) | |
this.wallet = new ethers.Wallet(privateKey, this.provider); | |
} | |
} | |
const privateKey = "<PRIVATE_KEY>"; | |
const rpcUrl = "<RPC_URL>"; | |
const uniswap = new Uniswap(rpcUrl, privateKey); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async unwrapNativeToken(fromTokenContractAddress, amountToSwap) { | |
const wrapEthContract = new ethers.Contract( | |
fromTokenContractAddress, | |
IWETH['abi'], | |
this.wallet | |
); | |
const tx = await wrapEthContract.withdraw(ethers.utils.parseUnits(amountToSwap, 18)); | |
const approveReceipt = await tx.wait(); | |
if (approveReceipt.status === 0) throw new Error(`Failed to swap WETH to ETH`); | |
console.log(`Succesfully swap WETH to ETH!`); | |
console.log(`Transaction Hash: ${approveReceipt.hash}`); | |
console.log(JSON.stringify(approveReceipt)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async wrapNativeToken(toTokenContractAddress, amountToSwap) { | |
const wrapEthContract = new ethers.Contract( | |
toTokenContractAddress, | |
IWETH['abi'], | |
this.wallet | |
); | |
const tx = await wrapEthContract.deposit({value: ethers.utils.parseUnits(amountToSwap, 18)}); | |
const approveReceipt = await tx.wait(); | |
if (approveReceipt.status === 0) throw new Error(`Failed to swap ETH to WETH`); | |
console.log(`Succesfully swap ETH to WETH!`); | |
console.log(`Transaction Hash: ${approveReceipt.hash}`); | |
console.log(JSON.stringify(approveReceipt)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment