diff --git a/packages/massa-web3/src/experimental/jsonRPCClient.ts b/packages/massa-web3/src/experimental/jsonRPCClient.ts new file mode 100644 index 00000000..868aabae --- /dev/null +++ b/packages/massa-web3/src/experimental/jsonRPCClient.ts @@ -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) + } +} diff --git a/packages/massa-web3/src/experimental/publicAPI.ts b/packages/massa-web3/src/experimental/publicAPI.ts index 63045b34..d8d3096b 100644 --- a/packages/massa-web3/src/experimental/publicAPI.ts +++ b/packages/massa-web3/src/experimental/publicAPI.ts @@ -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) } diff --git a/packages/massa-web3/test/experimental/integration/accountOperation.spec.ts b/packages/massa-web3/test/experimental/integration/accountOperation.spec.ts index 012c78b0..b2b97f7d 100644 --- a/packages/massa-web3/test/experimental/integration/accountOperation.spec.ts +++ b/packages/massa-web3/test/experimental/integration/accountOperation.spec.ts @@ -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', diff --git a/packages/massa-web3/test/experimental/integration/publicAPI.spec.ts b/packages/massa-web3/test/experimental/integration/publicAPI.spec.ts index b975d2c1..de6deb05 100644 --- a/packages/massa-web3/test/experimental/integration/publicAPI.spec.ts +++ b/packages/massa-web3/test/experimental/integration/publicAPI.spec.ts @@ -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' @@ -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[] diff --git a/packages/massa-web3/test/experimental/integration/smartContract.spec.ts b/packages/massa-web3/test/experimental/integration/smartContract.spec.ts index 3691b8b0..2f836656 100644 --- a/packages/massa-web3/test/experimental/integration/smartContract.spec.ts +++ b/packages/massa-web3/test/experimental/integration/smartContract.spec.ts @@ -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,