Skip to content

Commit

Permalink
sardine call data (#49)
Browse files Browse the repository at this point in the history
* sardine call data

* path fix

* fix tokenDecimals type for sardine api call

* onClickCheckout fix

* clean up chainid
  • Loading branch information
SamueleA authored May 29, 2024
1 parent 4fdbf5e commit a9f0740
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 70 deletions.
59 changes: 38 additions & 21 deletions examples/react/src/components/Connected.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ import {
useWriteContract
} from 'wagmi'

import { messageToSign } from '../constants'
import { abi } from '../constants/nft-abi'
import { delay, getCheckoutSettings } from '../utils'
import { messageToSign, abi } from '../constants'
import { delay, getCheckoutSettings, getOrderbookCalldata } from '../utils'

// append ?debug to url to enable debug mode
const searchParams = new URLSearchParams(location.search)
Expand Down Expand Up @@ -242,22 +241,36 @@ export const Connected = () => {
})
}

// const onClickCheckout = () => {
// setIsCheckoutInfoModalOpen(true)
// }
const onClickCheckout = () => {
setIsCheckoutInfoModalOpen(true)
}

const onCheckoutInfoConfirm = () => {
setIsCheckoutInfoModalOpen(false)
if (checkoutOrderId !== '' && checkoutTokenContractAddress !== '' && checkoutTokenId !== '') {
const checkoutSettings = getCheckoutSettings(
checkoutOrderId,
address || '',
checkoutTokenContractAddress,
checkoutTokenId,
ChainId.POLYGON,
1,
true
)
const chainId = ChainId.POLYGON
const orderbookAddress = '0xB537a160472183f2150d42EB1c3DD6684A55f74c'
const recipientAddress = address || ''
const nftQuantity = '1'

const checkoutSettings = getCheckoutSettings({
chainId,
contractAddress: orderbookAddress,
recipientAddress,
currencyQuantity: '100000',
currencySymbol: 'USDC',
currencyAddress: '0x3c499c542cef5e3811e1192ce70d8cc03d5c3359',
currencyDecimals: '6',
nftId: checkoutTokenId,
nftAddress: checkoutTokenContractAddress,
nftQuantity,
isDev: true,
calldata: getOrderbookCalldata({
orderId: checkoutOrderId,
quantity: nftQuantity,
recipient: recipientAddress
}),
})
triggerCheckout(checkoutSettings)
}
}
Expand Down Expand Up @@ -363,13 +376,17 @@ export const Connected = () => {
)}

{isDebugMode && (
<CardButton title="Generate EthAuth proof" description="Generate EthAuth proof" onClick={generateEthAuthProof} />
<>
<CardButton title="Generate EthAuth proof" description="Generate EthAuth proof" onClick={generateEthAuthProof} />

<CardButton
title="NFT Checkout"
description="Set orderbook order id, token contract address and token id to test checkout (on Polygon)"
onClick={onClickCheckout}
/>
</>
)}
{/* <CardButton
title="NFT Checkout"
description="Set orderbook order id, token contract address and token id to test checkout (on Polygon)"
onClick={onClickCheckout}
/> */}

<CardButton
title="Switch network"
description={`Current network: ${networkForCurrentChainId.title}`}
Expand Down
1 change: 1 addition & 0 deletions examples/react/src/constants/orderbook-abi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const orderbookAbi = [{"inputs":[{"internalType":"uint256","name":"requestId","type":"uint256"},{"internalType":"uint256","name":"quantity","type":"uint256"},{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256[]","name":"additionalFees","type":"uint256[]"},{"internalType":"address[]","name":"additionalFeeRecipients","type":"address[]"}],"name":"acceptRequest","outputs":[],"stateMutability":"nonpayable","type":"function"}]
74 changes: 53 additions & 21 deletions examples/react/src/utils/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { CheckoutSettings } from '@0xsequence/kit-checkout'
import { Address } from 'viem'
import { Hex, encodeFunctionData } from 'viem'

import { orderbookAbi } from '../constants/orderbook-abi'

export const truncateAtMiddle = (text: string, truncateAt: number) => {
let finalText = text
Expand All @@ -19,35 +21,65 @@ export const delay = (ms: number) => {
return new Promise(resolve => setTimeout(resolve, ms))
}

export const getCheckoutSettings = (
blockchainNftId: string,
recipientAddress: string | Address,
tokenContractAddress: string,
tokenId: string,
chainId: number,
quantity: number,
export interface GetCheckoutSettings {
chainId: number
contractAddress: string
recipientAddress: string
currencyQuantity: string
currencySymbol: string
currencyAddress: string
currencyDecimals: string
nftId: string
nftAddress: string
nftQuantity: string
calldata: string
nftDecimals?: string
isDev?: boolean
) => {
}

export const getCheckoutSettings = (args: GetCheckoutSettings) => {
const checkoutSettings: CheckoutSettings = {
sardineCheckout: {
chainId,
creditCardCheckout: {
defaultPaymentMethodType: 'us_debit',
platform: 'horizon',
contractAddress: '0xB537a160472183f2150d42EB1c3DD6684A55f74c',
blockchainNftId: blockchainNftId,
recipientAddress: recipientAddress,
quantity,
isDev
onSuccess: (hash) => { console.log('credit card checkout success', hash) },
onError: (e) => { console.log('credit card checkout error', e) },
...args
},
orderSummaryItems: [
{
chainId,
contractAddress: tokenContractAddress,
tokenId,
quantityRaw: String(quantity)
chainId: args.chainId,
contractAddress: args.nftAddress,
tokenId: args.nftId,
quantityRaw: String(args.nftQuantity)
}
]
}

return checkoutSettings
}

export interface GetOrderbookCalldata {
orderId: string,
quantity: string,
recipient: string,
}

export const getOrderbookCalldata = ({
orderId,
quantity,
recipient,
}: GetOrderbookCalldata) => {
const calldata = encodeFunctionData({
abi: orderbookAbi,
functionName: 'acceptRequest',
args: [
BigInt(orderId),
BigInt(quantity),
recipient as Hex,
[],
[]
]
})

return calldata
}
17 changes: 11 additions & 6 deletions packages/checkout/src/api/data.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TokenMetadata } from '@0xsequence/metadata'
import { ChainId, networks } from '@0xsequence/network'

import { SardineCheckout } from '../contexts/CheckoutModal'
import { CreditCardCheckout } from '../contexts'

export interface FetchSardineClientTokenReturn {
token: string
Expand All @@ -13,7 +13,7 @@ export interface MethodArguments {
}

export const fetchSardineClientToken = async (
order: SardineCheckout,
order: CreditCardCheckout,
isDev: boolean,
projectAccessKey: string,
tokenMetadata?: TokenMetadata
Expand Down Expand Up @@ -46,12 +46,17 @@ export const fetchSardineClientToken = async (
imageUrl: tokenMetadata?.image || 'https://www.sequence.market/images/placeholder.png',
network: networks[order.chainId as ChainId].name,
recipientAddress: order.recipientAddress,
platform: 'horizon',
blockchainNftId: order.blockchainNftId,
contractAddress: order.contractAddress,
platform: "calldata_execution",
executionType: 'smart_contract',
quantity: Number(order.quantity),
decimals: Number(order.decimals)
blockchainNftId: order.nftId,
quantity: Number(order.nftQuantity),
decimals: Number(order?.nftDecimals || 0),
tokenAmount: order.currencyQuantity,
tokenAddress: order.currencyAddress,
tokenSymbol: order.currencySymbol,
tokenDecimals: Number(order.currencyDecimals),
callData: order.calldata,
}
})
})
Expand Down
21 changes: 13 additions & 8 deletions packages/checkout/src/contexts/CheckoutModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,27 @@ interface OrderSummaryItem {
tokenId: string
}

export interface SardineCheckout {
export interface CreditCardCheckout {
defaultPaymentMethodType: 'us_debit' | 'us_credit' | 'international_debit' | 'international_credit' | 'ach'
chainId: number
platform: string
contractAddress: string
blockchainNftId: string
recipientAddress: string
quantity: number
decimals?: number
onSuccess?: (transactionHash: string, settings: SardineCheckout) => void
onError?: (error: Error, settings: SardineCheckout) => void
currencyQuantity: string
currencySymbol: string
currencyAddress: string
currencyDecimals: string
nftId: string
nftAddress: string
nftQuantity: string
nftDecimals?: string
calldata: string
onSuccess?: (transactionHash: string, settings: CreditCardCheckout) => void
onError?: (error: Error, settings: CreditCardCheckout) => void
isDev?: boolean
}

export interface CheckoutSettings {
sardineCheckout?: SardineCheckout
creditCardCheckout?: CreditCardCheckout
cryptoCheckout?: {
chainId: number
triggerTransaction: () => void
Expand Down
12 changes: 6 additions & 6 deletions packages/checkout/src/views/CheckoutSelection/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const CheckoutSelection = () => {
const projectAccessKey = useProjectAccessKey()

const cryptoCheckoutSettings = settings?.cryptoCheckout
const creditCardCheckoutSettings = settings?.sardineCheckout
const creditCardCheckoutSettings = settings?.creditCardCheckout
const displayCreditCardCheckout = !!creditCardCheckoutSettings
const displayCryptoCheckout = !!cryptoCheckoutSettings

Expand Down Expand Up @@ -64,17 +64,17 @@ export const CheckoutSelection = () => {

const orderSummaryItems = settings?.orderSummaryItems || []

const chainId = settings?.cryptoCheckout?.chainId || settings?.sardineCheckout?.chainId || 1
const chainId = settings?.cryptoCheckout?.chainId || settings?.creditCardCheckout?.chainId || 1

const { data: tokensMetadata } = useTokenMetadata(chainId, orderSummaryItems[0].contractAddress, [orderSummaryItems[0].tokenId])
const tokenMetadata = tokensMetadata ? tokensMetadata[0] : undefined

const triggerSardineTransaction = async () => {
console.log('trigger sardine transaction')

if (settings?.sardineCheckout) {
const isDev = settings?.sardineCheckout?.isDev || false
const { token, orderId } = await fetchSardineClientToken(settings.sardineCheckout, isDev, projectAccessKey, tokenMetadata)
if (settings?.creditCardCheckout) {
const isDev = settings?.creditCardCheckout?.isDev || false
const { token, orderId } = await fetchSardineClientToken(settings.creditCardCheckout, isDev, projectAccessKey, tokenMetadata)

setNavigation({
location: 'transaction-pending',
Expand All @@ -84,7 +84,7 @@ export const CheckoutSelection = () => {
}

const onClickPayWithCard = () => {
if (settings?.sardineCheckout) {
if (settings?.creditCardCheckout) {
triggerSardineTransaction()
} else {
setNavigation({
Expand Down
4 changes: 2 additions & 2 deletions packages/checkout/src/views/PendingTransaction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ export const PendingTransaction = () => {
const { setNavigation } = nav
const projectAccessKey = useProjectAccessKey()

const isDev = settings?.sardineCheckout?.isDev || false
const isDev = settings?.creditCardCheckout?.isDev || false
const url = isDev
? `https://crypto.sandbox.sardine.ai/?client_token=${authToken}&show_features=true`
: `https://crypto.sardine.ai/?client_token=${authToken}&show_features=true`

const pollForOrderStatus = async () => {
try {
console.log('Polling for transaction status')
const isDev = settings?.sardineCheckout?.isDev || false
const isDev = settings?.creditCardCheckout?.isDev || false

const pollResponse = await fetchSardineOrderStatus(orderId, isDev, projectAccessKey)
const status = pollResponse.resp.status
Expand Down
2 changes: 1 addition & 1 deletion packages/checkout/src/views/TransactionError.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const TransactionError = () => {
useEffect(() => {
setTimeout(() => {
closeCheckout()
settings?.sardineCheckout?.onError && settings?.sardineCheckout?.onError(navigation.params.error, settings?.sardineCheckout)
settings?.creditCardCheckout?.onError && settings?.creditCardCheckout?.onError(navigation.params.error, settings?.creditCardCheckout)
}, 3000)
}, [])

Expand Down
10 changes: 5 additions & 5 deletions packages/checkout/src/views/TransactionSuccess.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ export const TransactionSuccess = () => {
const nav = useNavigation()
const navigation = nav.navigation as TransactionSuccessNavigation

const chainId = settings?.sardineCheckout?.chainId || ChainId.POLYGON
const chainId = settings?.creditCardCheckout?.chainId || ChainId.POLYGON
const network = sequence.network.allNetworks.find(n => n.chainId === chainId)

useEffect(() => {
settings?.sardineCheckout?.onSuccess &&
settings?.sardineCheckout?.onSuccess(navigation.params.transactionHash, settings?.sardineCheckout)
settings?.sardineCheckout?.onSuccess &&
settings?.sardineCheckout?.onSuccess(navigation.params.transactionHash, settings?.sardineCheckout)
settings?.creditCardCheckout?.onSuccess &&
settings?.creditCardCheckout?.onSuccess(navigation.params.transactionHash, settings?.creditCardCheckout)
settings?.creditCardCheckout?.onSuccess &&
settings?.creditCardCheckout?.onSuccess(navigation.params.transactionHash, settings?.creditCardCheckout)
}, [])

return (
Expand Down

0 comments on commit a9f0740

Please sign in to comment.