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 5 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
"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",
"@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 +50,8 @@
"@pancakeswap/v3-sdk": "^3.7.0",
"@pangolindex/sdk": "^1.1.0",
"@perp/sdk-curie": "^1.16.0",
"@shibaswap/sdk": "^1.1.17",
"@chewyswap/sdk": "git+https://github.com/PooDoge/chewyswap-sdk#main",
"@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: 8 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,8 @@ export const validateSpender: Validator = mkValidator(
val === 'curve' ||
val === 'carbonamm' ||
val === 'balancer' ||
isAddress(val))
val === 'shibaswap' ||
isAddress(val)),
);

export const validateNonce: Validator = mkValidator(
Expand All @@ -74,33 +75,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
86 changes: 86 additions & 0 deletions src/chains/shibarium/shibarium.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
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';

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 === '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);
}
}
38 changes: 38 additions & 0 deletions src/chains/shibarium/shibarium.validators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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' || isAddress(val)),
);

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

export const validateAllowancesRequest: RequestValidator = mkRequestValidator([
validateAddress,
validateSpender,
validateTokenSymbols,
]);
45 changes: 45 additions & 0 deletions src/connectors/chewyswap/chewyswap.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { ConfigManagerV2 } from '../../services/config-manager-v2';
import { AvailableNetworks } from '../../services/config-manager-types';

export namespace ChewyswapConfig {
export interface NetworkConfig {
allowedSlippage: string;
gasLimitEstimate: number;
ttl: number;
routerAddress: (chain: string, network: string) => string;
tradingTypes: Array<string>;
chainType: string;
availableNetworks: Array<AvailableNetworks>;
}

export const config: NetworkConfig = {
allowedSlippage: ConfigManagerV2.getInstance().get(
'chewyswap.allowedSlippage',
),
gasLimitEstimate: ConfigManagerV2.getInstance().get(
'chewyswap.gasLimitEstimate',
),
ttl: ConfigManagerV2.getInstance().get('chewyswap.ttl'),
routerAddress: (chain: string, network: string) => {
const address = ConfigManagerV2.getInstance().get(
'chewyswap.contractAddresses.' +
chain +
'.' +
network +
'.routerAddress',
);
if (address === undefined) {
throw new Error('Router address not found');
}
return address;
},
tradingTypes: ['AMM'],
chainType: 'EVM',
availableNetworks: [
{
chain: 'shibarium',
networks: ['mainnet', 'puppynet'],
},
],
};
}
Loading
Loading