Skip to content

Commit

Permalink
fix some js files
Browse files Browse the repository at this point in the history
  • Loading branch information
pgherveou committed Feb 7, 2025
1 parent 1f324a5 commit 3248fb9
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 62 deletions.
8 changes: 4 additions & 4 deletions substrate/frame/revive/rpc/examples/js/.prettierrc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"trailingComma": "es5",
"tabWidth": 4,
"semi": false,
"singleQuote": true
"trailingComma": "es5",
"tabWidth": 4,
"semi": false,
"singleQuote": true
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"extends": "solhint:recommended"
"extends": "solhint:recommended"
}
3 changes: 2 additions & 1 deletion substrate/frame/revive/rpc/examples/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"preview": "vite preview"
"preview": "vite preview",
"prettier": "prettier --write ."
},
"dependencies": {
"@parity/revive": "^0.0.9",
Expand Down
105 changes: 56 additions & 49 deletions substrate/frame/revive/rpc/examples/js/src/geth-diff.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import {
waitForHealth,
polkadotSdkPath,
} from './util.ts'
import { afterAll, afterEach, beforeAll, describe, expect, test } from 'bun:test'
import { encodeFunctionData, Hex, parseEther } from 'viem'
import { afterAll, afterEach, describe, expect, test } from 'bun:test'
import { encodeFunctionData, Hex, parseEther, decodeEventLog, keccak256, toHex } from 'viem'
import { ErrorsAbi } from '../abi/Errors'
import { FlipperCallerAbi } from '../abi/FlipperCaller'
import { FlipperAbi } from '../abi/Flipper'
import { EventExampleAbi } from '../abi/EventExample'
import { Subprocess, spawn } from 'bun'
import { fail } from 'node:assert'

const procs: Subprocess[] = []
if (!process.env.USE_LIVE_SERVERS) {
if (process.env.START_GETH) {
process.env.USE_ETH_RPC = 'true'
procs.push(
// Run geth on port 8546
await (async () => {
Expand All @@ -30,7 +30,12 @@ if (!process.env.USE_LIVE_SERVERS) {

await waitForHealth('http://localhost:8546').catch()
return proc
})(),
})()
)
}

if (process.env.START_SUBSTRATE_NODE) {
procs.push(
//Run the substate node
(() => {
killProcessOnPort(9944)
Expand All @@ -42,13 +47,19 @@ if (!process.env.USE_LIVE_SERVERS) {
'-l=error,evm=debug,sc_rpc_server=info,runtime::revive=debug',
],
{
stdout: Bun.file('/tmp/kitchensink.out.log'),
stderr: Bun.file('/tmp/kitchensink.err.log'),
stdout: Bun.file('/tmp/substrate-node.out.log'),
stderr: Bun.file('/tmp/substrate-node.err.log'),
cwd: polkadotSdkPath,
}
)
})(),
// Run eth-rpc on 8545
})()
)
}

if (process.env.START_ETH_RPC) {
process.env.USE_ETH_RPC = 'true'
// Run eth-rpc on 8545
procs.push(
await (async () => {
killProcessOnPort(8545)
console.log('Starting eth-rpc')
Expand Down Expand Up @@ -79,53 +90,49 @@ afterAll(async () => {
procs.forEach((proc) => proc.kill())
})

const envs = await Promise.all([createEnv('geth'), createEnv('kitchensink')])
const envs = await Promise.all([
...(process.env.USE_GETH ? [createEnv('geth')] : []),
...(process.env.USE_ETH_RPC ? [createEnv('eth-rpc')] : []),
])

for (const env of envs) {
describe(env.serverWallet.chain.name, () => {
let errorsAddr: Hex = '0x'
let flipperAddr: Hex = '0x'
let flipperCallerAddr: Hex = '0x'
beforeAll(async () => {
{
const getErrorTesterAddr = (() => {
let contractAddress: Hex = '0x'
return async () => {
if (contractAddress !== '0x') {
return contractAddress
}
const hash = await env.serverWallet.deployContract({
abi: ErrorsAbi,
bytecode: getByteCode('Errors', env.evm),
})
const deployReceipt = await env.serverWallet.waitForTransactionReceipt({ hash })
if (!deployReceipt.contractAddress)
throw new Error('Contract address should be set')
errorsAddr = deployReceipt.contractAddress
}

{
const hash = await env.serverWallet.deployContract({
abi: FlipperAbi,
bytecode: getByteCode('Flipper', env.evm),
})
const deployReceipt = await env.serverWallet.waitForTransactionReceipt({ hash })
if (!deployReceipt.contractAddress)
throw new Error('Contract address should be set')
flipperAddr = deployReceipt.contractAddress
contractAddress = deployReceipt.contractAddress!
return contractAddress
}
})()

{
const getEventExampleAddr = (() => {
let contractAddress: Hex = '0x'
return async () => {
if (contractAddress !== '0x') {
return contractAddress
}
const hash = await env.serverWallet.deployContract({
abi: FlipperCallerAbi,
args: [flipperAddr],
bytecode: getByteCode('FlipperCaller', env.evm),
abi: EventExampleAbi,
bytecode: getByteCode('EventExample', env.evm),
})
const deployReceipt = await env.serverWallet.waitForTransactionReceipt({ hash })
if (!deployReceipt.contractAddress)
throw new Error('Contract address should be set')
flipperCallerAddr = deployReceipt.contractAddress
contractAddress = deployReceipt.contractAddress!
return contractAddress
}
})
})()

test('triggerAssertError', async () => {
try {
await env.accountWallet.readContract({
address: errorsAddr,
address: await getErrorTesterAddr(),
abi: ErrorsAbi,
functionName: 'triggerAssertError',
})
Expand All @@ -143,7 +150,7 @@ for (const env of envs) {
test('triggerRevertError', async () => {
try {
await env.accountWallet.readContract({
address: errorsAddr,
address: await getErrorTesterAddr(),
abi: ErrorsAbi,
functionName: 'triggerRevertError',
})
Expand All @@ -161,7 +168,7 @@ for (const env of envs) {
test('triggerDivisionByZero', async () => {
try {
await env.accountWallet.readContract({
address: errorsAddr,
address: await getErrorTesterAddr(),
abi: ErrorsAbi,
functionName: 'triggerDivisionByZero',
})
Expand All @@ -181,7 +188,7 @@ for (const env of envs) {
test('triggerOutOfBoundsError', async () => {
try {
await env.accountWallet.readContract({
address: errorsAddr,
address: await getErrorTesterAddr(),
abi: ErrorsAbi,
functionName: 'triggerOutOfBoundsError',
})
Expand All @@ -201,7 +208,7 @@ for (const env of envs) {
test('triggerCustomError', async () => {
try {
await env.accountWallet.readContract({
address: errorsAddr,
address: await getErrorTesterAddr(),
abi: ErrorsAbi,
functionName: 'triggerCustomError',
})
Expand All @@ -219,7 +226,7 @@ for (const env of envs) {
test('eth_call (not enough funds)', async () => {
try {
await env.emptyWallet.simulateContract({
address: errorsAddr,
address: await getErrorTesterAddr(),
abi: ErrorsAbi,
functionName: 'valueMatch',
value: parseEther('10'),
Expand Down Expand Up @@ -255,7 +262,7 @@ for (const env of envs) {
test('eth_estimate (not enough funds)', async () => {
try {
await env.emptyWallet.estimateContractGas({
address: errorsAddr,
address: await getErrorTesterAddr(),
abi: ErrorsAbi,
functionName: 'valueMatch',
value: parseEther('10'),
Expand All @@ -273,7 +280,7 @@ for (const env of envs) {
test('eth_estimate call caller (not enough funds)', async () => {
try {
await env.emptyWallet.estimateContractGas({
address: errorsAddr,
address: await getErrorTesterAddr(),
abi: ErrorsAbi,
functionName: 'valueMatch',
value: parseEther('10'),
Expand All @@ -291,7 +298,7 @@ for (const env of envs) {
test('eth_estimate (revert)', async () => {
try {
await env.serverWallet.estimateContractGas({
address: errorsAddr,
address: await getErrorTesterAddr(),
abi: ErrorsAbi,
functionName: 'valueMatch',
value: parseEther('11'),
Expand Down Expand Up @@ -322,7 +329,7 @@ for (const env of envs) {
expect(balance).toBe(0n)
try {
await env.emptyWallet.estimateContractGas({
address: errorsAddr,
address: await getErrorTesterAddr(),
abi: ErrorsAbi,
functionName: 'setState',
args: [true],
Expand Down Expand Up @@ -352,7 +359,7 @@ for (const env of envs) {
{
data,
from: env.emptyWallet.account.address,
to: errorsAddr,
to: await getErrorTesterAddr(),
},
],
})
Expand Down
8 changes: 4 additions & 4 deletions substrate/frame/revive/rpc/examples/js/src/spammer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import { FlipperAbi } from '../abi/Flipper'

//Run the substate node
console.log('🚀 Start kitchensink...')
console.log('🚀 Start substrate-node...')
killProcessOnPort(9944)
spawn(
[
Expand All @@ -20,8 +20,8 @@ spawn(
'-l=error,evm=debug,sc_rpc_server=info,runtime::revive=debug',
],
{
stdout: Bun.file('/tmp/kitchensink.out.log'),
stderr: Bun.file('/tmp/kitchensink.err.log'),
stdout: Bun.file('/tmp/substrate-node.out.log'),
stderr: Bun.file('/tmp/substrate-node.err.log'),
cwd: polkadotSdkPath,
}
)
Expand Down Expand Up @@ -60,7 +60,7 @@ spawn(
)
await waitForHealth('http://localhost:8545').catch()

const env = await createEnv('kitchensink')
const env = await createEnv('eth-rpc')
const wallet = env.accountWallet

console.log('🚀 Deploy flipper...')
Expand Down
6 changes: 3 additions & 3 deletions substrate/frame/revive/rpc/examples/js/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ export function killProcessOnPort(port: number) {
}

export let jsonRpcErrors: JsonRpcError[] = []
export async function createEnv(name: 'geth' | 'kitchensink') {
export async function createEnv(name: 'geth' | 'eth-rpc') {
const gethPort = process.env.GETH_PORT || '8546'
const kitchensinkPort = process.env.KITCHENSINK_PORT || '8545'
const url = `http://localhost:${name == 'geth' ? gethPort : kitchensinkPort}`
const ethRpcPort = process.env.ETH_RPC_PORT || '8545'
const url = `http://localhost:${name == 'geth' ? gethPort : ethRpcPort}`
const chain = defineChain({
id: name == 'geth' ? 1337 : 420420420,
name,
Expand Down

0 comments on commit 3248fb9

Please sign in to comment.