-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor web3Provider structure: introduce PublicProvider and remove …
…smartContracts
- Loading branch information
Showing
5 changed files
with
305 additions
and
270 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.