UNPKG

@mysten/sui

Version:

Sui TypeScript API(Work in Progress)

442 lines (441 loc) 13.4 kB
var __typeError = (msg) => { throw TypeError(msg); }; var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg); var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj)); var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value); var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value); var _jsonRpcClient; import { fromBase64 } from "@mysten/bcs"; import { bcs } from "../../bcs/index.js"; import { batch } from "../../transactions/plugins/utils.js"; import { Transaction } from "../../transactions/Transaction.js"; import { normalizeStructTag } from "../../utils/sui-types.js"; import { Experimental_CoreClient } from "../core.js"; import { ObjectError } from "../errors.js"; import { parseTransactionEffects } from "./utils.js"; class JSONRpcTransport extends Experimental_CoreClient { constructor(jsonRpcClient) { super({ network: jsonRpcClient.network }); __privateAdd(this, _jsonRpcClient); __privateSet(this, _jsonRpcClient, jsonRpcClient); } async getObjects(options) { const batches = batch(options.objectIds, 50); const results = []; for (const batch2 of batches) { const objects = await __privateGet(this, _jsonRpcClient).multiGetObjects({ ids: batch2, options: { showOwner: true, showType: true, showBcs: true }, signal: options.signal }); for (const [idx, object] of objects.entries()) { if (object.error) { results.push(ObjectError.fromResponse(object.error, batch2[idx])); } else { results.push(parseObject(object.data)); } } } return { objects: results }; } async getOwnedObjects(options) { const objects = await __privateGet(this, _jsonRpcClient).getOwnedObjects({ owner: options.address, limit: options.limit, cursor: options.cursor, options: { showOwner: true, showType: true, showBcs: true }, signal: options.signal }); return { objects: objects.data.map((result) => { if (result.error) { throw ObjectError.fromResponse(result.error); } return parseObject(result.data); }), hasNextPage: objects.hasNextPage, cursor: objects.nextCursor ?? null }; } async getCoins(options) { const coins = await __privateGet(this, _jsonRpcClient).getCoins({ owner: options.address, coinType: options.coinType, limit: options.limit, cursor: options.cursor, signal: options.signal }); return { objects: coins.data.map((coin) => { return { id: coin.coinObjectId, version: coin.version, digest: coin.digest, balance: coin.balance, type: `0x2::coin::Coin<${coin.coinType}>`, content: Coin.serialize({ id: coin.coinObjectId, balance: { value: coin.balance } }).toBytes(), owner: { $kind: "ObjectOwner", ObjectOwner: options.address } }; }), hasNextPage: coins.hasNextPage, cursor: coins.nextCursor ?? null }; } async getBalance(options) { const balance = await __privateGet(this, _jsonRpcClient).getBalance({ owner: options.address, coinType: options.coinType, signal: options.signal }); return { balance: { coinType: balance.coinType, balance: balance.totalBalance } }; } async getAllBalances(options) { const balances = await __privateGet(this, _jsonRpcClient).getAllBalances({ owner: options.address, signal: options.signal }); return { balances: balances.map((balance) => ({ coinType: balance.coinType, balance: balance.totalBalance })), hasNextPage: false, cursor: null }; } async getTransaction(options) { const transaction = await __privateGet(this, _jsonRpcClient).getTransactionBlock({ digest: options.digest, options: { showRawInput: true, showObjectChanges: true, showRawEffects: true, showEvents: true }, signal: options.signal }); return { transaction: parseTransaction(transaction) }; } async executeTransaction(options) { const transaction = await __privateGet(this, _jsonRpcClient).executeTransactionBlock({ transactionBlock: options.transaction, signature: options.signatures, options: { showRawEffects: true, showEvents: true, showObjectChanges: true, showRawInput: true }, signal: options.signal }); return { transaction: parseTransaction(transaction) }; } async dryRunTransaction(options) { const tx = Transaction.from(options.transaction); const result = await __privateGet(this, _jsonRpcClient).dryRunTransactionBlock({ transactionBlock: options.transaction, signal: options.signal }); return { transaction: { digest: await tx.getDigest(), effects: parseTransactionEffectsJson({ effects: result.effects, objectChanges: result.objectChanges }), signatures: [], bcs: options.transaction } }; } async getReferenceGasPrice(options) { const referenceGasPrice = await __privateGet(this, _jsonRpcClient).getReferenceGasPrice({ signal: options?.signal }); return { referenceGasPrice: String(referenceGasPrice) }; } async getDynamicFields(options) { const dynamicFields = await __privateGet(this, _jsonRpcClient).getDynamicFields({ parentId: options.parentId, limit: options.limit, cursor: options.cursor }); return { dynamicFields: dynamicFields.data.map((dynamicField) => { return { id: dynamicField.objectId, type: dynamicField.objectType, name: { type: dynamicField.name.type, bcs: fromBase64(dynamicField.bcsName) } }; }), hasNextPage: dynamicFields.hasNextPage, cursor: dynamicFields.nextCursor }; } async verifyZkLoginSignature(options) { const result = await __privateGet(this, _jsonRpcClient).verifyZkLoginSignature({ bytes: options.bytes, signature: options.signature, intentScope: options.intentScope, author: options.author }); return { success: result.success, errors: result.errors }; } } _jsonRpcClient = new WeakMap(); function parseObject(object) { return { id: object.objectId, version: object.version, digest: object.digest, type: object.type, content: object.bcs?.dataType === "moveObject" ? fromBase64(object.bcs.bcsBytes) : new Uint8Array(), owner: parseOwner(object.owner) }; } function parseOwner(owner) { if (owner === "Immutable") { return { $kind: "Immutable", Immutable: true }; } if ("ConsensusV2" in owner) { return { $kind: "ConsensusV2", ConsensusV2: { authenticator: { $kind: "SingleOwner", SingleOwner: owner.ConsensusV2.authenticator.SingleOwner }, startVersion: owner.ConsensusV2.start_version } }; } if ("AddressOwner" in owner) { return { $kind: "AddressOwner", AddressOwner: owner.AddressOwner }; } if ("ObjectOwner" in owner) { return { $kind: "ObjectOwner", ObjectOwner: owner.ObjectOwner }; } if ("Shared" in owner) { return { $kind: "Shared", Shared: { initialSharedVersion: owner.Shared.initial_shared_version } }; } throw new Error(`Unknown owner type: ${JSON.stringify(owner)}`); } function parseTransaction(transaction) { const parsedTx = bcs.SenderSignedData.parse(fromBase64(transaction.rawTransaction))[0]; const objectTypes = {}; transaction.objectChanges?.forEach((change) => { if (change.type !== "published") { objectTypes[change.objectId] = change.objectType; } }); return { digest: transaction.digest, effects: parseTransactionEffects({ effects: new Uint8Array(transaction.rawEffects), objectTypes }), bcs: bcs.TransactionData.serialize(parsedTx.intentMessage.value).toBytes(), signatures: parsedTx.txSignatures }; } function parseTransactionEffectsJson({ bytes, effects, epoch, objectChanges }) { const changedObjects = []; const unchangedSharedObjects = []; objectChanges?.forEach((change) => { switch (change.type) { case "published": changedObjects.push({ id: change.packageId, inputState: "DoesNotExist", inputVersion: null, inputDigest: null, inputOwner: null, outputState: "PackageWrite", outputVersion: change.version, outputDigest: change.digest, outputOwner: null, idOperation: "Created", objectType: null }); break; case "transferred": changedObjects.push({ id: change.objectId, inputState: "Exists", inputVersion: change.version, inputDigest: change.digest, inputOwner: { $kind: "AddressOwner", AddressOwner: change.sender }, outputState: "ObjectWrite", outputVersion: change.version, outputDigest: change.digest, outputOwner: parseOwner(change.recipient), idOperation: "None", objectType: change.objectType }); break; case "mutated": changedObjects.push({ id: change.objectId, inputState: "Exists", inputVersion: change.previousVersion, inputDigest: null, inputOwner: parseOwner(change.owner), outputState: "ObjectWrite", outputVersion: change.version, outputDigest: change.digest, outputOwner: parseOwner(change.owner), idOperation: "None", objectType: change.objectType }); break; case "deleted": changedObjects.push({ id: change.objectId, inputState: "Exists", inputVersion: change.version, inputDigest: effects.deleted?.find((d) => d.objectId === change.objectId)?.digest ?? null, inputOwner: null, outputState: "DoesNotExist", outputVersion: null, outputDigest: null, outputOwner: null, idOperation: "Deleted", objectType: change.objectType }); break; case "wrapped": changedObjects.push({ id: change.objectId, inputState: "Exists", inputVersion: change.version, inputDigest: null, inputOwner: { $kind: "AddressOwner", AddressOwner: change.sender }, outputState: "ObjectWrite", outputVersion: change.version, outputDigest: effects.wrapped?.find((w) => w.objectId === change.objectId)?.digest ?? null, outputOwner: { $kind: "ObjectOwner", ObjectOwner: change.sender }, idOperation: "None", objectType: change.objectType }); break; case "created": changedObjects.push({ id: change.objectId, inputState: "DoesNotExist", inputVersion: null, inputDigest: null, inputOwner: null, outputState: "ObjectWrite", outputVersion: change.version, outputDigest: change.digest, outputOwner: parseOwner(change.owner), idOperation: "Created", objectType: change.objectType }); break; } }); return { bcs: bytes ?? null, digest: effects.transactionDigest, version: 2, status: effects.status.status === "success" ? { success: true, error: null } : { success: false, error: effects.status.error }, epoch: epoch ?? null, gasUsed: effects.gasUsed, transactionDigest: effects.transactionDigest, gasObject: { id: effects.gasObject?.reference.objectId, inputState: "Exists", inputVersion: null, inputDigest: null, inputOwner: null, outputState: "ObjectWrite", outputVersion: effects.gasObject.reference.version, outputDigest: effects.gasObject.reference.digest, outputOwner: parseOwner(effects.gasObject.owner), idOperation: "None", objectType: normalizeStructTag("0x2::coin::Coin<0x2::sui::SUI>") }, eventsDigest: effects.eventsDigest ?? null, dependencies: effects.dependencies ?? [], lamportVersion: effects.gasObject.reference.version, changedObjects, unchangedSharedObjects, auxiliaryDataDigest: null }; } const Balance = bcs.struct("Balance", { value: bcs.u64() }); const Coin = bcs.struct("Coin", { id: bcs.Address, balance: Balance }); export { JSONRpcTransport }; //# sourceMappingURL=jsonRPC.js.map