Skip to content

Commit

Permalink
feat: combine Usual 1:1 pricing in one integration
Browse files Browse the repository at this point in the history
  • Loading branch information
KanievskyiDanylo committed Dec 3, 2024
1 parent 8a8dfcf commit 16ea063
Show file tree
Hide file tree
Showing 9 changed files with 828 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/dex/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ import { ConcentratorArusd } from './concentrator-arusd/concentrator-arusd';
import { FxProtocolRusd } from './fx-protocol-rusd/fx-protocol-rusd';
import { AaveGsm } from './aave-gsm/aave-gsm';
import { LitePsm } from './lite-psm/lite-psm';
import { UsualBond } from './usual-bond/usual-bond';
import { StkGHO } from './stkgho/stkgho';
import { SkyConverter } from './sky-converter/sky-converter';
import { UsualMSmartM } from './usual-m-smart-m/usual-m-smart-m';
import { UsualMUsd0 } from './usual-m-usd0/usual-m-usd0';
import { UsualBond } from './usual/usual-bond';
import { UsualMSmartM } from './usual/usual-m-smart-m';
import { UsualMUsd0 } from './usual/usual-m-usd0';

const LegacyDexes = [
CurveV2,
Expand Down
10 changes: 10 additions & 0 deletions src/dex/usual/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Address } from '../../types';

export type PoolState = {};

export type UsualBondData = {};

export type DexParams = {
fromToken: { address: Address; decimals: number };
toToken: { address: Address; decimals: number };
};
69 changes: 69 additions & 0 deletions src/dex/usual/usual-bond.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import {
Address,
NumberAsString,
DexExchangeParam,
DexConfigMap,
} from '../../types';
import { SwapSide, Network } from '../../constants';
import { IDexHelper } from '../../dex-helper/idex-helper';
import { UsualBondData, DexParams } from './types';
import { Interface, JsonFragment } from '@ethersproject/abi';
import USD0PP_ABI from '../../abi/usual-bond/usd0pp.abi.json';
import { Usual } from './usual';
import { getDexKeysWithNetwork } from '../../utils';

const Config: DexConfigMap<DexParams> = {
UsualBond: {
[Network.MAINNET]: {
fromToken: {
address: '0x73a15fed60bf67631dc6cd7bc5b6e8da8190acf5',
decimals: 18,
},
toToken: {
address: '0x35d8949372d46b7a3d5a56006ae77b215fc69bc0',
decimals: 18,
},
},
},
};

export class UsualBond extends Usual {
public static dexKeysWithNetwork: { key: string; networks: Network[] }[] =
getDexKeysWithNetwork(Config);

usd0ppIface: Interface;

constructor(
readonly network: Network,
readonly dexKey: string,
readonly dexHelper: IDexHelper,
) {
super(network, dexKey, dexHelper, Config[dexKey][network]);
this.usd0ppIface = new Interface(USD0PP_ABI as JsonFragment[]);
}

async getDexParam(
srcToken: Address,
destToken: Address,
srcAmount: NumberAsString,
destAmount: NumberAsString,
recipient: Address,
data: UsualBondData,
side: SwapSide,
): Promise<DexExchangeParam> {
if (this.isFromToken(srcToken) && this.isToToken(destToken)) {
const exchangeData = this.usd0ppIface.encodeFunctionData('mint', [
srcAmount,
]);

return {
needWrapNative: false,
dexFuncHasRecipient: false,
exchangeData,
targetExchange: this.config.toToken.address,
returnAmountPos: undefined,
};
}
throw new Error('LOGIC ERROR');
}
}
122 changes: 122 additions & 0 deletions src/dex/usual/usual-e2e.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/* eslint-disable no-console */
import dotenv from 'dotenv';
dotenv.config();

import { testE2E } from '../../../tests/utils-e2e';
import { Tokens, Holders } from '../../../tests/constants-e2e';
import { Network, ContractMethod, SwapSide } from '../../constants';
import { StaticJsonRpcProvider } from '@ethersproject/providers';
import { generateConfig } from '../../config';

function testForNetwork(
network: Network,
dexKey: string,
tokenASymbol: string,
tokenBSymbol: string,
tokenAAmount: string,
tokenBAmount: string,
) {
const provider = new StaticJsonRpcProvider(
generateConfig(network).privateHttpProvider,
network,
);
const tokens = Tokens[network];
const holders = Holders[network];

const sideToContractMethods = new Map([
[SwapSide.SELL, [ContractMethod.swapExactAmountIn]],
]);

describe(`${network}`, () => {
sideToContractMethods.forEach((contractMethods, side) =>
describe(`${side}`, () => {
contractMethods.forEach((contractMethod: ContractMethod) => {
describe(`${contractMethod}`, () => {
it(`${tokenASymbol} -> ${tokenBSymbol}`, async () => {
await testE2E(
tokens[tokenASymbol],
tokens[tokenBSymbol],
holders[tokenASymbol],
side === SwapSide.SELL ? tokenAAmount : tokenBAmount,
side,
dexKey,
contractMethod,
network,
provider,
);
});
});
});
}),
);
});
}

describe('UsualBond E2E', () => {
const dexKey = 'UsualBond';

describe('Mainnet', () => {
const network = Network.MAINNET;

const tokenASymbol: string = 'USD0';
const tokenBSymbol: string = 'USD0++';

const tokenAAmount: string = '100000';
const tokenBAmount: string = '100000';

testForNetwork(
network,
dexKey,
tokenASymbol,
tokenBSymbol,
tokenAAmount,
tokenBAmount,
);
});
});

describe('UsualMSmartM E2E', () => {
const dexKey = 'UsualMSmartM';

describe('Mainnet', () => {
const network = Network.MAINNET;

const tokenASymbol: string = 'SmartM';
const tokenBSymbol: string = 'UsualM';

const tokenAAmount: string = '100000';
const tokenBAmount: string = '100000';

testForNetwork(
network,
dexKey,
tokenASymbol,
tokenBSymbol,
tokenAAmount,
tokenBAmount,
);
});
});

describe('UsualM<>Usd0 E2E', () => {
const dexKey = 'UsualMUsd0';

describe('Mainnet', () => {
const network = Network.MAINNET;

const tokenASymbol: string = 'UsualM';
const tokenBSymbol: string = 'USD0';

const tokenAAmount: string = '100000';
const tokenBAmount: string = '100000';

testForNetwork(
network,
dexKey,
tokenASymbol,
tokenBSymbol,
tokenAAmount,
tokenBAmount,
);
});
});
Loading

0 comments on commit 16ea063

Please sign in to comment.