Skip to content

Commit 1f0ff97

Browse files
committed
Refactor web3Provider structure: introduce PublicProvider and remove smartContracts
1 parent 6146f1a commit 1f0ff97

File tree

5 files changed

+305
-270
lines changed

5 files changed

+305
-270
lines changed

src/provider/interface.ts

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,35 @@ import {
1313
} from './'
1414
import { rpcTypes as t } from '../generated'
1515

16+
type AddressBalance = {
17+
address: string
18+
balance: bigint
19+
}
20+
21+
export type PublicProviderT = {
22+
balanceOf(addresses: string[], final: boolean): Promise<AddressBalance[]>
23+
networkInfos(): Promise<Network>
24+
readSC(params: ReadSCParams): Promise<ReadSCData>
25+
getOperationStatus(opId: string): Promise<OperationStatus>
26+
getEvents(filter: EventFilter): Promise<t.OutputEvents>
27+
getNodeStatus(): Promise<NodeStatusInfo>
28+
/** Storage */
29+
getStorageKeys(
30+
address: string,
31+
filter: Uint8Array | string,
32+
final?: boolean
33+
): Promise<Uint8Array[]>
34+
readStorage(
35+
address: string,
36+
keys: Uint8Array[] | string[],
37+
final?: boolean
38+
): Promise<Uint8Array[]>
39+
}
40+
1641
/**
1742
* Defines the expected structure for a provider.
1843
*/
19-
export type Provider = {
44+
export type Provider = PublicProviderT & {
2045
/** Retrieves the account's address. */
2146
get address(): string
2247

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

2954
/** Initiates a balance retrieval request for the account. */
3055
balance(final: boolean): Promise<bigint>
31-
networkInfos(): Promise<Network>
3256
sign(
3357
data: Uint8Array | string,
3458
signOptions?: SignOptions
@@ -41,22 +65,6 @@ export type Provider = {
4165
opts?: OperationOptions
4266
): Promise<Operation>
4367
callSC(params: CallSCParams): Promise<Operation>
44-
readSC(params: ReadSCParams): Promise<ReadSCData>
4568
executeSC(params: ExecuteScParams): Promise<Operation>
4669
deploySC(params: DeploySCParams): Promise<SmartContract>
47-
getOperationStatus(opId: string): Promise<OperationStatus>
48-
getEvents(filter: EventFilter): Promise<t.OutputEvents>
49-
getNodeStatus(): Promise<NodeStatusInfo>
50-
51-
/** Storage */
52-
getStorageKeys(
53-
address: string,
54-
filter: Uint8Array | string,
55-
final?: boolean
56-
): Promise<Uint8Array[]>
57-
readStorage(
58-
address: string,
59-
keys: Uint8Array[] | string[],
60-
final?: boolean
61-
): Promise<Uint8Array[]>
6270
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// a Web3Provider is the combination of a clientAPI and an private key account
2+
import { NodeStatusInfo, PublicProviderT, ReadSCData, ReadSCParams } from '..'
3+
import {
4+
CHAIN_ID,
5+
DatastoreEntry,
6+
EventFilter,
7+
Network,
8+
NetworkName,
9+
PublicAPI,
10+
PublicApiUrl,
11+
strToBytes,
12+
} from '../..'
13+
import { rpcTypes as t } from '../../generated'
14+
import { OperationStatus } from '../../operation'
15+
import { formatNodeStatusObject } from '../../client/formatObjects'
16+
17+
export class PublicProvider implements PublicProviderT {
18+
constructor(public client: PublicAPI) {}
19+
20+
static fromRPCUrl(url: string): PublicProvider {
21+
return new PublicProvider(new PublicAPI(url))
22+
}
23+
24+
static mainnet(): PublicProvider {
25+
return PublicProvider.fromRPCUrl(PublicApiUrl.Mainnet)
26+
}
27+
28+
static buildnet(): PublicProvider {
29+
return PublicProvider.fromRPCUrl(PublicApiUrl.Buildnet)
30+
}
31+
32+
async balanceOf(
33+
addresses: string[],
34+
final = true
35+
): Promise<{ address: string; balance: bigint }[]> {
36+
const balances = await Promise.all(
37+
addresses.map(async (address) => ({
38+
address,
39+
balance: await this.client.getBalance(address, final),
40+
}))
41+
)
42+
return balances
43+
}
44+
45+
async networkInfos(): Promise<Network> {
46+
const chainId = await this.client.getChainId()
47+
let name = 'Unknown'
48+
if (chainId === CHAIN_ID.Mainnet) {
49+
name = NetworkName.Mainnet
50+
} else if (chainId === CHAIN_ID.Buildnet) {
51+
name = NetworkName.Buildnet
52+
}
53+
54+
return {
55+
name,
56+
chainId,
57+
url: this.client.url,
58+
minimalFee: await this.client.getMinimalFee(),
59+
}
60+
}
61+
62+
public async getOperationStatus(opId: string): Promise<OperationStatus> {
63+
return this.client.getOperationStatus(opId)
64+
}
65+
66+
public async getEvents(filter: EventFilter): Promise<t.OutputEvents> {
67+
return this.client.getEvents(filter)
68+
}
69+
70+
public async getNodeStatus(): Promise<NodeStatusInfo> {
71+
const status = await this.client.status()
72+
return formatNodeStatusObject(status)
73+
}
74+
75+
/**
76+
* Reads smart contract function.
77+
* @param params - readSCParams.
78+
* @returns A promise that resolves to a ReadSCData.
79+
*/
80+
async readSC(params: ReadSCParams): Promise<ReadSCData> {
81+
const args = params.parameter ?? new Uint8Array()
82+
// TODO - check if this is correct
83+
const caller = params.caller ?? ''
84+
const readOnlyParams = {
85+
...params,
86+
caller,
87+
parameter: args instanceof Uint8Array ? args : args.serialize(),
88+
}
89+
return this.client.executeReadOnlyCall(readOnlyParams)
90+
}
91+
92+
public async getStorageKeys(
93+
address: string,
94+
filter: Uint8Array | string = new Uint8Array(),
95+
final = true
96+
): Promise<Uint8Array[]> {
97+
const filterBytes: Uint8Array =
98+
typeof filter === 'string' ? strToBytes(filter) : filter
99+
return this.client.getDataStoreKeys(address, filterBytes, final)
100+
}
101+
102+
public async readStorage(
103+
address: string,
104+
keys: Uint8Array[] | string[],
105+
final = true
106+
): Promise<Uint8Array[]> {
107+
const entries: DatastoreEntry[] = keys.map((key) => ({ address, key }))
108+
return this.client.getDatastoreEntries(entries, final)
109+
}
110+
}

src/provider/web3Provider/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
export * from './web3Provider'
21
export * from './constants'
3-
export * from './smartContracts'
2+
export * from './PublicProvider'
3+
export * from './web3Provider'

src/provider/web3Provider/smartContracts.ts

Lines changed: 0 additions & 209 deletions
This file was deleted.

0 commit comments

Comments
 (0)