Skip to content

Commit

Permalink
Liquidation rewards (#440)
Browse files Browse the repository at this point in the history
* Stop pools flashing on home screen because of loading state

* Reduce props drilling

* Update contracts

* Add types to contracts

* Back to public pyth

* Display all rewards distributors including pool-level ones

* Fix incorrect typescript part 1

* Fix update contracts

* More fixes for typescript

* More fixes for typescript and cleanup unused code

* Deps

* Cleanup test

* Cannot set incorrect timer
  • Loading branch information
noisekit authored Sep 6, 2024
1 parent 0116924 commit 135b7f0
Show file tree
Hide file tree
Showing 68 changed files with 733 additions and 1,808 deletions.
Binary file not shown.
Binary file not shown.
Binary file removed .yarn/cache/clsx-npm-2.1.1-96125b98be-cdfb57fa6c.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
116 changes: 115 additions & 1 deletion contracts/index.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,115 @@
declare module '@snx-v3/contracts';
declare module '@snx-v3/contracts' {
function importCoreProxy(
chainId?: number,
preset?: string
): Promise<{ address: string; abi: string[] }>;

function importAccountProxy(
chainId?: number,
preset?: string
): Promise<{ address: string; abi: string[] }>;

function importUSDProxy(
chainId?: number,
preset?: string
): Promise<{ address: string; abi: string[] }>;

function importV2x(
chainId?: number,
preset?: string
): Promise<{ address: string; abi: string[] }>;

function importSpotMarketProxy(
chainId?: number,
preset?: string
): Promise<{ address: string; abi: string[] }>;

function importMulticall3(
chainId?: number,
preset?: string
): Promise<{ address: string; abi: string[] }>;

function importLegacyMarket(
chainId?: number,
preset?: string
): Promise<{ address: string; abi: string[] }>;

function importOracleManagerProxy(
chainId?: number,
preset?: string
): Promise<{ address: string; abi: string[] }>;

function importPythERC7412Wrapper(
chainId?: number,
preset?: string
): Promise<{ address: string; abi: string[] }>;

function importRewardsDistributors(
chainId?: number,
preset?: string
): Promise<
{
address: string;
name: string;
poolId: string;

// undefined for Pool-level distributors
collateralType?: {
address: string;
symbol: string;
name: string;
decimals: number;
};

payoutToken: {
address: string;
symbol: string;
name: string;
decimals: number;
};
rewardManager: string;
isRegistered: boolean;
}[]
>;

function importExtras(
chainId?: number,
preset?: string
): Promise<{
[key: string]: string;
}>;

function importCollateralTokens(
chainId?: number,
preset?: string
): Promise<
{
address: string;
symbol: string;
name: string;
decimals: number;
depositingEnabled: boolean;
issuanceRatioD18: string;
liquidationRatioD18: string;
liquidationRewardD18: string;
oracleNodeId: string;
tokenAddress: string;
minDelegationD18: string;
}[]
>;

function importSystemToken(
chainId?: number,
preset?: string
): Promise<{
address: string;
symbol: string;
name: string;
decimals: number;
}>;

function importAllErrors(
chainId?: number,
preset?: string
): Promise<{ address: string; abi: string[] }>;
}
4 changes: 2 additions & 2 deletions liquidity/components/ClaimModal/ClaimModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ export const ClaimModal: React.FC<{

const { txnStatus } = txnState;

if (!params.poolId || !params.accountId || !collateralType)
if (!(params.poolId && params.accountId && collateralType && systemToken))
return (
<Flex gap={4} flexDirection="column">
<Skeleton maxW="232px" width="100%" height="20px" />
Expand All @@ -217,7 +217,7 @@ export const ClaimModal: React.FC<{
onClose();
}}
isOpen={isOpen}
symbol={isBase ? collateralType.symbol : systemToken?.symbol}
symbol={isBase ? collateralType.symbol : systemToken.symbol}
/>
);
};
2 changes: 1 addition & 1 deletion liquidity/components/RepayModal/RepayModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ export const RepayModal: React.FC<{
send(Events.RUN);
}, [onClose, send, state]);

if (!params.poolId || !params.accountId || !collateralType)
if (!(params.poolId && params.accountId && collateralType && systemToken))
return (
<Flex gap={4} flexDirection="column">
<Skeleton maxW="232px" width="100%" height="20px" />
Expand Down
6 changes: 3 additions & 3 deletions liquidity/components/WithdrawModal/WithdrawModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ export function WithdrawModal({
amount: withdrawAmount,
accountId,
collateralTypeAddress: isDebtWithdrawal
? systemToken.address
: liquidityPosition?.accountCollateral.tokenAddress,
? systemToken?.address
: liquidityPosition?.accountCollateral?.tokenAddress,
});

const { mutation: withdrawAndromeda } = useWithdrawBaseAndromeda({
Expand Down Expand Up @@ -246,7 +246,7 @@ export function WithdrawModal({
amount={withdrawAmount}
isOpen={isOpen}
onClose={onClose}
symbol={isDebtWithdrawal ? systemToken.symbol : collateralType?.symbol}
symbol={isDebtWithdrawal ? systemToken?.symbol : collateralType?.symbol}
state={txState}
onSubmit={onSubmit}
isDebtWithdrawal={isDebtWithdrawal}
Expand Down
6 changes: 2 additions & 4 deletions liquidity/lib/constants/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ export const SESSION_STORAGE_KEYS = {
};

export const offchainMainnetEndpoint =
process.env.PYTH_MAINNET_ENDPOINT ||
'https://hermes-mainnet.rpc.extrnode.com/9b85d7db-f562-48e2-ab56-79c01f212582';
process.env.PYTH_MAINNET_ENDPOINT || 'https://hermes.pyth.network';

export const offchainTestnetEndpoint =
process.env.PYTH_TESTNET_ENDPOINT ||
'https://hermes-mainnet.rpc.extrnode.com/9b85d7db-f562-48e2-ab56-79c01f212582';
process.env.PYTH_TESTNET_ENDPOINT || 'https://hermes.pyth.network';
15 changes: 1 addition & 14 deletions liquidity/lib/formatters/date.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,4 @@
import { convertToReadableInterval, unlockDateString, formatTimeToUnlock } from './date';

describe('convertToReadableInterval', () => {
it('should return null when seconds is 0', () => {
expect(convertToReadableInterval(100, 0)).toStrictEqual({ amount: 100, frequencyString: null });
});

it('should return "20,000 per week" when seconds is 2592000 and amount 94,172.72', () => {
expect(convertToReadableInterval(94_172.72, 2847783)).toStrictEqual({
amount: 20000,
frequencyString: 'per week',
});
});
});
import { unlockDateString, formatTimeToUnlock } from './date';

describe('unlockDateString function', () => {
// Test case 1: accountCollateralUnlockDate is undefined
Expand Down
12 changes: 0 additions & 12 deletions liquidity/lib/formatters/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,6 @@ export function convertSecondsToDisplayString(seconds: number) {
}
}

export function convertToReadableInterval(total: number, seconds: number) {
// If the distribution is immediate, we return immediate, otherwise we return the amount per week
const secondInWeek = 604800;
if (seconds === 0) {
return { amount: total, frequencyString: null };
} else {
const ratio = seconds / secondInWeek;
const amount = total / ratio;
return { amount: Math.floor(amount), frequencyString: 'per week' };
}
}

export const formatTimeToUnlock = (accountCollateralUnlockDate: Date | undefined) => {
if (!accountCollateralUnlockDate || accountCollateralUnlockDate.getTime() <= Date.now()) {
return undefined;
Expand Down
4 changes: 2 additions & 2 deletions liquidity/lib/useBlockchain/useBlockchain.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ export const appMetadata = {
explore: 'https://blog.synthetix.io',
};

export function useProviderForChain(network?: Network | null) {
export function useProviderForChain(network?: Network) {
return useMemo(
() => (network ? new ethers.providers.JsonRpcProvider(network.rpcUrl()) : undefined),
[network]
Expand Down Expand Up @@ -348,7 +348,7 @@ export function useNetwork() {

if (!network) {
return {
network: null,
network: undefined,
setNetwork,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ async function getPythFeedIdsFromCollateralList(
const getPriceUpdates = async (
priceIds: string[],
stalenessTolerance: number,
network: Network | null
network?: Network
) => {
const signedOffchainData = await priceService.getPriceFeedsUpdateData(priceIds);
const updateType = 1;
Expand Down
2 changes: 1 addition & 1 deletion liquidity/lib/useCollateralTypes/useCollateralTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const CollateralTypeSchema = CollateralConfigurationSchema.extend({
export type CollateralType = z.infer<typeof CollateralTypeSchema>;

async function loadCollateralTypes(chainId: number, preset: string): Promise<CollateralType[]> {
const tokenConfigsRaw: CollateralType[] = await importCollateralTokens(chainId, preset);
const tokenConfigsRaw = await importCollateralTokens(chainId, preset);

const tokenConfigs = tokenConfigsRaw
.map((config) => ({
Expand Down
2 changes: 1 addition & 1 deletion liquidity/lib/useCoreProxy/useCoreProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function useCoreProxy({
customNetwork,
isWrite = false,
}: {
customNetwork?: Network | null;
customNetwork?: Network;
isWrite?: boolean;
} = {}) {
const { network } = useNetwork();
Expand Down
2 changes: 1 addition & 1 deletion liquidity/lib/useLegacyMarket/useLegacyMarket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from '@snx-v3/useBlockchain';
import { importLegacyMarket } from '@snx-v3/contracts';

export function useLegacyMarket(customNetwork?: Network | null) {
export function useLegacyMarket(customNetwork?: Network) {
const providerForChain = useProviderForChain(customNetwork);
const { network } = useNetwork();
const provider = useProvider();
Expand Down
1 change: 1 addition & 0 deletions liquidity/lib/useRewards/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"dependencies": {
"@snx-v3/constants": "workspace:*",
"@snx-v3/useBlockchain": "workspace:*",
"@snx-v3/useCollateralTypes": "workspace:*",
"@snx-v3/useCoreProxy": "workspace:*",
"@snx-v3/useMulticall3": "workspace:*",
"@snx-v3/useRewardsDistributors": "workspace:*",
Expand Down
Loading

0 comments on commit 135b7f0

Please sign in to comment.