Skip to content

Commit

Permalink
[xp] add jsonRPCCLient
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjah committed Apr 23, 2024
1 parent e1bc608 commit e656c70
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 20 deletions.
13 changes: 13 additions & 0 deletions packages/massa-web3/src/experimental/jsonRPCClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { PublicAPI, Transport } from './publicAPI'

export class JsonRPCClient extends PublicAPI {
constructor(url: string) {
const u = new URL(url)
const protocol = u.protocol === 'https:' ? Transport.https : Transport.http
let port = parseInt(u.port)
if (isNaN(port)) {
port = protocol === Transport.https ? 443 : 80
}
super(protocol, u.hostname, port, u.pathname)
}
}
8 changes: 4 additions & 4 deletions packages/massa-web3/src/experimental/publicAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ export class PublicAPI {
this.connector = new MassaOpenRPCSpecification({
transport: {
type: transport,
host: host,
port: port,
path: path,
protocol: protocol,
host,
port,
path,
protocol,
},
} as Options)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import { Account } from '../../../src/experimental/account'
import { AccountOperation } from '../../../src/experimental/accountOperation'
import { OperationStatus } from '../../../src/experimental/basicElements'
import { PublicAPI, Transport } from '../../../src/experimental/publicAPI'
import { JsonRPCClient } from '../../../src/experimental/jsonRPCClient'
import 'dotenv/config'

describe('Basic use cases', () => {
test('AccountOperation - transfer', async () => {
const account = await Account.fromEnv()
const client = new PublicAPI(
Transport.https,
'buildnet.massa.net',
443,
'/api/v2'
)

const client = new JsonRPCClient('https://buildnet.massa.net/api/v2')

const accountOperation = new AccountOperation(account, client)
const transfer = await accountOperation.transfer(
'AU1wN8rn4SkwYSTDF3dHFY4U28KtsqKL1NnEjDZhHnHEy6cEQm53',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
ReadOnlyCall,
Slot,
} from '../../../src/experimental/generated/client'
import { PublicAPI, Transport } from '../../../src/experimental/publicAPI'
import { JsonRPCClient } from '../../../src/experimental/jsonRPCClient'
import { createCheckers } from 'ts-interface-checker'
import validator from '../../../src/experimental/generated/client-ti'
import { EventFilter } from '../../../src/experimental/client'
Expand All @@ -24,7 +24,7 @@ const {
ExecuteReadOnlyResponse,
} = createCheckers(validator)

const api = new PublicAPI(Transport.https, 'buildnet.massa.net', 443, '/api/v2')
const api = new JsonRPCClient('https://buildnet.massa.net/api/v2')

let lastSlot: Slot = { period: 0, thread: 0 }
let someEndorsement: string[]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import { Account } from '../../../src/experimental/account'
import { ByteCode } from '../../../src/experimental/smartContract'
import { PublicAPI, Transport } from '../../../src/experimental/publicAPI'
import 'dotenv/config'
import { JsonRPCClient } from '../../../src/experimental/jsonRPCClient'

describe('Basic use cases', () => {
test('Smart Contract - execute', async () => {
const account = await Account.fromEnv()
const client = new PublicAPI(
Transport.https,
'buildnet.massa.net',
443,
'/api/v2'
)

const client = new JsonRPCClient('https://buildnet.massa.net/api/v2')

const byteCode = new Uint8Array([1, 2, 3, 4])
const opts = {
fee: 1n,
Expand Down

1 comment on commit e656c70

@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 38.77% 547/1411
🔴 Branches 14.67% 54/368
🔴 Functions 26.76% 76/284
🔴 Lines 38.53% 536/1391

Test suite run success

8 tests passing in 3 suites.

Report generated by 🧪jest coverage report action from e656c70

Please sign in to comment.