Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add public provider #721

Closed
Closed
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
4 changes: 2 additions & 2 deletions src/operation/operation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Args, ArrayTypes } from '../basicElements'
import { rpcTypes as t } from '../generated'
import { Provider } from '../provider'
import { Provider, PublicProvider } from '../provider'
import { rawEventDecode } from '../utils'
import { OperationStatus } from './types'

Expand All @@ -12,7 +12,7 @@ const DEFAULT_WAIT_PERIOD_MS = 500
*/
export class Operation {
constructor(
public provider: Provider,
public provider: Provider | PublicProvider,
public id: string
) {}

Expand Down
28 changes: 4 additions & 24 deletions src/provider/interface.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
import { Address, EventFilter, Network, SmartContract } from '..'
import { Address, SmartContract } from '..'
import { Mas } from '../basicElements/mas'
import { Operation, OperationOptions, OperationStatus } from '../operation'
import { Operation, OperationOptions } from '../operation'
import {
CallSCParams,
DeploySCParams,
ReadSCData,
ReadSCParams,
SignedData,
NodeStatusInfo,
ExecuteScParams,
SignOptions,
PublicProvider,
} from './'
import { rpcTypes as t } from '../generated'

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

Expand All @@ -28,7 +25,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 +37,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[]>
}
113 changes: 113 additions & 0 deletions src/provider/web3Provider/PublicProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// a Web3Provider is the combination of a clientAPI and an private key account
import { NodeStatusInfo, ReadSCData, ReadSCParams } from '..'
import {
Account,
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 {
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.
*
* @remarks Be a aware that if you don't provide a caller address, it will generate a random one.
*/
async readSC(params: ReadSCParams): Promise<ReadSCData> {
const args = params.parameter ?? new Uint8Array()
const caller =
params.caller ?? (await Account.generate()).address.toString()
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'
Loading
Loading