Skip to content

Commit

Permalink
Merge pull request #256 from bancorprotocol/tiagofilipenunes-feat/car…
Browse files Browse the repository at this point in the history
…bondefi

feat/carbondefi
  • Loading branch information
fengtality authored Feb 2, 2024
2 parents 00a91a6 + 695facb commit bc7310b
Show file tree
Hide file tree
Showing 18 changed files with 3,487 additions and 6 deletions.
9 changes: 9 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ module.exports = {
globalTeardown: '<rootDir>/test/teardown.ts',
moduleNameMapper: {
eccrypto: '<rootDir>/test/mock/eccrypto-mock.js',
// Add carbon sdk subpath imports that are unsupported until jest v29.4.0
'@bancor/carbon-sdk/strategy-management':
'<rootDir>/node_modules/@bancor/carbon-sdk/dist/strategy-management/index.cjs',
'@bancor/carbon-sdk/utils':
'<rootDir>/node_modules/@bancor/carbon-sdk/dist/utils/index.cjs',
'@bancor/carbon-sdk/contracts-api':
'<rootDir>/node_modules/@bancor/carbon-sdk/dist/contracts-api/index.cjs',
'@bancor/carbon-sdk/chain-cache':
'<rootDir>/node_modules/@bancor/carbon-sdk/dist/chain-cache/index.cjs',
},
testPathIgnorePatterns: ['/node_modules/', 'test-helpers'],
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"test:scripts": "jest -i --verbose ./test-scripts/*.test.ts"
},
"dependencies": {
"@bancor/carbon-sdk": "^0.0.93-DEV",
"@cosmjs/proto-signing": "^0.31.1",
"@cosmjs/stargate": "^0.31.1",
"@crocswap/sdk": "^2.4.5",
Expand Down Expand Up @@ -163,4 +164,4 @@
"resolutions": {
"web3-utils": "1.7.3"
}
}
}
12 changes: 12 additions & 0 deletions src/amm/amm.controllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ import {
poolPrice as uniswapV3PoolPrice,
estimateGas as uniswapEstimateGas,
} from '../connectors/uniswap/uniswap.controllers';
import {
price as carbonPrice,
trade as carbonTrade,
estimateGas as carbonEstimateGas,
} from '../connectors/carbon/carbon.controllers';
import {
price as refPrice,
trade as refTrade,
Expand Down Expand Up @@ -81,6 +86,7 @@ import { Algorand } from '../chains/algorand/algorand';
import { Tinyman } from '../connectors/tinyman/tinyman';
import { Plenty } from '../connectors/plenty/plenty';
import { QuipuSwap } from '../connectors/quipuswap/quipuswap';
import { Carbonamm } from '../connectors/carbon/carbonAMM';

export async function price(req: PriceRequest): Promise<PriceResponse> {
const chain = await getInitializedChain<
Expand All @@ -97,6 +103,8 @@ export async function price(req: PriceRequest): Promise<PriceResponse> {
return plentyPrice(<Tezosish>chain, connector, req);
} else if (connector instanceof QuipuSwap) {
return quipuPrice(<Tezosish>chain, connector, req);
} else if (connector instanceof Carbonamm) {
return carbonPrice(<Ethereumish>chain, connector, req);
} else if ('routerAbi' in connector) {
// we currently use the presence of routerAbi to distinguish Uniswapish from RefAMMish
return uniswapPrice(<Ethereumish>chain, connector, req);
Expand All @@ -122,6 +130,8 @@ export async function trade(req: TradeRequest): Promise<TradeResponse> {
return plentyTrade(<Tezosish>chain, connector, req);
} else if (connector instanceof QuipuSwap) {
return quipuTrade(<Tezosish>chain, connector, req);
} else if (connector instanceof Carbonamm) {
return carbonTrade(<Ethereumish>chain, connector, req);
} else if ('routerAbi' in connector) {
// we currently use the presence of routerAbi to distinguish Uniswapish from RefAMMish
return uniswapTrade(<Ethereumish>chain, connector, req);
Expand Down Expand Up @@ -211,6 +221,8 @@ export async function estimateGas(
return plentyEstimateGas(<Tezosish>chain, connector);
} else if (connector instanceof QuipuSwap) {
return quipuEstimateGas(<Tezosish>chain, connector);
} else if (connector instanceof Carbonamm) {
return carbonEstimateGas(<Ethereumish>chain, connector);
} else if ('routerAbi' in connector) {
// we currently use the presence of routerAbi to distinguish Uniswapish from RefAMMish
return uniswapEstimateGas(<Ethereumish>chain, connector);
Expand Down
6 changes: 6 additions & 0 deletions src/chains/ethereum/ethereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { Perp } from '../../connectors/perp/perp';
import { SushiswapConfig } from '../../connectors/sushiswap/sushiswap.config';
import { OpenoceanConfig } from '../../connectors/openocean/openocean.config';
import { Curve } from '../../connectors/curve/curve';
import { CarbonConfig } from '../../connectors/carbon/carbon.config';

// MKR does not match the ERC20 perfectly so we need to use a separate ABI.
const MKR_ADDRESS = '0x9f8f72aa9304c8b593d555f12ef6589cc3a579a2';
Expand Down Expand Up @@ -188,6 +189,11 @@ export class Ethereum extends EthereumBase implements Ethereumish {
);
} else if (reqSpender === 'uniswapLP') {
spender = UniswapConfig.config.uniswapV3NftManagerAddress(this._chain);
} else if (reqSpender === 'carbonamm') {
spender = CarbonConfig.config.carbonContractsConfig(
'ethereum',
this._chain
).carbonControllerAddress;
} else if (reqSpender === 'perp') {
const perp = Perp.getInstance(this._chain, 'optimism');
if (!perp.ready()) {
Expand Down
1 change: 1 addition & 0 deletions src/chains/ethereum/ethereum.validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export const validateSpender: Validator = mkValidator(
val === 'pancakeswapLP' ||
val === 'xsswap' ||
val === 'curve' ||
val === 'carbonamm' ||
isAddress(val))
);

Expand Down
58 changes: 58 additions & 0 deletions src/connectors/carbon/carbon.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { ContractsConfig } from '@bancor/carbon-sdk/contracts-api';
import { MatchType } from '@bancor/carbon-sdk';
import { AvailableNetworks } from '../../services/config-manager-types';
import { ConfigManagerV2 } from '../../services/config-manager-v2';

export namespace CarbonConfig {
export interface NetworkConfig {
allowedSlippage: string;
gasLimitEstimate: number;
ttl: number;
tradingTypes: Array<string>;
chainType: string;
matchType: MatchType;
availableNetworks: Array<AvailableNetworks>;
carbonContractsConfig: (
chain: string,
network: string
) => Required<ContractsConfig>;
}

export const config: NetworkConfig = {
allowedSlippage: ConfigManagerV2.getInstance().get(
`carbon.allowedSlippage`
),
gasLimitEstimate: ConfigManagerV2.getInstance().get(
`carbon.gasLimitEstimate`
),
ttl: ConfigManagerV2.getInstance().get(`carbon.ttl`),
tradingTypes: ['AMM'],
chainType: 'EVM',
matchType: MatchType.Fast,
availableNetworks: [
{
chain: 'ethereum',
networks: Object.keys(
ConfigManagerV2.getInstance().get('carbon.contractAddresses.ethereum')
).filter((network) =>
Object.keys(
ConfigManagerV2.getInstance().get('ethereum.networks')
).includes(network)
),
},
],
carbonContractsConfig: (chain: string, network: string) => {
return {
carbonControllerAddress: ConfigManagerV2.getInstance().get(
`carbon.contractAddresses.${chain}.${network}.carbonControllerAddress`
),
multiCallAddress: ConfigManagerV2.getInstance().get(
`carbon.contractAddresses.${chain}.${network}.multiCallAddress`
),
voucherAddress: ConfigManagerV2.getInstance().get(
`carbon.contractAddresses.${chain}.${network}.voucherAddress`
),
};
},
};
}
Loading

0 comments on commit bc7310b

Please sign in to comment.