Skip to content

Commit

Permalink
Merge pull request #352 from hummingbot/staging
Browse files Browse the repository at this point in the history
sync / gateway staging -> master for Hummingbot gateway version 2.0.1
  • Loading branch information
nikspz authored Aug 28, 2024
2 parents c515922 + 4f89eb7 commit 8153070
Show file tree
Hide file tree
Showing 61 changed files with 3,108 additions and 3,206 deletions.
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = {
'src/chains/binance-smart-chain/binance-smart-chain.ts',
'src/chains/ethereum/ethereum.ts',
'src/chains/avalanche/avalanche.ts',
'src/chains/celo/celo.ts',
'src/chains/avalanche/pangolin/pangolin.ts',
'src/chains/cosmos/cosmos.ts',
'src/chains/near/near.ts',
Expand Down
13 changes: 6 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hummingbot-gateway",
"version": "2.0.0",
"version": "2.0.1",
"description": "Middleware that helps Hummingbot clients access standardized DEX API endpoints on different blockchain networks",
"main": "index.js",
"license": "Apache-2.0",
Expand Down Expand Up @@ -61,12 +61,12 @@
"@types/minimist": "^1.2.2",
"@types/morgan": "^1.9.3",
"@types/uuid": "^8.3.4",
"@uniswap/sdk": "3.0.2",
"@uniswap/sdk-core": "^3.0.0",
"@uniswap/smart-order-router": "^2.5.26",
"@uniswap/v3-core": "^1.0.0",
"@uniswap/sdk": "3.0.3",
"@uniswap/sdk-core": "^5.3.1",
"@uniswap/smart-order-router": "^3.39.0",
"@uniswap/v3-core": "^1.0.1",
"@uniswap/v3-periphery": "^1.1.1",
"@uniswap/v3-sdk": "^3.7.0",
"@uniswap/v3-sdk": "^3.13.1",
"abi-decoder": "^2.4.0",
"ajv": "^8.6.3",
"algosdk": "^2.2.0",
Expand Down Expand Up @@ -100,7 +100,6 @@
"osmojs": "16.5.1",
"promise-retry": "^2.0.1",
"quickswap-sdk": "^3.0.8",
"quipuswap-v3-sdk": "^0.0.7",
"swagger-ui-express": "^4.1.6",
"swap-router-sdk": "^1.21.1",
"tslib": "^2.3.1",
Expand Down
24 changes: 6 additions & 18 deletions src/amm/amm.controllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,6 @@ import {
trade as plentyTrade,
estimateGas as plentyEstimateGas,
} from '../connectors/plenty/plenty.controllers';
import {
price as quipuPrice,
trade as quipuTrade,
estimateGas as quipuEstimateGas,
} from '../connectors/quipuswap/quipuswap.controllers';
import {
getInitializedChain,
getConnector,
Expand All @@ -85,7 +80,6 @@ import {
import { Algorand } from '../chains/algorand/algorand';
import { Tinyman } from '../connectors/tinyman/tinyman';
import { Plenty } from '../connectors/plenty/plenty';
import { QuipuSwap } from '../connectors/quipuswap/quipuswap';
import { Osmosis } from '../chains/osmosis/osmosis';
import { Carbonamm } from '../connectors/carbon/carbonAMM';

Expand All @@ -97,17 +91,15 @@ export async function price(req: PriceRequest): Promise<PriceResponse> {
return chain.controller.price(chain as unknown as Osmosis, req);
}

const connector: Uniswapish | RefAMMish | Tinyman | Plenty | QuipuSwap =
await getConnector<Uniswapish | RefAMMish | Tinyman | Plenty | QuipuSwap>(
const connector: Uniswapish | RefAMMish | Tinyman | Plenty =
await getConnector<Uniswapish | RefAMMish | Tinyman | Plenty>(
req.chain,
req.network,
req.connector
);

if (connector instanceof Plenty) {
return plentyPrice(<Tezosish>chain, connector, req);
} else if (connector instanceof QuipuSwap) {
return quipuPrice(<Tezosish>chain, connector, req);
} else if (connector instanceof Carbonamm) {
return carbonPrice(<Ethereumish>chain, connector, req);
} else if ('routerAbi' in connector) {
Expand All @@ -128,17 +120,15 @@ export async function trade(req: TradeRequest): Promise<TradeResponse> {
return chain.controller.trade(chain as unknown as Osmosis, req);
}

const connector: Uniswapish | RefAMMish | Tinyman | Plenty | QuipuSwap =
await getConnector<Uniswapish | RefAMMish | Tinyman | Plenty | QuipuSwap>(
const connector: Uniswapish | RefAMMish | Tinyman | Plenty =
await getConnector<Uniswapish | RefAMMish | Tinyman | Plenty>(
req.chain,
req.network,
req.connector
);

if (connector instanceof Plenty) {
return plentyTrade(<Tezosish>chain, connector, req);
} else if (connector instanceof QuipuSwap) {
return quipuTrade(<Tezosish>chain, connector, req);
} else if (connector instanceof Carbonamm) {
return carbonTrade(<Ethereumish>chain, connector, req);
} else if ('routerAbi' in connector) {
Expand Down Expand Up @@ -238,17 +228,15 @@ export async function estimateGas(
return chain.controller.estimateGas(chain as unknown as Osmosis);
}

const connector: Uniswapish | RefAMMish | Tinyman | Plenty | QuipuSwap =
await getConnector<Uniswapish | RefAMMish | Plenty | QuipuSwap>(
const connector: Uniswapish | RefAMMish | Tinyman | Plenty =
await getConnector<Uniswapish | RefAMMish | Plenty>(
req.chain,
req.network,
req.connector
);

if (connector instanceof Plenty) {
return plentyEstimateGas(<Tezosish>chain, connector);
} else if (connector instanceof QuipuSwap) {
return quipuEstimateGas(<Tezosish>chain, connector);
} else if (connector instanceof Carbonamm) {
return carbonEstimateGas(<Ethereumish>chain, connector);
} else if ('routerAbi' in connector) {
Expand Down
2 changes: 1 addition & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export const startSwagger = async () => {

export const startGateway = async () => {
const port = ConfigManagerV2.getInstance().get('server.port');
const gateway_version="2.0.0"
const gateway_version="2.0.1"
if (!ConfigManagerV2.getInstance().get('server.id')) {
ConfigManagerV2.getInstance().set(
'server.id',
Expand Down
10 changes: 9 additions & 1 deletion src/chains/avalanche/avalanche.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { TraderjoeConfig } from '../../connectors/traderjoe/traderjoe.config';
import { PangolinConfig } from '../../connectors/pangolin/pangolin.config';
import { OpenoceanConfig } from '../../connectors/openocean/openocean.config';
import { Ethereumish } from '../../services/common-interfaces';
import { UniswapConfig } from '../../connectors/uniswap/uniswap.config';
import { SushiswapConfig } from '../../connectors/sushiswap/sushiswap.config';
import { ConfigManagerV2 } from '../../services/config-manager-v2';
import { EVMController } from '../ethereum/evm.controllers';
Expand Down Expand Up @@ -84,7 +85,14 @@ export class Avalanche extends EthereumBase implements Ethereumish {

getSpender(reqSpender: string): string {
let spender: string;
if (reqSpender === 'pangolin') {
if (reqSpender === 'uniswap') {
spender = UniswapConfig.config.uniswapV3SmartOrderRouterAddress(
'avalanche',
this._chain,
);
} else if (reqSpender === 'uniswapLP') {
spender = UniswapConfig.config.uniswapV3NftManagerAddress('avalanche', this._chain);
} else if (reqSpender === 'pangolin') {
spender = PangolinConfig.config.routerAddress(this._chain);
} else if (reqSpender === 'openocean') {
spender = OpenoceanConfig.config.routerAddress('avalanche', this._chain);
Expand Down
4 changes: 3 additions & 1 deletion src/chains/avalanche/avalanche.validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ export const validateSpender: Validator = mkValidator(

(val) =>
typeof val === 'string' &&
(val === 'pangolin' ||
(val === 'uniswap' ||
val === 'uniswapLP' ||
val === 'pangolin' ||
val === 'traderjoe' ||
val === 'openocean' ||
val === 'sushiswap' ||
Expand Down
8 changes: 8 additions & 0 deletions src/chains/binance-smart-chain/binance-smart-chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { SushiswapConfig } from '../../connectors/sushiswap/sushiswap.config';
import { ConfigManagerV2 } from '../../services/config-manager-v2';
import { OpenoceanConfig } from '../../connectors/openocean/openocean.config';
import { EVMController } from '../ethereum/evm.controllers';
import {UniswapConfig} from "../../connectors/uniswap/uniswap.config";

export class BinanceSmartChain extends EthereumBase implements Ethereumish {
private static _instances: { [name: string]: BinanceSmartChain };
Expand Down Expand Up @@ -113,6 +114,13 @@ export class BinanceSmartChain extends EthereumBase implements Ethereumish {
'binance-smart-chain',
this._chain
);
} else if (reqSpender === 'uniswap') {
spender = UniswapConfig.config.uniswapV3SmartOrderRouterAddress(
'binance-smart-chain',
this._chain
);
} else if (reqSpender === 'uniswapLP') {
spender = UniswapConfig.config.uniswapV3NftManagerAddress('binance-smart-chain', this._chain);
} else {
spender = reqSpender;
}
Expand Down
130 changes: 130 additions & 0 deletions src/chains/celo/celo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import abi from '../ethereum/ethereum.abi.json';
import { logger } from '../../services/logger';
import { Contract, Transaction, Wallet } from 'ethers';
import { EthereumBase } from '../ethereum/ethereum-base';
import { getEthereumConfig as getCeloConfig } from '../ethereum/ethereum.config';
import { Provider } from '@ethersproject/abstract-provider';
import { Ethereumish } from '../../services/common-interfaces';
import { ConfigManagerV2 } from '../../services/config-manager-v2';
import { EVMController } from '../ethereum/evm.controllers';
import { UniswapConfig } from '../../connectors/uniswap/uniswap.config';

export class Celo extends EthereumBase implements Ethereumish {
private static _instances: { [name: string]: Celo };
private _gasPrice: number;
private _gasPriceRefreshInterval: number | null;
private _nativeTokenSymbol: string;
private _chain: string;
public controller;

private constructor(network: string) {
const config = getCeloConfig('celo', network);
super(
'celo',
config.network.chainID,
config.network.nodeURL,
config.network.tokenListSource,
config.network.tokenListType,
config.manualGasPrice,
config.gasLimitTransaction,
ConfigManagerV2.getInstance().get('server.nonceDbPath'),
ConfigManagerV2.getInstance().get('server.transactionDbPath')
);
this._chain = config.network.name;
this._nativeTokenSymbol = config.nativeCurrencySymbol;

this._gasPrice = config.manualGasPrice;

this._gasPriceRefreshInterval =
config.network.gasPriceRefreshInterval !== undefined
? config.network.gasPriceRefreshInterval
: null;

this.updateGasPrice();
this.controller = EVMController;
}

public static getInstance(network: string): Celo {
if (Celo._instances === undefined) {
Celo._instances = {};
}
if (!(network in Celo._instances)) {
Celo._instances[network] = new Celo(network);
}

return Celo._instances[network];
}

public static getConnectedInstances(): { [name: string]: Celo } {
return Celo._instances;
}

// getters

public get gasPrice(): number {
return this._gasPrice;
}

public get nativeTokenSymbol(): string {
return this._nativeTokenSymbol;
}

public get chain(): string {
return this._chain;
}

getContract(tokenAddress: string, signerOrProvider?: Wallet | Provider) {
return new Contract(tokenAddress, abi.ERC20Abi, signerOrProvider);
}

getSpender(reqSpender: string): string {
let spender: string;
if (reqSpender === 'uniswap') {
spender = UniswapConfig.config.uniswapV3SmartOrderRouterAddress(
'celo',
this._chain,
);
} else if (reqSpender === 'uniswapLP') {
spender = UniswapConfig.config.uniswapV3NftManagerAddress('celo', this._chain);
} else {
spender = reqSpender;
}
return spender;
}

// cancel transaction
async cancelTx(wallet: Wallet, nonce: number): Promise<Transaction> {
logger.info(
'Canceling any existing transaction(s) with nonce number ' + nonce + '.'
);
return super.cancelTxWithGasPrice(wallet, nonce, this._gasPrice * 2);
}

/**
* Automatically update the prevailing gas price on the network.
*/
async updateGasPrice(): Promise<void> {
if (this._gasPriceRefreshInterval === null) {
return;
}

const gasPrice = await this.getGasPrice();
if (gasPrice !== null) {
this._gasPrice = gasPrice;
} else {
logger.info('gasPrice is unexpectedly null.');
}

setTimeout(
this.updateGasPrice.bind(this),
this._gasPriceRefreshInterval * 1000
);
}

async close() {
await super.close();
if (this._chain in Celo._instances) {
delete Celo._instances[this._chain];
}
}
}
41 changes: 41 additions & 0 deletions src/chains/celo/celo.validators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {
mkRequestValidator,
mkValidator,
RequestValidator,
Validator,
validateAmount,
validateToken,
validateTokenSymbols,
} from '../../services/validators';
import {
isAddress,
validateNonce,
validateAddress,
} from '../ethereum/ethereum.validators';

export const invalidSpenderError: string =
'The spender param is not a valid Celo address (0x followed by 40 hexidecimal characters).';

// given a request, look for a key called spender that is 'uniswap' or an Ethereum address
export const validateSpender: Validator = mkValidator(
'spender',
invalidSpenderError,

(val) =>
typeof val === 'string' &&
(val === 'uniswap' ||
val === 'uniswapLP' ||
isAddress(val))
);

export const validateCeloApproveRequest: RequestValidator =
mkRequestValidator([
validateAddress,
validateSpender,
validateToken,
validateAmount,
validateNonce,
]);

export const validateCeloAllowancesRequest: RequestValidator =
mkRequestValidator([validateAddress, validateSpender, validateTokenSymbols]);
5 changes: 4 additions & 1 deletion src/chains/ethereum/ethereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ export class Ethereum extends EthereumBase implements Ethereumish {
let spender: string;
if (reqSpender === 'uniswap') {
spender = UniswapConfig.config.uniswapV3SmartOrderRouterAddress(
this.chainName,
this._chain,
);
} else if (reqSpender === 'pancakeswap') {
Expand All @@ -196,7 +197,9 @@ export class Ethereum extends EthereumBase implements Ethereumish {
this._chain,
);
} else if (reqSpender === 'uniswapLP') {
spender = UniswapConfig.config.uniswapV3NftManagerAddress(this._chain);
spender = UniswapConfig.config.uniswapV3NftManagerAddress(
this.chainName,
this._chain);
} else if (reqSpender === 'carbonamm') {
spender = CarbonConfig.config.carbonContractsConfig(
'ethereum',
Expand Down
3 changes: 2 additions & 1 deletion src/chains/polygon/polygon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,11 @@ export class Polygon extends EthereumBase implements Ethereumish {
let spender: string;
if (reqSpender === 'uniswap') {
spender = UniswapConfig.config.uniswapV3SmartOrderRouterAddress(
'polygon',
this._chain
);
} else if (reqSpender === 'uniswapLP') {
spender = UniswapConfig.config.uniswapV3NftManagerAddress(this._chain);
spender = UniswapConfig.config.uniswapV3NftManagerAddress('polygon', this._chain);
} else if (reqSpender === 'quickswap') {
spender = QuickswapConfig.config.routerAddress(this._chain);
} else if (reqSpender === 'sushiswap') {
Expand Down
Loading

0 comments on commit 8153070

Please sign in to comment.