Skip to content

Commit

Permalink
Remove signer from contract hooks (#34)
Browse files Browse the repository at this point in the history
* JSON importers must only return JSON without wrapping into ethers.Contract (also future react-sdk compatibility)

* Update all the code to use new address-abi-only contracts and create ethers.Contract on the fly

* Fix rogue SpotMarket.getWrapper

* Deps

* Move importers again

* Update useClosePosition

* Collocate all contract importer hooks and unify the structure
  • Loading branch information
noisekit authored Nov 6, 2024
1 parent eedb8f2 commit d6160fc
Show file tree
Hide file tree
Showing 168 changed files with 992 additions and 1,111 deletions.
4 changes: 2 additions & 2 deletions liquidity/components/DepositModal/DepositModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ export const DepositModal: DepositModalProps = ({ onClose, isOpen, title, liquid
const { network } = useNetwork();

const { data: CoreProxy } = useCoreProxy();
const { data: SpotProxy } = useSpotMarketProxy();
const { data: SpotMarketProxy } = useSpotMarketProxy();
const { data: collateralType } = useCollateralType(collateralSymbol);

const isBase = isBaseAndromeda(network?.id, network?.preset);
Expand All @@ -281,7 +281,7 @@ export const DepositModal: DepositModalProps = ({ onClose, isOpen, title, liquid
utils.parseUnits(collateralNeeded.toString(), 6)
: utils.parseUnits(collateralNeeded.toString(), collateralType?.decimals)
: 0,
spender: isBase ? SpotProxy?.address : CoreProxy?.address,
spender: isBase ? SpotMarketProxy?.address : CoreProxy?.address,
});

const toast = useToast({ isClosable: true, duration: 9000 });
Expand Down
4 changes: 2 additions & 2 deletions liquidity/components/RepayModal/RepayModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export const RepayModal: React.FC<{
const toast = useToast({ isClosable: true, duration: 9000 });

const { data: CoreProxy } = useCoreProxy();
const { data: SpotProxy } = useSpotMarketProxy();
const { data: SpotMarketProxy } = useSpotMarketProxy();

const errorParser = useContractErrorParser();
const amountToDeposit = debtChange.abs().sub(availableCollateral || 0);
Expand All @@ -180,7 +180,7 @@ export const RepayModal: React.FC<{
? //Base USDC is 6 decimals
parseUnits(amountToDeposit.toString(), 6)
: amountToDeposit.toBN(),
spender: isBase ? SpotProxy?.address : CoreProxy?.address,
spender: isBase ? SpotMarketProxy?.address : CoreProxy?.address,
});

const [state, send] = useMachine(RepayMachine, {
Expand Down
7 changes: 2 additions & 5 deletions liquidity/components/WithdrawModal/WithdrawModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ContractError } from '@snx-v3/ContractError';
import { isBaseAndromeda } from '@snx-v3/isBaseAndromeda';
import { ManagePositionContext } from '@snx-v3/ManagePositionContext';
import { Multistep } from '@snx-v3/Multistep';
import { useAccountSpecificCollateral } from '@snx-v3/useAccountCollateral';
import { useAccountCollateral } from '@snx-v3/useAccountCollateral';
import { useNetwork } from '@snx-v3/useBlockchain';
import { useCollateralType } from '@snx-v3/useCollateralTypes';
import { useContractErrorParser } from '@snx-v3/useContractErrorParser';
Expand Down Expand Up @@ -133,10 +133,7 @@ export function WithdrawModal({
const accountId = liquidityPosition?.accountId;

const { data: systemToken } = useSystemToken();
const { data: systemTokenBalance } = useAccountSpecificCollateral(
accountId,
systemToken?.address
);
const { data: systemTokenBalance } = useAccountCollateral(accountId, systemToken?.address);

const { mutation: withdrawMain } = useWithdraw({
amount: withdrawAmount,
Expand Down
8 changes: 4 additions & 4 deletions liquidity/cypress/cypress/tasks/createAccount.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ export async function createAccount({ address, accountId }) {
const provider = new ethers.providers.JsonRpcProvider('http://127.0.0.1:8545');
const signer = provider.getSigner(address);

const coreProxy = new ethers.Contract(CoreProxy.address, CoreProxy.abi, signer);
const CoreProxyContract = new ethers.Contract(CoreProxy.address, CoreProxy.abi, signer);

const currentAccountOwner = await coreProxy.getAccountOwner(accountId);
const currentAccountOwner = await CoreProxyContract.getAccountOwner(accountId);
console.log('createAccount', { accountId, currentAccountOwner });

if (currentAccountOwner === address) {
return accountId;
}

const tx = await coreProxy['createAccount(uint128)'](accountId, { gasLimit: 10_000_000 });
const tx = await CoreProxyContract['createAccount(uint128)'](accountId, { gasLimit: 10_000_000 });
await tx.wait();

const newAccountOwner = await coreProxy.getAccountOwner(accountId);
const newAccountOwner = await CoreProxyContract.getAccountOwner(accountId);
console.log('createAccount', { accountId, newAccountOwner });

return accountId;
Expand Down
4 changes: 2 additions & 2 deletions liquidity/cypress/cypress/tasks/delegateCollateral.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ export async function delegateCollateral({ address, accountId, symbol, amount, p
const signer = provider.getSigner(address);
console.log('delegateCollateral', { address, accountId, symbol, amount, poolId });

const coreProxy = new ethers.Contract(CoreProxy.address, CoreProxy.abi, signer);
const CoreProxyContract = new ethers.Contract(CoreProxy.address, CoreProxy.abi, signer);
try {
const tx = await coreProxy.delegateCollateral(
const tx = await CoreProxyContract.delegateCollateral(
ethers.BigNumber.from(accountId),
ethers.BigNumber.from(poolId),
config.tokenAddress,
Expand Down
4 changes: 2 additions & 2 deletions liquidity/cypress/cypress/tasks/depositCollateral.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ export async function depositCollateral({ address, accountId, symbol, amount })
const signer = provider.getSigner(address);
console.log('depositCollateral', { address, accountId, symbol, amount });

const coreProxy = new ethers.Contract(CoreProxy.address, CoreProxy.abi, signer);
const CoreProxyContract = new ethers.Contract(CoreProxy.address, CoreProxy.abi, signer);

const tx = await coreProxy.deposit(
const tx = await CoreProxyContract.deposit(
ethers.BigNumber.from(accountId),
config.tokenAddress,
ethers.utils.parseEther(`${amount}`),
Expand Down
4 changes: 2 additions & 2 deletions liquidity/cypress/cypress/tasks/getSUSDC.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export async function getSUSDC({ address, amount }) {
const provider = new ethers.providers.JsonRpcProvider('http://127.0.0.1:8545');

const CoreProxy = await importCoreProxy();
const coreProxy = new ethers.Contract(CoreProxy.address, CoreProxy.abi, provider);
const owner = await coreProxy.owner();
const CoreProxyContract = new ethers.Contract(CoreProxy.address, CoreProxy.abi, provider);
const owner = await CoreProxyContract.owner();

await setEthBalance({ address: owner, balance: 1000 });
await setEthBalance({ address: CoreProxy.address, balance: 1000 });
Expand Down
22 changes: 10 additions & 12 deletions liquidity/cypress/cypress/tasks/setConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ export async function setConfig({ key, value }) {
const CoreProxy = await importCoreProxy();
const provider = new ethers.providers.JsonRpcProvider('http://127.0.0.1:8545');

const coreProxy = new ethers.Contract(CoreProxy.address, CoreProxy.abi, provider);
const CoreProxyContract = new ethers.Contract(CoreProxy.address, CoreProxy.abi, provider);

const owner = await coreProxy.owner();
const owner = await CoreProxyContract.owner();

const signer = provider.getSigner(owner);
console.log('setConfig', { owner });
Expand All @@ -18,25 +18,23 @@ export async function setConfig({ key, value }) {
await provider.send('anvil_impersonateAccount', [owner]);
console.log(
`await coreProxy.connect(signer).getConfig(ethers.utils.formatBytes32String(key))`,
await coreProxy.connect(signer).getConfig(ethers.utils.formatBytes32String(key))
await CoreProxyContract.connect(signer).getConfig(ethers.utils.formatBytes32String(key))
);
const oldValue = parseInt(
await coreProxy.connect(signer).getConfig(ethers.utils.formatBytes32String(key)),
await CoreProxyContract.connect(signer).getConfig(ethers.utils.formatBytes32String(key)),
16
);
console.log('setConfig', { key, oldValue });

const tx = await coreProxy
.connect(signer)
.setConfig(
ethers.utils.formatBytes32String(key),
ethers.utils.formatBytes32String(`${value}`),
{ gasLimit: 10_000_000 }
);
const tx = await CoreProxyContract.connect(signer).setConfig(
ethers.utils.formatBytes32String(key),
ethers.utils.formatBytes32String(`${value}`),
{ gasLimit: 10_000_000 }
);
await tx.wait();

const newValue = parseInt(
await coreProxy.connect(signer).getConfig(ethers.utils.formatBytes32String(key)),
await CoreProxyContract.connect(signer).getConfig(ethers.utils.formatBytes32String(key)),
16
);
console.log('setConfig', { key, newValue });
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"main": "index.ts",
"version": "0.0.1",
"dependencies": {
"@ethersproject/contracts": "^5.7.0",
"@snx-v3/contracts": "workspace:*",
"@snx-v3/useBlockchain": "workspace:*",
"@tanstack/react-query": "^5.8.3"
Expand Down
20 changes: 20 additions & 0 deletions liquidity/lib/contracts/useAccountProxy/useAccountProxy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { importAccountProxy } from '@snx-v3/contracts';
import { Network, useNetwork } from '@snx-v3/useBlockchain';
import { useQuery } from '@tanstack/react-query';

export function useAccountProxy(customNetwork?: Network) {
const { network } = useNetwork();
const targetNetwork = customNetwork || network;

return useQuery({
queryKey: [`${targetNetwork?.id}-${targetNetwork?.preset}`, 'AccountProxy'],
enabled: Boolean(targetNetwork),
queryFn: async function () {
if (!targetNetwork) throw new Error('OMFG');
return importAccountProxy(targetNetwork.id, targetNetwork.preset);
},
staleTime: Infinity,
// On some chains this is not available, and that is expected
throwOnError: false,
});
}
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export function useAllErrors(customNetwork?: Network) {
enabled: Boolean(targetNetwork),
queryFn: async function () {
if (!targetNetwork) throw new Error('OMFG');

return importAllErrors(targetNetwork.id, targetNetwork.preset);
},
staleTime: Infinity,
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"main": "index.ts",
"version": "0.0.1",
"dependencies": {
"@ethersproject/contracts": "^5.7.0",
"@snx-v3/contracts": "workspace:*",
"@snx-v3/useBlockchain": "workspace:*",
"@tanstack/react-query": "^5.8.3",
Expand Down
20 changes: 20 additions & 0 deletions liquidity/lib/contracts/useClosePosition/useClosePosition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { importClosePosition } from '@snx-v3/contracts';
import { Network, useNetwork } from '@snx-v3/useBlockchain';
import { useQuery } from '@tanstack/react-query';

export function useClosePosition(customNetwork?: Network) {
const { network } = useNetwork();
const targetNetwork = customNetwork || network;

return useQuery({
queryKey: [`${targetNetwork?.id}-${targetNetwork?.preset}`, 'ClosePosition'],
enabled: Boolean(targetNetwork),
queryFn: async function () {
if (!targetNetwork) throw 'OMFG';
return importClosePosition(targetNetwork.id, targetNetwork.preset);
},
staleTime: Infinity,
// On some chains this is not available, and that is expected
throwOnError: false,
});
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"main": "index.ts",
"version": "0.0.1",
"dependencies": {
"@ethersproject/contracts": "^5.7.0",
"@snx-v3/contracts": "workspace:*",
"@snx-v3/useBlockchain": "workspace:*",
"@tanstack/react-query": "^5.8.3",
Expand Down
20 changes: 20 additions & 0 deletions liquidity/lib/contracts/useCoreProxy/useCoreProxy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { importCoreProxy } from '@snx-v3/contracts';
import { Network, useNetwork } from '@snx-v3/useBlockchain';
import { useQuery } from '@tanstack/react-query';

export function useCoreProxy(customNetwork?: Network) {
const { network } = useNetwork();
const targetNetwork = customNetwork || network;

return useQuery({
queryKey: [`${targetNetwork?.id}-${targetNetwork?.preset}`, 'CoreProxy'],
enabled: Boolean(targetNetwork),
queryFn: async function () {
if (!targetNetwork) throw 'OMFG';
return importCoreProxy(targetNetwork.id, targetNetwork.preset);
},
staleTime: Infinity,
// On some chains this is not available, and that is expected
throwOnError: false,
});
}
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export function useDebtRepayer(customNetwork?: Network) {
enabled: Boolean(targetNetwork),
queryFn: async function () {
if (!targetNetwork) throw new Error('OMFG');

return importDebtRepayer(targetNetwork.id, targetNetwork.preset);
},
staleTime: Infinity,
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"main": "index.ts",
"version": "0.0.1",
"dependencies": {
"@ethersproject/contracts": "^5.7.0",
"@snx-v3/contracts": "workspace:*",
"@snx-v3/useBlockchain": "workspace:*",
"@tanstack/react-query": "^5.8.3",
Expand Down
20 changes: 20 additions & 0 deletions liquidity/lib/contracts/useLegacyMarket/useLegacyMarket.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { importLegacyMarket } from '@snx-v3/contracts';
import { Network, useNetwork } from '@snx-v3/useBlockchain';
import { useQuery } from '@tanstack/react-query';

export function useLegacyMarket(customNetwork?: Network) {
const { network } = useNetwork();
const targetNetwork = customNetwork || network;

return useQuery({
queryKey: [`${targetNetwork?.id}-${targetNetwork?.preset}`, 'LegacyMarket'],
enabled: Boolean(targetNetwork),
queryFn: async function () {
if (!targetNetwork) throw new Error('OMFG');
return importLegacyMarket(targetNetwork.id, targetNetwork.preset);
},
staleTime: Infinity,
// On some chains this is not available, and that is expected
throwOnError: false,
});
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"main": "index.ts",
"version": "0.0.1",
"dependencies": {
"@ethersproject/contracts": "^5.7.0",
"@snx-v3/contracts": "workspace:*",
"@snx-v3/useBlockchain": "workspace:*",
"@tanstack/react-query": "^5.8.3",
Expand Down
20 changes: 20 additions & 0 deletions liquidity/lib/contracts/useMulticall3/useMulticall3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { importMulticall3 } from '@snx-v3/contracts';
import { Network, useNetwork } from '@snx-v3/useBlockchain';
import { useQuery } from '@tanstack/react-query';

export function useMulticall3(customNetwork?: Network) {
const { network } = useNetwork();
const targetNetwork = customNetwork || network;

return useQuery({
queryKey: [`${targetNetwork?.id}-${targetNetwork?.preset}`, 'Multicall3'],
enabled: Boolean(targetNetwork),
queryFn: async function () {
if (!targetNetwork) throw new Error('OMFG');
return importMulticall3(targetNetwork.id, targetNetwork.preset);
},
staleTime: Infinity,
// On some chains this is not available, and that is expected
throwOnError: false,
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"main": "index.ts",
"version": "0.0.1",
"dependencies": {
"@ethersproject/contracts": "^5.7.0",
"@snx-v3/contracts": "workspace:*",
"@snx-v3/useBlockchain": "workspace:*",
"@tanstack/react-query": "^5.8.3",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { importOracleManagerProxy } from '@snx-v3/contracts';
import { Network, useNetwork } from '@snx-v3/useBlockchain';
import { useQuery } from '@tanstack/react-query';

export function useOracleManagerProxy(customNetwork?: Network) {
const { network } = useNetwork();
const targetNetwork = customNetwork || network;

return useQuery({
queryKey: [`${targetNetwork?.id}-${targetNetwork?.preset}`, 'OracleManagerProxy'],
enabled: Boolean(targetNetwork),
queryFn: async function () {
if (!targetNetwork) throw new Error('OMFG');
return importOracleManagerProxy(targetNetwork.id, targetNetwork.preset);
},
staleTime: Infinity,
// On some chains this is not available, and that is expected
throwOnError: false,
});
}
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ export function usePythFeeds(customNetwork?: Network) {
queryKey: [`${targetNetwork?.id}-${targetNetwork?.preset}`, 'PythFeeds'],
enabled: Boolean(targetNetwork),
queryFn: async function () {
if (!targetNetwork) {
throw new Error('OMFG');
}
return await importPythFeeds(targetNetwork.id, targetNetwork.preset);
if (!targetNetwork) throw new Error('OMFG');
return importPythFeeds(targetNetwork.id, targetNetwork.preset);
},
staleTime: Infinity,
// On some chains this is not available, and that is expected
throwOnError: false,
});
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
import { importRewardsDistributors } from '@snx-v3/contracts';
import { Network, useNetwork } from '@snx-v3/useBlockchain';
import { useQuery } from '@tanstack/react-query';
import { importRewardsDistributors } from '@snx-v3/contracts';

export function useRewardsDistributors(customNetwork?: Network) {
const { network } = useNetwork();
const targetNetwork = customNetwork || network;

return useQuery({
enabled: Boolean(targetNetwork),
queryKey: [`${targetNetwork?.id}-${targetNetwork?.preset}`, 'RewardsDistributors'],
enabled: Boolean(targetNetwork),
queryFn: async function () {
if (!targetNetwork) {
throw new Error('OMG');
}
const rewardDistributors = await importRewardsDistributors(
targetNetwork?.id,
targetNetwork?.preset
);
return rewardDistributors;
if (!targetNetwork) throw new Error('OMFG');
return importRewardsDistributors(targetNetwork.id, targetNetwork.preset);
},
staleTime: Infinity,
// On some chains this is not available, and that is expected
throwOnError: false,
});
}
File renamed without changes.
Loading

0 comments on commit d6160fc

Please sign in to comment.