mirror of
https://github.com/zoo-labs/zoo.git
synced 2026-11-10 22:32:34 +00:00
257 lines
8.0 KiB
TypeScript
257 lines
8.0 KiB
TypeScript
import { useCallback, useMemo, useState } from 'react'
|
|
import { AddressZero, HashZero } from '@ethersproject/constants'
|
|
import { Contract } from '@ethersproject/contracts'
|
|
import { splitSignature } from '@ethersproject/bytes'
|
|
|
|
import { useBentoMasterContractAllowed } from '../state/bentobox/hooks'
|
|
import { useActiveWeb3React, useBentoBoxContract } from './index'
|
|
import { useAllTransactions, useTransactionAdder } from '../state/transactions/hooks'
|
|
import { signMasterContractApproval } from '../entities/KashiCooker'
|
|
|
|
export enum BentoApprovalState {
|
|
UNKNOWN,
|
|
NOT_APPROVED,
|
|
PENDING,
|
|
FAILED,
|
|
APPROVED,
|
|
}
|
|
|
|
export enum BentoApproveOutcome {
|
|
SUCCESS,
|
|
REJECTED,
|
|
FAILED,
|
|
NOT_READY,
|
|
}
|
|
|
|
const useBentoHasPendingApproval = (masterContract: string, account: string, contractName?: string) => {
|
|
const allTransactions = useAllTransactions()
|
|
return useMemo(
|
|
() =>
|
|
typeof masterContract === 'string' &&
|
|
typeof account === 'string' &&
|
|
Object.keys(allTransactions).some((hash) => {
|
|
const tx = allTransactions[hash]
|
|
if (!tx) return false
|
|
if (tx.receipt) {
|
|
return false
|
|
} else {
|
|
const summary = tx.summary
|
|
if (!summary) return false
|
|
return summary === `Approving ${contractName} Master Contract`
|
|
}
|
|
}),
|
|
[allTransactions, account, masterContract]
|
|
)
|
|
}
|
|
|
|
export interface BentoPermit {
|
|
outcome: BentoApproveOutcome
|
|
signature?: { v: number; r: string; s: string }
|
|
data?: string
|
|
}
|
|
|
|
export interface BentoMasterApproveCallback {
|
|
approvalState: BentoApprovalState
|
|
approve: () => Promise<void>
|
|
getPermit: () => Promise<BentoPermit>
|
|
permit: BentoPermit
|
|
}
|
|
|
|
export interface BentoMasterApproveCallbackOptions {
|
|
otherBentoBoxContract?: Contract | null
|
|
contractName?: string
|
|
functionFragment?: string
|
|
}
|
|
|
|
// const useBentoMasterApproveCallback = (
|
|
// masterContract: string,
|
|
// { otherBentoBoxContract, contractName, functionFragment }: BentoMasterApproveCallbackOptions
|
|
// ): BentoMasterApproveCallback => {
|
|
// const { account, chainId, library } = useActiveWeb3React()
|
|
// const bentoBoxContract = useBentoBoxContract()
|
|
// const addTransaction = useTransactionAdder()
|
|
// const currentAllowed = useBentoMasterContractAllowed(masterContract, account || AddressZero)
|
|
// const pendingApproval = useBentoHasPendingApproval(masterContract, account, contractName)
|
|
// const [permit, setPermit] = useState<BentoPermit>(null)
|
|
//
|
|
// const approvalState: BentoApprovalState = useMemo(() => {
|
|
// if (permit) return BentoApprovalState.APPROVED
|
|
// if (pendingApproval) return BentoApprovalState.PENDING
|
|
//
|
|
// // We might not have enough data to know whether or not we need to approve
|
|
// if (currentAllowed === undefined) return BentoApprovalState.UNKNOWN
|
|
// if (!masterContract || !account) return BentoApprovalState.UNKNOWN
|
|
// if (!currentAllowed) return BentoApprovalState.NOT_APPROVED
|
|
//
|
|
// return BentoApprovalState.APPROVED
|
|
// }, [account, currentAllowed, masterContract, pendingApproval, permit])
|
|
//
|
|
// const getPermit = useCallback(async () => {
|
|
// if (approvalState !== BentoApprovalState.NOT_APPROVED) {
|
|
// console.error('approve was called unnecessarily')
|
|
// return
|
|
// }
|
|
//
|
|
// if (!masterContract) {
|
|
// console.error('masterContract is null')
|
|
// return
|
|
// }
|
|
//
|
|
// if (!account) {
|
|
// console.error('no account')
|
|
// return
|
|
// }
|
|
//
|
|
// try {
|
|
// const signature = await signMasterContractApproval(
|
|
// bentoBoxContract,
|
|
// masterContract,
|
|
// account,
|
|
// library,
|
|
// true,
|
|
// chainId
|
|
// )
|
|
//
|
|
// const { v, r, s } = splitSignature(signature)
|
|
// const permit = {
|
|
// outcome: BentoApproveOutcome.SUCCESS,
|
|
// signature: { v, r, s },
|
|
// data: (otherBentoBoxContract || bentoBoxContract)?.interface?.encodeFunctionData(
|
|
// functionFragment || 'setMasterContractApproval',
|
|
// [account, masterContract, true, v, r, s]
|
|
// ),
|
|
// }
|
|
//
|
|
// setPermit(permit)
|
|
// return permit
|
|
// } catch (e: any) {
|
|
// return {
|
|
// outcome: e.code === 4001 ? BentoApproveOutcome.REJECTED : BentoApproveOutcome.FAILED,
|
|
// }
|
|
// }
|
|
// }, [
|
|
// account,
|
|
// approvalState,
|
|
// bentoBoxContract,
|
|
// chainId,
|
|
// functionFragment,
|
|
// library,
|
|
// masterContract,
|
|
// otherBentoBoxContract,
|
|
// ])
|
|
//
|
|
// const approve = useCallback(async () => {
|
|
// try {
|
|
// const tx = await bentoBoxContract?.setMasterContractApproval(account, masterContract, true, 0, HashZero, HashZero)
|
|
//
|
|
// return addTransaction(tx, {
|
|
// summary: `Approving ${contractName} Master Contract`,
|
|
// })
|
|
// } catch (e) {}
|
|
// }, [account, addTransaction, bentoBoxContract, contractName, masterContract])
|
|
//
|
|
// return {
|
|
// approvalState,
|
|
// approve,
|
|
// getPermit,
|
|
// permit,
|
|
// }
|
|
// }
|
|
|
|
const useBentoMasterApproveCallback = (
|
|
masterContract: string,
|
|
{ otherBentoBoxContract, contractName, functionFragment }: BentoMasterApproveCallbackOptions
|
|
): BentoMasterApproveCallback => {
|
|
const { account, chainId, library } = useActiveWeb3React();
|
|
const bentoBoxContract = useBentoBoxContract();
|
|
const addTransaction = useTransactionAdder();
|
|
const currentAllowed = useBentoMasterContractAllowed(masterContract, account || AddressZero);
|
|
const pendingApproval = useBentoHasPendingApproval(masterContract, account, contractName);
|
|
const [permit, setPermit] = useState<BentoPermit>({} as BentoPermit);
|
|
|
|
const approvalState: BentoApprovalState = useMemo(() => {
|
|
if (typeof permit !== 'function' && permit) return BentoApprovalState.APPROVED;
|
|
if (pendingApproval) return BentoApprovalState.PENDING;
|
|
|
|
// We might not have enough data to know whether or not we need to approve
|
|
if (currentAllowed === undefined) return BentoApprovalState.UNKNOWN;
|
|
if (!masterContract || !account) return BentoApprovalState.UNKNOWN;
|
|
if (!currentAllowed) return BentoApprovalState.NOT_APPROVED;
|
|
|
|
return BentoApprovalState.APPROVED;
|
|
}, [account, currentAllowed, masterContract, pendingApproval, permit]);
|
|
|
|
const getPermit = useCallback(async (): Promise<BentoPermit> => {
|
|
if (approvalState !== BentoApprovalState.NOT_APPROVED) {
|
|
console.error('approve was called unnecessarily');
|
|
throw new Error('approve was called unnecessarily');
|
|
}
|
|
|
|
if (!masterContract) {
|
|
console.error('masterContract is null');
|
|
throw new Error('masterContract is null');
|
|
}
|
|
|
|
if (!account) {
|
|
console.error('no account');
|
|
throw new Error('no account');
|
|
}
|
|
|
|
try {
|
|
const signature = await signMasterContractApproval(
|
|
bentoBoxContract,
|
|
masterContract,
|
|
account,
|
|
library,
|
|
true,
|
|
chainId
|
|
);
|
|
|
|
const { v, r, s } = splitSignature(signature);
|
|
const permit: BentoPermit = {
|
|
outcome: BentoApproveOutcome.SUCCESS,
|
|
signature: { v, r, s },
|
|
data: (otherBentoBoxContract || bentoBoxContract)?.interface?.encodeFunctionData(
|
|
functionFragment || 'setMasterContractApproval',
|
|
[account, masterContract, true, v, r, s]
|
|
),
|
|
};
|
|
|
|
setPermit(() => permit);
|
|
return permit;
|
|
} catch (e: any) {
|
|
return {
|
|
outcome: e.code === 4001 ? BentoApproveOutcome.REJECTED : BentoApproveOutcome.FAILED,
|
|
};
|
|
}
|
|
}, [
|
|
account,
|
|
approvalState,
|
|
bentoBoxContract,
|
|
chainId,
|
|
functionFragment,
|
|
library,
|
|
masterContract,
|
|
otherBentoBoxContract,
|
|
]);
|
|
|
|
const approve = useCallback(async () => {
|
|
try {
|
|
const tx = await bentoBoxContract?.setMasterContractApproval(account, masterContract, true, 0, HashZero, HashZero);
|
|
|
|
return addTransaction(tx, {
|
|
summary: `Approving ${contractName} Master Contract`,
|
|
});
|
|
} catch (e) {}
|
|
}, [account, addTransaction, bentoBoxContract, contractName, masterContract]);
|
|
|
|
return {
|
|
approvalState,
|
|
approve,
|
|
getPermit,
|
|
permit,
|
|
};
|
|
};
|
|
|
|
export default useBentoMasterApproveCallback
|