Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ChewySwap integration #359

Open
wants to merge 12 commits into
base: development
Choose a base branch
from
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@
"test:scripts": "jest -i --verbose ./test-scripts/*.test.ts"
},
"dependencies": {
"@cosmjs/amino": "^0.32.2",
"@balancer-labs/sdk": "^1.1.5",
"@bancor/carbon-sdk": "^0.0.93-DEV",
"@chewyswap/swap-sdk": "^1.0.1",
"@cosmjs/amino": "^0.32.2",
"@cosmjs/proto-signing": "^0.31.1",
"@cosmjs/stargate": "^0.31.1",
"@cosmjs/tendermint-rpc": "^0.32.2",
Expand All @@ -50,6 +51,7 @@
"@pancakeswap/v3-sdk": "^3.7.0",
"@pangolindex/sdk": "^1.1.0",
"@perp/sdk-curie": "^1.16.0",
"@shibaswap/sdk": "^1.1.18",
"@sushiswap/sdk": "^5.0.0-canary.116",
"@taquito/rpc": "^17.0.0",
"@taquito/signer": "^17.0.0",
Expand Down Expand Up @@ -169,4 +171,4 @@
"resolutions": {
"web3-utils": "1.7.3"
}
}
}
15 changes: 14 additions & 1 deletion src/chains/ethereum/ethereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import { OpenoceanConfig } from '../../connectors/openocean/openocean.config';
import { Curve } from '../../connectors/curve/curve';
import { CarbonConfig } from '../../connectors/carbon/carbon.config';
import { BalancerConfig } from '../../connectors/balancer/balancer.config';
import { ShibaswapConfig } from '../../connectors/shibaswap/shibaswap.config';
import { ChewyswapConfig } from '../../connectors/chewyswap/chewyswap.config';

// MKR does not match the ERC20 perfectly so we need to use a separate ABI.
const MKR_ADDRESS = '0x9f8f72aa9304c8b593d555f12ef6589cc3a579a2';
Expand Down Expand Up @@ -191,6 +193,16 @@ export class Ethereum extends EthereumBase implements Ethereumish {
spender = PancakeSwapConfig.config.pancakeswapV3NftManagerAddress(
this._chain,
);
} else if (reqSpender === 'shibaswap') {
spender = ShibaswapConfig.config.routerAddress(
this.chainName,
this._chain,
);
} else if (reqSpender === 'chewyswap') {
spender = ChewyswapConfig.config.routerAddress(
this.chainName,
this._chain,
);
} else if (reqSpender === 'sushiswap') {
spender = SushiswapConfig.config.sushiswapRouterAddress(
this.chainName,
Expand All @@ -199,7 +211,8 @@ export class Ethereum extends EthereumBase implements Ethereumish {
} else if (reqSpender === 'uniswapLP') {
spender = UniswapConfig.config.uniswapV3NftManagerAddress(
this.chainName,
this._chain);
this._chain,
);
} else if (reqSpender === 'carbonamm') {
spender = CarbonConfig.config.carbonContractsConfig(
'ethereum',
Expand Down
16 changes: 9 additions & 7 deletions src/chains/ethereum/ethereum.validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const isAddress = (str: string): boolean => {
export const validateAddress: Validator = mkValidator(
'address',
invalidAddressError,
(val) => typeof val === 'string' && isAddress(val)
(val) => typeof val === 'string' && isAddress(val),
);

// given a request, look for a key called spender that is 'uniswap' or an Ethereum address
Expand All @@ -65,7 +65,9 @@ export const validateSpender: Validator = mkValidator(
val === 'curve' ||
val === 'carbonamm' ||
val === 'balancer' ||
isAddress(val))
val === 'shibaswap' ||
val === 'chewyswap' ||
isAddress(val)),
);

export const validateNonce: Validator = mkValidator(
Expand All @@ -74,33 +76,33 @@ export const validateNonce: Validator = mkValidator(
(val) =>
typeof val === 'undefined' ||
(typeof val === 'number' && val >= 0 && Number.isInteger(val)),
true
true,
);

export const validateMaxFeePerGas: Validator = mkValidator(
'maxFeePerGas',
invalidMaxFeePerGasError,
(val) => typeof val === 'string' && isNaturalNumberString(val),
true
true,
);

export const validateMaxPriorityFeePerGas: Validator = mkValidator(
'maxPriorityFeePerGas',
invalidMaxPriorityFeePerGasError,
(val) => typeof val === 'string' && isNaturalNumberString(val),
true
true,
);

export const validateChain: Validator = mkValidator(
'chain',
invalidChainError,
(val) => typeof val === 'string'
(val) => typeof val === 'string',
);

export const validateNetwork: Validator = mkValidator(
'network',
invalidNetworkError,
(val) => typeof val === 'string'
(val) => typeof val === 'string',
);

// request types and corresponding validators
Expand Down
89 changes: 89 additions & 0 deletions src/chains/shibarium/shibarium.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
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 getShibariumConfig } 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 { ShibaswapConfig } from '../../connectors/shibaswap/shibaswap.config';
import { ChewyswapConfig } from '../../connectors/chewyswap/chewyswap.config';

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

private constructor(network: string) {
const config = getShibariumConfig('shibarium', network);
super(
'shibarium',
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.controller = EVMController;
}

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

return Shibarium._instances[network];
}

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

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 === 'chewyswap') {
spender = ChewyswapConfig.config.routerAddress('shibarium', this._chain);
} else if (reqSpender === 'shibaswap') {
spender = ShibaswapConfig.config.routerAddress('shibarium', 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);
}
}
40 changes: 40 additions & 0 deletions src/chains/shibarium/shibarium.validators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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 Shibarium address (0x followed by 40 hexidecimal characters).';

// given a request, look for a key called spender that is 'uniswap', 'sushi' or an Ethereum address
export const validateSpender: Validator = mkValidator(
'spender',
invalidSpenderError,
(val) =>
typeof val === 'string' &&
(val === 'shibaswap' || val === 'chewyswap' || isAddress(val)),
);

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

export const validateAllowancesRequest: RequestValidator = mkRequestValidator([
validateAddress,
validateSpender,
validateTokenSymbols,
]);
Loading