-
Notifications
You must be signed in to change notification settings - Fork 0
feat(react-kit): decode wallet_sendCalls batch transactions in signing UI #118
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
Merged
+284
−1
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,236 @@ | ||
| import { | ||
| type Address, | ||
| erc20Abi, | ||
| formatEther, | ||
| formatUnits, | ||
| type Hex, | ||
| maxUint256, | ||
| } from 'viem' | ||
| import { useReadContract } from 'wagmi' | ||
| import type { BatchCall } from '../../types.js' | ||
| import { SigningActions } from '../components/SigningActions.js' | ||
| import { | ||
| decodeCollectionApproval, | ||
| isCollectionApproval, | ||
| } from '../utils/collectionApproval.js' | ||
| import { decodeErc20Approval, isErc20Approval } from '../utils/erc20Approval.js' | ||
| import { decodeErc20Transfer, isErc20Transfer } from '../utils/erc20Transfer.js' | ||
| import { isEthTransfer } from '../utils/ethTransfer.js' | ||
|
|
||
| interface BatchCallsProps { | ||
| calls: BatchCall[] | ||
| confirm: () => void | ||
| reject: () => void | ||
| } | ||
|
|
||
| function EthTransferItem({ to, value }: { to: Address; value: Hex }) { | ||
| return ( | ||
| <div className="rounded-lg bg-gray-50 p-4 border border-gray-100"> | ||
| <p className="text-sm font-medium text-gray-700">Send ETH</p> | ||
| <p className="text-lg font-bold text-gray-900"> | ||
| {formatEther(BigInt(value))} ETH | ||
| </p> | ||
| <div className="mt-1 text-sm text-gray-500"> | ||
| <span className="font-medium">To: </span> | ||
| <span className="font-mono break-all">{to}</span> | ||
| </div> | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| function Erc20TransferItem({ | ||
| contract, | ||
| to, | ||
| amount, | ||
| }: { | ||
| contract: Address | ||
| to: Address | ||
| amount: bigint | ||
| }) { | ||
| const { data: symbol } = useReadContract({ | ||
| address: contract, | ||
| abi: erc20Abi, | ||
| functionName: 'symbol', | ||
| }) | ||
| const { data: decimals } = useReadContract({ | ||
| address: contract, | ||
| abi: erc20Abi, | ||
| functionName: 'decimals', | ||
| }) | ||
|
|
||
| const formatted = | ||
| decimals != null ? formatUnits(amount, decimals) : String(amount) | ||
| const label = symbol ?? contract | ||
|
|
||
| return ( | ||
| <div className="rounded-lg bg-gray-50 p-4 border border-gray-100"> | ||
| <p className="text-sm font-medium text-gray-700">Send {label}</p> | ||
| <p className="text-lg font-bold text-gray-900"> | ||
| {formatted} {symbol ?? ''} | ||
| </p> | ||
| <div className="mt-1 text-sm text-gray-500"> | ||
| <span className="font-medium">To: </span> | ||
| <span className="font-mono break-all">{to}</span> | ||
| </div> | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| function Erc20ApprovalItem({ | ||
| contract, | ||
| spender, | ||
| amount, | ||
| }: { | ||
| contract: Address | ||
| spender: Address | ||
| amount: bigint | ||
| }) { | ||
| const { data: symbol } = useReadContract({ | ||
| address: contract, | ||
| abi: erc20Abi, | ||
| functionName: 'symbol', | ||
| }) | ||
| const { data: decimals } = useReadContract({ | ||
| address: contract, | ||
| abi: erc20Abi, | ||
| functionName: 'decimals', | ||
| }) | ||
|
|
||
| const isUnlimited = amount === maxUint256 | ||
| const formatted = isUnlimited | ||
| ? 'Unlimited' | ||
| : decimals != null | ||
| ? `${formatUnits(amount, decimals)} ${symbol ?? ''}` | ||
| : String(amount) | ||
|
|
||
| return ( | ||
| <div className="rounded-lg bg-gray-50 p-4 border border-gray-100"> | ||
| <p className="text-sm font-medium text-gray-700"> | ||
| Approve {symbol ?? contract} | ||
| </p> | ||
| <p className="text-lg font-bold text-gray-900">{formatted}</p> | ||
| <div className="mt-1 text-sm text-gray-500"> | ||
| <span className="font-medium">Spender: </span> | ||
| <span className="font-mono break-all">{spender}</span> | ||
| </div> | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| function CollectionApprovalItem({ | ||
| contract, | ||
| operator, | ||
| approved, | ||
| }: { | ||
| contract: Address | ||
| operator: Address | ||
| approved: boolean | ||
| }) { | ||
| return ( | ||
| <div className="rounded-lg bg-gray-50 p-4 border border-gray-100"> | ||
| <p className="text-sm font-medium text-gray-700"> | ||
| {approved ? 'Approve Collection' : 'Revoke Collection Approval'} | ||
| </p> | ||
| <div className="mt-1 text-sm text-gray-500"> | ||
| <span className="font-medium">Contract: </span> | ||
| <span className="font-mono break-all">{contract}</span> | ||
| </div> | ||
| <div className="mt-1 text-sm text-gray-500"> | ||
| <span className="font-medium">Operator: </span> | ||
| <span className="font-mono break-all">{operator}</span> | ||
| </div> | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| function UnknownCallItem({ call }: { call: BatchCall }) { | ||
| return ( | ||
| <div className="rounded-lg bg-gray-50 p-4 border border-gray-100"> | ||
| <p className="text-sm font-medium text-gray-700">Contract Call</p> | ||
| {call.to && ( | ||
| <div className="mt-1 text-sm text-gray-500"> | ||
| <span className="font-medium">To: </span> | ||
| <span className="font-mono break-all">{call.to}</span> | ||
| </div> | ||
| )} | ||
| {call.value && BigInt(call.value) > 0n && ( | ||
| <div className="mt-1 text-sm text-gray-500"> | ||
| <span className="font-medium">Value: </span> | ||
| <span>{formatEther(BigInt(call.value))} ETH</span> | ||
| </div> | ||
| )} | ||
| {call.data && ( | ||
| <div className="mt-1 text-sm text-gray-500"> | ||
| <span className="font-medium">Data: </span> | ||
| <span className="font-mono break-all">{call.data}</span> | ||
| </div> | ||
| )} | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| function CallItem({ call }: { call: BatchCall }) { | ||
| const tx = call as Parameters<typeof isEthTransfer>[0] | ||
|
|
||
| if (isEthTransfer(tx)) { | ||
| return <EthTransferItem to={call.to as Address} value={call.value as Hex} /> | ||
| } | ||
|
|
||
| if (isErc20Transfer(tx)) { | ||
| const decoded = decodeErc20Transfer(tx) | ||
| if (decoded) { | ||
| return ( | ||
| <Erc20TransferItem | ||
| contract={call.to as Address} | ||
| to={decoded.to} | ||
| amount={decoded.amount} | ||
| /> | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| if (isErc20Approval(tx)) { | ||
| const decoded = decodeErc20Approval(tx) | ||
| if (decoded) { | ||
| return ( | ||
| <Erc20ApprovalItem | ||
| contract={call.to as Address} | ||
| spender={decoded.spender} | ||
| amount={decoded.amount} | ||
| /> | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| if (isCollectionApproval(tx)) { | ||
| const decoded = decodeCollectionApproval(tx) | ||
| if (decoded) { | ||
| return ( | ||
| <CollectionApprovalItem | ||
| contract={call.to as Address} | ||
| operator={decoded.operator} | ||
| approved={decoded.approved} | ||
| /> | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| return <UnknownCallItem call={call} /> | ||
| } | ||
|
|
||
| export function BatchCalls({ calls, confirm, reject }: BatchCallsProps) { | ||
| return ( | ||
| <div className="flex flex-col gap-3"> | ||
| <h3 className="text-lg font-semibold text-gray-900"> | ||
| Batch Transaction ({calls.length}{' '} | ||
| {calls.length === 1 ? 'call' : 'calls'}) | ||
| </h3> | ||
|
|
||
| {calls.map((call, i) => ( | ||
| <CallItem key={`${call.to ?? 'unknown'}-${i}`} call={call} /> | ||
| ))} | ||
|
|
||
| <SigningActions confirm={confirm} reject={reject} /> | ||
| </div> | ||
| ) | ||
| } | ||
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are these items meant to be components in different files?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure yet. Depends on how they are going to be presented in the designs. I think they may be just separate expandable cards - for now it makes sense keeping them here.