|
| 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 | +} |
0 commit comments