Skip to content

Commit

Permalink
export types (#16)
Browse files Browse the repository at this point in the history
* update ccr type in mev-share bid example

* harden suaveRigil return types & clean up chain declaration

* use SuaveProvider type alias in suaveRigil chain declaration

* export suave types w/ other special types

* add SuaveProvider+SuaveWallet to type exports

* update test, remove empty export, lint

* refactor methods to get suave wallets/providers (-> viem/chains/utils)

* update bun lockfile to fix build
  • Loading branch information
zeroXbrock authored Dec 12, 2023
1 parent 107f633 commit a70c180
Show file tree
Hide file tree
Showing 34 changed files with 390 additions and 308 deletions.
11 changes: 7 additions & 4 deletions biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"enabled": true,
"formatWithErrors": false,
"indentStyle": "space",
"indentSize": 2,
"indentWidth": 2,
"lineWidth": 80
},
"linter": {
Expand All @@ -36,20 +36,23 @@
"noUnusedVariables": "error"
},
"complexity": {
"noForEach": "off"
"noForEach": "off",
"noBannedTypes": "off"
},
"performance": {
"noDelete": "off"
},
"style": {
"noNonNullAssertion": "off",
"useShorthandArrayType": "error"
"useShorthandArrayType": "error",
"noUselessElse": "off"
},
"suspicious": {
"noArrayIndexKey": "off",
"noAssignInExpressions": "off",
"noExplicitAny": "off",
"noRedeclare": "off"
"noRedeclare": "off",
"noConfusingVoidType": "off"
}
}
},
Expand Down
Binary file modified bun.lockb
Binary file not shown.
14 changes: 10 additions & 4 deletions examples/suave-web-demo/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import flashbotsLogo from './flashbots_icon.svg'
import { setupConnectButton, setupDripFaucetButton, setupSendBidButton } from './suave'
import { Logo } from './components'
import { custom, formatEther } from 'viem'
import { getSuaveWallet, getSuaveProvider } from 'viem/chains/utils'
import { suaveRigil } from 'viem/chains'

document.querySelector<HTMLDivElement>('#app')!.innerHTML = `
Expand Down Expand Up @@ -35,10 +36,15 @@ setupConnectButton(document.querySelector<HTMLButtonElement>('#connect')!,
console.error(err)
alert(err.message)
}
const suaveWallet = suaveRigil.newWallet({jsonRpcAccount: account, transport: custom(ethereum)})
const suaveWallet = getSuaveWallet({jsonRpcAccount: account, transport: custom(ethereum)})
console.log(suaveWallet)
const suaveProvider = suaveRigil.newPublicClient(custom(ethereum))
const suaveProvider = getSuaveProvider(custom(ethereum))
suaveProvider.getBalance({ address: account }).then((balance) => {
suaveProvider.getChainId().then((chainId) => {
if (chainId !== suaveRigil.id) {
alert(`wrong chain id. expected ${suaveRigil.id}, got ${chainId}`)
}
})
document.querySelector<HTMLDivElement>('#status-content')!.innerHTML = `
<div>
<p>SUAVE-ETH balance: ${formatEther(balance)}</p>
Expand All @@ -52,7 +58,7 @@ setupConnectButton(document.querySelector<HTMLButtonElement>('#connect')!,
console.error("error in setupSendBidButton", err)
alert(err.message + (err as any).data)
}
const suaveProvider = suaveRigil.newPublicClient(custom(ethereum))
const suaveProvider = getSuaveProvider(custom(ethereum))
suaveProvider.getTransactionReceipt({hash: txHash}).then((receipt) => {
console.log("receipt", receipt)
document.querySelector<HTMLDivElement>('#status-content')!.innerHTML = `
Expand Down Expand Up @@ -84,6 +90,6 @@ ${JSON.stringify(receipt, (_, value) =>
console.error("error in setupDripFaucetButton", err)
alert(err.message + (err as any).data)
}
console.log("funded account", txHash)
console.log("funded account. txhash:", txHash)
})
})
3 changes: 2 additions & 1 deletion examples/suave-web-demo/src/suave.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Address, Hex, createPublicClient, createWalletClient, http } from 'viem
import { privateKeyToAccount } from 'viem/accounts'
import { suaveRigil, goerli } from "viem/chains"
import { MevShareBid } from "../../suave/bids"
import { getSuaveWallet } from 'viem/chains/utils'

// TODO: get this from .env
// TODO: make zero-config script to generate .env (deploy contracts)
Expand All @@ -19,7 +20,7 @@ const goerliProvider = createPublicClient({
transport: http(GOERLI_RPC_URL_HTTP),
chain: goerli
})
const suaveAdminWallet = suaveRigil.newWallet({
const suaveAdminWallet = getSuaveWallet({
privateKey: ADMIN_KEY,
transport: http("http://localhost:8545"),
})
Expand Down
2 changes: 1 addition & 1 deletion examples/suave/bids/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class MevShareBid {
return {
to: this.mevShareContract,
data: this.newBidCalldata(),
type: SuaveTxTypes.ConfidentialRequest,
type: '0x43',
gas: 500000n,
gasPrice: 1000000000n,
chainId: this.chainId,
Expand Down
14 changes: 10 additions & 4 deletions examples/suave/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { http, Address, Hex, createPublicClient, formatEther } from 'viem'
import { goerli, suaveRigil } from 'viem/chains'
import { TransactionRequestSuave } from 'viem/chains/suave/types'
import { MevShareBid } from 'bids'
import { SuaveProvider, SuaveWallet, getSuaveProvider, getSuaveWallet } from 'viem/chains/utils'
import { HttpTransport } from 'viem'

const failEnv = (name: string) => {
throw new Error(`missing env var ${name}`)
Expand All @@ -26,16 +28,16 @@ const SUAVE_RPC_URL_HTTP: string =
const GOERLI_RPC_URL_HTTP: string =
process.env.GOERLI_RPC_URL_HTTP || 'http://localhost:8545'

const suaveProvider = suaveRigil.newPublicClient(http(SUAVE_RPC_URL_HTTP))
const suaveProvider: SuaveProvider<HttpTransport> = getSuaveProvider(http(SUAVE_RPC_URL_HTTP))
const goerliProvider = createPublicClient({
chain: goerli,
transport: http(GOERLI_RPC_URL_HTTP),
})
const adminWallet = suaveRigil.newWallet({
const adminWallet: SuaveWallet<HttpTransport> = getSuaveWallet({
transport: http(SUAVE_RPC_URL_HTTP),
privateKey: PRIVATE_KEY,
})
const wallet = suaveRigil.newWallet({
const wallet = getSuaveWallet({
transport: http(SUAVE_RPC_URL_HTTP),
privateKey: '0x01000070530220062104600650003002001814120800043ff33603df10300012',
})
Expand Down Expand Up @@ -107,7 +109,7 @@ async function testSuaveBids() {
gas: 26000n,
gasPrice: 10000000000n,
chainId: 5,
type: '0x0' as '0x0',
type: '0x0' as const,
}
const signedTx = await wallet.signTransaction(testTx)

Expand All @@ -132,6 +134,10 @@ async function testSuaveBids() {
return receipt
})
console.log('ccrReceipt', ccrReceipt)

// get tx too
const ccrTx = await suaveProvider.getTransaction({ hash: ccrRes })
console.log('ccrTx', ccrTx)
}

async function main() {
Expand Down
6 changes: 4 additions & 2 deletions src/accounts/utils/signTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import {
import { type SignErrorType, sign } from './sign.js'

export type SignTransactionParameters<
TTransactionSerializable extends TransactionSerializable = TransactionSerializable,
TTransactionSerializable extends
TransactionSerializable = TransactionSerializable,
> = {
privateKey: Hex
transaction: TTransactionSerializable
Expand All @@ -27,7 +28,8 @@ export type SignTransactionParameters<
}

export type SignTransactionReturnType<
TTransactionSerializable extends TransactionSerializable = TransactionSerializable,
TTransactionSerializable extends
TransactionSerializable = TransactionSerializable,
> = TransactionSerialized<GetTransactionType<TTransactionSerializable>>

export type SignTransactionErrorType =
Expand Down
20 changes: 11 additions & 9 deletions src/actions/public/createContractEventFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,17 @@ export type CreateContractEventFilterParameters<
args?: never
}
: MaybeExtractEventArgsFromAbi<
TAbi,
TEventName
> extends infer TEventFilterArgs
? {
args?: TEventFilterArgs | (TArgs extends TEventFilterArgs ? TArgs : never)
}
: {
args?: never
})
TAbi,
TEventName
> extends infer TEventFilterArgs
? {
args?:
| TEventFilterArgs
| (TArgs extends TEventFilterArgs ? TArgs : never)
}
: {
args?: never
})

export type CreateContractEventFilterReturnType<
TAbi extends Abi | readonly unknown[] = Abi,
Expand Down
56 changes: 0 additions & 56 deletions src/chains/definitions/suaveRigil.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
import type { Address } from 'abitype'
import { createPublicClient } from '../../clients/createPublicClient.js'
import type { Transport } from '../../clients/transports/createTransport.js'
import { http } from '../../clients/transports/http.js'
import { type Hex } from '../../types/misc.js'
import { defineChain } from '../../utils/chain.js'
import { formattersSuave } from '../suave/formatters.js'
import { getSuaveWallet } from '../suave/wallet.js'

const testnetUrlHttp = 'https://rpc.rigil.suave.flashbots.net'
const testnetUrlWs = 'wss://rpc.rigil.suave.flashbots.net'
const testnetExplorerUrl = 'https://rpc.rigil.suave.flashbots.net'

const defaultTransport = http(testnetUrlHttp)

export const suaveRigil = /*#__PURE__*/ defineChain(
{
id: 16813125,
Expand Down Expand Up @@ -87,55 +80,6 @@ export const suaveRigil = /*#__PURE__*/ defineChain(
},
},
testnet: true,
/** Creates a new SUAVE Wallet instance.
* @example
// new wallet with a private key
import { suaveRigil } from 'viem/chains'
import { http } from 'viem'
const wallet = suaveRigil.newWallet({
transport: http('http://localhost:8545'),
privateKey: '0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80',
})
* @example
// new wallet with a json-rpc account using window.ethereum
// it's assumed that the wallet is already connected to SUAVE chain
import { suaveRigil } from 'viem/chains'
import { custom } from 'viem'
const wallet = suaveRigil.newWallet({
transport: custom(window.ethereum),
jsonRpcAccount: '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',
})
*/
newWallet: (params: {
privateKey?: Hex
jsonRpcAccount?: Hex
transport?: Transport
}) =>
getSuaveWallet({
transport: params.transport ?? defaultTransport,
chain: suaveRigil,
privateKey: params.privateKey,
jsonRpcAccount: params.jsonRpcAccount && {
address: params.jsonRpcAccount,
type: 'json-rpc',
},
}),
/** Creates a new SUAVE Public Client instance.
* @example
import { suaveRigil } from 'viem/chains'
import { http } from 'viem'
const client = suaveRigil.newPublicClient(http('http://localhost:8545'))
* @example
// using window.ethereum
import { suaveRigil } from 'viem/chains'
import { custom } from 'viem'
const client = suaveRigil.newPublicClient(custom(window.ethereum))
*/
newPublicClient: (transport?: Transport) =>
createPublicClient({
transport: transport ?? defaultTransport,
chain: suaveRigil,
}),
},
{
formatters: formattersSuave,
Expand Down
2 changes: 0 additions & 2 deletions src/chains/suave/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,13 @@ export const formattersSuave = {
typeHex: args.typeHex,
} as TransactionSuave
} else {
console.log('formatting regular tx')
// Handle as regular Ethereum transaction
return formatTransaction(args as RpcTransaction) as Transaction
}
},
}),
transactionReceipt: /*#__PURE__*/ defineTransactionReceipt({
format(args: RpcTransactionReceiptSuave): TransactionReceiptSuave {
console.log('formatting tx receipt')
return {
...formatTransactionReceipt(args),
} as TransactionReceiptSuave
Expand Down
7 changes: 5 additions & 2 deletions src/chains/suave/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import type {
TransactionSerializableBase,
} from '../../types/transaction.js'

export type { SuaveWallet, SuaveProvider } from './wallet.js'

/// CUSTOM OVERRIDES ===========================================================

export enum SuaveTxRequestTypes {
Expand All @@ -29,9 +31,10 @@ const AllSuaveTypes = {
}

// syntactical sugar to allow inline string literals
export type SuaveTxType = `${typeof AllSuaveTypes[keyof typeof AllSuaveTypes]}`
export type SuaveTxType =
`${(typeof AllSuaveTypes)[keyof typeof AllSuaveTypes]}`
export type SuaveTxRequestType =
`${typeof SuaveTxRequestTypes[keyof typeof SuaveTxRequestTypes]}`
`${(typeof SuaveTxRequestTypes)[keyof typeof SuaveTxRequestTypes]}`

type ConfidentialOverrides = {
kettleAddress?: Address
Expand Down
Loading

0 comments on commit a70c180

Please sign in to comment.