Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

export types #16

Merged
merged 8 commits into from
Dec 12, 2023
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',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why replace with hard-coded value instead of const?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can use either one -- this is just more convenient, and a demonstration of what most people will likely do

gas: 500000n,
gasPrice: 1000000000n,
chainId: this.chainId,
Expand Down
16 changes: 10 additions & 6 deletions examples/suave/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { sleep } from 'bun'
import { http, Address, Hex, createPublicClient, formatEther } from 'viem'
import { http, Address, Hex, createPublicClient, formatEther, HttpTransport } from 'viem'
import { goerli, suaveRigil } from 'viem/chains'
import { TransactionRequestSuave } from 'viem/chains/suave/types'
import { SuaveProvider, SuaveWallet, TransactionRequestSuave } from 'viem/chains/suave/types'
import { MevShareBid } from 'bids'

const failEnv = (name: string) => {
Expand All @@ -26,16 +26,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> = suaveRigil.newPublicClient(http(SUAVE_RPC_URL_HTTP))
const goerliProvider = createPublicClient({
chain: goerli,
transport: http(GOERLI_RPC_URL_HTTP),
})
const adminWallet = suaveRigil.newWallet({
const adminWallet: SuaveWallet<HttpTransport> = suaveRigil.newWallet({
transport: http(SUAVE_RPC_URL_HTTP),
privateKey: PRIVATE_KEY,
})
const wallet = suaveRigil.newWallet({
const wallet: SuaveWallet<HttpTransport> = suaveRigil.newWallet({
transport: http(SUAVE_RPC_URL_HTTP),
privateKey: '0x01000070530220062104600650003002001814120800043ff33603df10300012',
})
Expand Down Expand Up @@ -107,7 +107,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 +132,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
96 changes: 53 additions & 43 deletions src/chains/definitions/suaveRigil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,41 @@ 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'
import {
type SuaveProvider,
type SuaveWallet,
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)

function getNewWallet<TTransport extends Transport>(params: {
privateKey?: Hex
jsonRpcAccount?: Hex
transport?: TTransport
}): SuaveWallet<TTransport> {
return getSuaveWallet({
transport: params.transport ?? defaultTransport,
privateKey: params.privateKey,
jsonRpcAccount: params.jsonRpcAccount && {
address: params.jsonRpcAccount,
type: 'json-rpc',
},
})
}

function getNewPublicClient<TTransport extends Transport>(
transport?: TTransport,
): SuaveProvider<TTransport> {
return createPublicClient({
transport: transport ?? defaultTransport,
chain: suaveRigil,
})
}

export const suaveRigil = /*#__PURE__*/ defineChain(
{
id: 16813125,
Expand Down Expand Up @@ -89,53 +116,36 @@ 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',
})
* // 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',
})
* // 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',
},
}),
newWallet: getNewWallet,
/** 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'))
* 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))
* // 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,
}),
newPublicClient: getNewPublicClient,
},
{
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
2 changes: 2 additions & 0 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 Down
31 changes: 19 additions & 12 deletions src/chains/suave/wallet.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { privateKeyToAccount } from '../../accounts/privateKeyToAccount.js'
import { sign } from '../../accounts/utils/sign.js'
import {
type Chain,
type JsonRpcAccount,
type PrivateKeyAccount,
type PublicClient,
type Transport,
type WalletClient,
createWalletClient,
Expand All @@ -23,6 +23,19 @@ import {
type TransactionSerializableSuave,
} from './types.js'

/// client types
export type SuaveWallet<TTransport extends Transport> = WalletClient<
TTransport,
typeof suaveRigil,
PrivateKeyAccount | JsonRpcAccount
>

export type SuaveProvider<TTransport extends Transport> = PublicClient<
TTransport,
typeof suaveRigil
>

/// helper functions
function formatSignature(signature: {
r: Hex
s: Hex
Expand Down Expand Up @@ -75,29 +88,23 @@ function getSigningMethod(transport: any, privateKey?: Hex, address?: Hex) {
}
}

export function getSuaveWallet<
TTransport extends Transport,
TChain extends Chain,
>(params: {
/** Get a SUAVE-enabled viem wallet. */
export function getSuaveWallet<TTransport extends Transport,>(params: {
transport: TTransport
chain: TChain
jsonRpcAccount?: JsonRpcAccount
privateKey?: Hex
}): WalletClient<
TTransport,
TChain,
PrivateKeyAccount // TODO: generalize account types (required to make metamask transport work)
> {
}): SuaveWallet<TTransport> {
if (!params.jsonRpcAccount && !params.privateKey) {
throw new Error("Must provide either 'jsonRpcAccount' or 'privateKey'")
}
if (params.jsonRpcAccount && params.privateKey) {
throw new Error("Cannot provide both 'jsonRpcAccount' and 'privateKey'")
}
// Overrides viem wallet methods with SUAVE equivalents.
return createWalletClient({
account: params.jsonRpcAccount ?? privateKeyToAccount(params.privateKey!),
transport: params.transport,
chain: params.chain,
chain: suaveRigil,
}).extend((client) => ({
async sendTransaction(txRequest: TransactionRequestSuave) {
const preparedTx = await client.prepareTransactionRequest(
Expand Down
Loading