Skip to content

Commit

Permalink
initial formatter and rigil chain config
Browse files Browse the repository at this point in the history
  • Loading branch information
dmarzzz committed Oct 3, 2023
1 parent 2cda9ef commit b5ac97c
Show file tree
Hide file tree
Showing 8 changed files with 620 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/chains/definitions/suaveRigil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { defineChain } from '../../utils/chain.js'
import { formattersSuave } from '../suave/formatters.js'

export const suaveRigil = /*#__PURE__*/ defineChain(
{
id: 424242,
name: 'Suave Rigil Testnet',
network: 'rigil-testnet',
nativeCurrency: {
decimals: 18,
name: 'Suave Goerli',
symbol: 'ETH',
},
rpcUrls: {
default: {
http: ['https://testnet.rpc.flashbots.net'],
webSocket: ['wss://testnet.rpc.flashbots.net'],
},
public: {
http: ['https://testnet.rpc.flashbots.net'],
webSocket: ['wss://testnet.rpc.flashbots.net'],
},
},
blockExplorers: {
default: {
name: 'Explorer',
url: 'https://testnet.explorer.flashbots.net',
},
},
contracts: {},
testnet: true,
},
{
formatters: formattersSuave,
},
)
1 change: 1 addition & 0 deletions src/chains/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +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 { syscoin } from './definitions/syscoin.js'
export { syscoinTestnet } from './definitions/syscoinTestnet.js'
export { taraxa } from './definitions/taraxa.js'
Expand Down
104 changes: 104 additions & 0 deletions src/chains/suave/formatters.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// import { describe, expect, test } from 'vitest'

// // Assuming you have similar actions for the Suave chain like the Celo ones provided.
// import { getBlock } from '../../actions/public/getBlock.js'
// import { getTransaction } from '../../actions/public/getTransaction.js'
// import { getTransactionReceipt } from '../../actions/public/getTransactionReceipt.js'

// import { suaveRigil } from '../index.js'

// describe('block', () => {
// test('formatter', () => {
// const { block } = suaveRigil.formatters!

// const formattedBlock = block.format({
// randomness: 'sampleRandomValue',
// transactions: [
// {
// ExecutionNode: 'sampleExecutionNode',
// ConfidentialComputeRequest: 'sampleRequest',
// ConfidentialComputeResult: 'sampleResult',
// // ... other RpcTransaction fields if present
// },
// ],
// })

// expect(formattedBlock).toMatchInlineSnapshot(`
// {
// "randomness": "sampleRandomValue",
// "transactions": [
// {
// "ExecutionNode": "sampleExecutionNode",
// "ConfidentialComputeRequest": "sampleRequest",
// "ConfidentialComputeResult": "sampleResult",
// // ... Other expected fields here
// }
// ]
// }
// `)
// })
// })

// describe('transaction', () => {
// test('formatter', () => {
// const { transaction } = suaveRigil.formatters!

// const inputTransaction = {
// ExecutionNode: 'sampleExecutionNode',
// ConfidentialComputeRequest: 'sampleRequest',
// ConfidentialComputeResult: 'sampleResult',
// // ... other fields if present
// }

// const formattedTransaction = transaction.format(inputTransaction)

// expect(formattedTransaction).toMatchInlineSnapshot(`
// {
// "ExecutionNode": "sampleExecutionNode",
// "ConfidentialComputeRequest": "sampleRequest",
// "ConfidentialComputeResult": "sampleResult",
// // ... Other expected fields here
// }
// `)
// })
// })

// describe('transactionReceipt', () => {
// test('formatter', () => {
// const { transactionReceipt } = suaveRigil.formatters!

// const inputReceipt = {
// // ... input fields based on SuaveRpcTransactionReceiptOverrides
// }

// const formattedReceipt = transactionReceipt.format(inputReceipt)

// expect(formattedReceipt).toMatchInlineSnapshot(`
// {
// // ... Expected fields here based on the SuaveRpcTransactionReceiptOverrides format
// }
// `)
// })
// })

// describe('transactionRequest', () => {
// test('formatter', () => {
// const { transactionRequest } = suaveRigil.formatters!

// const inputRequest = {
// ExecutionNode: 'sampleExecutionNode',
// ConfidentialComputeRequest: 'sampleRequest',
// // ... other fields if present
// }

// const formattedRequest = transactionRequest.format(inputRequest)

// expect(formattedRequest).toMatchInlineSnapshot(`
// {
// "ExecutionNode": "sampleExecutionNode",
// "ConfidentialComputeRequest": "sampleRequest",
// // ... Other expected fields here
// }
// `)
// })
// })
115 changes: 115 additions & 0 deletions src/chains/suave/formatters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { type ChainFormatters } from '../../types/chain.js'
import type { Hash } from '../../types/misc.js'
import type { RpcTransaction } from '../../types/rpc.js'
import { defineBlock } from '../../utils/formatters/block.js'
import {
defineTransaction,
formatTransaction,
} from '../../utils/formatters/transaction.js'
import { defineTransactionReceipt } from '../../utils/formatters/transactionReceipt.js'
import { defineTransactionRequest } from '../../utils/formatters/transactionRequest.js'

// Introduce the new types
export type ConfidentialComputeRequest = {
ExecutionNode: string // Assuming address is a string type
Wrapped: RpcTransaction // This might need to be adjusted to the actual Ethereum Transaction type
}

export type SuaveTransaction = {
ExecutionNode: string
ConfidentialComputeRequest: ConfidentialComputeRequest
ConfidentialComputeResult: string // Assuming bytes are represented as hexadecimal strings
// TODO: signature fields
}

import type {
SuaveBlockOverrides,
SuaveRpcTransaction,
SuaveRpcTransactionRequest,
SuaveTransactionReceipt,
SuaveTransactionReceiptOverrides,
SuaveTransactionRequest,
} from './types.js'

export const formattersSuave = {
block: /*#__PURE__*/ defineBlock({
exclude: ['difficulty', 'gasLimit', 'mixHash', 'nonce', 'uncles'],
format(
args: SuaveBlockOverrides & {
transactions: Hash[] | SuaveRpcTransaction[]
},
): SuaveBlockOverrides & {
transactions: Hash[] | SuaveTransaction[]
} {
const transactions = args.transactions?.map((transaction) => {
if (typeof transaction === 'string') return transaction
return {
...formatTransaction(transaction as RpcTransaction),
ExecutionNode: transaction.ExecutionNode,
ConfidentialComputeRequest: {
ExecutionNode: transaction.ExecutionNode,
Wrapped: transaction as RpcTransaction,
},
ConfidentialComputeResult: transaction.ConfidentialComputeResult,
// TODO : Signature fields
}
}) as Hash[] | SuaveTransaction[]
return {
transactions,
}
},
}),
transaction: /*#__PURE__*/ defineTransaction({
format(args: SuaveRpcTransaction): SuaveTransaction {
if (args.IsConfidential) {
return {
ExecutionNode: args.ExecutionNode,
ConfidentialComputeRequest: {
ExecutionNode: args.ExecutionNode,
Wrapped: args.ConfidentialComputeRequest, // This assumes that args.ConfidentialComputeRequest is of type Transaction
},
ConfidentialComputeResult: args.ConfidentialComputeResult,
// TODO : Signature fields
} as SuaveTransaction
} else {
return args as any // TODO : Handle as regular Ethereum transaction
}
},
}),
transactionReceipt: /*#__PURE__*/ defineTransactionReceipt({
format(args: SuaveTransactionReceiptOverrides): SuaveTransactionReceipt {
const {
ExecutionNode,
ConfidentialComputeRequest,
ConfidentialComputeResult,
...baseProps
} = args

return {
...baseProps,
ExecutionNode,
ConfidentialComputeRequest: {
...ConfidentialComputeRequest,
},
ConfidentialComputeResult,
// signature fields
} as SuaveTransactionReceipt
},
}),

transactionRequest: /*#__PURE__*/ defineTransactionRequest({
format(args: SuaveTransactionRequest): SuaveRpcTransactionRequest {
if (args.IsConfidential) {
const { ExecutionNode, IsConfidential } = args
return {
...args, // Include other properties from args
ExecutionNode: ExecutionNode,
IsConfidential: IsConfidential,
// We omit the ConfidentialComputeRequest here
} as SuaveRpcTransactionRequest
} else {
return args as any // TODO : Handle as regular Ethereum transaction
}
},
}),
} as const satisfies ChainFormatters
96 changes: 96 additions & 0 deletions src/chains/suave/parsers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// import { expect, test } from 'vitest'

// import { accounts } from '~test/src/constants.js'
// import {
// parseEther,
// parseTransaction as parseTransaction_,
// serializeTransaction,
// toRlp,
// } from '../../index.js'
// import { parseTransactionSuave } from './parsers.js'
// import { serializeTransactionSuave } from './serializers.js'
// import type { TransactionSerializableSuave } from './types.js'

// test('should be able to parse a standard Suave transaction', () => {
// const signedTransaction = /* Sample Suave signed transaction */;

// expect(parseTransactionSuave(signedTransaction)).toMatchInlineSnapshot(`
// {
// "chainId": /* Some chain ID */,
// "gas": /* Some gas amount */,
// "to": /* Some address */,
// "value": /* Some value */,
// "ExecutionNode": /* Execution Node value */,
// "ConfidentialComputeRequest": /* Compute Request value */,
// "ConfidentialComputeResult": /* Compute Result value */
// }
// `)
// })

// test('should parse a Suave transaction with data', () => {
// const transactionWithData = {
// ...transaction,
// data: '0x1234', // Example data for this test
// }

// const serialized = serializeTransactionSuave(transactionWithData)

// expect(parseTransactionSuave(serialized)).toMatchInlineSnapshot(`
// {
// ...otherTransactionDetails,
// "data": "0x1234"
// }
// `)
// })

// test('should parse a Suave transaction with Execution Node', () => {
// const transactionWithNode = {
// ...transaction,
// ExecutionNode: accounts[1].address, // Example address
// }

// const serialized = serializeTransactionSuave(transactionWithNode)

// expect(parseTransactionSuave(serialized)).toMatchInlineSnapshot(`
// {
// ...otherTransactionDetails,
// "ExecutionNode": "0x70997970c51812dc3a010c7d01b50e0d17dc79c8"
// }
// `)
// })

// test('invalid transaction (all missing)', () => {
// expect(() =>
// parseTransactionSuave(`0xYourPrefix${toRlp([]).slice(2)}`),
// ).toThrowErrorMatchingInlineSnapshot(`
// "Invalid serialized transaction of type \\"suave\\" was provided.

// Serialized Transaction: \\"YourSerializedTransaction\\"
// Missing Attributes: /* List of missing attributes */

// Version: viem@YourVersion"
// `)
// })

// test('invalid transaction (missing ConfidentialComputeRequest)', () => {
// const transactionMissingCompute = {
// ...transaction,
// ConfidentialComputeRequest: undefined,
// }

// const serialized = serializeTransactionSuave(transactionMissingCompute)

// expect(() =>
// parseTransactionSuave(serialized),
// ).toThrowErrorMatchingInlineSnapshot(`
// "Invalid serialized transaction of type \\"suave\\" was provided.

// Serialized Transaction: \\"YourSerializedTransaction\\"
// Missing Attributes: ConfidentialComputeRequest

// Version: viem@YourVersion"
// `)
// })

// // ... Additional tests specific to your needs ...

Loading

0 comments on commit b5ac97c

Please sign in to comment.