Skip to content
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

#251 Add ABI encoding for raw input form #256

Merged
merged 40 commits into from
Nov 27, 2024

Conversation

nevendyulgerov
Copy link
Contributor

@nevendyulgerov nevendyulgerov commented Oct 14, 2024

I rebranded the form and reorganised related files for it under packages/ui/src/GenericInputForm.

Summary of the changes:

  • Changed form name to Generic Input
  • Added ABI to Hex mode and slightly changed labels for the other modes to resemble those from cartesi-cli
  • Added fields for the ABI to Hex mode as follows:
    • ABI method select for choosing whether to use existing specifications or create a new one
      • When the existing specification option is selected:
        • Select with options for choosing an existing specification is displayed
        • Select with options for choosing a function from that existing specification is displayed
        • Text fields for the parameters for the selected function are displayed
      • When the new specification option is selected:
        • Segmented control is displayed with 2 options - JSON ABI and ABI Parameters:
          • When JSON ABI is selected:
            • Textarea for the human ABI is displayed
            • Select with options for choosing a function from the filled human ABI is displayed
            • Text fields for the parameters for the selected function are displayed
          • When ABI Parameters is selected:
            • Text field for the ABI params is displayed
            • Text fields for the parameters from the filled ABI params are displayed
  • For encoding the parameters, I'm using encodeAbiParameters from viem.
  • I used as a reference for the implementation cartesi-cli and more specifically https://github.com/cartesi/cli/blob/main/apps/cli/src/commands/send/generic.ts

@nevendyulgerov nevendyulgerov self-assigned this Oct 14, 2024
Copy link

vercel bot commented Oct 14, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
rollups-explorer-arbitrum-mainnet ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 25, 2024 8:32am
rollups-explorer-arbitrum-sepolia ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 25, 2024 8:32am
rollups-explorer-base-mainnet ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 25, 2024 8:32am
rollups-explorer-base-sepolia ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 25, 2024 8:32am
rollups-explorer-mainnet ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 25, 2024 8:32am
rollups-explorer-optimism-mainnet ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 25, 2024 8:32am
rollups-explorer-optimism-sepolia ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 25, 2024 8:32am
rollups-explorer-sepolia ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 25, 2024 8:32am
rollups-explorer-workshop ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 25, 2024 8:32am

@nevendyulgerov nevendyulgerov added Type: Feature Add a new feature to the system apps: web labels Oct 14, 2024
@nevendyulgerov nevendyulgerov linked an issue Oct 14, 2024 that may be closed by this pull request
8 tasks
@nevendyulgerov nevendyulgerov marked this pull request as ready for review October 14, 2024 11:06
Copy link
Collaborator

@brunomenezes brunomenezes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @nevendyulgerov,
There is a problem when ABI encoding uses a JSON_ABI format. I notice in the index.ts#encodeFunctionParamsDebounced() is doing the encoding, but the underneath function assumes it is an ABI parameter (i.e. on-the-fly definition), but that is not what we want for JSON_ABI because it will not include the 4-bytes selector that is the function signature for the JSON_ABI format; Therefore, both the DApp backend will fail to parse its payload and also CartesiScan will fail to do the same using the JSON_ABI specification saved.

I'll use the onChess ABI as an example:
network deployed: 84532 (base-sepolia)
Address: 0xc93796ff6ed6b8d15d68ecb793df221ecf042774

Getting the input index 13, we have an input encoded by the function move that expects two arguments (address game, string move)

Suppose I follow the same steps through the form. I expect the same encoded value to be generated as the rawInput that goes to the InputBox. But it generates a different output.

Expected: 0x17799d270000000000000000000000002189f5ec6516583b6a0cddf47c712355316d0ff6000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000026436000000000000000000000000000000000000000000000000000000000000

Generated: 0x0000000000000000000000002189f5ec6516583b6a0cddf47c712355316d0ff6000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000026436000000000000000000000000000000000000000000000000000000000000

It is just the function that needs to change based on the ABI definition chosen (ABI Parameters or JSON-ABI). For the latter the 4-bytes selector needs to be included because it tells the decoding function what ABI's function to pick.

I added information about the onChess app + the ABI so you can play around. Reach out if you have questions.

onChess ABI

function create(uint256 bet, string timeControl, uint32 minRating, uint32 maxRating) returns (address)
function cancel()
function move(address game, string move)
function resign(address game)
function claim(address game)
function withdraw(uint256 amount)
function withdrawRake()
function setRakeDivider(uint32 rake)
function transferOwnership(address newOwner)
function upgrade(address dapp)
[
 {
   "name": "create",
   "type": "function",
   "stateMutability": "nonpayable",
   "inputs": [
     {
       "type": "uint256",
       "name": "bet"
     },
     {
       "type": "string",
       "name": "timeControl"
     },
     {
       "type": "uint32",
       "name": "minRating"
     },
     {
       "type": "uint32",
       "name": "maxRating"
     }
   ],
   "outputs": [
     {
       "type": "address"
     }
   ]
 },
 {
   "name": "cancel",
   "type": "function",
   "stateMutability": "nonpayable",
   "inputs": [],
   "outputs": []
 },
 {
   "name": "move",
   "type": "function",
   "stateMutability": "nonpayable",
   "inputs": [
     {
       "type": "address",
       "name": "game"
     },
     {
       "type": "string",
       "name": "move"
     }
   ],
   "outputs": []
 },
 {
   "name": "resign",
   "type": "function",
   "stateMutability": "nonpayable",
   "inputs": [
     {
       "type": "address",
       "name": "game"
     }
   ],
   "outputs": []
 },
 {
   "name": "claim",
   "type": "function",
   "stateMutability": "nonpayable",
   "inputs": [
     {
       "type": "address",
       "name": "game"
     }
   ],
   "outputs": []
 },
 {
   "name": "withdraw",
   "type": "function",
   "stateMutability": "nonpayable",
   "inputs": [
     {
       "type": "uint256",
       "name": "amount"
     }
   ],
   "outputs": []
 },
 {
   "name": "withdrawRake",
   "type": "function",
   "stateMutability": "nonpayable",
   "inputs": [],
   "outputs": []
 },
 {
   "name": "setRakeDivider",
   "type": "function",
   "stateMutability": "nonpayable",
   "inputs": [
     {
       "type": "uint32",
       "name": "rake"
     }
   ],
   "outputs": []
 },
 {
   "name": "transferOwnership",
   "type": "function",
   "stateMutability": "nonpayable",
   "inputs": [
     {
       "type": "address",
       "name": "newOwner"
     }
   ],
   "outputs": []
 },
 {
   "name": "upgrade",
   "type": "function",
   "stateMutability": "nonpayable",
   "inputs": [
     {
       "type": "address",
       "name": "dapp"
     }
   ],
   "outputs": []
 }
]

FormValues,
FormTransformedValues,
} from "./types";
import { prepareSignatures } from "web/src/components/specification/utils";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a boundary leak in the sense that packages know about apps. A change in the apps/web in this function would have an unexpected effect on the packages/ui. That also creates an odd "circular" dependency: web depends on pkg/ui, but pkg/ui also depends on web.

I think it would be sensible to copy the prepareSignature to this utils file and use it here and also in the validations.ts (I added a comment there.)

Do you agree @nevendyulgerov?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, @brunomenezes , I refactored it.

import { isAddress, isHex, parseAbi, parseAbiParameters } from "viem";
import { FormValues } from "./types";
import { isBlank } from "ramda-adjunct";
import { prepareSignatures } from "web/src/components/specification/utils";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

highlighted reasons here

@brunomenezes brunomenezes merged commit 038b071 into main Nov 27, 2024
23 of 24 checks passed
@brunomenezes brunomenezes deleted the feature/251-add-abi-encoding-for-raw-input-form branch November 27, 2024 20:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
apps: web Type: Feature Add a new feature to the system
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add ABI encoding capability to send raw input
2 participants