-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NO-CHANGELOG] Adds Smart Checkout to state and flow of Sale Widget (#…
- Loading branch information
1 parent
5f45c15
commit e4b8b11
Showing
8 changed files
with
260 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
packages/checkout/widgets-lib/src/widgets/sale/hooks/useSmartCheckout.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { Web3Provider } from '@ethersproject/providers'; | ||
import { | ||
Checkout, | ||
ERC20ItemRequirement, | ||
GasAmount, | ||
GasTokenType, | ||
ItemType, | ||
SmartCheckoutResult, | ||
TransactionOrGasType, | ||
} from '@imtbl/checkout-sdk'; | ||
import { BigNumber } from 'ethers'; | ||
import { useCallback, useState } from 'react'; | ||
import { Item, SaleErrorTypes, SmartCheckoutError } from '../types'; | ||
|
||
type UseSmartCheckoutInput = { | ||
checkout: Checkout | undefined; | ||
provider: Web3Provider | undefined; | ||
items: Item[], | ||
amount: string, | ||
contractAddress: string, | ||
}; | ||
|
||
const MAX_GAS_LIMIT = '30000000'; | ||
|
||
const getItemRequirements = (amount: string, spenderAddress: string, contractAddress: string) | ||
: ERC20ItemRequirement[] => [ | ||
{ | ||
type: ItemType.ERC20, | ||
contractAddress, | ||
spenderAddress, | ||
amount, | ||
}, | ||
]; | ||
|
||
const getGasEstimate = (): GasAmount => ({ | ||
type: TransactionOrGasType.GAS, | ||
gasToken: { | ||
type: GasTokenType.NATIVE, | ||
limit: BigNumber.from(MAX_GAS_LIMIT), | ||
}, | ||
}); | ||
|
||
export const useSmartCheckout = ({ | ||
checkout, provider, items, amount, contractAddress, | ||
}: UseSmartCheckoutInput) => { | ||
const [smartCheckoutResult, setSmartCheckoutResult] = useState<SmartCheckoutResult | undefined>( | ||
undefined, | ||
); | ||
const [smartCheckoutError, setSmartCheckoutError] = useState<SmartCheckoutError | undefined>( | ||
undefined, | ||
); | ||
|
||
const smartCheckout = useCallback(async () => { | ||
if (!checkout || !provider) { | ||
return undefined; | ||
} | ||
|
||
const signer = provider.getSigner(); | ||
const spenderAddress = await signer?.getAddress() || ''; | ||
|
||
const itemRequirements = getItemRequirements(amount, spenderAddress, contractAddress); | ||
const gasEstimate = getGasEstimate(); | ||
|
||
try { | ||
const res = await checkout.smartCheckout( | ||
{ | ||
provider, | ||
itemRequirements, | ||
transactionOrGasAmount: gasEstimate, | ||
}, | ||
); | ||
|
||
setSmartCheckoutResult(res); | ||
return res; | ||
} catch (err: any) { | ||
setSmartCheckoutError({ | ||
type: SaleErrorTypes.SMART_CHECKOUT_ERROR, | ||
data: { error: err }, | ||
}); | ||
} | ||
return undefined; | ||
}, [checkout, provider, items, amount, contractAddress]); | ||
|
||
return { | ||
smartCheckout, smartCheckoutResult, smartCheckoutError, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.