Skip to content

Commit

Permalink
Feat/update collatearl values (#254)
Browse files Browse the repository at this point in the history
* chore: update cannon version in contracts

* feat: fix all reads stale price issues

* fix: dedup check

* fix: failing test
  • Loading branch information
Rickk137 authored Apr 24, 2024
1 parent 4519e17 commit c3b26ff
Show file tree
Hide file tree
Showing 18 changed files with 104 additions and 76 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion contracts/deployments/8453-andromeda/index.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "synthetix-omnibus",
"version": "15",
"version": "19",
"preset": "andromeda",
"url": "ipfs://QmeSt2mnJKE8qmRhLyYbHQQxDKpsFbcWnw5e7JF4xVbN6k",
"chainId": 8453,
Expand Down
2 changes: 1 addition & 1 deletion contracts/deployments/84532-andromeda/index.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "synthetix-omnibus",
"version": "15",
"version": "22",
"preset": "andromeda",
"url": "ipfs://QmeSt2mnJKE8qmRhLyYbHQQxDKpsFbcWnw5e7JF4xVbN6k",
"chainId": 84532,
Expand Down
2 changes: 1 addition & 1 deletion contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"@ethersproject/abi": "^5.7.0",
"@ethersproject/providers": "^5.7.2",
"@typechain/ethers-v5": "^11.1.2",
"@usecannon/cli": "2.11.21",
"@usecannon/cli": "2.12.5-alpha.0",
"debug": "^4.3.4",
"ethers": "^5.7.2",
"prettier": "^3.1.0",
Expand Down
2 changes: 1 addition & 1 deletion liquidity/lib/parser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"main": "index.ts",
"version": "0.0.1",
"dependencies": {
"viem": "^2.8.9"
"viem": "^2.9.26"
}
}
35 changes: 21 additions & 14 deletions liquidity/lib/useAccountCollateral/useAccountCollateral.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { useQuery } from '@tanstack/react-query';
import { useCoreProxy } from '@snx-v3/useCoreProxy';
import { CoreProxyType } from '@synthetixio/v3-contracts';
import { useNetwork } from '@snx-v3/useBlockchain';
import { useDefaultProvider, useNetwork } from '@snx-v3/useBlockchain';
import { Wei, wei } from '@synthetixio/wei';
import { useCollateralTypes } from '@snx-v3/useCollateralTypes';
import { erc7412Call } from '@snx-v3/withERC7412';
import { useCollateralPriceUpdates } from '../useCollateralPriceUpdates';

export type AccountCollateralType = {
tokenAddress: string;
Expand Down Expand Up @@ -65,33 +66,33 @@ export function useAccountCollateral({
const { network } = useNetwork();

const collateralTypes = useCollateralTypes(includeDelegationOff);

const tokenAddresses = collateralTypes.data?.map((c) => c.tokenAddress) ?? [];
const provider = useDefaultProvider();
const { data: priceUpdateTx } = useCollateralPriceUpdates();

return useQuery({
queryKey: [
`${network?.id}-${network?.preset}`,
'AccountCollateral',
{ accountId },
{ tokens: tokenAddresses },
{ tokens: tokenAddresses, priceUpdateTx: priceUpdateTx?.data },
],
enabled: Boolean(CoreProxy && accountId && tokenAddresses.length > 0),
queryFn: async function () {
if (!CoreProxy || !accountId || tokenAddresses.length < 1 || !network) {
if (!CoreProxy || !accountId || tokenAddresses.length < 1 || !network || !provider) {
throw 'useAccountCollateral should be disabled';
}
const { calls, decoder } = await loadAccountCollateral({
accountId,
tokenAddresses,
CoreProxy,
});
const data = await erc7412Call(
network,
CoreProxy.provider,
calls,
decoder,
'useAccountCollateral'
);
const allCalls = [...calls];
if (priceUpdateTx) {
allCalls.unshift(priceUpdateTx as any);
}

const data = await erc7412Call(network, provider, calls, decoder, 'useAccountCollateral');

return data.map((x) => ({
...x,
Expand All @@ -104,28 +105,34 @@ export function useAccountCollateral({
export function useAccountSpecificCollateral(accountId?: string, collateralAddress?: string) {
const { data: CoreProxy } = useCoreProxy();
const { network } = useNetwork();
const provider = useDefaultProvider();
const { data: priceUpdateTx } = useCollateralPriceUpdates();

return useQuery({
queryKey: [
`${network?.id}-${network?.preset}`,
'AccountSpecificCollateral',
{ accountId },
{ token: collateralAddress },
{ token: collateralAddress, priceUpdateTx: priceUpdateTx?.data },
],
enabled: Boolean(CoreProxy && accountId && collateralAddress),
queryFn: async function () {
if (!CoreProxy || !accountId || !collateralAddress || !network) {
if (!CoreProxy || !accountId || !collateralAddress || !network || !provider) {
throw 'useAccountSpecificCollateral should not be enabled';
}
const { calls, decoder } = await loadAccountCollateral({
accountId,
tokenAddresses: [collateralAddress],
CoreProxy,
});
const allCalls = [...calls];
if (priceUpdateTx) {
allCalls.unshift(priceUpdateTx as any);
}

const data = await erc7412Call(
network,
CoreProxy.provider,
provider,
calls,
decoder,
'useAccountSpecificCollateral'
Expand Down
7 changes: 6 additions & 1 deletion liquidity/lib/useBlockchain/useBlockchain.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,15 @@ export const appMetadata = {
explore: 'https://blog.synthetix.io',
};

export function useProviderForChain(network?: Network) {
export function useProviderForChain(network?: Network | null) {
return network ? new ethers.providers.JsonRpcProvider(network.rpcUrl()) : undefined;
}

export function useDefaultProvider() {
const { network } = useNetwork();
return useProviderForChain(network);
}

export function useWallet() {
const [{ wallet }, conn, disconn] = useConnectWallet();

Expand Down
32 changes: 22 additions & 10 deletions liquidity/lib/useCollateralPrices/useCollateralPrices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { useCoreProxy } from '@snx-v3/useCoreProxy';
import { CoreProxyType } from '@synthetixio/v3-contracts';
import { ZodBigNumber } from '@snx-v3/zod';
import Wei, { wei } from '@synthetixio/wei';
import { useNetwork } from '@snx-v3/useBlockchain';
import { useDefaultProvider, useNetwork } from '@snx-v3/useBlockchain';
import { erc7412Call } from '@snx-v3/withERC7412';
import { useCollateralTypes } from '@snx-v3/useCollateralTypes';
import { useCollateralPriceUpdates } from '../useCollateralPriceUpdates';

const PriceSchema = ZodBigNumber.transform((x) => wei(x));

Expand Down Expand Up @@ -44,25 +45,36 @@ export const useCollateralPrices = () => {
const { data: CoreProxy } = useCoreProxy();
const { data: collateralData } = useCollateralTypes();

const provider = useDefaultProvider();
const collateralAddresses = collateralData?.map((x) => x.tokenAddress);
const { data: priceUpdateTx } = useCollateralPriceUpdates();

return useQuery({
enabled: Boolean(CoreProxy && collateralAddresses && collateralAddresses?.length > 0),
queryKey: [`${network?.id}-${network?.preset}`, 'CollateralPrices', { collateralAddresses }],
queryKey: [
`${network?.id}-${network?.preset}`,
'CollateralPrices',
{ collateralAddresses, priceUpdateTx: priceUpdateTx?.data },
],
queryFn: async () => {
if (!CoreProxy || !collateralAddresses || collateralAddresses.length == 0 || !network) {
if (
!CoreProxy ||
!collateralAddresses ||
collateralAddresses.length == 0 ||
!network ||
!provider
) {
throw 'useCollateralPrices missing required data';
}

const { calls, decoder } = await loadPrices({ CoreProxy, collateralAddresses });

const prices = await erc7412Call(
network,
CoreProxy.provider,
calls,
decoder,
'useCollateralPrices'
);
const allCalls = [...calls];
if (priceUpdateTx) {
allCalls.unshift(priceUpdateTx as any);
}

const prices = await erc7412Call(network, provider, allCalls, decoder, 'useCollateralPrices');

return collateralAddresses.reduce((acc: Record<string, Wei | undefined>, address, i) => {
acc[address] = prices[i];
Expand Down
16 changes: 12 additions & 4 deletions liquidity/lib/usePoolConfiguration/usePoolConfiguration.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { useQuery } from '@tanstack/react-query';
import { useCoreProxy } from '@snx-v3/useCoreProxy';
import { useNetwork } from '@snx-v3/useBlockchain';
import { useDefaultProvider, useNetwork } from '@snx-v3/useBlockchain';
import { z } from 'zod';
import { SmallIntSchema, WeiSchema } from '@snx-v3/zod';
import { ethers } from 'ethers';
import { erc7412Call } from '@snx-v3/withERC7412';
import { fetchPriceUpdates, priceUpdatesToPopulatedTx } from '@snx-v3/fetchPythPrices';
import { useAllCollateralPriceIds } from '@snx-v3/useAllCollateralPriceIds';
import { useCollateralPriceUpdates } from '../useCollateralPriceUpdates';

export const MarketConfigurationSchema = z.object({
id: SmallIntSchema,
Expand All @@ -27,12 +28,14 @@ export const usePoolConfiguration = (poolId?: string) => {
const { network } = useNetwork();
const { data: CoreProxy } = useCoreProxy();
const { data: collateralPriceUpdates } = useAllCollateralPriceIds();
const provider = useDefaultProvider();
const { data: priceUpdateTx } = useCollateralPriceUpdates();

return useQuery({
enabled: Boolean(CoreProxy && poolId && collateralPriceUpdates),
queryKey: [`${network?.id}-${network?.preset}`, 'PoolConfiguration', { poolId }],
queryFn: async () => {
if (!CoreProxy || !poolId || !collateralPriceUpdates || !network) {
if (!CoreProxy || !poolId || !collateralPriceUpdates || !network || !provider) {
throw Error('usePoolConfiguration should not be enabled');
}
const marketsData = await CoreProxy.getPoolConfiguration(ethers.BigNumber.from(poolId));
Expand All @@ -49,10 +52,15 @@ export const usePoolConfiguration = (poolId?: string) => {
const calls = await Promise.all(
markets.map((m) => CoreProxy.populateTransaction.isMarketCapacityLocked(m.id))
);

const allCalls = collateralPriceCalls.concat(calls);
if (priceUpdateTx) {
allCalls.unshift(priceUpdateTx as any);
}
const decoded = await erc7412Call(
network,
CoreProxy.provider,
collateralPriceCalls.concat(calls),
provider,
allCalls,
(encoded) => {
if (!Array.isArray(encoded)) throw Error('Expected array');
return encoded.map((x) =>
Expand Down
15 changes: 12 additions & 3 deletions liquidity/lib/useVaultsData/useVaultsData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import { wei } from '@synthetixio/wei';
import { useQuery } from '@tanstack/react-query';
import { z } from 'zod';
import { ZodBigNumber } from '@snx-v3/zod';
import { useNetwork } from '@snx-v3/useBlockchain';
import { useDefaultProvider, useNetwork } from '@snx-v3/useBlockchain';
import { erc7412Call } from '@snx-v3/withERC7412';
import { useAllCollateralPriceIds } from '@snx-v3/useAllCollateralPriceIds';
import { fetchPriceUpdates, priceUpdatesToPopulatedTx } from '@snx-v3/fetchPythPrices';
import { useCollateralPriceUpdates } from '../useCollateralPriceUpdates';

const VaultCollateralSchema = z
.object({ value: ZodBigNumber, amount: ZodBigNumber })
Expand All @@ -19,6 +20,8 @@ export const useVaultsData = (poolId?: number) => {
const { data: collateralTypes } = useCollateralTypes();
const { data: CoreProxyContract } = useCoreProxy();
const { data: collateralPriceUpdates } = useAllCollateralPriceIds();
const provider = useDefaultProvider();
const { data: priceUpdateTx } = useCollateralPriceUpdates();

return useQuery({
queryKey: [
Expand All @@ -27,6 +30,7 @@ export const useVaultsData = (poolId?: number) => {
{
pool: poolId,
tokens: collateralTypes ? collateralTypes?.map((x) => x.tokenAddress).sort() : [],
priceUpdateTx: priceUpdateTx?.data,
},
],
queryFn: async () => {
Expand All @@ -35,7 +39,8 @@ export const useVaultsData = (poolId?: number) => {
!collateralTypes ||
!poolId ||
!collateralPriceUpdates ||
!network
!network ||
!provider
) {
throw Error('useVaultsData should not be enabled when missing data');
}
Expand All @@ -61,9 +66,13 @@ export const useVaultsData = (poolId?: number) => {

const calls = await Promise.all([collateralPriceUpdateCallsP, collateralCallsP, debtCallsP]);

if (priceUpdateTx) {
calls.unshift(priceUpdateTx as any);
}

return await erc7412Call(
network,
CoreProxyContract.provider,
provider,
calls.flat(),
(multicallResult) => {
if (!Array.isArray(multicallResult)) throw Error('Expected array');
Expand Down
2 changes: 1 addition & 1 deletion liquidity/lib/withERC7412/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"@synthetixio/v3-contracts": "workspace:*",
"@synthetixio/wei": "^2.74.4",
"ethers": "^5.7.2",
"viem": "^2.8.9",
"viem": "^2.9.26",
"zod": "^3.22.4"
}
}
5 changes: 2 additions & 3 deletions liquidity/ui/src/pages/Pool/CollateralSection.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe('Pool page / Collateral section', () => {
it('should render when no vaults data available', () => {
cy.viewport(800, 500);

cy.mount(<CollateralSectionUi isLoading={false} vaultsData={[] as VaultsDataType} />);
cy.mount(<CollateralSectionUi vaultsData={[] as VaultsDataType} />);
cy.get('[data-testid="pool collateral types"]').should(
'include.text',
'Pool Collateralization'
Expand All @@ -22,7 +22,6 @@ describe('Pool page / Collateral section', () => {

cy.mount(
<CollateralSectionUi
isLoading={false}
vaultsData={
[
{
Expand All @@ -39,6 +38,7 @@ describe('Pool page / Collateral section', () => {
},
] as VaultsDataType
}
collateralPriceByAddress={{}}
/>
);
cy.get('[data-testid="pool collateral types"]').should(
Expand All @@ -65,7 +65,6 @@ describe('Pool page / Collateral section', () => {

cy.mount(
<CollateralSectionUi
isLoading={false}
collateralPriceByAddress={{
COLLATERAL_ADDRESS_SNX: wei(10),
COLLATERAL_ADDRESS_OMG: wei(20),
Expand Down
Loading

0 comments on commit c3b26ff

Please sign in to comment.