Skip to content

Commit

Permalink
Toliman chain details (#58)
Browse files Browse the repository at this point in the history
* add toliman definitions

* linter

* detect localhost in customRpc, use rigil/toliman chain defs accordingly

* lint

* require chainId for formatter, harden prepareTxRequest return type

* lint

* chore: format

* tighten type type from prepareTxRequest

* lint

---------

Co-authored-by: zeroXbrock <[email protected]>
  • Loading branch information
zeroXbrock and zeroXbrock authored Jul 31, 2024
1 parent 10caf97 commit a32aa32
Show file tree
Hide file tree
Showing 12 changed files with 146 additions and 92 deletions.
6 changes: 1 addition & 5 deletions examples/suave-web-demo/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,11 @@ setupConnectButton(document.querySelector<HTMLButtonElement>('#connect')!,
jsonRpcAccount: account,
transport: custom(ethereum),
customRpc: SUAVE_RPC_URL_HTTP,
chain: suaveRigil,
})
console.log(suaveWallet)
const suaveProvider = getSuaveProvider(http(SUAVE_RPC_URL_HTTP))
suaveProvider.getBalance({ address: account }).then((balance: any) => {
suaveProvider.getChainId().then((chainId: any) => {
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 Down
12 changes: 6 additions & 6 deletions examples/suave-web-demo/src/suave.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
http,
} from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { suaveRigil, holesky } from 'viem/chains'
import { holesky as l1Chain, suaveRigil } from 'viem/chains'
import { OFAOrder } from '../../suave/bids'
import { getSuaveWallet } from 'viem/chains/utils'
import BidContractDeployment from '../../suave/deployedAddress.json'
Expand All @@ -15,23 +15,24 @@ import BidContractDeployment from '../../suave/deployedAddress.json'
const KETTLE_ADDRESS: Address = '0xb5feafbdd752ad52afb7e1bd2e40432a485bbb7f'
const ADMIN_KEY: Hex =
'0x91ab9a7e53c220e6210460b65a7a3bb2ca181412a8a7b43ff336b3df1737ce12'
export const L1_RPC_URL_HTTP: string = 'https://holesky.rigil.suave.flashbots.net'
export const L1_RPC_URL_HTTP: string = 'https://holesky.toliman.suave.flashbots.net'
export const SUAVE_RPC_URL_HTTP: string = 'http://localhost:8545'

const l1Wallet = createWalletClient({
account: privateKeyToAccount(
'0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80',
),
chain: holesky,
chain: l1Chain,
transport: http(L1_RPC_URL_HTTP),
})
const l1Provider = createPublicClient({
chain: holesky,
chain: l1Chain,
transport: http(L1_RPC_URL_HTTP),
})
const suaveAdminWallet = getSuaveWallet({
privateKey: ADMIN_KEY,
transport: http(SUAVE_RPC_URL_HTTP),
chain: suaveRigil,
})

/** Sets up "connect to wallet" button and holds wallet instance. */
Expand All @@ -43,7 +44,6 @@ export function setupConnectButton(
let account = null
element.innerHTML = 'connect to wallet'

console.log(suaveRigil.id)
const setConnected = async (ethereum: any) => {
if (connected) return
element.innerHTML = `connecting to ${connected}`
Expand Down Expand Up @@ -85,7 +85,7 @@ export function setupSendBidButton(
console.log('signed L1 tx', signedTx)

// create bid & send ccr
const decryptionCondition = 1n + (await l1Provider.getBlockNumber())
const decryptionCondition = (await l1Provider.getBlockNumber())
console.log("decryptionCondition", decryptionCondition)
const bid = new OFAOrder(
decryptionCondition,
Expand Down
5 changes: 3 additions & 2 deletions examples/suave/bids.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import {
Hex,
encodeAbiParameters,
encodeFunctionData,
} from 'viem'
import { TransactionRequestSuave } from 'viem/chains/suave/types'
} from '@flashbots/suave-viem'
import { TransactionRequestSuave } from '@flashbots/suave-viem/chains/utils'
import OFAContract from './contracts/out/OFA.sol/OFAPrivate.json'

/** Factory class to create MEV-Share bids on SUAVE. */
Expand Down Expand Up @@ -61,6 +61,7 @@ export class OFAOrder {
to: this.OFAContract,
data: this.newOrderCalldata(),
isEIP712,
gas: 10000000n,
kettleAddress: this.kettle,
confidentialInputs: this.confidentialInputsBytes(),
}
Expand Down
2 changes: 1 addition & 1 deletion examples/suave/deployContracts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ FUNDED_PRV_KEY=0x91ab9a7e53c220e6210460b65a7a3bb2ca181412a8a7b43ff336b3df1737ce1
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
cd $SCRIPT_DIR/contracts
forge build
createOutput=$(forge create --legacy --private-key $FUNDED_PRV_KEY --chain-id 16813125 -r http://localhost:8545 src/OFA.sol:OFAPrivate)
createOutput=$(forge create --legacy --private-key $FUNDED_PRV_KEY -r http://localhost:8545 src/OFA.sol:OFAPrivate)
deployedAddress=$(echo "$createOutput" | grep 'Deployed to:' | awk '{print $3}')
echo "Deployed to: $deployedAddress"
echo '{"address": ''"'$deployedAddress'"}' > $SCRIPT_DIR/deployedAddress.json
29 changes: 11 additions & 18 deletions examples/suave/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { sleep } from 'bun'
import { http, Address, Hex, createPublicClient, formatEther, isHex } from 'viem'
import { holesky } from 'viem/chains'
import { TransactionRequestSuave } from 'viem/chains/suave/types'
import { http, Address, Hex, createPublicClient, formatEther, isHex } from '@flashbots/suave-viem'
import { holesky, suaveRigil } from '@flashbots/suave-viem/chains'
import { TransactionRequestSuave } from '@flashbots/suave-viem/chains/utils'
import { OFAOrder } from './bids'
import { SuaveProvider, SuaveWallet, getSuaveProvider, getSuaveWallet, parseTransactionSuave } from 'viem/chains/utils'
import { HttpTransport } from 'viem'
import { SuaveProvider, SuaveWallet, getSuaveProvider, getSuaveWallet, parseTransactionSuave } from '@flashbots/suave-viem/chains/utils'
import { HttpTransport } from '@flashbots/suave-viem'
import BidContractDeployment from './deployedAddress.json'

const failEnv = (name: string) => {
Expand Down Expand Up @@ -48,14 +48,17 @@ const l1Provider = createPublicClient({
})
const adminWallet: SuaveWallet<HttpTransport> = getSuaveWallet({
transport: http(SUAVE_RPC_URL_HTTP),
privateKey: PRIVATE_KEY,
privateKey: PRIVATE_KEY,
chain: suaveRigil,
})
const wallet = getSuaveWallet({
transport: http(SUAVE_RPC_URL_HTTP),
privateKey: "0x6c45335a22461ccdb978b78ab61b238bad2fae4544fb55c14eb096c875ccfc52",
privateKey: PRIVATE_KEY,
chain: suaveRigil,
})
console.log('admin', adminWallet.account.address)
console.log('wallet', wallet.account.address)
console.log('wallet chain id', wallet.chain.id)

const retryExceptionsWithTimeout = async (
timeout_ms: number,
Expand Down Expand Up @@ -94,15 +97,6 @@ const fundAccount = async (wallet: Address, amount: bigint) => {
}
}

async function checkL1Balance(minBalance?: bigint) {
const balance = await l1Provider.getBalance({ address: wallet.account.address })
const absoluteMin = minBalance || 1n
if (balance < absoluteMin) {
throw new Error(`L1 balance too low: ${formatEther(balance)} ETH (needed ${formatEther(absoluteMin)}).\nPlease fund this account: ${wallet.account.address}`)
}
console.log(`L1 balance: ${formatEther(balance)} ETH`)
}

/** MEV-Share implementation on SUAVE.
*
* To run this, you'll need to deploy the contract first.
Expand All @@ -122,9 +116,8 @@ async function testSuaveBids() {
data: '0x686f776479' as Hex,
gas: 26000n,
gasPrice: 10000000000n,
chainId: 17000,
type: '0x0' as const,
}
checkL1Balance(testTx.gas * testTx.gasPrice)
const signedTx = await wallet.signTransaction(testTx)

console.log("signed tx", signedTx)
Expand Down
4 changes: 2 additions & 2 deletions src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ First, see these instructions for spinning up a local devnet. Then install **[@f

```ts
import { http, type Hex } from '@flashbots/suave-viem';
import { suaveRigil } from '@flashbots/suave-viem/chains';
import { suaveToliman as suaveChain } from '@flashbots/suave-viem/chains';
import {
getSuaveProvider,
getSuaveWallet,
Expand Down Expand Up @@ -115,7 +115,7 @@ const ccr: TransactionRequestSuave = {
gasPrice: 10000000000n, // Gas price for the transaction
gas: 420000n, // Gas limit for the transaction
type: SuaveTxRequestTypes.ConfidentialRequest, // (0x43)
chainId: suaveRigil.id,
chainId: suaveChain.id,
data: '0x236eb5a70000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000008f21fdd6b4f4cacd33151777a46c122797c8bf170000000000000000000000000000000000000000000000000000000000000000',
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,42 @@ import type { Address } from 'abitype'
import { defineChain } from '../../utils/chain.js'
import { formattersSuave } from '../suave/formatters.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 suaveChainConfig = ({
networkName,
chainId,
currencySymbol,
}: { networkName: string; chainId: number; currencySymbol: string }) => {
const networkNamePascal = `${networkName[0].toUpperCase()}${networkName.substring(
1,
)}`
const networkRpc = `rpc.${networkName}.suave.flashbots.net`
const networkUrlHttp = `https://${networkRpc}`
const networkUrlWs = `wss://${networkRpc}`
const explorerUrl = `https://explorer.${networkName}.suave.flashbots.net/`

export const suaveRigil = /*#__PURE__*/ defineChain(
{
id: 16813125,
name: 'Suave Rigil Testnet',
network: 'rigil-testnet',
return {
id: chainId,
name: `Suave ${networkNamePascal} Testnet`,
network: `${networkName}-testnet`,
nativeCurrency: {
decimals: 18,
name: 'Suave Goerli',
symbol: 'ETH',
name: `${networkNamePascal} ETH`,
symbol: currencySymbol,
},
rpcUrls: {
default: {
http: [testnetUrlHttp],
webSocket: [testnetUrlWs],
http: [networkUrlHttp],
webSocket: [networkUrlWs],
},
public: {
http: [testnetUrlHttp],
webSocket: [testnetUrlWs],
http: [networkUrlHttp],
webSocket: [networkUrlWs],
},
},
blockExplorers: {
default: {
name: 'SUAVE Rigil Explorer',
url: testnetExplorerUrl,
name: `SUAVE ${networkNamePascal} Explorer`,
url: explorerUrl,
},
},
contracts: {
Expand Down Expand Up @@ -80,7 +88,27 @@ export const suaveRigil = /*#__PURE__*/ defineChain(
},
},
testnet: true,
}
}

export const suaveToliman = /*#__PURE__*/ defineChain(
suaveChainConfig({
networkName: 'toliman',
chainId: 33626250,
currencySymbol: 'TEETH',
}),
{
formatters: formattersSuave,
// serializers: serializersSuave,
},
)

export const suaveRigil = /*#__PURE__*/ defineChain(
suaveChainConfig({
networkName: 'rigil',
chainId: 16813125,
currencySymbol: 'RETH',
}),
{
formatters: formattersSuave,
// serializers: serializersSuave,
Expand Down
2 changes: 1 addition & 1 deletion src/chains/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export { skaleTitanTestnet } from './definitions/skale/titanTestnet.js'
export { songbird } from './definitions/songbird.js'
export { songbirdTestnet } from './definitions/songbirdTestnet.js'
export { shardeumSphinx } from './definitions/shardeumSphinx.js'
export { suaveRigil } from './definitions/suaveRigil.js'
export { suaveRigil, suaveToliman } from './definitions/suave.js'
export { syscoin } from './definitions/syscoin.js'
export { syscoinTestnet } from './definitions/syscoinTestnet.js'
export { taraxa } from './definitions/taraxa.js'
Expand Down
Loading

0 comments on commit a32aa32

Please sign in to comment.