Skip to content

Commit 25b164b

Browse files
authored
Implement apeswap pricefetcher (#7)
* create bakeryswap price fetching script * create ape swap price fetching script * cleanup
1 parent 590019b commit 25b164b

File tree

6 files changed

+137
-8
lines changed

6 files changed

+137
-8
lines changed

program/lib/interactive/market/actions.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ const pairs = [
2626
];
2727

2828
const exchanges = [
29+
{
30+
name: 'Apeswap',
31+
description: 'Apeswap Decentralized Exchange'
32+
},
2933
{
3034
name: 'Uniswap',
3135
description: 'Uniswap Decentralized Exchange'

program/lib/interactive/market/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ function startInteractiveMarketFetcher() {
3030
case 'Bakeryswap':
3131
fetcher.fetchBakeryswapPairPrice(answers.pair);
3232
break;
33+
case 'Apeswap':
34+
fetcher.fetchApeswapPairPrice(answers.pair);
35+
break;
3336
default:
3437
}
3538
});
36-
37-
3839
}
3940

4041
exports.init = () => {
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
const Web3 = require('web3');
2+
const { mainnet } = require('../../addresses');
3+
const web3 = new Web3(new Web3.providers.WebsocketProvider(process.env.MORALIAS_BSC_MAINNET_WSS_URL));
4+
const _ = require("lodash");
5+
const BigNumber = require("bignumber.js");
6+
7+
const apeswap = {
8+
factory: new web3.eth.Contract(mainnet.apeswap.factory.ABI, mainnet.apeswap.factory.address),
9+
router: new web3.eth.Contract(mainnet.apeswap.router.ABI, mainnet.apeswap.router.address),
10+
}
11+
12+
const baseAmount = 1;
13+
14+
process.on('message', function (data) {
15+
if (data === false) {
16+
process.exit(1);
17+
} else {
18+
fetchData(data);
19+
}
20+
});
21+
22+
async function fetchData(data) {
23+
let stableToken = {
24+
name: mainnet.tokenPairs.Binance[data.pair].stableToken.name,
25+
address: mainnet.tokenPairs.Binance[data.pair].stableToken.address,
26+
decimals: mainnet.tokenPairs.Binance[data.pair].stableToken.decimals,
27+
}
28+
29+
let tradingToken = {
30+
name: mainnet.tokenPairs.Binance[data.pair].tradingToken.name,
31+
address: mainnet.tokenPairs.Binance[data.pair].tradingToken.address,
32+
decimals: mainnet.tokenPairs.Binance[data.pair].tradingToken.decimals,
33+
}
34+
35+
36+
console.log(`Fetching Ape Swap Prices...`)
37+
web3.eth.subscribe('newBlockHeaders', (error, result) => {
38+
if (!error) {
39+
return;
40+
}
41+
console.error(error);
42+
})
43+
.on("Connected To Binance SmartChain", subscriptionId => {
44+
console.log(`You are connected on ${subscriptionId}`);
45+
})
46+
.on('data', async block => {
47+
console.log('-------------------------------------------------------------');
48+
console.log(`Trading ${tradingToken.name}/${stableToken.name} ...`);
49+
const exchangeAmount = await new BigNumber(baseAmount);
50+
const shiftedExchangeAmount = await new BigNumber(exchangeAmount).shiftedBy(tradingToken.decimals);
51+
let tokenIn = tradingToken.address
52+
let tokenOut = stableToken.address;
53+
const rawValue = await apeswap.router.methods.getAmountsOut(shiftedExchangeAmount, [tokenIn, tokenOut]).call();
54+
const shiftedValue = await new BigNumber(rawValue[1]).shiftedBy(-stableToken.decimals);
55+
56+
57+
process.send({
58+
tokenIn: {
59+
name: tradingToken.name,
60+
value: exchangeAmount.toString(),
61+
},
62+
tokenOut: {
63+
name: stableToken.name,
64+
value: shiftedValue.toString()
65+
}
66+
});
67+
68+
})
69+
.on('error', error => {
70+
console.log(error);
71+
});
72+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
require("dotenv").config()
2+
3+
const fetchProcess = require('child_process');
4+
const pad = require("pad");
5+
const colors = require("colors");
6+
7+
async function fetchData(data) {
8+
const process = fetchProcess.fork(__dirname+'/apeswapPriceFetcher.js', [], {silent: true});
9+
process.send(data)
10+
process.stdout.on('data', function(standardOutData) {
11+
console.log(standardOutData.toString());
12+
});
13+
14+
process.stderr.on('data', function(standardErrorData) {
15+
if(standardErrorData.toString().includes('Warning:')) {
16+
} else {
17+
console.log('Error: ' + standardErrorData );
18+
}
19+
});
20+
21+
process.on('message', function(response) {
22+
23+
24+
console.log(
25+
pad(colors.red('Token In:'), 30), response.tokenIn.name,
26+
pad(colors.red('Value: '), 30), response.tokenIn.value
27+
);
28+
console.log(
29+
pad(colors.green('Token Out: '), 30), response.tokenOut.name,
30+
pad(colors.green('Value Out: '), 30), response.tokenOut.value)
31+
;
32+
33+
34+
process.send(data = false);
35+
});
36+
37+
process.on('close', function(code, data) {
38+
console.log('Done. Closing ' + code);
39+
});
40+
}
41+
42+
module.exports = {
43+
getAssetInfo: function(data) {
44+
return fetchData(data);
45+
}
46+
}

program/utils/marketfetcher/bakeryswap/bakeryswapPriceFetcher.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ async function fetchData(data) {
3333
}
3434

3535

36-
//console.log(mainnet.tokenPairs.Binance[data.pair].stableToken.name);
3736
console.log(`Fetching Bakery Swap Prices...`)
3837
web3.eth.subscribe('newBlockHeaders', (error, result) => {
3938
if (!error) {
@@ -51,7 +50,6 @@ async function fetchData(data) {
5150
const shiftedExchangeAmount = await new BigNumber(exchangeAmount).shiftedBy(tradingToken.decimals);
5251
let tokenIn = tradingToken.address
5352
let tokenOut = stableToken.address;
54-
// call getAmountsOut in PancakeSwap
5553
const rawValue = await bakeryswap.router.methods.getAmountsOut(shiftedExchangeAmount, [tokenIn, tokenOut]).call();
5654
const shiftedValue = await new BigNumber(rawValue[1]).shiftedBy(-stableToken.decimals);
5755

program/utils/marketfetcher/index.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
require('dotenv').config();
2-
const _ = require('lodash');
2+
const ApeswapService = require('../marketfetcher/apeswap');
3+
const BakeryswapService = require('../marketfetcher/bakeryswap');
34
const CMKService = require('../marketfetcher/cmk');
45
const DyDxService = require('../marketfetcher/dydx');
5-
const UniswapService = require('../marketfetcher/uniswap');
66
const KyberService = require('../marketfetcher/kyber');
77
const SushiswapService = require('../marketfetcher/sushiswap');
8-
const PancakeswapService = require('../marketfetcher/pancakeswap');
9-
const BakeryswapService = require('../marketfetcher/bakeryswap')
8+
const UniswapService = require('../marketfetcher/uniswap');
9+
10+
11+
function fetchApeswapPairPrice(pair) {
12+
let data = {pair:pair};
13+
return ApeswapService.getAssetInfo(data);
14+
}
1015

1116
function fetchBakeryswapPairPrice(pair) {
1217
let data = {pair:pair};
@@ -59,6 +64,9 @@ module.exports = {
5964
},
6065
fetchBakeryswapPairPrice: function(pair) {
6166
return fetchBakeryswapPairPrice(pair);
67+
},
68+
fetchApeswapPairPrice: function(pair) {
69+
return fetchApeswapPairPrice(pair);
6270
}
6371
}
6472
}

0 commit comments

Comments
 (0)