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-417] Handle threshold bigger than 1 #158

Merged
merged 1 commit into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
23 changes: 12 additions & 11 deletions src/hooks/useApplicationActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,8 +467,6 @@ const useApplicationActions = (

let proposalAllocationAmount = ''

const addressToGrantDataCap = clientContractAddress ?? clientAddress

if (allocationAmount) {
proposalAllocationAmount = allocationAmount
} else {
Expand All @@ -483,16 +481,18 @@ const useApplicationActions = (
}

const proposalTx = await getProposalTx(
addressToGrantDataCap,
clientAddress,
proposalAllocationAmount,
allocatorType,
!!clientContractAddress,
clientContractAddress,
)

if (proposalTx?.pendingVerifyClientTransaction) {
throw new Error('This datacap allocation is already proposed')
}

const addressToGrantDataCap = clientContractAddress ?? clientAddress

const messageCID = await sendProposal({
allocatorType,
contractAddress:
Expand Down Expand Up @@ -596,10 +596,11 @@ const useApplicationActions = (
async ({ requestId, userName }) => {
setMessage(`Searching the pending transactions...`)

let clientAddress = getClientAddress()
const clientAddress = getClientAddress()

let clientAddressAddress
if (initialApplication['Client Contract Address']) {
clientAddress = initialApplication['Client Contract Address']
clientAddressAddress = initialApplication['Client Contract Address']
}

const activeRequest = initialApplication['Allocation Requests'].find(
Expand All @@ -614,7 +615,7 @@ const useApplicationActions = (
clientAddress,
datacap,
allocatorType,
!!initialApplication['Client Contract Address'],
clientAddressAddress,
)

if (!proposalTx?.pendingVerifyClientTransaction) {
Expand Down Expand Up @@ -667,17 +668,17 @@ const useApplicationActions = (

if (
!proposalTx?.pendingIncreaseAllowanceTransaction &&
initialApplication['Client Contract Address']
clientAddressAddress
) {
throw new Error(
'This increase allowance is not proposed yet. You may need to wait some time if the proposal was just sent.',
)
} else {
} else if (clientAddressAddress) {
const increaseMessageCID = await sendApproval(
proposalTx?.pendingIncreaseAllowanceTransaction,
)

if (messageCID == null) {
if (increaseMessageCID == null) {
throw new Error(
'Error sending proposal. Please try again or contact support.',
)
Expand All @@ -687,7 +688,7 @@ const useApplicationActions = (
`Checking the 'verify client' transaction, it may take a few minutes, please wait... Do not close this window.`,
)

const response = await getStateWaitMsg(messageCID)
const response = await getStateWaitMsg(increaseMessageCID)

if (
typeof response.data === 'object' &&
Expand Down
133 changes: 64 additions & 69 deletions src/hooks/useWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ interface WalletState {
clientAddress: string,
datacap: string,
allocatorType: AllocatorTypeEnum,
isClientContractAddress?: boolean,
clientContractAddress?: string | null,
) => Promise<{
pendingVerifyClientTransaction: any
pendingIncreaseAllowanceTransaction: any
Expand Down Expand Up @@ -252,7 +252,7 @@ const useWallet = (): WalletState => {
clientAddress: string,
datacap: string,
allocatorType: AllocatorTypeEnum,
isClientContractAddress?: boolean,
clientContractAddress?: string | null,
): Promise<{
pendingVerifyClientTransaction: any
pendingIncreaseAllowanceTransaction: any
Expand All @@ -261,9 +261,7 @@ const useWallet = (): WalletState => {
if (multisigAddress == null) throw new Error('Multisig address not set.')

const bytesDatacap = Math.floor(anyToBytes(datacap))
let evmClientContractAddress
let pendingTxs

try {
pendingTxs = await wallet.api.pendingTransactions(multisigAddress)
} catch (error) {
Expand All @@ -273,15 +271,6 @@ const useWallet = (): WalletState => {
)
}

let pendingVerifyClientTransaction
let pendingIncreaseAllowanceTransaction

if (isClientContractAddress) {
evmClientContractAddress = (
await getEvmAddressFromFilecoinAddress(clientAddress)
).data
}

const verifiedAbi = parseAbi([
'function addVerifiedClient(bytes clientAddress, uint256 amount)',
])
Expand All @@ -290,83 +279,89 @@ const useWallet = (): WalletState => {
'function increaseAllowance(address client, uint256 amount)',
])

if (allocatorType !== AllocatorTypeEnum.CONTRACT) {
const pendingForClient = pendingTxs?.filter(
(tx: any) =>
tx?.parsed?.params?.address === clientAddress &&
tx?.parsed?.params?.cap === BigInt(bytesDatacap),
)
pendingVerifyClientTransaction = pendingForClient.length
? pendingForClient.at(-1)
: undefined
} else {
for (const transaction of pendingTxs) {
if (!transaction.parsed?.params) {
continue
let pendingVerifyClientTransaction
let pendingIncreaseAllowanceTransaction

for (const transaction of pendingTxs) {
if (!transaction.parsed?.params) {
continue
}

let paramsHex: string
let dataHex: Hex
if (clientContractAddress && !pendingIncreaseAllowanceTransaction) {
paramsHex = transaction.parsed.params.toString('hex')
dataHex = `0x${paramsHex}`
try {
const increaseDecodedData = decodeFunctionData({
abi: increaseAllowanceAbi,
data: dataHex,
})

const [increaseClientAddress, increaseAmount] =
increaseDecodedData.args

const evmClientAddress = (
await getEvmAddressFromFilecoinAddress(clientAddress)
).data

if (
increaseClientAddress.toLocaleLowerCase() === evmClientAddress &&
increaseAmount === BigInt(bytesDatacap)
) {
pendingIncreaseAllowanceTransaction = transaction
continue
}
} catch (err) {
console.error(err)
}
const paramsHex: string = transaction.parsed.params.toString('hex')
const dataHex: Hex = `0x${paramsHex}`
let decodedData
}

if (!pendingVerifyClientTransaction) {
if (!pendingVerifyClientTransaction) {
if (allocatorType !== AllocatorTypeEnum.CONTRACT) {
const addressToGrantDataCap = clientContractAddress ?? clientAddress

if (
transaction?.parsed?.params?.address === addressToGrantDataCap &&
transaction?.parsed?.params?.cap === BigInt(bytesDatacap)
) {
pendingVerifyClientTransaction = transaction
continue
}
} else {
paramsHex = transaction.parsed.params.toString('hex')
dataHex = `0x${paramsHex}`
try {
decodedData = decodeFunctionData({
const decodedData = decodeFunctionData({
abi: verifiedAbi,
data: dataHex,
})

const [clientAddressData, amount] = decodedData.args
const address = newFromString(clientAddress)
const address = newFromString(
clientContractAddress ?? clientAddress,
)
const addressHex: Hex = `0x${Buffer.from(address.bytes).toString('hex')}`

if (
clientAddressData === addressHex &&
amount === BigInt(bytesDatacap)
) {
pendingVerifyClientTransaction = transaction
continue
}
} catch (err) {
console.error(err)
}
}
}

try {
if (
isClientContractAddress &&
evmClientContractAddress &&
!pendingIncreaseAllowanceTransaction
) {
const increaseDecodedData = decodeFunctionData({
abi: increaseAllowanceAbi,
data: dataHex,
})

const [increaseClientContractAddress, increaseAmount] =
increaseDecodedData.args

if (
increaseClientContractAddress.toLocaleLowerCase() ===
evmClientContractAddress &&
increaseAmount === BigInt(bytesDatacap)
) {
pendingIncreaseAllowanceTransaction = transaction
}
}
} catch (err) {
console.error(err)
}

if (!isClientContractAddress && pendingVerifyClientTransaction) {
break
}

if (
isClientContractAddress &&
pendingVerifyClientTransaction &&
pendingIncreaseAllowanceTransaction
) {
break
}
if (
(!clientContractAddress && pendingVerifyClientTransaction) ||
(pendingVerifyClientTransaction &&
pendingIncreaseAllowanceTransaction)
) {
break
}
}

Expand Down