Skip to content
This repository was archived by the owner on Feb 25, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# Configuration file

APPNAME=Hummingbot Gateway API
NODE_ENV=dev
PORT=5000
PROTOCOLS=["celo", "terra", "balancer", "eth"]

# use only if ip whitelist is required for local or docker instance
# note that docker instance does not use 127.0.0.1 address
Expand All @@ -16,13 +13,14 @@ IP_WHITELIST=
TERRA_LCD_URL=https://tequila-lcd.terra.dev
TERRA_CHAIN=tequila-0004

# Balancer
# Ethereum
# - network: mainnet, kovan, etc
# - rpc url: infura or other rpc url
BALANCER_NETWORK={network}
ETHEREUM CHAIN={network}
ETHEREUM_RPC_URL=https://{network}.infura.io/v3/{api_key}

# subgraph_network:
# Balancer
# subgraph_network
# Reference: https://docs.balancer.finance/sor/development#subgraph
# - mainnet: balancer
# - kovan: balancer-kovan
Expand All @@ -31,10 +29,14 @@ REACT_APP_SUBGRAPH_URL=https://api.thegraph.com/subgraphs/name/balancer-labs/{su

# exchange_proxy:
# Reference: https://docs.balancer.finance/smart-contracts/addresses
# - kovan: 0x4e67bf5bD28Dd4b570FBAFe11D0633eCbA2754Ec
# - mainnet: 0x3E66B66Fd1d0b02fDa6C811Da9E0547970DB2f21
# - kovan: 0x4e67bf5bD28Dd4b570FBAFe11D0633eCbA2754Ec
EXCHANGE_PROXY={exchange_proxy}

# Uniswap
# Reference: https://uniswap.org/docs/v2/smart-contracts/router02/
UNISWAP_ROUTER=0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D

# cert
CERT_PATH={full_path_to_certs_folder}
CERT_PASSPHRASE={passphrase}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"@uniswap/sdk": "^3.0.3",
"@balancer-labs/sor": "^0.3.3",
"@terra-money/terra.js": "^0.5.8",
"bignumber.js": "^9.0.0",
Expand Down
2 changes: 2 additions & 0 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import balancerRoutes from './routes/balancer.route'
// import celoRoutes from './routes/celo.route'
import ethRoutes from './routes/eth.route'
import terraRoutes from './routes/terra.route'
import uniswapRoutes from './routes/uniswap.route'

// terminate if environment not found
const result = dotenv.config();
Expand Down Expand Up @@ -40,6 +41,7 @@ app.use('/eth', ethRoutes);
// app.use('/celo', celoRoutes);
app.use('/terra', terraRoutes);
app.use('/balancer', balancerRoutes);
app.use('/uniswap', uniswapRoutes);

app.get('/', (req, res, next) => {
const cert = req.connection.getPeerCertificate()
Expand Down
31 changes: 26 additions & 5 deletions src/routes/balancer.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@ import express from 'express';

import { getParamData, latency, reportConnectionError, statusMessages } from '../services/utils';
import Balancer from '../services/balancer';
import Ethereum from '../services/eth';

require('dotenv').config()
const debug = require('debug')('router')

const router = express.Router()
const balancer = new Balancer(process.env.BALANCER_NETWORK)
const eth = new Ethereum(process.env.BALANCER_NETWORK)
const balancer = new Balancer(process.env.ETHEREUM_CHAIN)

const denomMultiplier = 1e18
const swapMoreThanMaxPriceError = 'Swap price exceeds maxPrice'
const swapLessThanMaxPriceError = 'Swap price lower than maxPrice'
const swapMoreThanMaxPriceError = 'Price too high'
const swapLessThanMaxPriceError = 'Price too low'

router.use((req, res, next) => {
const cert = req.connection.getPeerCertificate()
Expand Down Expand Up @@ -49,6 +47,7 @@ router.post('/sell-price', async (req, res) => {
"quote":"0x....."
"base":"0x....."
"amount":0.1
"swaps": 4 (optional)
}
*/
const initTime = Date.now()
Expand All @@ -57,13 +56,18 @@ router.post('/sell-price', async (req, res) => {
const baseTokenAddress = paramData.base
const quoteTokenAddress = paramData.quote
const amount = new BigNumber(parseInt(paramData.amount * denomMultiplier))
let maxSwaps
if (paramData.maxSwaps) {
maxSwaps = parseInt(paramData.maxSwaps)
}

try {
// fetch the optimal pool mix from balancer-sor
const { swaps, expectedOut } = await balancer.priceSwapIn(
baseTokenAddress, // tokenIn is base asset
quoteTokenAddress, // tokenOut is quote asset
amount,
maxSwaps,
)

if (swaps != null && expectedOut != null) {
Expand Down Expand Up @@ -109,13 +113,18 @@ router.post('/buy-price', async (req, res) => {
const baseTokenAddress = paramData.base
const quoteTokenAddress = paramData.quote
const amount = new BigNumber(parseInt(paramData.amount * denomMultiplier))
let maxSwaps
if (paramData.maxSwaps) {
maxSwaps = parseInt(paramData.maxSwaps)
}

try {
// fetch the optimal pool mix from balancer-sor
const { swaps, expectedIn } = await balancer.priceSwapOut(
quoteTokenAddress, // tokenIn is quote asset
baseTokenAddress, // tokenOut is base asset
amount,
maxSwaps,
)
if (swaps != null && expectedIn != null) {
res.status(200).json({
Expand Down Expand Up @@ -174,6 +183,10 @@ router.post('/sell', async (req, res) => {
if (paramData.gasPrice) {
gasPrice = parseFloat(paramData.gasPrice)
}
let maxSwaps
if (paramData.maxSwaps) {
maxSwaps = parseInt(paramData.maxSwaps)
}

const minAmountOut = maxPrice / amount * denomMultiplier
debug('minAmountOut', minAmountOut)
Expand All @@ -184,6 +197,7 @@ router.post('/sell', async (req, res) => {
baseTokenAddress, // tokenIn is base asset
quoteTokenAddress, // tokenOut is quote asset
amount,
maxSwaps,
)

const price = expectedOut / amount
Expand All @@ -200,6 +214,8 @@ router.post('/sell', async (req, res) => {
gasPrice,
)

debug(txObj)

// submit response
res.status(200).json({
network: balancer.network,
Expand Down Expand Up @@ -260,13 +276,18 @@ router.post('/buy', async (req, res) => {
if (paramData.gasPrice) {
gasPrice = parseFloat(paramData.gasPrice)
}
let maxSwaps
if (paramData.maxSwaps) {
maxSwaps = parseInt(paramData.maxSwaps)
}

try {
// fetch the optimal pool mix from balancer-sor
const { swaps, expectedIn } = await balancer.priceSwapOut(
quoteTokenAddress, // tokenIn is quote asset
baseTokenAddress, // tokenOut is base asset
amount,
maxSwaps,
)

const price = expectedIn / amount
Expand Down
6 changes: 4 additions & 2 deletions src/routes/eth.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import Ethereum from '../services/eth';
const router = express.Router()
const eth = new Ethereum(process.env.ETHEREUM_CHAIN)
const separator = ','
const spenders = { balancer: process.env.EXCHANGE_PROXY,
uniswap: process.env.UNISWAP_ROUTER }
const spenders = {
balancer: process.env.EXCHANGE_PROXY,
uniswap: process.env.UNISWAP_ROUTER
}

const debug = require('debug')('router')

Expand Down
Loading