Skip to content

Commit

Permalink
Merge pull request #23 from ideal-lab5/feat/async-finalized-block-number
Browse files Browse the repository at this point in the history
feat: add async finalized block number
  • Loading branch information
juangirini authored Oct 14, 2024
2 parents d8d93e0 + ae898df commit d59682d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
37 changes: 19 additions & 18 deletions src/MurmurClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
CreateResponse,
ExecuteRequest,
ExecuteResponse,
NewRequest,
CreateRequest,
} from './types'

export class MurmurClient {
Expand Down Expand Up @@ -86,16 +86,10 @@ export class MurmurClient {
)
}

if (!this.finalizedBlockNumber) {
throw new Error(
`No finalized blocks have been observed - are you sure the chain is running?`
)
}

const request: NewRequest = {
const request: CreateRequest = {
validity,
current_block: this.finalizedBlockNumber,
round_pubkey: (await this.getRoundPublic()).toString(),
current_block: await this.getFinalizedBlockNumber(),
round_pubkey: await this.getRoundPublic(),
}

try {
Expand Down Expand Up @@ -127,15 +121,9 @@ export class MurmurClient {
call: Call,
callback: (result: any) => Promise<void> = async () => {}
): Promise<void> {
if (!this.finalizedBlockNumber) {
throw new Error(
`No finalized blocks have been observed - are you sure the chain is running?`
)
}

const request: ExecuteRequest = {
runtime_call: this.encodeCall(call),
current_block: this.finalizedBlockNumber,
current_block: await this.getFinalizedBlockNumber(),
}
try {
const response = (await this.http.post('/execute', request))
Expand All @@ -159,7 +147,7 @@ export class MurmurClient {
}
}

private async getRoundPublic(): Promise<String> {
private async getRoundPublic(): Promise<string> {
await this.idn.isReady
let roundPublic = await this.idn.query.etf.roundPublic()
return roundPublic.toString()
Expand Down Expand Up @@ -190,4 +178,17 @@ export class MurmurClient {
private encodeCall(ext: Call): number[] {
return Array.from(ext.inner.toU8a())
}

private async getFinalizedBlockNumber(): Promise<number> {
return new Promise((resolve) => {
const checkFinalizedBlockNumber = () => {
if (this.finalizedBlockNumber !== null) {
resolve(this.finalizedBlockNumber)
} else {
setTimeout(checkFinalizedBlockNumber, 100) // Check again after 100ms
}
}
checkFinalizedBlockNumber()
})
}
}
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SubmittableExtrinsic } from '@polkadot/api/types'
import { ISubmittableResult } from '@polkadot/types/types'

export type NewRequest = {
export type CreateRequest = {
validity: number
current_block: number
round_pubkey: string
Expand Down

0 comments on commit d59682d

Please sign in to comment.