-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add hooks * Add more hooks * Rename marketId to synthMarketId and perpsMarketId * Refactor: Simplify parameters in useDelegateCollateral
- Loading branch information
Showing
26 changed files
with
961 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { ethers } from 'ethers'; | ||
|
||
export async function delegateCollateral({ | ||
provider, | ||
walletAddress, | ||
CoreProxyContract, | ||
accountId, | ||
poolId, | ||
tokenAddress, | ||
delegateAmount, | ||
}: { | ||
provider: ethers.providers.Web3Provider; | ||
walletAddress: string; | ||
CoreProxyContract: { address: string; abi: string[] }; | ||
accountId: ethers.BigNumber; | ||
poolId: ethers.BigNumber; | ||
tokenAddress: string; | ||
delegateAmount: ethers.BigNumber; | ||
}) { | ||
const signer = provider.getSigner(walletAddress); | ||
const CoreProxy = new ethers.Contract(CoreProxyContract.address, CoreProxyContract.abi, signer); | ||
|
||
const delegateCollateralTxnArgs = [ | ||
// | ||
accountId, | ||
poolId, | ||
tokenAddress, | ||
delegateAmount, | ||
ethers.utils.parseEther('1'), // Leverage | ||
]; | ||
console.log('delegateCollateralTxnArgs', delegateCollateralTxnArgs); | ||
|
||
console.time('delegateCollateral'); | ||
const tx: ethers.ContractTransaction = await CoreProxy.delegateCollateral(...delegateCollateralTxnArgs); | ||
console.timeEnd('delegateCollateral'); | ||
console.log({ tx }); | ||
const txResult = await tx.wait(); | ||
console.log({ txResult }); | ||
return txResult; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { ethers } from 'ethers'; | ||
|
||
export async function delegateCollateralWithPriceUpdate({ | ||
provider, | ||
walletAddress, | ||
CoreProxyContract, | ||
MulticallContract, | ||
accountId, | ||
poolId, | ||
tokenAddress, | ||
delegateAmount, | ||
priceUpdateTxn, | ||
}: { | ||
provider: ethers.providers.Web3Provider; | ||
walletAddress: string; | ||
CoreProxyContract: { address: string; abi: string[] }; | ||
MulticallContract: { address: string; abi: string[] }; | ||
accountId: ethers.BigNumber; | ||
poolId: ethers.BigNumber; | ||
tokenAddress: string; | ||
delegateAmount: ethers.BigNumber; | ||
priceUpdateTxn: { | ||
target: string; | ||
callData: string; | ||
value: number; | ||
requireSuccess: boolean; | ||
}; | ||
}) { | ||
const CoreProxyInterface = new ethers.utils.Interface(CoreProxyContract.abi); | ||
const MulticallInterface = new ethers.utils.Interface(MulticallContract.abi); | ||
|
||
const delegateCollateralTxnArgs = [ | ||
// | ||
accountId, | ||
poolId, | ||
tokenAddress, | ||
delegateAmount, | ||
ethers.utils.parseEther('1'), // Leverage | ||
]; | ||
console.log('delegateCollateralTxnArgs', delegateCollateralTxnArgs); | ||
|
||
const delegateCollateralTxn = { | ||
target: CoreProxyContract.address, | ||
callData: CoreProxyInterface.encodeFunctionData('delegateCollateral', [ | ||
// | ||
...delegateCollateralTxnArgs, | ||
]), | ||
value: 0, | ||
requireSuccess: true, | ||
}; | ||
console.log({ delegateCollateralTxn }); | ||
|
||
const signer = provider.getSigner(walletAddress); | ||
|
||
const multicallTxn = { | ||
from: walletAddress, | ||
to: MulticallContract.address, | ||
data: MulticallInterface.encodeFunctionData('aggregate3Value', [[priceUpdateTxn, delegateCollateralTxn]]), | ||
value: priceUpdateTxn.value, | ||
}; | ||
console.log({ multicallTxn }); | ||
|
||
console.time('delegateCollateralWithPriceUpdate'); | ||
const tx: ethers.ContractTransaction = await signer.sendTransaction(multicallTxn); | ||
console.timeEnd('delegateCollateralWithPriceUpdate'); | ||
console.log({ tx }); | ||
const txResult = await tx.wait(); | ||
console.log({ txResult }); | ||
return txResult; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { ethers } from 'ethers'; | ||
|
||
export async function fetchAccountAvailableCollateral({ | ||
provider, | ||
CoreProxyContract, | ||
accountId, | ||
tokenAddress, | ||
}: { | ||
provider: ethers.providers.BaseProvider; | ||
CoreProxyContract: { address: string; abi: string[] }; | ||
accountId: ethers.BigNumber; | ||
tokenAddress: string; | ||
}) { | ||
const CoreProxy = new ethers.Contract(CoreProxyContract.address, CoreProxyContract.abi, provider); | ||
console.time('fetchAccountAvailableCollateral'); | ||
const accountAvailableCollateral = await CoreProxy.getAccountAvailableCollateral(accountId, tokenAddress); | ||
console.timeEnd('fetchAccountAvailableCollateral'); | ||
return accountAvailableCollateral; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { ethers } from 'ethers'; | ||
|
||
export async function fetchPerpsSettleOrder({ | ||
provider, | ||
walletAddress, | ||
PerpsMarketProxyContract, | ||
perpsAccountId, | ||
}: { | ||
provider: ethers.providers.Web3Provider; | ||
walletAddress: string; | ||
PerpsMarketProxyContract: { address: string; abi: string[] }; | ||
perpsAccountId: ethers.BigNumber; | ||
}) { | ||
const signer = provider.getSigner(walletAddress); | ||
const PerpsMarketProxy = new ethers.Contract(PerpsMarketProxyContract.address, PerpsMarketProxyContract.abi, signer); | ||
|
||
console.time('fetchPerpsSettleOrder'); | ||
const tx: ethers.ContractTransaction = await PerpsMarketProxy.settleOrder(perpsAccountId); | ||
console.timeEnd('fetchPerpsSettleOrder'); | ||
const txResult = await tx.wait(); | ||
console.log({ txResult }); | ||
return txResult; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { ethers } from 'ethers'; | ||
|
||
export async function fetchPerpsSettleOrderWithPriceUpdate({ | ||
provider, | ||
walletAddress, | ||
PerpsMarketProxyContract, | ||
MulticallContract, | ||
perpsAccountId, | ||
priceUpdateTxn, | ||
}: { | ||
provider: ethers.providers.Web3Provider; | ||
walletAddress?: string; | ||
PerpsMarketProxyContract: { address: string; abi: string[] }; | ||
MulticallContract: { address: string; abi: string[] }; | ||
perpsAccountId: ethers.BigNumber; | ||
priceUpdateTxn: { | ||
target: string; | ||
callData: string; | ||
value: ethers.BigNumber; | ||
requireSuccess: boolean; | ||
}; | ||
}) { | ||
const PerpsMarketPoxyInterface = new ethers.utils.Interface(PerpsMarketProxyContract.abi); | ||
const MulticallInterface = new ethers.utils.Interface(MulticallContract.abi); | ||
|
||
const settleOrderTxn = { | ||
target: PerpsMarketProxyContract.address, | ||
callData: PerpsMarketPoxyInterface.encodeFunctionData('settleOrder', [perpsAccountId]), | ||
value: 0, | ||
requireSuccess: true, | ||
}; | ||
console.log({ settleOrderTxn }); | ||
|
||
const signer = provider.getSigner(walletAddress); | ||
|
||
const multicallTxn = { | ||
from: walletAddress, | ||
to: MulticallContract.address, | ||
data: MulticallInterface.encodeFunctionData('aggregate3Value', [[priceUpdateTxn, settleOrderTxn]]), | ||
value: priceUpdateTxn.value, | ||
}; | ||
console.log({ multicallTxn }); | ||
|
||
console.time('fetchPerpsSettleOrderWithPriceUpdate'); | ||
const tx: ethers.ContractTransaction = await signer.sendTransaction(multicallTxn); | ||
console.timeEnd('fetchPerpsSettleOrderWithPriceUpdate'); | ||
|
||
const txResult = await tx.wait(); | ||
console.log({ txResult }); | ||
return txResult; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { ethers } from 'ethers'; | ||
|
||
export async function fetchPositionCollateral({ | ||
provider, | ||
CoreProxyContract, | ||
accountId, | ||
poolId, | ||
tokenAddress, | ||
}: { | ||
provider: ethers.providers.BaseProvider; | ||
CoreProxyContract: { address: string; abi: string[] }; | ||
accountId: ethers.BigNumber; | ||
poolId: ethers.BigNumber; | ||
tokenAddress: string; | ||
}) { | ||
const CoreProxy = new ethers.Contract(CoreProxyContract.address, CoreProxyContract.abi, provider); | ||
const positionCollateral = await CoreProxy.getPositionCollateral(accountId, poolId, tokenAddress); | ||
console.log({ positionCollateral }); | ||
return positionCollateral; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { ethers } from 'ethers'; | ||
import { getPythVaa } from './getPythVaa'; | ||
|
||
export async function fetchStrictPriceUpdateTxn({ | ||
commitmentTime, | ||
commitmentPriceDelay, | ||
PythERC7412WrapperContract, | ||
feedId, | ||
}: { | ||
commitmentTime: ethers.BigNumber; | ||
commitmentPriceDelay: ethers.BigNumber; | ||
PythERC7412WrapperContract: { address: string; abi: string[] }; | ||
feedId: string; | ||
}) { | ||
console.time('fetchStrictPriceUpdateTxn'); | ||
const PythERC7412WrapperInterface = new ethers.utils.Interface(PythERC7412WrapperContract.abi); | ||
const timestamp = commitmentTime.add(commitmentPriceDelay); | ||
const offchainData = await getPythVaa({ pythPriceFeedId: feedId, timestamp: timestamp.toNumber() }); | ||
const updateType = 2; | ||
const offchainDataEncoded = ethers.utils.defaultAbiCoder.encode( | ||
['uint8', 'uint64', 'bytes32[]', 'bytes[]'], | ||
[updateType, timestamp, [feedId], [offchainData]] | ||
); | ||
console.timeEnd('fetchStrictPriceUpdateTxn'); | ||
const priceUpdateTxn = { | ||
target: PythERC7412WrapperContract.address, | ||
callData: PythERC7412WrapperInterface.encodeFunctionData('fulfillOracleQuery', [offchainDataEncoded]), | ||
value: ethers.BigNumber.from(1), | ||
requireSuccess: true, | ||
}; | ||
console.log({ priceUpdateTxn }); | ||
return priceUpdateTxn; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { EvmPriceServiceConnection } from '@pythnetwork/pyth-evm-js'; | ||
|
||
const PYTH_MAINNET_ENDPOINT = process.env.PYTH_MAINNET_ENDPOINT || 'https://hermes.pyth.network'; | ||
|
||
const priceService = new EvmPriceServiceConnection(PYTH_MAINNET_ENDPOINT); | ||
|
||
function base64ToHex(str: string) { | ||
const raw = Buffer.from(str, 'base64'); | ||
return `0x${raw.toString('hex')}`; | ||
} | ||
|
||
export async function getPythVaa({ pythPriceFeedId, timestamp }: { pythPriceFeedId: string; timestamp: number }) { | ||
const [priceUpdateData] = await priceService.getVaa(pythPriceFeedId, timestamp); | ||
return base64ToHex(priceUpdateData); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { ethers } from 'ethers'; | ||
import { useErrorParser } from './useErrorParser'; | ||
import { useImportContract } from './useImports'; | ||
import { useSynthetix } from './useSynthetix'; | ||
|
||
export function useAccounts({ provider, walletAddress }: { walletAddress?: string; provider?: ethers.providers.BaseProvider }) { | ||
const { chainId } = useSynthetix(); | ||
const errorParser = useErrorParser(); | ||
const { data: AccountProxyContract } = useImportContract('AccountProxy'); | ||
|
||
return useQuery<ethers.BigNumber[]>({ | ||
enabled: Boolean(chainId && AccountProxyContract?.address && walletAddress && provider), | ||
queryKey: [chainId, 'Accounts', { AccountProxy: AccountProxyContract?.address }, { ownerAddress: walletAddress }], | ||
queryFn: async () => { | ||
if (!(chainId && AccountProxyContract?.address && walletAddress && provider)) throw 'OMFG'; | ||
const AccountProxy = new ethers.Contract(AccountProxyContract.address, AccountProxyContract.abi, provider); | ||
const numberOfAccountTokens = await AccountProxy.balanceOf(walletAddress); | ||
if (numberOfAccountTokens.eq(0)) { | ||
// No accounts created yet | ||
return []; | ||
} | ||
const accountIndexes = Array.from(Array(numberOfAccountTokens.toNumber()).keys()); | ||
const accounts = await Promise.all(accountIndexes.map((i) => AccountProxy.tokenOfOwnerByIndex(walletAddress, i))); | ||
return accounts; | ||
}, | ||
select: (accounts) => accounts.map((accountId) => ethers.BigNumber.from(accountId)), | ||
throwOnError: (error) => { | ||
// TODO: show toast | ||
errorParser(error); | ||
return false; | ||
}, | ||
}); | ||
} |
Oops, something went wrong.