Skip to content

Commit 67d4c4b

Browse files
committed
Fix response formatting and tests
1 parent f5b7711 commit 67d4c4b

File tree

2 files changed

+95
-91
lines changed

2 files changed

+95
-91
lines changed

packages/portalnetwork/src/client/provider.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export class UltralightProvider {
4242
if (!SUPPORTED_METHODS.has(method)) {
4343
throw this.createError(
4444
ERROR_CODES.UNSUPPORTED_METHOD,
45-
`The Provider does not support the requested method`,
45+
`The provider does not support the requested method`,
4646
)
4747
}
4848

@@ -173,16 +173,19 @@ export class UltralightProvider {
173173
}
174174

175175
private async getTransactionCount(address: string, block: string) {
176-
return this.portal.ETH.getTransactionCount(hexToBytes(address), block)
176+
const txCount = await this.portal.ETH.getTransactionCount(hexToBytes(address), block)
177+
return formatResponse('0x' + (txCount !== undefined ? txCount.toString(16) : ''))
177178
}
178179

179180
private async getCode(codeAddress: string, codeBlock: string) {
180-
return this.portal.ETH.getCode(hexToBytes(codeAddress), codeBlock)
181+
const code = await this.portal.ETH.getCode(hexToBytes(codeAddress), codeBlock)
182+
return formatResponse('0x' + (code !== undefined ? code.toString() : ''))
181183
}
182184

183185
private async getBalance(balanceAddress: string, balanceBlock: bigint) {
186+
184187
const balance = await this.portal.ETH.getBalance(hexToBytes(balanceAddress), balanceBlock)
185-
return formatResponse('0x' + (balance ? balance.toString(16) : '0'))
188+
return formatResponse('0x' + (balance !== undefined ? balance.toString(16) : ''))
186189
}
187190

188191
private async getStorageAt(storageAddress: string, position: string, storageBlock: string) {
@@ -191,17 +194,17 @@ export class UltralightProvider {
191194
hexToBytes(position),
192195
storageBlock,
193196
)
194-
return formatResponse('0x' + (storage ? storage.toString() : '0'))
197+
return formatResponse('0x' + (storage !== undefined ? storage.toString() : ''))
195198
}
196199

197200
private async call(callObject: any, callBlock: bigint) {
198201
const result = await this.portal.ETH.call(callObject, callBlock)
199-
return formatResponse('0x' + result.toString())
202+
return formatResponse('0x' + (result !== undefined ? result.toString() : ''))
200203
}
201204

202205
private createError(code: number, message: string) {
203206
const error = new Error(message)
204-
;(error as any).code = code
207+
; (error as any).code = code
205208
return error
206209
}
207210
}

packages/portalnetwork/test/client/provider.spec.ts

Lines changed: 85 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -23,101 +23,102 @@ it('Test provider functionality', async () => {
2323
enr,
2424
privateKey,
2525
},
26-
supportedNetworks: [{ networkId: NetworkId.HistoryNetwork }],
26+
supportedNetworks: [{ networkId: NetworkId.HistoryNetwork }, { networkId: NetworkId.StateNetwork }],
2727
})
2828

29-
29+
3030
// Stub getBlockByHash for unit testing
3131
provider.portal.ETH.getBlockByHash = async (_hash: Uint8Array) => {
3232
return Block.fromBlockData({ header: BlockHeader.fromHeaderData({ number: 2n }) })
3333
}
3434

3535
provider.portal.ETH.getBlockByNumber = async (blockNumber: number | bigint | "latest" | "finalized") => {
36-
return Block.fromBlockData({
37-
header: BlockHeader.fromHeaderData({
38-
number: typeof blockNumber === 'string' ? 0n : blockNumber,
36+
return Block.fromBlockData({
37+
header: BlockHeader.fromHeaderData({
38+
number: typeof blockNumber === 'string' ? 0n : blockNumber,
3939

40-
})
4140
})
42-
}
43-
44-
provider.portal.ETH.getTransactionCount = async (_address: Uint8Array) => {
45-
return BigInt('0x5')
46-
}
47-
48-
provider.portal.ETH.getCode = async (_address: Uint8Array) => {
49-
return new Uint8Array(Buffer.from('60806040', 'hex'))
50-
}
51-
52-
provider.portal.ETH.getBalance = async (_address: Uint8Array) => {
53-
return 1000000000000000000n
54-
}
55-
56-
provider.portal.ETH.getStorageAt = async (_address: Uint8Array, _position: Uint8Array) => {
57-
const result = new Uint8Array(32)
58-
result[30] = 0x00
59-
result[31] = 0x01
60-
return Buffer.from(result).toString('hex')
61-
}
62-
63-
provider.portal.ETH.call = async (_txObject: any) => {
64-
return Buffer.from([0x00, 0x01]).toString('hex')
65-
}
66-
67-
const blockByHash = await provider.request({
68-
method: 'eth_getBlockByHash',
69-
params: ['0x123', false]
70-
}) as { result: { number: string } }
71-
expect(blockByHash.result.number).toBe('0x2')
72-
73-
const blockByNumber = await provider.request({
74-
method: 'eth_getBlockByNumber',
75-
params: [100, false]
76-
}) as { result: { number: string } }
77-
expect(blockByNumber.result.number).toBe('0x64')
78-
79-
const balance = await provider.request({
80-
method: 'eth_getBalance',
81-
params: ['0x3DC00AaD844393c110b61aED5849b7c82104e748', '0x0']
82-
}) as { result: string }
83-
expect(balance.result).toBe('0xde0b6b3a7640000')
84-
85-
const storage = await provider.request({
86-
method: 'eth_getStorageAt',
87-
params: [
88-
'0x1234567890123456789012345678901234567890',
89-
'0x0000000000000000000000000000000000000000000000000000000000000000',
90-
'0x64'
91-
]
92-
}) as { result: string }
93-
expect(storage.result).toBe('0x' + '00'.repeat(30) + '0001')
94-
95-
const call = await provider.request({
96-
method: 'eth_call',
97-
params: [{
98-
to: '0x1234567890123456789012345678901234567890',
99-
data: '0x70a08231000000000000000000000000'
100-
}, '0x64']
101-
}) as { result: string }
102-
expect(call.result).toBe('0x0001')
103-
104-
await expect(provider.request({
105-
method: 'eth_unsupportedMethod',
106-
params: []
107-
})).rejects.toThrow()
108-
109-
await expect(provider.request({
110-
method: 'eth_getBlockByHash',
111-
params: ['0x123']
112-
})).resolves.toEqual({
113-
error: {
114-
code: -32602,
115-
message: 'Invalid params for eth_getBlockByHash'
116-
},
117-
id: null,
118-
jsonrpc: '2.0',
11941
})
42+
}
43+
44+
provider.portal.ETH.getTransactionCount = async (_address: Uint8Array) => {
45+
return BigInt('0x5')
46+
}
47+
48+
provider.portal.ETH.getCode = async (_address: Uint8Array) => {
49+
return new Uint8Array(Buffer.from('60806040', 'hex'))
50+
}
51+
52+
provider.portal.ETH.getBalance = async (_address: Uint8Array) => {
53+
return 1000000000000000000n
54+
}
55+
56+
provider.portal.ETH.getStorageAt = async (_address: Uint8Array, _position: Uint8Array) => {
57+
const result = new Uint8Array(32)
58+
result[30] = 0x00
59+
result[31] = 0x01
60+
return Buffer.from(result).toString('hex')
61+
}
62+
63+
provider.portal.ETH.call = async (_txObject: any) => {
64+
return Buffer.from([0x00, 0x01]).toString('hex')
65+
}
66+
67+
const blockByHash = await provider.request({
68+
method: 'eth_getBlockByHash',
69+
params: ['0x123', false]
70+
}) as { result: { number: string } }
71+
expect(blockByHash.result.number).toBe('0x2')
72+
73+
const blockByNumber = await provider.request({
74+
method: 'eth_getBlockByNumber',
75+
params: [100, false]
76+
}) as { result: { number: string } }
77+
expect(blockByNumber.result.number).toBe('0x64')
78+
79+
const balance = await provider.request({
80+
method: 'eth_getBalance',
81+
params: ['0x3DC00AaD844393c110b61aED5849b7c82104e748', '0x0']
82+
}) as { result: string }
83+
expect(balance.result).toBe('0xde0b6b3a7640000')
84+
85+
86+
const storage = await provider.request({
87+
method: 'eth_getStorageAt',
88+
params: [
89+
'0x1234567890123456789012345678901234567890',
90+
'0x0000000000000000000000000000000000000000000000000000000000000000',
91+
'0x64'
92+
]
93+
}) as { result: string }
94+
expect(storage.result).toBe('0x' + '00'.repeat(30) + '0001')
95+
96+
const call = await provider.request({
97+
method: 'eth_call',
98+
params: [{
99+
to: '0x1234567890123456789012345678901234567890',
100+
data: '0x70a08231000000000000000000000000'
101+
}, '0x64']
102+
}) as { result: string }
103+
expect(call.result).toBe('0x0001')
104+
105+
await expect(provider.request({
106+
method: 'eth_unsupportedMethod',
107+
params: []
108+
})).rejects.toThrow()
109+
110+
await expect(provider.request({
111+
method: 'eth_getBlockByHash',
112+
params: ['0x123']
113+
})).resolves.toEqual({
114+
error: {
115+
code: -32602,
116+
message: 'Invalid params for eth_getBlockByHash'
117+
},
118+
id: null,
119+
jsonrpc: '2.0',
120+
})
120121

121-
await (provider as any).portal.stop()
122+
await provider.portal.stop()
122123

123124
})

0 commit comments

Comments
 (0)