Skip to content

Instantly share code, notes, and snippets.

@mounibec
Last active April 17, 2021 08:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mounibec/1850126b9f9e81ae95564b5dd8397642 to your computer and use it in GitHub Desktop.
Save mounibec/1850126b9f9e81ae95564b5dd8397642 to your computer and use it in GitHub Desktop.
{
"name": "paraswap-trade-examples",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"bignumber.js": "^9.0.1",
"paraswap": "^4.1.2",
"web3": "^1.3.5"
}
}
const {ParaSwap} = require("paraswap");
const BN = require("bignumber.js");
const Web3 = require("web3");
const PROVIDER_URL = process.env.PROVIDER_URL;
const USER_ADDRESS = process.env.USER_ADDRESS;
const referrer = "chucknorris";
const SLIPPAGE = 1;//1%
const networks = {
MAINNET: 1,
POLYGON: 137
}
const tokens = {
[networks.MAINNET]: [
{
"decimals": 18,
"symbol": "ETH",
"address": "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"
},
{
"decimals": 6,
"symbol": "USDC",
"address": "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"
},
{
"decimals": 18,
"symbol": "DAI",
"address": "0x6B175474E89094C44Da98b954EedeAC495271d0F"
}
],
[networks.POLYGON]: [
{
"decimals": 18,
"symbol": "MATIC",
"address": "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"
},
{
"decimals": 8,
"symbol": "WBTC",
"address": "0x1bfd67037b42cf73acf2047067bd4f2c47d9bfd6"
}
]
}
function t(symbol, network) {
network = network || networks.MAINNET;
return tokens[network].find(t => t.symbol === symbol);
}
class ParaSwapper {
constructor(network){
this.web3Provider = new Web3(PROVIDER_URL);
this.paraSwap = new ParaSwap(network);
}
async getRate(from, to, amount) {
return this.paraSwap.getRate(from.address, to.address, amount, 'SELL', {referrer}, from.decimals, to.decimals);
}
async buildSwap(from, to, srcAmount, minAmount, priceRoute) {
return this.paraSwap.buildTx(from.address, to.address, srcAmount, minAmount, priceRoute, USER_ADDRESS, referrer);
}
}
async function swap(_srcAmount, from, to, network) {
try {
const srcAmount = new BN(_srcAmount).times(10 ** from.decimals).toFixed(0);
const ps = new ParaSwapper(network);
const priceRoute = await ps.getRate(from, to, srcAmount);
const minAmount = new BN(priceRoute.destAmount).times(1 - SLIPPAGE / 100).toFixed(0);
const transaction = await ps.buildSwap(from, to, srcAmount, minAmount, priceRoute);
console.log("transaction", transaction);
} catch (error) {
console.error(error);
}
}
/*
swap(
1,
t("ETH", networks.MAINNET),
t("DAI", networks.MAINNET),
networks.MAINNET
);
*/
swap(
1,
t("MATIC", networks.POLYGON),
t("WBTC", networks.POLYGON),
networks.POLYGON
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment