Skip to content

Commit

Permalink
Merge pull request #157 from Juneo-io/dev
Browse files Browse the repository at this point in the history
v0.0.123
  • Loading branch information
alekswaslet authored Jul 12, 2024
2 parents e192284 + ca1e599 commit 00e9775
Show file tree
Hide file tree
Showing 20 changed files with 468 additions and 334 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "juneojs",
"version": "0.0.122",
"version": "0.0.123",
"description": "Juneo JS Library",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
43 changes: 21 additions & 22 deletions src/api/client.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
import { type AxiosResponse } from 'axios'
import axios, { type AxiosResponse } from 'axios'
import { HttpError, JsonRpcError, NetworkError, ProtocolError } from '../utils'
import axios from 'axios'

const JsonRpcVersion: string = '2.0'
const JsonRpcVersion = '2.0'
const HttpHeaders = {
'Content-Type': 'application/json;charset=UTF-8'
'Content-Type': 'application/json'
}
export const HttpProtocol: string = 'http'
export const HttpsProtocol: string = 'https'
const DefaultProtocol: string = HttpsProtocol
export const HttpProtocol = 'http'
export const HttpsProtocol = 'https'
const DefaultProtocol = HttpsProtocol
const Protocols: string[] = [HttpProtocol, HttpsProtocol]

export class JuneoClient {
private nextRequestId: number = 1
private protocol: string = DefaultProtocol
host: string = ''
private nextRequestId = 1
private protocol = DefaultProtocol
host = ''

private constructor () {}

static parse (address: string): JuneoClient {
const client: JuneoClient = new JuneoClient()
const client = new JuneoClient()
client.parseAddress(address)
return client
}

parseAddress (address: string): void {
const protocolSplit: string[] = address.split('://')
const protocol: string = protocolSplit.length > 1 ? protocolSplit[0] : DefaultProtocol
const host: string = protocolSplit.length > 1 ? protocolSplit[1] : protocolSplit[0]
const protocol = protocolSplit.length > 1 ? protocolSplit[0] : DefaultProtocol
const host = protocolSplit.length > 1 ? protocolSplit[1] : protocolSplit[0]
this.setProtocol(protocol)
this.host = host
}
Expand All @@ -48,8 +47,8 @@ export class JuneoClient {
}

async rpcCall (endpoint: string, request: JsonRpcRequest): Promise<JsonRpcResponse> {
const jsonRpcObject: any = request.getJsonRpcObject(this.nextRequestId++)
const response: AxiosResponse = await this.post(endpoint, JSON.stringify(jsonRpcObject))
const jsonRpcObject = request.getJsonRpcObject(this.nextRequestId++)
const response = await this.post(endpoint, JSON.stringify(jsonRpcObject))
const status = response.status
if (status < 200 || status >= 300) {
throw new HttpError(`request status is not accepted "${status}"`)
Expand All @@ -64,7 +63,7 @@ export class JuneoClient {
return new JsonRpcResponse(data.jsonrpc, data.id, data.result)
}

private async post (endpoint: string, data: any): Promise<AxiosResponse> {
async post (endpoint: string, data: any): Promise<AxiosResponse> {
return await axios
.post(endpoint, data, {
method: 'post',
Expand All @@ -89,12 +88,12 @@ export class JsonRpcRequest {
}

getJsonRpcObject (id: number): any {
const jsonRpcObject: any = {}
jsonRpcObject.jsonrpc = JsonRpcVersion
jsonRpcObject.id = id
jsonRpcObject.method = this.method
jsonRpcObject.params = this.params
return jsonRpcObject
return {
jsonrpc: JsonRpcVersion,
id,
method: this.method,
params: this.params
}
}
}

Expand Down
36 changes: 32 additions & 4 deletions src/juneo.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { InfoAPI, JEVMAPI, JuneoClient, JVMAPI, PlatformAPI } from './api'
import { JEVM_ID, type JEVMBlockchain, type JVMBlockchain, type PlatformBlockchain } from './chain'
import { SocotraNetwork, type MCN } from './network'
import { GenesisNetwork, MainNetwork, SocotraNetwork, type MCN } from './network'

export class MCNProvider {
mcn: MCN
Expand Down Expand Up @@ -37,9 +37,37 @@ export class MCNProvider {

async getStaticProvider (): Promise<MCNProvider> {
return this
// const url = this.mcn.access.getStaticUrl()
// const client: JuneoClient = JuneoClient.parse(url)
// return new MCNProvider(this.mcn, client)
}

static fromId (id: number): MCNProvider {
switch (id) {
case MainNetwork.id: {
return new MCNProvider(MainNetwork)
}
case SocotraNetwork.id: {
return new MCNProvider(SocotraNetwork)
}
case GenesisNetwork.id: {
return new MCNProvider(GenesisNetwork)
}
default: {
throw new Error(`unsupported network id: ${id}`)
}
}
}

static fromHrp (hrp: string): MCNProvider {
switch (hrp) {
case MainNetwork.hrp: {
return new MCNProvider(MainNetwork)
}
case SocotraNetwork.hrp: {
return new MCNProvider(SocotraNetwork)
}
default: {
throw new Error(`unsupported network hrp: ${hrp}`)
}
}
}
}

Expand Down
Loading

0 comments on commit 00e9775

Please sign in to comment.