-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5e8ca4a
commit 3b6a0f4
Showing
12 changed files
with
588 additions
and
276 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import Centrifuge from '@centrifuge/sdk' | ||
export const centrifuge = new Centrifuge({ environment: 'demo' }) |
66 changes: 66 additions & 0 deletions
66
sdk-consumer/src/components/Transactions/TransactionToasts.tsx
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,66 @@ | ||
import { Stack, Toast, ToastStatus } from '@centrifuge/fabric' | ||
import { useTransactions } from './TransactionsProvider' | ||
|
||
const toastStatus: { [key: string]: ToastStatus } = { | ||
creating: 'pending', | ||
unconfirmed: 'pending', | ||
pending: 'pending', | ||
succeeded: 'ok', | ||
failed: 'critical', | ||
} | ||
|
||
const toastSublabel = { | ||
creating: 'Creating transaction', | ||
unconfirmed: 'Signing transaction', | ||
pending: 'Transaction pending', | ||
succeeded: 'Transaction successful', | ||
failed: 'Transaction failed', | ||
} | ||
|
||
const TOAST_DURATION = 10000 | ||
const ERROR_TOAST_DURATION = 60000 | ||
|
||
export type TransactionToastsProps = { | ||
positionProps?: { | ||
top?: number | string | ||
right?: number | string | ||
bottom?: number | string | ||
left?: number | string | ||
width?: number | string | ||
zIndex?: number | string | ||
} | ||
} | ||
|
||
export function TransactionToasts({ | ||
positionProps = { | ||
top: 64, | ||
right: 1, | ||
}, | ||
}: TransactionToastsProps) { | ||
const { transactions, updateTransaction } = useTransactions() | ||
|
||
const dismiss = (txId: string) => () => updateTransaction(txId, { dismissed: true }) | ||
|
||
return ( | ||
<Stack gap={2} position="fixed" width={330} zIndex="onTopOfTheWorld" {...positionProps}> | ||
{transactions | ||
.filter((tx) => !tx.dismissed && !['creating', 'unconfirmed'].includes(tx.status)) | ||
.map((tx) => { | ||
return ( | ||
<Toast | ||
label={tx.title} | ||
sublabel={(tx.status === 'failed' && tx.failedReason) || toastSublabel[tx.status]} | ||
status={toastStatus[tx.status]} | ||
onDismiss={dismiss(tx.id)} | ||
onStatusChange={(newStatus) => { | ||
if (['ok', 'critical'].includes(newStatus)) { | ||
setTimeout(dismiss(tx.id), newStatus === 'ok' ? TOAST_DURATION : ERROR_TOAST_DURATION) | ||
} | ||
}} | ||
key={tx.id} | ||
/> | ||
) | ||
})} | ||
</Stack> | ||
) | ||
} |
83 changes: 83 additions & 0 deletions
83
sdk-consumer/src/components/Transactions/TransactionsProvider.tsx
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,83 @@ | ||
import * as React from 'react' | ||
import { TransactionToasts } from './TransactionToasts' | ||
|
||
export type TransactionStatus = 'creating' | 'unconfirmed' | 'pending' | 'succeeded' | 'failed' | ||
export type Transaction = { | ||
id: string | ||
title: string | ||
status: TransactionStatus | ||
hash?: string | ||
result?: any | ||
failedReason?: string | ||
error?: any | ||
dismissed?: boolean | ||
} | ||
|
||
type TransactionsContextType = { | ||
transactions: Transaction[] | ||
addTransaction: (tx: Transaction) => void | ||
addOrUpdateTransaction: (tx: Transaction) => void | ||
updateTransaction: (id: string, update: Partial<Transaction> | ((prev: Transaction) => Partial<Transaction>)) => void | ||
} | ||
|
||
const TransactionsContext = React.createContext<TransactionsContextType>(null as any) | ||
|
||
type TransactionProviderProps = { | ||
children: React.ReactNode | ||
} | ||
|
||
export function TransactionProvider({ children }: TransactionProviderProps) { | ||
const [transactions, setTransactions] = React.useState<Transaction[]>([]) | ||
|
||
const addTransaction = React.useCallback((tx: Transaction) => { | ||
setTransactions((prev) => [...prev, tx]) | ||
}, []) | ||
|
||
const updateTransaction = React.useCallback( | ||
(id: string, update: Partial<Transaction> | ((prev: Transaction) => Partial<Transaction>)) => { | ||
setTransactions((prev) => | ||
prev.map((tx) => | ||
tx.id === id ? { ...tx, dismissed: false, ...(typeof update === 'function' ? update(tx) : update) } : tx | ||
) | ||
) | ||
}, | ||
[] | ||
) | ||
|
||
const addOrUpdateTransaction = React.useCallback((tx: Transaction) => { | ||
setTransactions((prev) => { | ||
if (prev.find((t) => t.id === tx.id)) { | ||
return prev.map((t) => (t.id === tx.id ? { ...t, dismissed: false, ...tx } : t)) | ||
} | ||
return [...prev, tx] | ||
}) | ||
}, []) | ||
|
||
const ctx: TransactionsContextType = React.useMemo( | ||
() => ({ | ||
transactions, | ||
addTransaction, | ||
updateTransaction, | ||
addOrUpdateTransaction, | ||
}), | ||
[transactions, addTransaction, updateTransaction, addOrUpdateTransaction] | ||
) | ||
|
||
return ( | ||
<TransactionsContext.Provider value={ctx}> | ||
{children} | ||
<TransactionToasts /> | ||
</TransactionsContext.Provider> | ||
) | ||
} | ||
|
||
export function useTransactions() { | ||
const ctx = React.useContext(TransactionsContext) | ||
if (!ctx) throw new Error('useTransactions must be used within Provider') | ||
return ctx | ||
} | ||
|
||
export function useTransaction(id?: string) { | ||
const { transactions } = useTransactions() | ||
return id ? transactions.find((tx) => tx.id === id) : null | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,11 @@ | ||
import { createConfig, http } from 'wagmi' | ||
import { base, mainnet } from 'wagmi/chains' | ||
import { sepolia } from 'wagmi/chains' | ||
import { injected } from 'wagmi/connectors' | ||
|
||
export const wagmiConfig = createConfig({ | ||
chains: [mainnet, base], | ||
chains: [sepolia], | ||
connectors: [injected()], | ||
transports: { | ||
[mainnet.id]: http(), | ||
[base.id]: http(), | ||
[sepolia.id]: http(), | ||
}, | ||
}) |
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,13 @@ | ||
import { useMemo } from 'react' | ||
import { useAccount } from 'wagmi' | ||
import { centrifuge } from '../centrifuge' | ||
import { useCentrifugeQuery } from './useCentrifugeQuery' | ||
|
||
const tUSD = '0x8503b4452Bf6238cC76CdbEE223b46d7196b1c93' | ||
|
||
export function useAccountBalance() { | ||
const { address } = useAccount() | ||
const balance$ = useMemo(() => (address ? centrifuge.balance(tUSD, address) : undefined), [address]) | ||
console.log('balance$', balance$) | ||
return useCentrifugeQuery(balance$) | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.