Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions packages/networks/evm-chains/src/models/Transaction.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,39 @@
import type { TransactionInterface } from '@multiplechain/types'
import { TransactionStatusEnum } from '@multiplechain/types'
import { Provider } from '../services/Provider.ts'

export class Transaction implements TransactionInterface {
/**
* Each transaction has its own unique ID defined by the user
*/
id: string

/**
* Provider instance
*/
private readonly provider: Provider

constructor(id: string) {
this.id = id
this.provider = Provider.instance
}

/**
* @returns Raw transaction data that is taken by blockchain network via RPC.
*/
getData(): object {
return {}
async getData(): Promise<object | null> {
try {
const data = (await this.provider.ethers.getTransaction(this.id)) ?? {}
const receipt = (await this.provider.ethers.getTransactionReceipt(this.id)) ?? {}
const result: object = { ...data, ...receipt }
return Object.keys(result).length !== 0 ? result : null
} catch (error) {
const e = error as Error
if (String(e.message).includes('timeout')) {
throw new Error('rpc-timeout')
}
throw new Error('data-request-failed')
}
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/networks/evm-chains/src/services/Ethers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ export class Ethers {
}
}

get getJsonRpc(): JsonRpcProvider {
public get jsonRpc(): JsonRpcProvider {
return this.jsonRpcProvider
}

get getWebsocket(): WebSocketProvider | undefined {
public get websocket(): WebSocketProvider | undefined {
return this.websocketProvider
}

Expand Down
22 changes: 12 additions & 10 deletions packages/networks/evm-chains/src/services/Provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,45 +24,46 @@ export class Provider implements Omit<ProviderInterface, 'update' | 'network'> {
/**
* Ethers service
*/
private ethers: Ethers
private _ethers: Ethers

/**
* Static instance of the provider
*/
private static instance: Provider
private static _instance: Provider

/**
* @param network - Network configuration of the provider
*/
constructor(network: EvmNetworkConfigInterface) {
this.network = network
this.ethers = new Ethers(network)
Provider._instance = this
this._ethers = new Ethers(network)
}

/**
* @returns Ethers service instance
*/
get getEthers(): Ethers {
return this.ethers
get ethers(): Ethers {
return this._ethers
}

/**
* Get the static instance of the provider
* @returns Provider
*/
static get getInstance(): Provider {
if (Provider.instance === undefined) {
static get instance(): Provider {
if (Provider._instance === undefined) {
throw new Error('Provider is not initialized')
}
return Provider.instance
return Provider._instance
}

/**
* Initialize the static instance of the provider
* @param network - Network configuration of the provider
*/
static initialize(network: EvmNetworkConfigInterface): void {
Provider.instance = new Provider(network)
Provider._instance = new Provider(network)
}

/**
Expand All @@ -71,7 +72,8 @@ export class Provider implements Omit<ProviderInterface, 'update' | 'network'> {
*/
update(network: EvmNetworkConfigInterface): void {
this.network = network
this.ethers = new Ethers(network)
Provider._instance = this
this._ethers = new Ethers(network)
}

/**
Expand Down
13 changes: 11 additions & 2 deletions packages/networks/evm-chains/tests/models.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import { describe, it, expect } from 'vitest'

import { Transaction } from '../src/models/Transaction.ts'

const txId = '0x566002399664e92f82ed654c181095bdd7ff3d3f1921d963257585891f622251'

describe('Tests for models', () => {
it('should load models', async () => {
expect(1).toBe(1)
const tx = new Transaction(txId)
it('Transaction Id', async () => {
expect(tx.getId()).toBe(txId)
})

it('Transaction Data', async () => {
expect(await tx.getData()).toBeTypeOf('object')
})
})
15 changes: 15 additions & 0 deletions packages/networks/evm-chains/tests/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Provider } from '../src/services/Provider.ts'

export const provider = new Provider({
id: 11155111,
hexId: '0xaa36a7',
mainnetId: 1,
name: 'Ethereum Sepolia Testnet (QR)',
rpcUrl: 'https://sepolia.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161',
wsUrl: 'wss://sepolia.infura.io/ws/v3/9aa3d95b3bc440fa88ea12eaa4456161',
explorerUrl: 'https://sepolia.etherscan.io/',
nativeCurrency: {
symbol: 'ETH',
decimals: 18
}
})
3 changes: 2 additions & 1 deletion vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ export default mergeConfig(
test: {
environment: 'jsdom',
exclude: [...configDefaults.exclude, 'e2e/*'],
root: fileURLToPath(new URL('./', import.meta.url))
root: fileURLToPath(new URL('./', import.meta.url)),
setupFiles: ['./packages/networks/evm-chains/tests/setup.ts']
}
})
)