Skip to content

Commit

Permalink
static collect simulation and collect fee tx implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
vnaysngh-mudrex committed Feb 9, 2024
1 parent 1883dbf commit d397f51
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 31 deletions.
158 changes: 130 additions & 28 deletions src/hooks/useV3PositionFees.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,25 @@ import { useBlockNumber as uBlockNumber, useContract, useContractRead } from '@s
import NFTPositionManagerABI from 'contracts/nonfungiblepositionmanager/abi.json'
import { useV3NFTPositionManagerContract } from './useContract'
import { DEFAULT_CHAIN_ID, MAX_UINT128, NONFUNGIBLE_POOL_MANAGER_ADDRESS } from 'constants/tokens'
import { CallData, cairo, validateAndParseAddress } from 'starknet'
import {
BigNumberish,
CallData,
RpcProvider,
TransactionType,
WeierstrassSignatureType,
cairo,
ec,
hash,
validateAndParseAddress,
} from 'starknet'
import POOL_ABI from 'contracts/pool/abi.json'
import { toI32 } from 'utils/toI32'
import { useAccountDetails } from './starknet-react'
import { useQuery } from 'react-query'

const provider = new RpcProvider({
nodeUrl: 'https://starknet-testnet.public.blastapi.io/rpc/v0_6',
})

// compute current + counterfactual fees for a v3 position
export function useV3PositionFees(
Expand Down Expand Up @@ -82,38 +97,125 @@ export const usePositionOwner = (tokenId: number) => {
return { ownerOf: ownerOf ? validateAndParseAddress(ownerOf.toString()) : undefined, isLoading, error }
}

export const useStaticFeeResults = (poolAddress: string, owner: string, position: Position, asWETH: false) => {
const callData = {
owner,
tick_lower: toI32(position.tickLower),
tick_upper: toI32(position.tickUpper),
amount0_requested: MAX_UINT128,
amount1_requested: MAX_UINT128,
export const useStaticFeeResults = (
poolAddress: string,
owner: string,
position: Position,
asWETH: false,
parsedTokenId: number
) => {
const { account, address, connector, chainId } = useAccountDetails()

const privateKey = '0x1234567890987654321'

const message: BigNumberish[] = [1, 128, 18, 14]

const msgHash = hash.computeHashOnElements(message)
const signature: WeierstrassSignatureType = ec.starkCurve.sign(msgHash, privateKey)

const collectSelector = useMemo(() => {
if (!chainId) return
return {
contract_address: NONFUNGIBLE_POOL_MANAGER_ADDRESS[chainId],
selector: hash.getSelectorFromName('collect'),
}
}, [chainId])

const totalTx = {
totalTx: '0x1',
}
position.pool.token0
const collect_call_data_length = { approve_call_data_length: '0x05' }

const nonce_results = useQuery({
queryKey: [`nonce/${poolAddress}/${parsedTokenId}/${account?.address}`],
queryFn: async () => {
if (!account) return
const results = await account?.getNonce()
return cairo.felt(results.toString())
},
onSuccess: (data) => {
// Handle the successful data fetching here if needed
},
})

const compiledData = CallData.compile(callData)
const fee_results = useQuery({
queryKey: [`fee/${address}/${nonce_results.data}/${parsedTokenId}`],
queryFn: async () => {
if (!account || !address || !nonce_results || !parsedTokenId || !connector || !collectSelector) return
const nonce_data = nonce_results.data
if (!nonce_data) return undefined
const nonce = Number(nonce_data)
const isConnectorBraavos = connector.id === 'braavos'

const { data } = useContractRead({
functionName: 'static_collect',
args: [compiledData],
abi: POOL_ABI,
address: poolAddress,
watch: true,
const collect_call_data = {
tokenId: cairo.uint256(parsedTokenId),
recipient: address,
amount0_max: MAX_UINT128,
amount1_max: MAX_UINT128,
}
const payload = isConnectorBraavos
? {
contractAddress: address,
calldata: CallData.compile({
...totalTx,
...collectSelector,
...{ collect_offset: '0x0' },
...collect_call_data_length,
...{ total_call_data_length: '0x5' },
...collect_call_data,
}),
}
: {
contractAddress: address,
calldata: CallData.compile({
...totalTx,
...collectSelector,
...collect_call_data_length,
...collect_call_data,
}),
}

// const compiledCall = CallData.
const response = await provider.simulateTransaction(
[
{
type: TransactionType.INVOKE,
...payload,
signature,
nonce,
},
],
{
skipValidate: true,
}
)

// return response

const typedResponse: any = response

const tx_response: any = typedResponse
? typedResponse[0]?.transaction_trace.execute_invocation?.result
: undefined

return tx_response
},
})

if (data) {
const dataObj = data as any
return [
CurrencyAmount.fromRawAmount(
asWETH ? position.pool.token0 : unwrappedToken(position.pool.token0),
dataObj[0].toString()
),
CurrencyAmount.fromRawAmount(
asWETH ? position.pool.token1 : unwrappedToken(position.pool.token1),
dataObj[1].toString()
),
]
if (fee_results && fee_results.data) {
const results: string[] = fee_results.data
if (results) {
return [
CurrencyAmount.fromRawAmount(
asWETH ? position.pool.token0 : unwrappedToken(position.pool.token0),
results[results.length - 2].toString()
),
CurrencyAmount.fromRawAmount(
asWETH ? position.pool.token1 : unwrappedToken(position.pool.token1),
results[results.length - 1].toString()
),
]
} else return [undefined, undefined]
} else {
return [undefined, undefined]
}
Expand Down
10 changes: 7 additions & 3 deletions src/pages/Pool/PositionPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import { useV3PositionTokenURI } from '../../hooks/usePositionTokenURI'
import { ExplorerDataType, getExplorerLink } from '../../utils/getExplorerLink'
import { LoadingRows } from './styled'
import { useContractWrite } from '@starknet-react/core'
import { cairo, Call, validateAndParseAddress } from 'starknet'
import { cairo, Call, CallData, validateAndParseAddress } from 'starknet'
import { DEFAULT_CHAIN_ID, MAX_UINT128, NONFUNGIBLE_POOL_MANAGER_ADDRESS } from 'constants/tokens'

const PositionPageButtonPrimary = styled(ButtonPrimary)`
Expand Down Expand Up @@ -395,8 +395,11 @@ function CollectFees(props) {
txHash,
isCollectPending,
showCollectAsWeth,
parsedTokenId,
} = props
const [feeValue0, feeValue1] = useStaticFeeResults(poolAddress, owner, position, showCollectAsWeth)
const [feeValue0, feeValue1] = useStaticFeeResults(poolAddress, owner, position, showCollectAsWeth, parsedTokenId)

const theme = useTheme()

const feeValueUpper = inverted ? feeValue0 : feeValue1
const feeValueLower = inverted ? feeValue1 : feeValue0
Expand Down Expand Up @@ -700,7 +703,7 @@ function PositionPageContent() {

const collectFeeParams = {
tokenId: cairo.uint256(tokenId),
recipient: account,
recipient: address,
amount0_max: MAX_UINT128,
amount1_max: MAX_UINT128,
}
Expand Down Expand Up @@ -930,6 +933,7 @@ function PositionPageContent() {
txHash={txHash}
isCollectPending={isCollectPending}
showCollectAsWeth={showCollectAsWeth}
parsedTokenId={parsedTokenId}
/>
) : null}
</AutoColumn>
Expand Down

0 comments on commit d397f51

Please sign in to comment.