Skip to content

Commit

Permalink
add read storage for provider
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjah committed Sep 26, 2024
1 parent b8f0c7e commit 15e1585
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 10 deletions.
17 changes: 9 additions & 8 deletions src/client/publicAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,14 @@ export class PublicAPI {
inputs: DatastoreEntry[],
final = true
): Promise<Uint8Array[]> {
const entriesQuery = inputs.map((entry) => ({
key: Array.from(entry.key),
address: entry.address,
}))
const entriesQuery = inputs.map((entry) => {
const byteKey: Uint8Array =
typeof entry.key === 'string' ? strToBytes(entry.key) : entry.key
return {
key: Array.from(byteKey),
address: entry.address,
}
})
const res = await withRetry(
() => this.connector.get_datastore_entries(entriesQuery),
this.options.retry!
Expand All @@ -238,10 +242,7 @@ export class PublicAPI {
address: string,
final = true
): Promise<Uint8Array> {
const byteKey: Uint8Array = typeof key === 'string' ? strToBytes(key) : key
return this.getDatastoreEntries([{ key: byteKey, address }], final).then(
(r) => r[0]
)
return this.getDatastoreEntries([{ key, address }], final).then((r) => r[0])
}

async getSlotTransfers(slot: Slot): Promise<Transfer[]> {
Expand Down
2 changes: 1 addition & 1 deletion src/client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,5 @@ export type ReadOnlyCallResult = {

export type DatastoreEntry = {
address: string
key: Uint8Array
key: Uint8Array | string
}
12 changes: 12 additions & 0 deletions src/provider/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,16 @@ export type Provider = {
getOperationStatus(opId: string): Promise<OperationStatus>
getEvents(filter: EventFilter): Promise<SCEvent[]>
getNodeStatus(): Promise<NodeStatusInfo>

/** Storage */
getStorageKeys(
address: string,
filter: Uint8Array | string,
final?: boolean
): Promise<Uint8Array[]>
readStorage(
address: string,
keys: Uint8Array[] | string[],
final?: boolean
): Promise<Uint8Array[]>
}
27 changes: 26 additions & 1 deletion src/provider/web3Provider/smartContracts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { Account, minBigInt, PublicAPI } from '../..'
import {
Account,
DatastoreEntry,
minBigInt,
PublicAPI,
strToBytes,
} from '../..'
import { U64 } from '../../basicElements/serializers/number/u64'
import { ErrorInsufficientBalance, ErrorMaxGas } from '../../errors'
import { MAX_GAS_CALL, MIN_GAS_CALL } from '../../smartContracts'
Expand Down Expand Up @@ -168,4 +174,23 @@ export class SCProvider {
datastore,
})
}

public async getStorageKeys(
address: string,
filter: Uint8Array | string = new Uint8Array(),
final = true
): Promise<Uint8Array[]> {
const filterBytes: Uint8Array =
typeof filter === 'string' ? strToBytes(filter) : filter
return this.client.getDataStoreKeys(address, filterBytes, final)
}

public async readStorage(
address: string,
keys: Uint8Array[] | string[],
final = true
): Promise<Uint8Array[]> {
const entries: DatastoreEntry[] = keys.map((key) => ({ address, key }))
return this.client.getDatastoreEntries(entries, final)
}
}

1 comment on commit 15e1585

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage report for experimental massa-web3

St.
Category Percentage Covered / Total
🟡 Statements 63.28% 1134/1792
🔴 Branches 43.41% 191/440
🔴 Functions 46.44% 209/450
🟡 Lines 63.62% 1128/1773

Test suite run success

129 tests passing in 13 suites.

Report generated by 🧪jest coverage report action from 15e1585

Please sign in to comment.