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

560 xp add sc manager #563

Merged
merged 8 commits into from
Apr 23, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './signature'
export * from './keys'
export * from './operationManager'
export * from './operation'
export * from './storage'
112 changes: 100 additions & 12 deletions packages/massa-web3/src/experimental/basicElements/operation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SCOutputEvent } from '../client'
import { EventFilter, SCOutputEvent } from '../client'
import { BlockchainClient } from '../client'

/**
Expand All @@ -10,24 +10,109 @@
public id: string
) {}

/**
* Gets the status of the operation.
*
* @returns The status of the operation.
*/
async getStatus(): Promise<OperationStatus> {
return await this.client.getOperationStatus(this.id)
}

async waitSpeculativeExecution(): Promise<OperationStatus> {
throw new Error('Not implemented')
/**
* Waits for the operation to reach a specific status.
*
* @param exitThreshold - The minimal status to wait for.
* @param timeout - The maximum time to wait.
* @param period - The time interval to check the status.
*
* @returns The status of the operation or NotFound if the timeout is reached.
*/
private wait(

Check warning on line 31 in packages/massa-web3/src/experimental/basicElements/operation.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🕹️ Function is not covered

Warning! Not covered function
exitThreshold: OperationStatus,
timeout: number = 60000,

Check warning on line 33 in packages/massa-web3/src/experimental/basicElements/operation.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
period: number = 500

Check warning on line 34 in packages/massa-web3/src/experimental/basicElements/operation.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
): Promise<OperationStatus> {
return new Promise<OperationStatus>((resolve) => {

Check warning on line 36 in packages/massa-web3/src/experimental/basicElements/operation.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🕹️ Function is not covered

Warning! Not covered function
let elapsedTime = 0

Check warning on line 37 in packages/massa-web3/src/experimental/basicElements/operation.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
const checkStatusInterval = setInterval(async () => {

Check warning on line 38 in packages/massa-web3/src/experimental/basicElements/operation.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🕹️ Function is not covered

Warning! Not covered function
const currentStatus = await this.getStatus()

Check warning on line 39 in packages/massa-web3/src/experimental/basicElements/operation.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
if (currentStatus >= exitThreshold) {
clearInterval(checkStatusInterval)

Check warning on line 41 in packages/massa-web3/src/experimental/basicElements/operation.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
resolve(currentStatus)

Check warning on line 42 in packages/massa-web3/src/experimental/basicElements/operation.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
} else if (elapsedTime >= timeout) {
clearInterval(checkStatusInterval)

Check warning on line 44 in packages/massa-web3/src/experimental/basicElements/operation.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
resolve(OperationStatus.NotFound)

Check warning on line 45 in packages/massa-web3/src/experimental/basicElements/operation.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 46 in packages/massa-web3/src/experimental/basicElements/operation.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 46 in packages/massa-web3/src/experimental/basicElements/operation.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 46 in packages/massa-web3/src/experimental/basicElements/operation.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

Check warning on line 46 in packages/massa-web3/src/experimental/basicElements/operation.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

Check warning on line 46 in packages/massa-web3/src/experimental/basicElements/operation.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
elapsedTime += period

Check warning on line 47 in packages/massa-web3/src/experimental/basicElements/operation.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}, period)

Check warning on line 48 in packages/massa-web3/src/experimental/basicElements/operation.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
})

Check warning on line 49 in packages/massa-web3/src/experimental/basicElements/operation.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

async waitFinalExecution(): Promise<OperationStatus> {
throw new Error('Not implemented')
/**
* Waits for the operation to be included in a block.
*
* @param timeout - The maximum time to wait.
* @param period - The time interval to check the status.
*
* @returns The status of the operation or NotFound if the timeout is reached.
*/
async waitSpeculativeExecution(

Check warning on line 60 in packages/massa-web3/src/experimental/basicElements/operation.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🕹️ Function is not covered

Warning! Not covered function
timeout?: number,
period?: number
): Promise<OperationStatus> {
return this.wait(OperationStatus.SpeculativeSuccess, timeout, period)

Check warning on line 64 in packages/massa-web3/src/experimental/basicElements/operation.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

/**
* Waits for the block containing the operation to be final.
*
* @param timeout - The maximum time to wait.
* @param period - The time interval to check the status.
*
* @returns The status of the operation or NotFound if the timeout is reached.
*/
async waitFinalExecution(

Check warning on line 75 in packages/massa-web3/src/experimental/basicElements/operation.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🕹️ Function is not covered

Warning! Not covered function
timeout?: number,
period?: number
): Promise<OperationStatus> {
return this.wait(OperationStatus.Success, timeout, period)

Check warning on line 79 in packages/massa-web3/src/experimental/basicElements/operation.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

/**
* Gets the events of the operation once the block reaches the final state.
*
* @returns The events of the operation.
*/
async getFinalEvents(): Promise<SCOutputEvent[]> {
throw new Error('Not implemented')
if ((await this.waitFinalExecution()) === OperationStatus.NotFound) {
return Promise.reject(new Error('Operation not found'))

Check warning on line 89 in packages/massa-web3/src/experimental/basicElements/operation.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 90 in packages/massa-web3/src/experimental/basicElements/operation.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 90 in packages/massa-web3/src/experimental/basicElements/operation.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

const filter = {
operationId: this.id,
isFinal: true,
} as EventFilter

Check warning on line 95 in packages/massa-web3/src/experimental/basicElements/operation.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

return this.client.getEvents(filter)

Check warning on line 97 in packages/massa-web3/src/experimental/basicElements/operation.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

/**
* Gets the events of the speculative execution of the operation.
*
* @returns The speculative events of the operation.
*/
async getSpeculativeEvents(): Promise<SCOutputEvent[]> {
throw new Error('Not implemented')
if ((await this.waitSpeculativeExecution()) === OperationStatus.NotFound) {
return Promise.reject(new Error('Operation not found'))

Check warning on line 107 in packages/massa-web3/src/experimental/basicElements/operation.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 108 in packages/massa-web3/src/experimental/basicElements/operation.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 108 in packages/massa-web3/src/experimental/basicElements/operation.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

const filter = {
operationId: this.id,
isFinal: false,
} as EventFilter

Check warning on line 113 in packages/massa-web3/src/experimental/basicElements/operation.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

return this.client.getEvents(filter)

Check warning on line 115 in packages/massa-web3/src/experimental/basicElements/operation.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}
}

Expand All @@ -36,8 +121,16 @@
*
* @remarks
* This enumeration captures the lifecycle stages of a blockchain operation, from initiation to finalization.
*
* @privateRemarks
* Keeps the order of the stages in the lifecycle as it is used by the wait method.
*/
export enum OperationStatus {
/**
* The operation has not been found within the blockchain, either because it is not yet processed or does not exist.
*/
NotFound,

/**
* The operation has been recognized and is awaiting inclusion in the blockchain ledger.
*/
Expand All @@ -62,9 +155,4 @@
* The operation has failed and the block containing the failure has been confirmed as final.
*/
Error,

/**
* The operation has not been found within the blockchain, either because it is not yet processed or does not exist.
*/
NotFound,
}
49 changes: 49 additions & 0 deletions packages/massa-web3/src/experimental/basicElements/storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { fromMAS } from '@massalabs/web3-utils'

/**
* Calculates the cost of deploying a smart contract.
*/
export class StorageCost {
/**
* Calculates the cost of a given number of bytes.
*
* @param numberOfBytes - The number of bytes.
*
* @returns The cost in the smallest unit of the Massa currency.
*/
static bytes(numberOfBytes: number): bigint {

Check warning on line 14 in packages/massa-web3/src/experimental/basicElements/storage.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🕹️ Function is not covered

Warning! Not covered function
return BigInt(numberOfBytes) * fromMAS(0.0001)

Check warning on line 15 in packages/massa-web3/src/experimental/basicElements/storage.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

/**
* Calculates the cost of creating a new account.
*
* @returns The cost in the smallest unit of the Massa currency.
*/
static account(): bigint {

Check warning on line 23 in packages/massa-web3/src/experimental/basicElements/storage.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🕹️ Function is not covered

Warning! Not covered function
return fromMAS(0.001)

Check warning on line 24 in packages/massa-web3/src/experimental/basicElements/storage.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

/**
* Calculates the cost of deploying a smart contract.
*
* @remarks
* The cost of deploying a smart contract includes the cost of creating a new account.
*
* @param numberOfBytes - The number of bytes of the smart contract.
*
* @returns The cost in the smallest unit of the Massa currency.
*/
static smartContract(numberOfBytes: number): bigint {

Check warning on line 37 in packages/massa-web3/src/experimental/basicElements/storage.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🕹️ Function is not covered

Warning! Not covered function
return StorageCost.bytes(numberOfBytes) + StorageCost.account()

Check warning on line 38 in packages/massa-web3/src/experimental/basicElements/storage.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

/**
* Calculates the cost of creating a new entry in the datastore.
*
* @returns The cost in the smallest unit of the Massa currency.
*/
static newEntry(): bigint {

Check warning on line 46 in packages/massa-web3/src/experimental/basicElements/storage.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🕹️ Function is not covered

Warning! Not covered function
return StorageCost.bytes(4)

Check warning on line 47 in packages/massa-web3/src/experimental/basicElements/storage.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}
}
12 changes: 12 additions & 0 deletions packages/massa-web3/src/experimental/client/client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { OperationStatus } from '../basicElements'
import { Slot } from '../generated/client'

export interface SendOperationInput {
data: Uint8Array
Expand All @@ -10,6 +11,16 @@ export interface SCOutputEvent {
data: string
}

export interface EventFilter {
start?: Slot
end?: Slot
smartContractAddress?: string
callerAddress?: string
operationId?: string
isFinal?: boolean
isError?: boolean
}

/*
* Blockchain client functions needed by the Operation class to send operations to the blockchain.
*/
Expand All @@ -19,4 +30,5 @@ export interface BlockchainClient {
fetchPeriod(): Promise<number>
getOperationStatus(operationId: string): Promise<OperationStatus>
getBalance(address: string, speculative?: boolean): Promise<bigint>
getEvents(filter: EventFilter): Promise<SCOutputEvent[]>
}
20 changes: 15 additions & 5 deletions packages/massa-web3/src/experimental/publicAPI.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { OperationStatus } from './basicElements'
import { SendOperationInput } from './client'
import { SendOperationInput, EventFilter as EvtFilter } from './client'
import {
OperationInput,
Pagination,
Expand Down Expand Up @@ -95,7 +95,7 @@
speculative: boolean = false
): Promise<bigint> {
return this.getAddressInfo(address).then((r) => {
console.log('getAddressInfo', r)

Check warning on line 98 in packages/massa-web3/src/experimental/publicAPI.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected console statement
return BigInt(speculative ? r.candidate_balance : r.final_balance)
})
}
Expand Down Expand Up @@ -176,10 +176,20 @@
return this.connector.get_endorsements(endorsementIds)
}

async getFilteredScOutputEvent(
filter: EventFilter
): Promise<SCOutputEvent[]> {
return this.connector.get_filtered_sc_output_event(filter)
async getEvents(filter: EvtFilter): Promise<SCOutputEvent[]> {
filter = {
start: filter.start,
end: filter.end,
emitter_address: filter.smartContractAddress,
original_caller_address: filter.callerAddress,
original_operation_id: filter.operationId,
is_final: filter.isFinal,
is_error: filter.isError,
} as EventFilter

const events = await this.connector.get_filtered_sc_output_event(filter)

return events
}

async getGraphInterval(
Expand Down
Loading
Loading