Skip to content

Commit

Permalink
Refactor web3Provider structure: introduce PublicProvider and remove …
Browse files Browse the repository at this point in the history
…smartContracts
  • Loading branch information
Ben-Rey committed Dec 26, 2024
1 parent 6146f1a commit 36dd055
Show file tree
Hide file tree
Showing 5 changed files with 305 additions and 270 deletions.
44 changes: 26 additions & 18 deletions src/provider/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,35 @@ import {
} from './'
import { rpcTypes as t } from '../generated'

type AddressBalance = {
address: string
balance: bigint
}

export type PublicProviderT = {
balanceOf(addresses: string[], final: boolean): Promise<AddressBalance[]>
networkInfos(): Promise<Network>
readSC(params: ReadSCParams): Promise<ReadSCData>
getOperationStatus(opId: string): Promise<OperationStatus>
getEvents(filter: EventFilter): Promise<t.OutputEvents>
getNodeStatus(): Promise<NodeStatusInfo>
/** Storage */
getStorageKeys(
address: string,
filter: Uint8Array | string,
final?: boolean
): Promise<Uint8Array[]>
readStorage(
address: string,
keys: Uint8Array[] | string[],
final?: boolean
): Promise<Uint8Array[]>
}

/**
* Defines the expected structure for a provider.
*/
export type Provider = {
export type Provider = PublicProviderT & {
/** Retrieves the account's address. */
get address(): string

Expand All @@ -28,7 +53,6 @@ export type Provider = {

/** Initiates a balance retrieval request for the account. */
balance(final: boolean): Promise<bigint>
networkInfos(): Promise<Network>
sign(
data: Uint8Array | string,
signOptions?: SignOptions
Expand All @@ -41,22 +65,6 @@ export type Provider = {
opts?: OperationOptions
): Promise<Operation>
callSC(params: CallSCParams): Promise<Operation>
readSC(params: ReadSCParams): Promise<ReadSCData>
executeSC(params: ExecuteScParams): Promise<Operation>
deploySC(params: DeploySCParams): Promise<SmartContract>
getOperationStatus(opId: string): Promise<OperationStatus>
getEvents(filter: EventFilter): Promise<t.OutputEvents>
getNodeStatus(): Promise<NodeStatusInfo>

/** Storage */
getStorageKeys(
address: string,
filter: Uint8Array | string,
final?: boolean
): Promise<Uint8Array[]>
readStorage(
address: string,
keys: Uint8Array[] | string[],
final?: boolean
): Promise<Uint8Array[]>
}
110 changes: 110 additions & 0 deletions src/provider/web3Provider/PublicProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// a Web3Provider is the combination of a clientAPI and an private key account
import { NodeStatusInfo, PublicProviderT, ReadSCData, ReadSCParams } from '..'
import {
CHAIN_ID,
DatastoreEntry,
EventFilter,
Network,
NetworkName,
PublicAPI,
PublicApiUrl,
strToBytes,
} from '../..'
import { rpcTypes as t } from '../../generated'
import { OperationStatus } from '../../operation'
import { formatNodeStatusObject } from '../../client/formatObjects'

export class PublicProvider implements PublicProviderT {
constructor(public client: PublicAPI) {}

static fromRPCUrl(url: string): PublicProvider {
return new PublicProvider(new PublicAPI(url))
}

static mainnet(): PublicProvider {
return PublicProvider.fromRPCUrl(PublicApiUrl.Mainnet)
}

static buildnet(): PublicProvider {
return PublicProvider.fromRPCUrl(PublicApiUrl.Buildnet)
}

async balanceOf(
addresses: string[],
final = true
): Promise<{ address: string; balance: bigint }[]> {
const balances = await Promise.all(
addresses.map(async (address) => ({
address,
balance: await this.client.getBalance(address, final),
}))
)
return balances
}

async networkInfos(): Promise<Network> {
const chainId = await this.client.getChainId()
let name = 'Unknown'
if (chainId === CHAIN_ID.Mainnet) {
name = NetworkName.Mainnet
} else if (chainId === CHAIN_ID.Buildnet) {
name = NetworkName.Buildnet
}

return {
name,
chainId,
url: this.client.url,
minimalFee: await this.client.getMinimalFee(),
}
}

public async getOperationStatus(opId: string): Promise<OperationStatus> {
return this.client.getOperationStatus(opId)
}

public async getEvents(filter: EventFilter): Promise<t.OutputEvents> {
return this.client.getEvents(filter)
}

public async getNodeStatus(): Promise<NodeStatusInfo> {
const status = await this.client.status()
return formatNodeStatusObject(status)
}

/**
* Reads smart contract function.
* @param params - readSCParams.
* @returns A promise that resolves to a ReadSCData.
*/
async readSC(params: ReadSCParams): Promise<ReadSCData> {
const args = params.parameter ?? new Uint8Array()
// TODO - check if this is correct
const caller = params.caller ?? ''
const readOnlyParams = {
...params,
caller,
parameter: args instanceof Uint8Array ? args : args.serialize(),
}
return this.client.executeReadOnlyCall(readOnlyParams)
}

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)
}
}
4 changes: 2 additions & 2 deletions src/provider/web3Provider/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './web3Provider'
export * from './constants'
export * from './smartContracts'
export * from './PublicProvider'
export * from './Web3Provider'

Check failure on line 3 in src/provider/web3Provider/index.ts

View workflow job for this annotation

GitHub Actions / test / build

Cannot find module './Web3Provider' or its corresponding type declarations.
209 changes: 0 additions & 209 deletions src/provider/web3Provider/smartContracts.ts

This file was deleted.

Loading

0 comments on commit 36dd055

Please sign in to comment.