-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: mini-app transactions,
frog/vercel
deprecation (wevm#503)
* feat: mini-app transaction requests and response listening * nit: lint * refactor: jsonrpc, split next and web * nit: lint * feat: `JsonRpcError` added * nit: lint * nit: better error * nit: error * chore: changesets * chore: up changeset * chore: changesets up * nit: remove `frog/next` * nit: stuff * nit: lint
- Loading branch information
Showing
34 changed files
with
417 additions
and
74 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
--- | ||
"frog": minor | ||
--- | ||
|
||
**Breaking Change**: `frog/vercel` was deleted. If you used `handle` from this package, import it from `frog/next`. | ||
**Breaking Change:** `frog/next` no longer exports `postComposerCreateCastActionMessage`. Use `createCast` from `frog/web`. | ||
|
||
Introduced `frog/web` for client-side related logic in favor of `frog/next`. | ||
For backwards compatibility, all the previous exports are kept, but will be | ||
deprecated in future, except for NextJS related `handle` function. | ||
|
||
Added functionality for the Mini-App JSON-RPC requests. [See more](https://warpcast.notion.site/Miniapp-Transactions-1216a6c0c10180b7b9f4eec58ec51e55). | ||
Added `createCast`, `sendTransaction`, `contractTransaction` and `signTypedData` to `frog/web`. |
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
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 |
---|---|---|
@@ -1,10 +1 @@ | ||
// TODO: Rename this package to `js` as most of it doesn't strictly depend | ||
// on Next.JS specific features. Only `handle` does. | ||
|
||
export { getFrameMetadata } from './getFrameMetadata.js' | ||
export { handle } from '../vercel/index.js' | ||
export { isFrameRequest } from './isFrameRequest.js' | ||
export { | ||
postComposerActionMessage, | ||
postComposerCreateCastActionMessage, | ||
} from './postComposerActionMessage.js' | ||
export { handle } from './handle.js' |
This file was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,68 @@ | ||
import { | ||
type Abi, | ||
AbiFunctionNotFoundError, | ||
type ContractFunctionArgs, | ||
type ContractFunctionName, | ||
type EncodeFunctionDataParameters, | ||
type GetAbiItemParameters, | ||
encodeFunctionData, | ||
getAbiItem, | ||
} from 'viem' | ||
import type { ContractTransactionParameters } from '../../types/transaction.js' | ||
import type { JsonRpcResponseError } from './internal/jsonRpc/types.js' | ||
import { postSendTransactionRequestMessage } from './internal/postSendTransactionRequestMessage.js' | ||
import { | ||
type EthSendTransactionSuccessBody, | ||
waitForSendTransactionResponse, | ||
} from './internal/waitForSendTransactionResponse.js' | ||
|
||
type ContractTransactionReturnType = EthSendTransactionSuccessBody | ||
type ContractTransactionErrorType = JsonRpcResponseError | ||
export type { | ||
ContractTransactionParameters, | ||
ContractTransactionReturnType, | ||
ContractTransactionErrorType, | ||
} | ||
|
||
export async function contractTransaction< | ||
const abi extends Abi | readonly unknown[], | ||
functionName extends ContractFunctionName<abi, 'nonpayable' | 'payable'>, | ||
args extends ContractFunctionArgs< | ||
abi, | ||
'nonpayable' | 'payable', | ||
functionName | ||
>, | ||
>( | ||
parameters: ContractTransactionParameters<abi, functionName, args>, | ||
requestIdOverride?: string, | ||
): Promise<ContractTransactionReturnType> { | ||
const { abi, chainId, functionName, gas, to, args, attribution, value } = | ||
parameters | ||
|
||
const abiItem = getAbiItem({ | ||
abi: abi, | ||
name: functionName, | ||
args, | ||
} as GetAbiItemParameters) | ||
if (!abiItem) throw new AbiFunctionNotFoundError(functionName) | ||
|
||
const abiErrorItems = (abi as Abi).filter((item) => item.type === 'error') | ||
|
||
const requestId = postSendTransactionRequestMessage( | ||
{ | ||
abi: [abiItem, ...abiErrorItems], | ||
attribution, | ||
chainId, | ||
data: encodeFunctionData({ | ||
abi, | ||
args, | ||
functionName, | ||
} as EncodeFunctionDataParameters), | ||
gas, | ||
to, | ||
value, | ||
}, | ||
requestIdOverride, | ||
) | ||
return waitForSendTransactionResponse(requestId) | ||
} |
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,20 @@ | ||
import type { JsonRpcResponseError } from './internal/jsonRpc/types.js' | ||
import { | ||
type CreateCastRequestMessageParameters, | ||
postCreateCastRequestMessage, | ||
} from './internal/postCreateCastRequestMessage.js' | ||
import type { FcCreateCastSuccessBody } from './internal/waitForCreateCastResponse.js' | ||
import { waitForCreateCastResponse } from './internal/waitForCreateCastResponse.js' | ||
|
||
type CreateCastParameters = CreateCastRequestMessageParameters | ||
type CreateCastReturnType = FcCreateCastSuccessBody | ||
type CreateCastErrorType = JsonRpcResponseError | ||
export type { CreateCastParameters, CreateCastReturnType, CreateCastErrorType } | ||
|
||
export async function createCast( | ||
parameters: CreateCastParameters, | ||
requestIdOverride?: string, | ||
): Promise<CreateCastReturnType> { | ||
const requestId = postCreateCastRequestMessage(parameters, requestIdOverride) | ||
return waitForCreateCastResponse(requestId) | ||
} |
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,10 @@ | ||
export class JsonRpcError extends Error { | ||
code: number | ||
requestId: string | ||
constructor(requestId: string, code: number, message: string) { | ||
super(message) | ||
this.name = 'JsonRpcError' | ||
this.code = code | ||
this.requestId = requestId | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/web/actions/internal/jsonRpc/listenForJsonRpcResponseMessage.ts
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,27 @@ | ||
import type { JsonRpcResponseFailure, JsonRpcResponseSuccess } from './types.js' | ||
|
||
export function listenForJsonRpcResponseMessage<resultType>( | ||
handler: ( | ||
message: JsonRpcResponseSuccess<resultType> | JsonRpcResponseFailure, | ||
) => unknown, | ||
requestId: string, | ||
) { | ||
if (typeof window === 'undefined') | ||
throw new Error( | ||
'`listenForJsonRpcResponseMessage` must be called in the Client Component.', | ||
) | ||
|
||
const listener = ( | ||
event: MessageEvent< | ||
JsonRpcResponseSuccess<resultType> | JsonRpcResponseFailure | ||
>, | ||
) => { | ||
if (event.data.id !== requestId) return | ||
|
||
handler(event.data) | ||
} | ||
|
||
window.parent.addEventListener('message', listener) | ||
|
||
return () => window.parent.removeEventListener('message', listener) | ||
} |
26 changes: 26 additions & 0 deletions
26
src/web/actions/internal/jsonRpc/postJsonRpcRequestMessage.ts
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,26 @@ | ||
import type { JsonRpcMethod } from './types.js' | ||
|
||
export type PostJsonRpcRequestMessageReturnType = string | ||
|
||
export function postJsonRpcRequestMessage( | ||
method: JsonRpcMethod, | ||
parameters: any, | ||
requestIdOverride?: string, | ||
): PostJsonRpcRequestMessageReturnType { | ||
if (typeof window === 'undefined') | ||
throw new Error( | ||
'`postJsonRpcRequestMessage` must be called in the Client Component.', | ||
) | ||
|
||
const requestId = requestIdOverride ?? crypto.randomUUID() | ||
window.parent.postMessage( | ||
{ | ||
jsonrpc: '2.0', | ||
id: requestId, | ||
method, | ||
params: parameters, | ||
}, | ||
'*', | ||
) | ||
return requestId | ||
} |
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,18 @@ | ||
export type JsonRpcResponseSuccess<resultType> = { | ||
jsonrpc: '2.0' | ||
id: string | number | null | ||
result: resultType | ||
} | ||
|
||
export type JsonRpcResponseError = { | ||
code: number | ||
message: string | ||
} | ||
|
||
export type JsonRpcResponseFailure = { | ||
jsonrpc: '2.0' | ||
id: string | number | null | ||
error: JsonRpcResponseError | ||
} | ||
|
||
export type JsonRpcMethod = 'fc_requestWalletAction' | 'fc_createCast' |
18 changes: 18 additions & 0 deletions
18
src/web/actions/internal/jsonRpc/waitForJsonRpcResponse.ts
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,18 @@ | ||
import { JsonRpcError } from './errors.js' | ||
import { listenForJsonRpcResponseMessage } from './listenForJsonRpcResponseMessage.js' | ||
|
||
export function waitForJsonRpcResponse<resultType>( | ||
requestId: string, | ||
): Promise<resultType> { | ||
return new Promise<resultType>((resolve, reject) => { | ||
listenForJsonRpcResponseMessage<resultType>((message) => { | ||
if ('result' in message) { | ||
resolve(message.result) | ||
return | ||
} | ||
reject( | ||
new JsonRpcError(requestId, message.error.code, message.error.message), | ||
) | ||
}, requestId) | ||
}) | ||
} |
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,23 @@ | ||
import { | ||
type PostJsonRpcRequestMessageReturnType, | ||
postJsonRpcRequestMessage, | ||
} from './jsonRpc/postJsonRpcRequestMessage.js' | ||
|
||
export type CreateCastRequestMessageParameters = { | ||
embeds: string[] | ||
text: string | ||
} | ||
|
||
export type CreateCastRequestMessageReturnType = | ||
PostJsonRpcRequestMessageReturnType | ||
|
||
export function postCreateCastRequestMessage( | ||
parameters: CreateCastRequestMessageParameters, | ||
requestIdOverride?: string, | ||
) { | ||
return postJsonRpcRequestMessage( | ||
'fc_createCast', | ||
parameters, | ||
requestIdOverride, | ||
) | ||
} |
Oops, something went wrong.