Skip to content

Commit

Permalink
fix: loan tests (#1425)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsimao authored Jul 20, 2023
1 parent c26b788 commit c290fb3
Show file tree
Hide file tree
Showing 24 changed files with 824 additions and 1,287 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ const CollateralForm = ({ asset, variant, isOpen, onSigning }: CollateralFormPro
/>
<AuthCTA type='submit' size='large' disabled={isBtnDisabled} loading={transaction.isLoading}>
{variant === 'enable'
? t('use_ticker_as_collateral', { ticker: asset.currency.ticker })
: t('disable_ticker', { ticker: asset.currency.ticker })}
? t('loans.use_ticker_as_collateral', { ticker: asset.currency.ticker })
: t('loans.disable_ticker', { ticker: asset.currency.ticker })}
</AuthCTA>
</Flex>
</form>
Expand Down
21 changes: 21 additions & 0 deletions src/test/mocks/@interlay/interbtc-api/extrinsic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { ExtrinsicData } from '@interlay/interbtc-api';
import { SubmittableExtrinsic } from '@polkadot/api/types';
import { ISubmittableResult } from '@polkadot/types/types';

const EXTRINSIC: SubmittableExtrinsic<'promise', ISubmittableResult> = ({
signAndSend: jest.fn().mockImplementation(async (_a, _b, cb) => {
return new Promise((resolve) => {
resolve(jest.fn());

setTimeout(() => {
cb({ status: { isReady: true, isInBlock: true, isFinalized: true, type: 'Finalized' } });
}, 1);
});
})
} as unknown) as SubmittableExtrinsic<'promise', ISubmittableResult>;

const EXTRINSIC_DATA: ExtrinsicData = {
extrinsic: EXTRINSIC
};

export { EXTRINSIC, EXTRINSIC_DATA };
79 changes: 6 additions & 73 deletions src/test/mocks/@interlay/interbtc-api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import { Signer } from '@polkadot/types/types';

import {
MOCK_AMM,
MOCK_API,
MOCK_LOANS,
MOCK_SYSTEM,
MOCK_TOKENS,
MOCK_TRANSACTION,
mockApiCreateType,
mockBtcRelayGetLatestBlockHeight,
mockChainType,
mockElectrsAPIGetLatestBlockHeight,
mockFeeGetIssueFee,
mockFeeGetIssueGriefingCollateralRate,
Expand All @@ -29,36 +30,13 @@ import {
mockRedeemGetMaxBurnableTokens,
mockRedeemGetPremiumRedeemFeeRate,
mockRedeemRequest,
mockSystemChain,
mockTokensBalance,
mockTokensSubscribeToBalance,
mockTokensTotal,
mockVaultsGet,
mockVaultsGetPremiumRedeemVaults,
mockVaultsGetVaultsWithIssuableTokens,
mockVaultsGetVaultsWithRedeemableTokens
} from './parachain';
import { mockGetForeignAssets } from './parachain/assetRegistry';
import { mockGetStakedBalance, mockVotingBalance } from './parachain/escrow';
import {
mockBorrow,
mockClaimAllSubsidyRewards,
mockDisableAsCollateral,
mockEnableAsCollateral,
mockGetAccountSubsidyRewards,
mockGetBorrowPositionsOfAccount,
mockGetLendingStats,
mockGetLendPositionsOfAccount,
mockGetLendTokenExchangeRates,
mockGetLendTokens,
mockGetLoanAssets,
mockLend,
mockRepay,
mockRepayAll,
mockWithdraw,
mockWithdrawAll
} from './parachain/loans';
import { mockClaimVesting, mockVestingSchedules } from './parachain/vesting';

const mockSetAccount = jest.fn((_account: AddressOrPair, _signer?: Signer) => undefined);

Expand All @@ -68,31 +46,7 @@ const mockCollateralCurrencies = [Polkadot, Interlay];
const mockInterBtcApi: Partial<Record<keyof InterBtcApi, unknown>> = {
removeAccount: jest.fn(),
setAccount: mockSetAccount,
api: {
createType: mockApiCreateType,
rpc: {
system: {
chain: mockSystemChain,
chainType: mockChainType
}
},
on: jest.fn(),
query: {
vesting: {
vestingSchedules: mockVestingSchedules as any
},
oracle: {
aggregate: {
keys: jest.fn().mockReturnValue([])
}
}
},
tx: {
vesting: {
claim: mockClaimVesting
}
}
},
api: MOCK_API.PROMISE,
assetRegistry: {
getForeignAssets: mockGetForeignAssets
},
Expand All @@ -111,24 +65,7 @@ const mockInterBtcApi: Partial<Record<keyof InterBtcApi, unknown>> = {
getRequestLimits: mockIssueGetRequestLimits,
request: mockIssueRequest
},
loans: {
getLendTokens: mockGetLendTokens,
getLendPositionsOfAccount: mockGetLendPositionsOfAccount,
getBorrowPositionsOfAccount: mockGetBorrowPositionsOfAccount,
getLoanAssets: mockGetLoanAssets,
getAccruedRewardsOfAccount: mockGetAccountSubsidyRewards,
lend: mockLend,
withdraw: mockWithdraw,
withdrawAll: mockWithdrawAll,
borrow: mockBorrow,
repay: mockRepay,
repayAll: mockRepayAll,
enableAsCollateral: mockEnableAsCollateral,
disableAsCollateral: mockDisableAsCollateral,
claimAllSubsidyRewards: mockClaimAllSubsidyRewards,
getLendingStats: mockGetLendingStats,
getLendTokenExchangeRates: mockGetLendTokenExchangeRates
},
loans: MOCK_LOANS.MODULE,
oracle: {
getExchangeRate: mockOracleGetExchangeRate
},
Expand All @@ -143,11 +80,7 @@ const mockInterBtcApi: Partial<Record<keyof InterBtcApi, unknown>> = {
request: mockRedeemRequest
},
system: MOCK_SYSTEM.MODULE,
tokens: {
balance: mockTokensBalance,
total: mockTokensTotal,
subscribeToBalance: mockTokensSubscribeToBalance
},
tokens: MOCK_TOKENS.MODULE,
vaults: {
get: mockVaultsGet,
getVaultsWithIssuableTokens: mockVaultsGetVaultsWithIssuableTokens,
Expand Down
10 changes: 5 additions & 5 deletions src/test/mocks/@interlay/interbtc-api/parachain/amm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Big from 'big.js';

import { GOVERNANCE_TOKEN, RELAY_CHAIN_NATIVE_TOKEN, WRAPPED_TOKEN } from '@/config/relay-chains';

import { DEFAULT_EXTRINSIC } from './extrinsic';
import { EXTRINSIC_DATA } from '../extrinsic';

const LP_TOKEN_A_NAME = `LP ${GOVERNANCE_TOKEN.ticker}-${RELAY_CHAIN_NATIVE_TOKEN.ticker}`;

Expand Down Expand Up @@ -154,10 +154,10 @@ const MODULE: Record<keyof AMMAPI, jest.Mock<any, any>> = {
getLpTokens: jest.fn().mockResolvedValue(LP_TOKENS),
getOptimalTrade: jest.fn().mockReturnValue(TRADE),
// MUTATIONS
addLiquidity: jest.fn().mockResolvedValue(DEFAULT_EXTRINSIC),
removeLiquidity: jest.fn().mockResolvedValue(DEFAULT_EXTRINSIC),
claimFarmingRewards: jest.fn().mockResolvedValue(DEFAULT_EXTRINSIC),
swap: jest.fn().mockResolvedValue(DEFAULT_EXTRINSIC)
addLiquidity: jest.fn().mockResolvedValue(EXTRINSIC_DATA),
removeLiquidity: jest.fn().mockResolvedValue(EXTRINSIC_DATA),
claimFarmingRewards: jest.fn().mockResolvedValue(EXTRINSIC_DATA),
swap: jest.fn().mockResolvedValue(EXTRINSIC_DATA)
};

const MOCK_AMM = {
Expand Down
61 changes: 55 additions & 6 deletions src/test/mocks/@interlay/interbtc-api/parachain/api.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,63 @@
import { ApiPromise } from '@polkadot/api';
import { Text, TypeRegistry } from '@polkadot/types';
import { Registry } from '@polkadot/types/types';
import Big from 'big.js';

const mockApiCreateType = jest.fn(<T extends unknown>(type: string, data: T) => data);
import { EXTRINSIC } from '../extrinsic';

const mockRegistry = ({ chainDecimals: [], chainSS58: 0, chainTokens: [] } as unknown) as Registry;

const mockSystemChain = jest.fn().mockReturnValue(new Text(mockRegistry, 'interBTC')) as any;
const REGISTRY = ({ chainDecimals: [], chainSS58: 0, chainTokens: [] } as unknown) as Registry;
const SYSTEM_CHAIN = new Text(REGISTRY, 'interBTC');

const registry = new TypeRegistry();
const CHAIN_TYPE = registry.createType('ChainType', 'Live');

const VESTING_SCHEDULES = {
EMPTY: [],
FULL: [{ start: new Big(0), period: new Big(0), periodCount: new Big(1), perPeriod: new Big(1) }]
};

// add here data that is being used in tests
const DATA = { VESTING_SCHEDULES };

// add here mocks that are being manipulated in tests
const MODULE = {
vestingSchedules: jest.fn().mockReturnValue(VESTING_SCHEDULES.EMPTY),
claimVesting: jest.fn().mockReturnValue(EXTRINSIC)
};

const mockChainType = jest.fn().mockReturnValue(registry.createType('ChainType', 'Live')) as any;
// maps module to ApiPromise
const PROMISE: Partial<Record<keyof ApiPromise, unknown>> = {
on: jest.fn(),
createType: jest.fn().mockImplementation((_, data) => data),
rpc: {
system: {
chain: jest.fn().mockReturnValue(SYSTEM_CHAIN),
chainType: jest.fn().mockReturnValue(CHAIN_TYPE)
}
},
query: {
vesting: {
vestingSchedules: MODULE.vestingSchedules
},
oracle: {
aggregate: {
keys: jest.fn().mockReturnValue([])
}
}
},
tx: {
vesting: {
claim: MODULE.claimVesting
},
multiTransactionPayment: {
withFeeSwapPath: jest.fn().mockReturnValue(EXTRINSIC)
}
}
};

export { mockApiCreateType, mockChainType, mockSystemChain };
const MOCK_API = {
DATA,
MODULE,
PROMISE
};
export { MOCK_API };
17 changes: 0 additions & 17 deletions src/test/mocks/@interlay/interbtc-api/parachain/extrinsic.ts

This file was deleted.

1 change: 1 addition & 0 deletions src/test/mocks/@interlay/interbtc-api/parachain/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export * from './btcRelay';
export * from './electrsAPI';
export * from './fee';
export * from './issue';
export * from './loans';
export * from './oracle';
export * from './redeem';
export * from './system';
Expand Down
Loading

2 comments on commit c290fb3

@vercel
Copy link

@vercel vercel bot commented on c290fb3 Jul 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on c290fb3 Jul 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.