Skip to content

Commit

Permalink
Arbitrum support (#301)
Browse files Browse the repository at this point in the history
* Add missing chains to withERC7412

* Fix displaying Arbthetix

* Fallback for UNKNOWN Token Icon

* @snx-v3/useTokenInfo

* Support arbitrum deployment

* Fix rewards for different pool collaterals

* Show all rewards when looking at the pool

* Fix supported chains

* Add all the subgraphs for all deployments

* Typecheck harder
  • Loading branch information
noisekit authored Jun 13, 2024
1 parent 335919e commit 6873cc6
Show file tree
Hide file tree
Showing 31 changed files with 454 additions and 169 deletions.
59 changes: 59 additions & 0 deletions contracts/importers/importAllErrors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
export async function importAllErrors(chainId, preset) {
if (!preset) {
throw new Error(`Missing preset`);
}
const deployment = `${Number(chainId).toFixed(0)}-${preset}`;
switch (deployment) {
case '1-main': {
const [{ default: abi }] = await Promise.all([
import('@synthetixio/v3-contracts/1-main/AllErrors.readable.json'),
]);
return { address: undefined, abi };
}
case '11155111-main': {
const [{ default: abi }] = await Promise.all([
import('@synthetixio/v3-contracts/11155111-main/AllErrors.readable.json'),
]);
return { address: undefined, abi };
}
case '10-main': {
const [{ default: abi }] = await Promise.all([
import('@synthetixio/v3-contracts/10-main/AllErrors.readable.json'),
]);
return { address: undefined, abi };
}
case '8453-andromeda': {
const [{ default: abi }] = await Promise.all([
import('@synthetixio/v3-contracts/8453-andromeda/AllErrors.readable.json'),
]);
return { address: undefined, abi };
}
case '84532-andromeda': {
const [{ default: abi }] = await Promise.all([
import('@synthetixio/v3-contracts/84532-andromeda/AllErrors.readable.json'),
]);
return { address: undefined, abi };
}
case '42161-main': {
const [{ default: abi }] = await Promise.all([
import('@synthetixio/v3-contracts/42161-main/AllErrors.readable.json'),
]);
return { address: undefined, abi };
}
case '421614-main': {
const [{ default: abi }] = await Promise.all([
import('@synthetixio/v3-contracts/421614-main/AllErrors.readable.json'),
]);
return { address: undefined, abi };
}
case '42161-arbthetix': {
const [{ default: abi }] = await Promise.all([
import('@synthetixio/v3-contracts/42161-arbthetix/AllErrors.readable.json'),
]);
return { address: undefined, abi };
}
default: {
throw new Error(`Unsupported deployment ${deployment} for AllErrors`);
}
}
}
2 changes: 1 addition & 1 deletion contracts/importers/importMulticall3.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const abi = [
'function aggregate3Value(tuple(address target, bool allowFailure, uint256 value, bytes callData)[] calls) payable returns (tuple(bool success, bytes returnData)[] returnData)',
];

export async function importMulticall3(chainId, preset = 'main') {
export async function importMulticall3(chainId, preset) {
const deployment = `${Number(chainId).toFixed(0)}-${preset}`;
switch (deployment) {
case '1-main': {
Expand Down
1 change: 1 addition & 0 deletions contracts/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './importers/importAllErrors';
export * from './importers/importCoreProxy';
export * from './importers/importAccountProxy';
export * from './importers/importUSDProxy';
Expand Down
3 changes: 2 additions & 1 deletion liquidity/components/icons/CollateralIcon/CollateralIcon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const CollateralIcon = ({
return <SNXIcon fill={fill} color={color} {...props} />;
case 'OP':
return <OptimismIcon {...props} />;
case 'sUSD':
case 'USDx':
case 'sUSD':
case 'snxUSD':
return <DollarCircle {...props} />;
Expand All @@ -40,6 +40,7 @@ export const CollateralIcon = ({
case 'fUSDC':
return <SUSDCIcon {...props} />;
case 'ARB':
case 'fARB':
return <ArbitrumIcon {...props} />;
default:
return <UnknownIcon {...props} />;
Expand Down
16 changes: 12 additions & 4 deletions liquidity/lib/constants/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,22 @@ export const ONBOARD_KEY = 'sec_jykTuCK0ZuqXWf3wNYqizxs2';

export const getSubgraphUrl = (networkName = 'optimism-mainnet') => {
switch (networkName) {
case 'mainnet':
return 'https://subgraph.satsuma-prod.com/ce5e03f52f3b/synthetix/synthetix-mainnet/api';
case 'sepolia':
return 'https://subgraph.satsuma-prod.com/ce5e03f52f3b/synthetix/synthetix-sepolia/api';
case 'optimism-mainnet':
return 'https://subgraph.satsuma-prod.com/ce5e03f52f3b/synthetix/synthetix-optimism-mainnet/api';
case 'arbitrum':
return `https://subgraph.satsuma-prod.com/ce5e03f52f3b/synthetix/synthetix-arbitrum-mainnet/version/v1/api`;
return `https://subgraph.satsuma-prod.com/ce5e03f52f3b/synthetix/synthetix-arbitrum-mainnet/api`;
case 'arbitrum-sepolia':
return `https://subgraph.satsuma-prod.com/ce5e03f52f3b/synthetix/synthetix-arbitrum-sepolia/api`;
case 'base-sepolia':
return 'https://subgraph.satsuma-prod.com/ce5e03f52f3b/synthetix/synthetix-base-sepolia-andromeda/version/v1.0.1/api';
return 'https://subgraph.satsuma-prod.com/ce5e03f52f3b/synthetix/synthetix-base-sepolia-andromeda/api';
case 'base':
return 'https://subgraph.satsuma-prod.com/ce5e03f52f3b/synthetix/synthetix-base-mainnet-andromeda/version/v1.1/api';
return 'https://subgraph.satsuma-prod.com/ce5e03f52f3b/synthetix/synthetix-base-mainnet-andromeda/api';
default:
return `https://api.thegraph.com/subgraphs/name/snx-v3/${networkName}`;
return `https://subgraph.satsuma-prod.com/ce5e03f52f3b/synthetix/synthetix-${networkName}/api`;
}
};

Expand Down
1 change: 1 addition & 0 deletions liquidity/lib/useAccountCollateral/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"@snx-v3/useCollateralTypes": "workspace:*",
"@snx-v3/useCoreProxy": "workspace:*",
"@snx-v3/useGetUSDTokens": "workspace:*",
"@snx-v3/useTokenInfo": "workspace:*",
"@snx-v3/withERC7412": "workspace:*",
"@synthetixio/wei": "^2.74.4",
"@tanstack/react-query": "^5.8.3",
Expand Down
65 changes: 48 additions & 17 deletions liquidity/lib/useAccountCollateral/useAccountCollateral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useCollateralTypes } from '@snx-v3/useCollateralTypes';
import { useCoreProxy } from '@snx-v3/useCoreProxy';
import { useGetUSDTokens } from '@snx-v3/useGetUSDTokens';
import { erc7412Call } from '@snx-v3/withERC7412';
import { useTokenInfo } from '@snx-v3/useTokenInfo';
import { Wei, wei } from '@synthetixio/wei';
import { useQuery } from '@tanstack/react-query';
import { ethers } from 'ethers';
Expand Down Expand Up @@ -70,26 +71,50 @@ export function useAccountCollateral({
}) {
const { data: CoreProxy } = useCoreProxy();
const { network } = useNetwork();
const collateralTypes = useCollateralTypes(includeDelegationOff);
const { data: collateralTypes } = useCollateralTypes(includeDelegationOff);
const { data: USDTokens } = useGetUSDTokens();

const provider = useDefaultProvider();
const { data: priceUpdateTx } = useAllCollateralPriceUpdates();

const { data: stablecoinInfo } = useTokenInfo(USDTokens?.snxUSD);

return useQuery({
queryKey: [
`${network?.id}-${network?.preset}`,
'AccountCollateral',
{ accountId },
{ tokens: JSON.stringify(USDTokens), priceUpdateTx: stringToHash(priceUpdateTx?.data) },
{ stablecoin: USDTokens?.snxUSD },
{ priceUpdateTx: stringToHash(priceUpdateTx?.data) },
],
enabled: Boolean(CoreProxy && accountId && USDTokens?.snxUSD),
enabled: Boolean(
network &&
provider &&
CoreProxy &&
accountId &&
collateralTypes &&
collateralTypes.length > 0 &&
stablecoinInfo
),
queryFn: async function () {
if (!CoreProxy || !accountId || !network || !provider || !USDTokens || !USDTokens?.snxUSD) {
throw 'useAccountCollateral should be disabled';
if (
!(
network &&
provider &&
CoreProxy &&
accountId &&
collateralTypes &&
collateralTypes.length > 0 &&
stablecoinInfo
)
) {
throw new Error('OMG');
}
const tokenAddresses =
collateralTypes.data?.map((c) => c.tokenAddress).concat(USDTokens.snxUSD) ?? [];

const tokenAddresses = collateralTypes
.map((c) => c.tokenAddress)
.concat([stablecoinInfo.tokenAddress]);

const { calls, decoder } = await loadAccountCollateral({
accountId,
tokenAddresses,
Expand All @@ -99,17 +124,23 @@ export function useAccountCollateral({
if (priceUpdateTx) {
allCalls.unshift(priceUpdateTx as any);
}
const data = await erc7412Call(network, provider, allCalls, decoder, 'useAccountCollateral');

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

return data.map((x) => ({
...x,
symbol:
collateralTypes.data?.find((c) => c.tokenAddress === x.tokenAddress)?.symbol ?? 'sUSD',
displaySymbol:
collateralTypes.data?.find((c) => c.tokenAddress === x.tokenAddress)?.displaySymbol ??
'snxUSD',
}));
return data.map((x) => {
if (`${stablecoinInfo.tokenAddress}`.toLowerCase() === `${x.tokenAddress}`.toLowerCase()) {
return Object.assign(x, {
symbol: stablecoinInfo.symbol,
displaySymbol: stablecoinInfo.name,
});
}
const collateralType = collateralTypes.find(
(c) => `${c.tokenAddress}`.toLowerCase() === `${x.tokenAddress}`.toLowerCase()
);
return Object.assign(x, {
symbol: collateralType?.symbol ?? '',
displaySymbol: collateralType?.displaySymbol ?? '',
});
});
},
});
}
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 @@ -83,7 +83,7 @@ export const NETWORKS: Network[] = [
rpcUrl: (INFURA_KEY?: string) =>
`https://base-mainnet.infura.io/v3/${INFURA_KEY ?? DEFAULT_INFURA_KEY}`,
label: 'Base',
isSupported: false,
isSupported: true,
publicRpcUrl: 'https://base.publicnode.com',
isTestnet: false,
},
Expand Down Expand Up @@ -135,7 +135,7 @@ export const NETWORKS: Network[] = [
rpcUrl: (INFURA_KEY?: string) =>
`https://base-sepolia.infura.io/v3/${INFURA_KEY ?? DEFAULT_INFURA_KEY}`,
label: 'Base Sepolia',
isSupported: false, // hidden by default but if wallet switched to Base Sepolia it will be visible
isSupported: true,
publicRpcUrl: 'https://sepolia.base.org',
isTestnet: true,
},
Expand Down
2 changes: 1 addition & 1 deletion liquidity/lib/useBorrow/useBorrow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export const useBorrow = ({
]);
const allCalls = collateralPriceCalls.concat(calls);

const erc7412Tx = await withERC7412(network, allCalls, 'borrow', CoreProxy.interface);
const erc7412Tx = await withERC7412(network, allCalls, 'borrow');

const gasOptionsForTransaction = formatGasPriceForTransaction({
gasLimit: erc7412Tx.gasLimit,
Expand Down
2 changes: 1 addition & 1 deletion liquidity/lib/useClearDebt/useClearDebt.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export const useClearDebt = ({
calls.unshift(priceUpdateTx as any);
}

const erc7412Tx = await withERC7412(network, calls, 'useRepay', CoreProxy.interface);
const erc7412Tx = await withERC7412(network, calls, 'useRepay');

const gasOptionsForTransaction = formatGasPriceForTransaction({
gasLimit: erc7412Tx.gasLimit,
Expand Down
2 changes: 1 addition & 1 deletion liquidity/lib/useDeposit/useDeposit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export const useDeposit = ({
]);
const allCalls = collateralPriceCalls.concat(calls);

const erc7412Tx = await withERC7412(network, allCalls, 'useDeposit', CoreProxy.interface);
const erc7412Tx = await withERC7412(network, allCalls, 'useDeposit');

const gasOptionsForTransaction = formatGasPriceForTransaction({
gasLimit: erc7412Tx.gasLimit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,7 @@ export const useDepositBaseAndromeda = ({
calls.unshift(priceUpdateTx as any);
}

const erc7412Tx = await withERC7412(
network,
calls,
'useDepositBaseAndromeda',
CoreProxy.interface
);
const erc7412Tx = await withERC7412(network, calls, 'useDepositBaseAndromeda');

const gasOptionsForTransaction = formatGasPriceForTransaction({
gasLimit: erc7412Tx.gasLimit,
Expand Down
2 changes: 1 addition & 1 deletion liquidity/lib/useRepay/useRepay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export const useRepay = ({
]);
const allCalls = collateralPriceCalls.concat(calls);

const erc7412Tx = await withERC7412(network, allCalls, 'useRepay', CoreProxy.interface);
const erc7412Tx = await withERC7412(network, allCalls, 'useRepay');

const gasOptionsForTransaction = formatGasPriceForTransaction({
gasLimit: erc7412Tx.gasLimit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export const useRepayBaseAndromeda = ({
calls.push(priceUpdateTx as any);
}

const erc7412Tx = await withERC7412(network, calls, 'useRepay', CoreProxy.interface);
const erc7412Tx = await withERC7412(network, calls, 'useRepay');

const gasOptionsForTransaction = formatGasPriceForTransaction({
gasLimit: erc7412Tx.gasLimit,
Expand Down
Loading

0 comments on commit 6873cc6

Please sign in to comment.