Skip to content

Commit

Permalink
feat: wallet_sendCalls
Browse files Browse the repository at this point in the history
  • Loading branch information
jxom committed Nov 13, 2024
1 parent 0c6016e commit 47e9f41
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
53 changes: 53 additions & 0 deletions playground/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export function App() {
<Register />
<Login />
<SendTransaction />
<SendCalls />

<hr />

Expand Down Expand Up @@ -153,3 +154,55 @@ function SendTransaction() {
</div>
)
}

function SendCalls() {
const [result, setResult] = useState<string | null>(null)
return (
<div>
<h3>wallet_sendCalls</h3>
<button
onClick={async () => {
const [account] = await oddworld.provider.request({
method: 'eth_accounts',
})
const hash = await oddworld.provider.request({
method: 'wallet_sendCalls',
params: [
{
calls: [
{
to: ExperimentERC20.address,
data: encodeFunctionData({
abi: ExperimentERC20.abi,
functionName: 'approve',
args: [account, parseEther('50')],
}),
},
{
to: ExperimentERC20.address,
data: encodeFunctionData({
abi: ExperimentERC20.abi,
functionName: 'transferFrom',
args: [
account,
'0x0000000000000000000000000000000000000000',
parseEther('50'),
],
}),
},
],
from: account,
version: '1',
},
],
})
setResult(hash)
}}
type="button"
>
Approve + Transfer 50 EXP
</button>
<pre>{result}</pre>
</div>
)
}
25 changes: 25 additions & 0 deletions src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export function create(
async request({ method, params }) {
switch (method) {
case 'eth_accounts':
if (accounts.length === 0) throw new Provider.DisconnectedError()
return accounts.map((account) => account.address)

case 'eth_chainId':
Expand Down Expand Up @@ -121,6 +122,30 @@ export function create(
case 'oddworld_ping':
return 'pong'

case 'wallet_sendCalls': {
if (!headless) throw new Provider.UnsupportedMethodError()
if (accounts.length === 0) throw new Provider.DisconnectedError()

const [{ calls, from }] = params as RpcSchema.ExtractParams<
Schema,
'wallet_sendCalls'
>

if (!from)
throw new RpcResponse.InvalidParamsError({
...RpcResponse.InvalidParamsError,
message: 'Missing required parameter: from',
})

const account = accounts.find((account) => account.address === from)
if (!account) throw new Provider.UnauthorizedError()

return await AccountDelegation.execute(client, {
account,
calls: calls as AccountDelegation.Calls,
})
}

default:
if (method.startsWith('wallet_'))
throw new Provider.UnsupportedMethodError()
Expand Down
6 changes: 5 additions & 1 deletion src/internal/accountDelegation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ export type Account = {
keys: readonly Key[]
}

export type Calls = { to: Address.Address; value?: bigint; data?: Hex.Hex }[]
export type Calls = readonly {
to: Address.Address
value?: bigint | undefined
data?: Hex.Hex | undefined
}[]

type Key_Base<type extends string, properties> = properties & {
expiry: bigint
Expand Down

0 comments on commit 47e9f41

Please sign in to comment.