Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIL-368 Fetch client remaining data cap #152

Merged
merged 3 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 { getClientAllowance } = 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 getClientAllowance(
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,
getClientAllowance,
])

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[]>
getClientAllowance: (
clientAddress: string,
contractAddress: string,
) => Promise<bigint>
submitClientAllowedSpsAndMaxDeviation: (
clientAddress: string,
contractAddress: string,
Expand Down Expand Up @@ -749,6 +753,41 @@ const useWallet = (): WalletState => {
[],
)

const getClientAllowance = useCallback(
lukasz-wal marked this conversation as resolved.
Show resolved Hide resolved
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,
getClientAllowance,
}
}

Expand Down