Skip to content

Commit

Permalink
Merge pull request #183 from okto-hq/main
Browse files Browse the repository at this point in the history
dynamic abi encoder code
  • Loading branch information
oviawork authored Dec 7, 2024
2 parents f5d48b9 + 11e49cd commit 5fba9c7
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 13 deletions.
64 changes: 53 additions & 11 deletions app/tools/components/AbiEncoder.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use client'

import React, { useState } from 'react'
import { ethers, Contract, JsonRpcProvider} from 'ethers'
import { ethers, Contract, JsonRpcProvider } from 'ethers'
import { Button } from '@/components/ui/button'
import { Input } from '@/components/ui/input'
import { Textarea } from '@/components/ui/textarea'
Expand All @@ -22,6 +22,7 @@ export default function AbiEncoder() {
const [providerUrl, setProviderUrl] = useState('')
const [encodedData, setEncodedData] = useState('')
const [error, setError] = useState('')
const [isCopied, setIsCopied] = useState(false)

const handleEncodeData = async () => {
try {
Expand Down Expand Up @@ -51,21 +52,43 @@ export default function AbiEncoder() {
}
}

const copyToClipboard = () => {
navigator.clipboard.writeText(encodedData)
const copyToClipboard = (text: string) => {
navigator.clipboard.writeText(text)
setIsCopied(true)
setTimeout(() => setIsCopied(false), 2000)
}

const codeSnippet = `const { ethers } = require('ethers');
const iface = new ethers.Interface(ABI);
const data = iface.encodeFunctionData("function_name", [arg1, arg2, arg3, ...]);`

const abiPlaceholder = `[
{
"inputs": [
{
"internalType": "uint256",
"name": "num",
"type": "uint256"
}
],
"name": "store",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
]`

return (
<div className="space-y-4">
<h2 className="text-xl font-semibold mb-2">ABI Encoder</h2>
<div className="space-y-4 max-w-full">
{error && (
<div className="text-red-500 bg-red-100 p-2 rounded-md">{error}</div>
)}
<div>
<Label htmlFor="abi-json">ABI JSON</Label>
<Textarea
id="abi-json"
placeholder={`[{"inputs":[{"internalType":"uint256","name":"num","type":"uint256"}],"name":"store","outputs":[],"stateMutability":"nonpayable","type":"function"}]`}
placeholder={abiPlaceholder}
rows={5}
value={abi}
onChange={(e) => setAbi(e.target.value)}
Expand Down Expand Up @@ -99,7 +122,7 @@ export default function AbiEncoder() {
/>
</div>
<div>
<label htmlFor="provider-url">Provider URL</label>
<Label htmlFor="provider-url">Provider URL</Label>
<Select
id="provider-url"
value={providerUrl}
Expand All @@ -116,15 +139,34 @@ export default function AbiEncoder() {
Encode Data
</Button>
{encodedData && (
<div className="mt-4">
<div className="my-3 p-2 bg-gray-100 dark:bg-slate-800 rounded-md break-all max-w-full overflow-x-auto">
<div className="mt-4 space-y-3">
<div className="my-3 p-2 bg-gray-100 dark:bg-slate-800 rounded-md break-all max-w-full overflow-x-auto text-sm">
{encodedData}
</div>
<Button type="button" onClick={copyToClipboard}>
Copy Encoded Data to Clipboard
<Button type="button" onClick={() => copyToClipboard(encodedData)}>
{isCopied ? 'Copied!' : 'Copy Encoded Data to Clipboard'}
</Button>
</div>
)}

{/* Code Snippet Section */}
<div className="mt-6 space-y-2">
<h3 className="text-lg font-semibold">Dynamic Hex Encoding Snippet</h3>
<div className="relative bg-gray-100 dark:bg-slate-800 p-2 rounded-md overflow-x-auto text-sm">
<pre className="font-mono whitespace-pre">
{codeSnippet}
</pre>
<Button
type="button"
onClick={() => copyToClipboard(codeSnippet)}
className="absolute top-2 right-2"
size="sm"
variant="secondary"
>
{isCopied ? 'Copied!' : 'Copy'}
</Button>
</div>
</div>
</div>
)
}
7 changes: 6 additions & 1 deletion app/tools/components/GoogleIdTokenGenerator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default function GoogleIdTokenGenerator() {
const [idToken, setIdToken] = useState('')
const [error, setError] = useState('')
const [isSignedIn, setIsSignedIn] = useState(false)
const [isCopied, setIsCopied] = useState(false)

const ClientId = 'MTA0NjI3MTUyMTE1NS0wbTQ1M3BvaTVndWEwM2tlaGRjbjV1b24xdnZ1NXU5ai5hcHBzLmdvb2dsZXVzZXJjb250ZW50LmNvbQ=='

Expand Down Expand Up @@ -68,6 +69,8 @@ export default function GoogleIdTokenGenerator() {

const handleCopy = () => {
navigator.clipboard.writeText(idToken)
setIsCopied(true)
setTimeout(() => setIsCopied(false), 2000)
}

return (
Expand All @@ -91,7 +94,9 @@ export default function GoogleIdTokenGenerator() {
>
{idToken}
</div>
<Button onClick={handleCopy}>Copy ID Token to Clipboard</Button>
<Button onClick={handleCopy}>
{isCopied ? 'Copied!' : 'Copy ID Token to Clipboard'}
</Button>
</div>
</>
)}
Expand Down
2 changes: 1 addition & 1 deletion app/tools/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export default function ToolsPage() {
</CardHeader>
</Card>
</DialogTrigger>
<DialogContent className="max-w-md">
<DialogContent className="max-w-3xl w-full">
<DialogHeader>
<DialogTitle>{tool.title}</DialogTitle>
</DialogHeader>
Expand Down

0 comments on commit 5fba9c7

Please sign in to comment.