-
-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #352 from hummingbot/staging
sync / gateway staging -> master for Hummingbot gateway version 2.0.1
- Loading branch information
Showing
61 changed files
with
3,108 additions
and
3,206 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
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
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]; | ||
} | ||
} | ||
} |
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
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]); |
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
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
Oops, something went wrong.