Skip to content

Commit

Permalink
Feat/add public provider (#722)
Browse files Browse the repository at this point in the history
* add PublicProvider class and update provider interfaces

* refactor: update Args constructor and enhance SmartContract provider handling

* test: integrate publicProvider into SmartContract tests

* refactor: update MNS and token classes to support PublicProvider

* refactor: replace Provider with PublicProvider in EventPoller and Operation classes

* refactor: enhance provider type handling and add provider type guard

* refactor: migrate Web3Provider to JsonRpcProvider and update related imports

* refactor: optimize balance retrieval by using getMultipleAddressInfo method

* refactor: ensure retro compatibility by exporting JsonRpcProvider and JsonRpcPublicProvider as Web3Provider
  • Loading branch information
Ben-Rey authored Jan 2, 2025
1 parent 6146f1a commit cd69a51
Show file tree
Hide file tree
Showing 21 changed files with 698 additions and 507 deletions.
2 changes: 1 addition & 1 deletion src/basicElements/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export class Args {
* @param offset - The optional offset to start deserializing from.
*/
constructor(
public serialized = new Uint8Array(),
public serialized: Uint8Array = new Uint8Array(),
public offset = DEFAULT_OFFSET
) {}

Expand Down
6 changes: 3 additions & 3 deletions src/contracts-wrappers/mns.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Args, bytesToStr, strToBytes, U256 } from '../basicElements'
import { Operation } from '../operation'
import { Provider } from '../provider'
import { Provider, PublicProvider } from '../provider'
import { CallSCOptions, ReadSCOptions, SmartContract } from '../smartContracts'
import { checkNetwork } from './tokens'

Expand Down Expand Up @@ -36,12 +36,12 @@ const DOMAIN_KEY_PREFIX = [0x3]
const OWNED_TOKENS_KEY = strToBytes('ownedTokens')

export class MNS extends SmartContract {
static mainnet(provider: Provider): MNS {
static mainnet(provider: Provider | PublicProvider): MNS {
checkNetwork(provider, true)
return new MNS(provider, MNS_CONTRACTS.mainnet)
}

static buildnet(provider: Provider): MNS {
static buildnet(provider: Provider | PublicProvider): MNS {
checkNetwork(provider, false)
return new MNS(provider, MNS_CONTRACTS.buildnet)
}
Expand Down
33 changes: 18 additions & 15 deletions src/contracts-wrappers/tokens.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { Provider } from '../provider'
import { Provider, PublicProvider } from '../provider'
import { CHAIN_ID, Network } from '../utils'
import { MRC20 } from './token'

Expand All @@ -22,7 +22,10 @@ export const BUILDNET_TOKENS = {
WMAS: 'AS12FW5Rs5YN2zdpEnqwj4iHUUPt9R4Eqjq2qtpJFNKW3mn33RuLU',
}

export function checkNetwork(provider: Provider, isMainnet: boolean): void {
export function checkNetwork(
provider: PublicProvider | Provider,
isMainnet: boolean
): void {
provider.networkInfos().then((network: Network) => {
if (isMainnet && network.chainId !== CHAIN_ID.Mainnet) {
console.warn('This contract is only available on mainnet')
Expand All @@ -35,49 +38,49 @@ export function checkNetwork(provider: Provider, isMainnet: boolean): void {
///////////////// MAINNET TOKENS //////////////////////

export class USDCe extends MRC20 {
constructor(public provider: Provider) {
constructor(public provider: Provider | PublicProvider) {
checkNetwork(provider, true)
super(provider, MAINNET_TOKENS.USDCe)
}
}

export class USDTb extends MRC20 {
constructor(public provider: Provider) {
constructor(public provider: Provider | PublicProvider) {
checkNetwork(provider, true)
super(provider, MAINNET_TOKENS.USDTb)
}
}

export class DAIe extends MRC20 {
constructor(public provider: Provider) {
constructor(public provider: Provider | PublicProvider) {
checkNetwork(provider, true)
super(provider, MAINNET_TOKENS.DAIe)
}
}

export class WETHe extends MRC20 {
constructor(public provider: Provider) {
constructor(public provider: Provider | PublicProvider) {
checkNetwork(provider, true)
super(provider, MAINNET_TOKENS.WETHe)
}
}

export class WETHb extends MRC20 {
constructor(public provider: Provider) {
constructor(public provider: Provider | PublicProvider) {
checkNetwork(provider, true)
super(provider, MAINNET_TOKENS.WETHb)
}
}

export class PUR extends MRC20 {
constructor(public provider: Provider) {
constructor(public provider: Provider | PublicProvider) {
checkNetwork(provider, true)
super(provider, MAINNET_TOKENS.PUR)
}
}

export class WMAS extends MRC20 {
constructor(public provider: Provider) {
constructor(public provider: Provider | PublicProvider) {
checkNetwork(provider, true)
super(provider, MAINNET_TOKENS.WMAS)
}
Expand All @@ -86,42 +89,42 @@ export class WMAS extends MRC20 {
///////////////// BUILDNET TOKENS //////////////////////

export class DAIs extends MRC20 {
constructor(public provider: Provider) {
constructor(public provider: Provider | PublicProvider) {
checkNetwork(provider, false)
super(provider, BUILDNET_TOKENS.DAIs)
}
}

export class WETHs extends MRC20 {
constructor(public provider: Provider) {
constructor(public provider: Provider | PublicProvider) {
checkNetwork(provider, false)
super(provider, BUILDNET_TOKENS.WETHs)
}
}

export class USDCs extends MRC20 {
constructor(public provider: Provider) {
constructor(public provider: Provider | PublicProvider) {
checkNetwork(provider, false)
super(provider, BUILDNET_TOKENS.USDCs)
}
}

export class USDTbt extends MRC20 {
constructor(public provider: Provider) {
constructor(public provider: Provider | PublicProvider) {
checkNetwork(provider, false)
super(provider, BUILDNET_TOKENS.USDTbt)
}
}

export class WETHbt extends MRC20 {
constructor(public provider: Provider) {
constructor(public provider: Provider | PublicProvider) {
checkNetwork(provider, false)
super(provider, BUILDNET_TOKENS.WETHbt)
}
}

export class WMASBuildnet extends MRC20 {
constructor(public provider: Provider) {
constructor(public provider: Provider | PublicProvider) {
checkNetwork(provider, false)
super(provider, BUILDNET_TOKENS.WMAS)
}
Expand Down
6 changes: 3 additions & 3 deletions src/events/eventsPoller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Provider } from '../provider'
import { PublicProvider } from '../provider'
import { EventFilter, NB_THREADS } from '../client'
import EventEmitter from 'eventemitter3'
import { rpcTypes as t } from '../generated'
Expand Down Expand Up @@ -36,7 +36,7 @@ export class EventPoller extends EventEmitter {
* @param pollIntervalMs - The interval in milliseconds to poll for events.
*/
public constructor(
private readonly provider: Provider,
private readonly provider: PublicProvider,
private readonly eventsFilter: EventFilter,
private readonly pollIntervalMs: number
) {
Expand Down Expand Up @@ -90,7 +90,7 @@ export class EventPoller extends EventEmitter {
*/
// eslint-disable-next-line max-params
public static start(
provider: Provider,
provider: PublicProvider,
eventsFilter: EventFilter,
onData?: (data: t.OutputEvents) => void,
onError?: (err: Error) => void,
Expand Down
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
File renamed without changes.
7 changes: 7 additions & 0 deletions src/provider/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Provider } from './interface'

export function isProvider(provider: unknown): provider is Provider {
return (
typeof provider === 'object' && provider !== null && 'callSC' in provider
)
}
3 changes: 2 additions & 1 deletion src/provider/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './types'
export * from './constants'
export * from './interface'
export * from './web3Provider'
export * from './jsonRpcProvider'
49 changes: 27 additions & 22 deletions src/provider/interface.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,44 @@
import { Address, EventFilter, Network, SmartContract } from '..'
import { Mas } from '../basicElements/mas'
import { Operation, OperationOptions, OperationStatus } from '../operation'
import { rpcTypes as t } from '../generated'
import {
CallSCParams,
DeploySCParams,
ReadSCData,
ReadSCParams,
SignedData,
NodeStatusInfo,
ExecuteScParams,
SignOptions,
NodeStatusInfo,
ReadSCData,
ReadSCParams,
} from './'
import { rpcTypes as t } from '../generated'

export type PublicProvider = {
balanceOf(
addresses: string[],
final?: boolean
): Promise<{ address: string; balance: bigint }[]>
networkInfos(): Promise<Network>
getOperationStatus(opId: string): Promise<OperationStatus>
getEvents(filter: EventFilter): Promise<t.OutputEvents>
getNodeStatus(): Promise<NodeStatusInfo>
readSC(params: ReadSCParams): Promise<ReadSCData>
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 = PublicProvider & {
/** Retrieves the account's address. */
get address(): string

Expand All @@ -28,7 +50,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 +62,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[]>
}
2 changes: 2 additions & 0 deletions src/provider/jsonRpcProvider/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './jsonRpcProvider'
export * from './jsonRpcPublicProvider'
Loading

1 comment on commit cd69a51

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage report for experimental massa-web3

St.
Category Percentage Covered / Total
🟡 Statements 66.79% 1253/1876
🔴 Branches 43.79% 201/459
🔴 Functions 47.23% 222/470
🟡 Lines 67.31% 1246/1851

Test suite run success

134 tests passing in 15 suites.

Report generated by 🧪jest coverage report action from cd69a51

Please sign in to comment.