Skip to content

Commit

Permalink
FIL-368 Fetch client remaining data cap (#152)
Browse files Browse the repository at this point in the history
* FIL-368 Fetch client remaining DataCap
  • Loading branch information
lukasz-wal authored Oct 31, 2024
1 parent 5c3e357 commit 1d02be5
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 4 deletions.
35 changes: 31 additions & 4 deletions src/components/cards/AppInfoCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ const AppInfoCard: React.FC<ComponentProps> = ({
mutationChangeAllowedSPsApproval,
} = useApplicationActions(initialApplication, repo, owner)

const { getAllowanceFromClientContract } = useWallet()
const [buttonText, setButtonText] = useState('')
const [modalMessage, setModalMessage] = useState<ReactNode | null>(null)
const [error, setError] = useState<boolean>(false)
Expand Down Expand Up @@ -199,10 +200,32 @@ const AppInfoCard: React.FC<ComponentProps> = ({
unit: amountType,
isDialogOpen: false,
})

const address = application.Lifecycle['On Chain Address']
const response = await getAllowanceForClient(address)

let clientAllowance

const contractAddress = application['Client Contract Address'] ?? address

const response = await getAllowanceForClient(contractAddress)

if (application['Client Contract Address']) {
clientAllowance = await getAllowanceFromClientContract(
address,
application['Client Contract Address'],
)
}

if (response.success) {
const allowance = parseFloat(response.data ?? 0)
const allowanceResult = clientAllowance
? Number(clientAllowance) > parseFloat(response.data)
? response.data
: clientAllowance.toString()
: response.data

const allowance = parseFloat(
allowanceResult.length ? allowanceResult : '0',
)
const lastAllocation = getLastDatacapAllocation(application)
if (lastAllocation === undefined) return

Expand Down Expand Up @@ -252,7 +275,11 @@ const AppInfoCard: React.FC<ComponentProps> = ({
}
}
})()
}, [application, isApplicationUpdatedLessThanOneMinuteAgo])
}, [
application,
isApplicationUpdatedLessThanOneMinuteAgo,
getAllowanceFromClientContract,
])

useEffect(() => {
if (
Expand Down Expand Up @@ -361,7 +388,7 @@ const AppInfoCard: React.FC<ComponentProps> = ({
return
}

if (isApiCalling && application.Lifecycle.State !== 'ChangingSp') {
if (isApiCalling) {
setButtonText('Processing...')
return
}
Expand Down
40 changes: 40 additions & 0 deletions src/hooks/useWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ interface WalletState {
clientAddress: string,
contractAddress: string,
) => Promise<string[]>
getAllowanceFromClientContract: (
clientAddress: string,
contractAddress: string,
) => Promise<bigint>
submitClientAllowedSpsAndMaxDeviation: (
clientAddress: string,
contractAddress: string,
Expand Down Expand Up @@ -749,6 +753,41 @@ const useWallet = (): WalletState => {
[],
)

const getAllowanceFromClientContract = useCallback(
async (client: string, contractAddress: string): Promise<bigint> => {
const abi = parseAbi([
'function allowances(address client) external view returns (uint256)',
])

const [evmClientAddress, evmContractAddress] = await Promise.all([
getEvmAddressFromFilecoinAddress(client),
getEvmAddressFromFilecoinAddress(contractAddress),
])

const calldataHex: Hex = encodeFunctionData({
abi,
args: [evmClientAddress.data],
})

const response = await makeStaticEthCall(
evmContractAddress.data,
calldataHex,
)

if (response.error) {
return BigInt(0)
}

const decodedData = decodeFunctionResult({
abi,
data: response.data as `0x${string}`,
})

return decodedData
},
[],
)

const getClientConfig = useCallback(
async (client: string, contractAddress: string): Promise<string | null> => {
const abi = parseAbi([
Expand Down Expand Up @@ -1054,6 +1093,7 @@ const useWallet = (): WalletState => {
getClientConfig,
getChangeSpsProposalTxs,
sendClientIncreaseAllowance,
getAllowanceFromClientContract,
}
}

Expand Down

0 comments on commit 1d02be5

Please sign in to comment.