TypeScript SDK (@opnet/ts-sdk)

The @opnet/ts-sdk package provides TypeScript bindings for building OP_NET applications. It wraps the JSON-RPC API with a type-safe client and includes utilities for wallet management, contract interaction, and transaction building.

Installation

npm install @opnet/ts-sdk

MetashrewClient

The primary client for interacting with OPP_NET RPC endpoints.

import { MetashrewClient } from '@opnet/ts-sdk';

const client = new MetashrewClient({
  rpcUrl: 'https://rpc.opnet.dev/testnet4'
});

// Get latest block number
const blockNumber = await client.getBlockNumber();

// Get chain ID
const chainId = await client.getChainId();

// Get total contract count
const count = await client.getContractCount();

// Get contract at index
const address = await client.getContractAtIndex(0);

Contract Calls

// Simulate a read-only call
const result = await client.call({
  to: 'contract_address_hex',
  calldata: 'selector_and_args_hex'
});

// Get contract bytecode
const code = await client.getCode('contract_address_hex');

// Get storage value
const value = await client.getStorageAt(
  'contract_address_hex',
  'storage_key_hex'
);

// Get deployer pubkey
const deployer = await client.getDeployer('contract_address_hex');

Block & Transaction Queries

// Get block by number
const block = await client.getBlockByNumber(height);

// Get block by hash
const block = await client.getBlockByHash(hashHex);

// Get transaction by hash
const tx = await client.getTransactionByHash(txHashHex);

// Get transaction receipt
const receipt = await client.getTransactionReceipt(txHashHex);

// Get gas info
const gas = await client.getGas(height);

UTXOs

// Get UTXOs for an address
const utxos = await client.getUTXOs('tb1q...');
// Returns: { transactionId, outputIndex, value, scriptPubKey }[]

Mining & Epochs

// Get current epoch template
const template = await client.getEpochTemplate();
// Returns: { epochNumber, targetChecksum }

// Validate a PoW solution
const result = await client.submitEpoch(epochNumber, saltHex, pubkeyHex);
// Returns: { valid, difficulty }

// Get epoch by number
const epoch = await client.getEpochByNumber(epochNumber);

Traces

// Get execution trace for a transaction
const trace = await client.getTrace(txHashHex);
// Returns: { callType, target, caller, calldata, success,
//            gasUsed, exitData, storageWrites, events, subCalls }

// Get all traces for a block
const traces = await client.getBlockTraces(height);

WASM Wallet Bindings

The SDK includes WASM-compiled wallet utilities for browser environments:

import { Keystore, TransactionBuilder } from '@opnet/ts-sdk/wallet';

// Create a new wallet
const keystore = Keystore.create('passphrase', 'testnet4');

// Load existing wallet
const keystore = Keystore.load(jsonString, 'passphrase', 'testnet4');

// Get address
const address = keystore.getAddress(0); // derivation index

// Build and sign transactions
const builder = new TransactionBuilder(keystore, client);
const tx = await builder.buildInteraction({
  contractSeed: 'seed_hex',
  calldata: 'calldata_hex',
  feeRate: 1.0
});

Provider Pattern

import { OPNetProvider } from '@opnet/ts-sdk';

// High-level provider that combines client + wallet
const provider = new OPNetProvider({
  rpcUrl: 'https://rpc.opnet.dev/testnet4',
  network: 'testnet4'
});

// Use provider for all operations
const balance = await provider.getBalance(address);
const result = await provider.simulateCall(contractAddr, calldata);

CDN Usage

<!-- Load from OPP_NET CDN -->
<script src="https://rpc.opnet.dev/dist/@opnet/ts-sdk/index.js"></script>
<script>
  const client = new OpnetSDK.MetashrewClient({
    rpcUrl: 'https://rpc.opnet.dev/testnet4'
  });
</script>