Last active
May 14, 2021 22:45
-
-
Save mounibec/9bd04f359bd303d8ae23e170774d5a4b to your computer and use it in GitHub Desktop.
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": "paraswap_api_trade", | |
"version": "1.0.0", | |
"main": "index.js", | |
"license": "MIT", | |
"dependencies": { | |
"bignumber.js": "^9.0.1", | |
"axios": "^0.19.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
const axios = require('axios'); | |
const BN = require('bignumber.js'); | |
const dotenv = require('dotenv'); | |
dotenv.config(); | |
const API_URL = 'https://apiv4.paraswap.io/v2'; | |
const USER_ADDRESS = process.env.USER_ADDRESS; | |
const NULL_ADDRESS = '0x0000000000000000000000000000000000000000'; | |
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.network = network; | |
} | |
async getRate(from, to, fromAmount) { | |
const pricesURL = `${API_URL}/prices/?from=${from.address}&to=${to.address}&amount=${fromAmount}&fromDecimals=${from.decimals}&toDecimals=${to.decimals}&side=SELL&network=${this.network}`; | |
const { data: { priceRoute } } = await axios.get(pricesURL, { headers: { 'X-Partner': referrer } }); | |
return priceRoute; | |
} | |
async buildSwap(from, to, srcAmount, minAmount, priceRoute) { | |
const txURL = `${API_URL}/transactions/${this.network}`; | |
const txConfig = { | |
priceRoute, | |
srcToken: from.address, | |
srcDecimals: from.decimals, | |
destToken: to.address, | |
destDecimals: to.decimals, | |
srcAmount, | |
destAmount: minAmount, | |
userAddress: USER_ADDRESS, | |
referrer, | |
receiver: NULL_ADDRESS, | |
}; | |
const { data } = await axios.post(txURL, txConfig); | |
return data; | |
} | |
} | |
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.response.data); | |
} | |
} | |
/* | |
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