Skip to content

Commit

Permalink
refactor(sdk): typing & error handling (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
red8paw authored May 29, 2024
1 parent 3444985 commit b06b046
Show file tree
Hide file tree
Showing 27 changed files with 1,746 additions and 1,642 deletions.
4 changes: 2 additions & 2 deletions apps/playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"@radix-ui/react-slot": "^1.0.2",
"@radix-ui/react-toast": "^1.1.5",
"@roninnetwork/walletgo": "0.5.10",
"@sky-mavis/mavis-id-sdk": "*",
"@sky-mavis/mavis-id-sdk": "workspace:*",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.1",
"ethers": "5.7.2",
Expand All @@ -34,7 +34,7 @@
"devDependencies": {
"@next/bundle-analyzer": "13.1.6",
"@next/eslint-plugin-next": "14.2.3",
"@sky-mavis/eslint-config-default": "*",
"@sky-mavis/eslint-config-default": "workspace:*",
"@typechain/ethers-v5": "10.0.0",
"@types/node": "20.12.11",
"@types/react": "18.2.38",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const SIGN_DATA = {
domain: {
name: "MarketGateway",
version: "1",
chainId: "2021",
chainId: 2021,
verifyingContract: "0xfff9ce5f71ca6178d3beecedb61e7eff1602950e",
},
primaryType: "Order",
Expand Down
2 changes: 1 addition & 1 deletion apps/playground/src/connectors/MavisIdConnector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class MavisIdConnector extends BaseConnector<MavisIdProvider> {
chainId: chainId,
})

const accounts = await newProvider.request({ method: "eth_requestAccounts" })
const accounts = await newProvider.request<string[]>({ method: "eth_requestAccounts" })

if (accounts.length) {
this.provider = newProvider
Expand Down
4 changes: 0 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
{
"private": true,
"workspaces": [
"packages/*",
"apps/*"
],
"scripts": {
"build": "turbo run build",
"dev": "turbo run dev --no-cache --parallel --continue",
Expand Down
2 changes: 1 addition & 1 deletion packages/mavis-id-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"@rollup/plugin-babel": "6.0.4",
"@rollup/plugin-commonjs": "25.0.7",
"@rollup/plugin-node-resolve": "15.2.3",
"@sky-mavis/eslint-config-default": "*",
"@sky-mavis/eslint-config-default": "workspace:*",
"@types/node": "20.12.11",
"eslint": "8.57.0",
"fs-extra": "11.2.0",
Expand Down
67 changes: 8 additions & 59 deletions packages/mavis-id-sdk/src/common/chain.ts
Original file line number Diff line number Diff line change
@@ -1,60 +1,9 @@
export function toHexChainId(chainId: number): string {
return `0x${chainId.toString(16)}`
}

export enum SupportedChainIds {
Ethereum = 1,
Goerli = 5,
RoninMainnet = 2020,
RoninTestnet = 2021,
}

type IRpcConfig = Record<number, string>

const DEFAULT_RPC_CONFIG: IRpcConfig = {
[SupportedChainIds.RoninMainnet]: "https://api.roninchain.com/rpc",
[SupportedChainIds.RoninTestnet]: "https://saigon-testnet.roninchain.com/rpc",
}

export interface IChainInfo {
chainId: number
blockExplorerUrl?: string
chainName: string
iconUrl?: string
nativeCurrency: {
name: string
symbol: string
decimals: number
}
rpcUrl: string
}

export type IChainsConfig = Record<number, IChainInfo>

export const DEFAULT_CHAINS_CONFIG: IChainsConfig = {
[SupportedChainIds.RoninMainnet]: {
chainId: SupportedChainIds.RoninMainnet,
blockExplorerUrl: "https://app.roninchain.com",
chainName: "Ronin Mainnet",
iconUrl: "https://cdn.skymavis.com/explorer-cdn/asset/favicon/apple-touch-icon.png",
nativeCurrency: {
name: "Ronin",
symbol: "RON",
decimals: 18,
},
rpcUrl: DEFAULT_RPC_CONFIG[SupportedChainIds.RoninMainnet],
},

[SupportedChainIds.RoninTestnet]: {
chainId: SupportedChainIds.RoninTestnet,
blockExplorerUrl: "https://saigon-app.roninchain.com",
chainName: "Saigon Testnet",
iconUrl: "https://cdn.skymavis.com/explorer-cdn/asset/favicon/apple-touch-icon.png",
nativeCurrency: {
name: "tRonin",
symbol: "tRON",
decimals: 18,
},
rpcUrl: DEFAULT_RPC_CONFIG[SupportedChainIds.RoninTestnet],
},
import type { Chain } from "viem"
import { goerli, mainnet, ronin, saigon } from "viem/chains"

export const VIEM_CHAIN_MAPPING: Record<number, Chain> = {
[ronin.id]: ronin,
[saigon.id]: saigon,
[mainnet.id]: mainnet,
[goerli.id]: goerli,
}
9 changes: 0 additions & 9 deletions packages/mavis-id-sdk/src/common/debug.ts

This file was deleted.

41 changes: 0 additions & 41 deletions packages/mavis-id-sdk/src/common/eip1193-event.ts

This file was deleted.

57 changes: 51 additions & 6 deletions packages/mavis-id-sdk/src/common/eip1193.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,55 @@
import { IEip1193EventEmitter } from "./eip1193-event"
import type {
Address,
EIP1193Events,
EIP1193Parameters,
Hash,
Hex,
PublicRpcSchema,
TypedDataDefinition,
} from "viem"

export interface IEip1193RequestArgs {
readonly method: string
readonly params?: Array<any>
import type { GenericTransaction } from "./tx"

export interface Eip1193Provider extends EIP1193Events {
request: <ReturnType = unknown>(
args: EIP1193Parameters<MavisIdRequestSchema>,
) => Promise<ReturnType>
}

export interface IEip1193Provider extends IEip1193EventEmitter {
request: <T = any>(args: IEip1193RequestArgs) => Promise<T>
export type MavisIdRequestSchema = [
...PublicRpcSchema,

{
Method: "eth_accounts"
Parameters?: undefined
ReturnType: Address[]
},
{
Method: "eth_requestAccounts"
Parameters?: undefined
ReturnType: Address[]
},
{
Method: "eth_sendTransaction"
Parameters: [transaction: GenericTransaction]
ReturnType: Hash
},
{
Method: "eth_signTypedData_v4"
Parameters: [address: Address, typedData: TypedDataDefinition]
ReturnType: Hex
},
{
Method: "personal_sign"
Parameters: [data: Hex, address: Address]
ReturnType: Hex
},
]

export enum Eip1193EventName {
accountsChanged = "accountsChanged",
chainChanged = "chainChanged",
connect = "connect",
disconnect = "disconnect",
message = "message",
}
33 changes: 0 additions & 33 deletions packages/mavis-id-sdk/src/common/error.ts

This file was deleted.

14 changes: 0 additions & 14 deletions packages/mavis-id-sdk/src/common/storage.ts

This file was deleted.

76 changes: 45 additions & 31 deletions packages/mavis-id-sdk/src/common/tx.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,56 @@
export type AccessList = Array<{ address: string; storageKeys: Array<string> }>
import type { AccessList, Address, Hex } from "viem"

export type AccessListish =
| AccessList
| Array<[string, Array<string>]>
| Record<string, Array<string>>
export interface GenericTransaction {
// "0x64" for ronin gas sponsor
type?: "0x0" | "0x1" | "0x2" | "0x64"

export type BigNumberish = bigint | string | number
nonce?: Hex
to: Address | null
from?: Address

export type Bytes = ArrayLike<number>
value?: Hex
input?: Hex
data?: Hex

export type BytesLike = Bytes | string
gas?: Hex
gasPrice?: Hex

export const ZERO_TX_DATA = 0
// EIP-2930; Type 1 & EIP-1559; Type 2
accessList?: AccessList

// EIP-1559; Type 2
maxPriorityFeePerGas?: Hex
maxFeePerGas?: Hex
maxFeePerBlobGas?: Hex
blobVersionedHashes?: Array<Hex>
blobs?: Array<Hex>

chainId?: Hex
}

export type IUnsignedTransaction = {
to: string
value: string
from?: string
nonce?: string
data?: string
type?: number
gas?: string
gasPrice?: string
input?: string
s?: string
r?: string
v?: string
chainId?: string
// Typed-Transaction features
export interface FilledTransaction {
// "0x64" for ronin gas sponsor
type: "0x0" | "0x1" | "0x2" | "0x64"

nonce?: Hex
to: Address | null
from: Address

value: Hex
input: Hex

gas?: Hex
gasPrice?: Hex

// EIP-2930; Type 1 & EIP-1559; Type 2
accessList?: AccessListish
accessList: AccessList

// EIP-1559; Type 2
maxPriorityFeePerGas?: BigNumberish
maxFeePerGas?: BigNumberish
// EIP-1559; Type 100
payerS?: string
payerR?: string
payerV?: string
maxPriorityFeePerGas?: Hex
maxFeePerGas?: Hex
maxFeePerBlobGas: Hex
blobVersionedHashes: Array<Hex>
blobs: Array<Hex>

chainId: Hex
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Deferred } from "../utils/defer"
import { JsonRpcError } from "./error"
import { normalizeIdError } from "../utils/error"

export type CallbackMessage = {
state: string
Expand Down Expand Up @@ -62,8 +62,9 @@ export class CommunicateHelper {

switch (type) {
case "fail": {
const error = callbackMessage.error
return responseHandler.reject(new JsonRpcError(error.code, error.message))
const err = normalizeIdError(callbackMessage.error)

return responseHandler.reject(err)
}

default: {
Expand Down
Loading

0 comments on commit b06b046

Please sign in to comment.