-
-
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.
- Loading branch information
Showing
13 changed files
with
1,185 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
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 getETCChainConfig } from '../ethereum/ethereum.config'; | ||
import { Provider } from '@ethersproject/abstract-provider'; | ||
import { Chain as Ethereumish } from '../../services/common-interfaces'; | ||
import { ConfigManagerV2 } from '../../services/config-manager-v2'; | ||
import { EVMController } from '../ethereum/evm.controllers'; | ||
import { ETCSwapConfig } from '../../connectors/etcswap/etcswap.config'; | ||
|
||
export class ETCChain extends EthereumBase implements Ethereumish { | ||
private static _instances: { [name: string]: ETCChain }; | ||
private _chain: string; | ||
private _gasPrice: number; | ||
private _gasPriceRefreshInterval: number | null; | ||
private _nativeTokenSymbol: string; | ||
public controller; | ||
|
||
private constructor(network: string) { | ||
const config = getETCChainConfig('etc', network); | ||
super( | ||
'etc', | ||
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): ETCChain { | ||
if (ETCChain._instances === undefined) { | ||
ETCChain._instances = {}; | ||
} | ||
if (!(network in ETCChain._instances)) { | ||
ETCChain._instances[network] = new ETCChain(network); | ||
} | ||
|
||
return ETCChain._instances[network]; | ||
} | ||
|
||
public static getConnectedInstances(): { [name: string]: ETCChain } { | ||
return ETCChain._instances; | ||
} | ||
|
||
/** | ||
* Automatically update the prevailing gas price on the network from the connected RPC node. | ||
*/ | ||
async updateGasPrice(): Promise<void> { | ||
if (this._gasPriceRefreshInterval === null) { | ||
return; | ||
} | ||
|
||
const gasPrice: number = (await this.provider.getGasPrice()).toNumber(); | ||
|
||
this._gasPrice = gasPrice * 1e-9; | ||
|
||
setTimeout( | ||
this.updateGasPrice.bind(this), | ||
this._gasPriceRefreshInterval * 1000 | ||
); | ||
} | ||
|
||
// 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 === 'etcswapLP') { | ||
spender = ETCSwapConfig.config.etcswapV3NftManagerAddress( | ||
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); | ||
} | ||
} |
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,45 @@ | ||
import { | ||
buildConfig, | ||
NetworkConfig as V2NetworkConfig, | ||
} from '../../network/network.utils'; | ||
import { ConfigManagerV2 } from '../../services/config-manager-v2'; | ||
|
||
export namespace ETCSwapConfig { | ||
export interface NetworkConfig extends Omit<V2NetworkConfig, 'tradingTypes'> { | ||
maximumHops: number; | ||
etcswapV3SmartOrderRouterAddress: (network: string) => string; | ||
etcswapV3NftManagerAddress: (network: string) => string; | ||
tradingTypes: (type: string) => Array<string>; | ||
useRouter?: boolean; | ||
feeTier?: string; | ||
} | ||
|
||
export const v2Config: V2NetworkConfig = buildConfig( | ||
'etcswap', | ||
['AMM'], | ||
[ | ||
{ chain: 'etc', networks: ['mainnet'] }, | ||
], | ||
'EVM', | ||
); | ||
|
||
export const config: NetworkConfig = { | ||
...v2Config, | ||
...{ | ||
maximumHops: ConfigManagerV2.getInstance().get(`etcswap.maximumHops`), | ||
etcswapV3SmartOrderRouterAddress: (network: string) => | ||
ConfigManagerV2.getInstance().get( | ||
`etcswap.contractAddresses.${network}.etcswapV3SmartOrderRouterAddress`, | ||
), | ||
etcswapV3NftManagerAddress: (network: string) => | ||
ConfigManagerV2.getInstance().get( | ||
`etcswap.contractAddresses.${network}.etcswapV3NftManagerAddress`, | ||
), | ||
tradingTypes: (type: string) => { | ||
return type === 'swap' ? ['AMM'] : ['AMM_LP']; | ||
}, | ||
useRouter: ConfigManagerV2.getInstance().get(`etcswap.useRouter`), | ||
feeTier: ConfigManagerV2.getInstance().get(`etcswap.feeTier`), | ||
}, | ||
}; | ||
} |
Oops, something went wrong.