Skip to content

Commit

Permalink
Merge pull request hummingbot#221 from hummingbot/staging
Browse files Browse the repository at this point in the history
Sync gateway / staging to main gateway 1.21.0
  • Loading branch information
rapcmia authored Oct 30, 2023
2 parents 8f4b983 + 1170c85 commit 3c6138f
Show file tree
Hide file tree
Showing 35 changed files with 488 additions and 2,666 deletions.
1 change: 0 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ module.exports = {
'src/connectors/uniswap/uniswap.config.ts',
'src/connectors/uniswap/uniswap.ts',
'src/connectors/uniswap/uniswap.lp.helper.ts',
'src/connectors/defira/defira.ts',
'src/connectors/openocean/openocean.ts',
'src/connectors/pangolin/pangolin.ts',
'src/chains/injective/injective.mappers.ts',
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hummingbot-gateway",
"version": "1.20.0",
"version": "1.21.0",
"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 @@ -59,7 +59,6 @@
"@uniswap/v3-core": "^1.0.0",
"@uniswap/v3-periphery": "^1.1.1",
"@uniswap/v3-sdk": "^3.7.0",
"@zuzu-cat/defira-sdk": "^1.0.0",
"abi-decoder": "^2.4.0",
"ajv": "^8.6.3",
"algosdk": "^2.2.0",
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="1.20.0"; // gateway version
const gateway_version="1.21.0"; // gateway version
if (!ConfigManagerV2.getInstance().get('server.id')) {
ConfigManagerV2.getInstance().set(
'server.id',
Expand Down
28 changes: 16 additions & 12 deletions src/chains/cosmos/cosmos-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { BigNumber } from 'ethers';
import { AccountData, DirectSignResponse } from '@cosmjs/proto-signing';

import { IndexedTx, setupIbcExtension } from '@cosmjs/stargate';
import { logger } from '../../services/logger';

//Cosmos
const { DirectSecp256k1Wallet } = require('@cosmjs/proto-signing');
Expand Down Expand Up @@ -58,8 +59,7 @@ export class CosmosBase {
private _tokenMap: Record<string, Token> = {};

private _ready: boolean = false;
private _initializing: boolean = false;
private _initPromise: Promise<void> = Promise.resolve();
private _initialized: Promise<boolean> = Promise.resolve(false);

public chainName;
public rpcUrl;
Expand Down Expand Up @@ -93,17 +93,21 @@ export class CosmosBase {
}

async init(): Promise<void> {
if (!this.ready() && !this._initializing) {
this._initializing = true;
this._initPromise = this.loadTokens(
this.tokenListSource,
this.tokenListType
).then(() => {
this._ready = true;
this._initializing = false;
});
await this._initialized; // Wait for any previous init() calls to complete
if (!this.ready()) {
// If we're not ready, this._initialized will be a Promise that resolves after init() completes
this._initialized = (async () => {
try {
await this.loadTokens(this.tokenListSource, this.tokenListType)
return true;
} catch (e) {
logger.error(`Failed to initialize ${this.chainName} chain: ${e}`);
return false;
}
})();
this._ready = await this._initialized; // Wait for the initialization to complete
}
return this._initPromise;
return;
}

async loadTokens(
Expand Down
48 changes: 28 additions & 20 deletions src/chains/ethereum/ethereum-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class EthereumBase {
private _tokenMap: Record<string, TokenInfo> = {};
// there are async values set in the constructor
private _ready: boolean = false;
private _initializing: boolean = false;
private _initialized: Promise<boolean> = Promise.resolve(false);
public chainName;
public chainId;
public rpcUrl;
Expand Down Expand Up @@ -121,14 +121,22 @@ export class EthereumBase {
}

async init(): Promise<void> {
if (!this.ready() && !this._initializing) {
this._initializing = true;
await this._nonceManager.init(
async (address) => await this.provider.getTransactionCount(address)
);
await this.loadTokens(this.tokenListSource, this.tokenListType);
this._ready = true;
this._initializing = false;
await this._initialized; // Wait for any previous init() calls to complete
if (!this.ready()) {
// If we're not ready, this._initialized will be a Promise that resolves after init() completes
this._initialized = (async () => {
try {
await this._nonceManager.init(
async (address) => await this.provider.getTransactionCount(address)
);
await this.loadTokens(this.tokenListSource, this.tokenListType);
return true;
} catch (e) {
logger.error(`Failed to initialize ${this.chainName} chain: ${e}`);
return false;
}
})();
this._ready = await this._initialized; // Wait for the initialization to complete
}
return;
}
Expand Down Expand Up @@ -241,7 +249,7 @@ export class EthereumBase {
const balance: BigNumber = await contract.balanceOf(wallet.address);
logger.info(
`Raw balance of ${contract.address} for ` +
`${wallet.address}: ${balance.toString()}`
`${wallet.address}: ${balance.toString()}`
);
return { value: balance, decimals: decimals };
}
Expand All @@ -255,10 +263,10 @@ export class EthereumBase {
): Promise<TokenValue> {
logger.info(
'Requesting spender ' +
spender +
' allowance for owner ' +
wallet.address +
'.'
spender +
' allowance for owner ' +
wallet.address +
'.'
);
const allowance = await contract.allowance(wallet.address, spender);
logger.info(allowance);
Expand Down Expand Up @@ -311,12 +319,12 @@ export class EthereumBase {
): Promise<Transaction> {
logger.info(
'Calling approve method called for spender ' +
spender +
' requesting allowance ' +
amount.toString() +
' from owner ' +
wallet.address +
'.'
spender +
' requesting allowance ' +
amount.toString() +
' from owner ' +
wallet.address +
'.'
);
return this.nonceManager.provideNonce(
nonce,
Expand Down
1 change: 0 additions & 1 deletion src/chains/ethereum/ethereum.validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ export const validateSpender: Validator = mkValidator(
val === 'viperswap' ||
val === 'openocean' ||
val === 'quickswap' ||
val === 'defira' ||
val === 'mad_meerkat' ||
val === 'vvs' ||
val === 'pancakeswap' ||
Expand Down
4 changes: 1 addition & 3 deletions src/chains/harmony/harmony.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,7 @@ export class Harmony extends EthereumBase implements Ethereumish {

getSpender(reqSpender: string): string {
let spender: string;
if (reqSpender === 'defira') {
spender = '0x3C8BF7e25EbfAaFb863256A4380A8a93490d8065';
} else if (reqSpender === 'openocean') {
if (reqSpender === 'openocean') {
spender = OpenoceanConfig.config.routerAddress('ethereum', this._chain);
} else if (reqSpender === 'sushiswap') {
spender = SushiswapConfig.config.sushiswapRouterAddress(
Expand Down
5 changes: 1 addition & 4 deletions src/chains/harmony/harmony.validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ export const validateSpender: Validator = mkValidator(
invalidSpenderError,
(val) =>
typeof val === 'string' &&
(val === 'sushiswap' ||
val === 'defira' ||
val === 'openocean' ||
isValidAddress(val))
(val === 'sushiswap' || val === 'openocean' || isValidAddress(val))
);

export const validateNonce: Validator = mkValidator(
Expand Down
90 changes: 49 additions & 41 deletions src/chains/injective/injective.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export interface InjectiveWallet {
export class Injective {
private static _instances: LRUCache<string, Injective>;
private _ready: boolean = false;
private _initializing: boolean = false;
private _initialized: Promise<boolean> = Promise.resolve(false);

private _network: Network;
private _chainId: ChainId;
Expand Down Expand Up @@ -152,48 +152,56 @@ export class Injective {
}

public async init(): Promise<void> {
if (!this.ready() && !this._initializing) {
this._initializing = true;
// initialize nonce manager
await this._nonceManager.init(
async (address: string) => await this.getTransactionCount(address)
);
// start updating block number
this._blockUpdateIntervalID = setInterval(async () => {
await this.updateCurrentBlockNumber();
}, 2000) as unknown as number;

// get tokens
const rawMarkets = await this._spotApi.fetchMarkets();
for (const market of rawMarkets) {
if (market.baseToken) {
const token = {
address: '',
chainId: chainIdToInt(this._chainId),
name: market.baseToken.name,
decimals: market.baseToken.decimals,
symbol: market.baseToken.symbol,
denom: market.baseDenom,
};
this._symbolToToken[market.baseToken.symbol] = token;
this._denomToToken[market.baseDenom] = token;
}
await this._initialized; // Wait for any previous init() calls to complete
if (!this.ready()) {
// If we're not ready, this._initialized will be a Promise that resolves after init() completes
this._initialized = (async () => {
try {
// initialize nonce manager
await this._nonceManager.init(
async (address: string) => await this.getTransactionCount(address)
);
// start updating block number
this._blockUpdateIntervalID = setInterval(async () => {
await this.updateCurrentBlockNumber();
}, 2000) as unknown as number;

// get tokens
const rawMarkets = await this._spotApi.fetchMarkets();
for (const market of rawMarkets) {
if (market.baseToken) {
const token = {
address: '',
chainId: chainIdToInt(this._chainId),
name: market.baseToken.name,
decimals: market.baseToken.decimals,
symbol: market.baseToken.symbol,
denom: market.baseDenom,
};
this._symbolToToken[market.baseToken.symbol] = token;
this._denomToToken[market.baseDenom] = token;
}

if (market.quoteToken) {
const token = {
address: '',
chainId: chainIdToInt(this._chainId),
name: market.quoteToken.name,
decimals: market.quoteToken.decimals,
symbol: market.quoteToken.symbol,
denom: market.quoteDenom,
};
this._symbolToToken[market.quoteToken.symbol] = token;
this._denomToToken[market.quoteDenom] = token;
if (market.quoteToken) {
const token = {
address: '',
chainId: chainIdToInt(this._chainId),
name: market.quoteToken.name,
decimals: market.quoteToken.decimals,
symbol: market.quoteToken.symbol,
denom: market.quoteDenom,
};
this._symbolToToken[market.quoteToken.symbol] = token;
this._denomToToken[market.quoteDenom] = token;
}
}
return true;
} catch (e) {
logger.error(`Failed to initialize ${this.chainName} chain: ${e}`);
return false;
}
this._ready = true;
this._initializing = false;
}
})();
this._ready = await this._initialized; // Wait for the initialization to complete
}
return;
}
Expand Down
34 changes: 20 additions & 14 deletions src/chains/near/near.base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ export class NearBase {
private _tokenMap: Record<string, TokenInfo> = {};
// there are async values set in the constructor
private _ready: boolean = false;
private _initializing: boolean = false;
private _initPromise: Promise<void> = Promise.resolve();
private _initialized: Promise<boolean> = Promise.resolve(false);
private _keyStore: keyStores.InMemoryKeyStore;
private _connection: Near | undefined;

Expand Down Expand Up @@ -107,18 +106,25 @@ export class NearBase {
}

async init(): Promise<void> {
if (!this.ready() && !this._initializing) {
this._initializing = true;
this._connection = await this.connectProvider();
this._initPromise = this.loadTokens(
this.tokenListSource,
this.tokenListType
).then(() => {
this._ready = true;
this._initializing = false;
});
await this._initialized; // Wait for any previous init() calls to complete
if (!this.ready()) {
// If we're not ready, this._initialized will be a Promise that resolves after init() completes
this._initialized = (async () => {
try {
this._connection = await this.connectProvider();
await this.loadTokens(
this.tokenListSource,
this.tokenListType
)
return true;
} catch (e) {
logger.error(`Failed to initialize ${this.chainName} chain: ${e}`);
return false;
}
})();
this._ready = await this._initialized; // Wait for the initialization to complete
}
return this._initPromise;
return;
}

async connectProvider(): Promise<Near> {
Expand Down Expand Up @@ -260,7 +266,7 @@ export class NearBase {
}
logger.info(
`Raw balance of ${contract.contractId} for ` +
`${contract.account.accountId}: ${balance}`
`${contract.account.accountId}: ${balance}`
);
return balance;
}
Expand Down
Loading

0 comments on commit 3c6138f

Please sign in to comment.