@mysten/sui
Version:
Sui TypeScript API(Work in Progress)
462 lines (461 loc) • 14.6 kB
JavaScript
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __typeError = (msg) => {
throw TypeError(msg);
};
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
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 jsonRPC_exports = {};
__export(jsonRPC_exports, {
JSONRpcTransport: () => JSONRpcTransport
});
module.exports = __toCommonJS(jsonRPC_exports);
var import_bcs = require("@mysten/bcs");
var import_bcs2 = require("../../bcs/index.js");
var import_utils = require("../../transactions/plugins/utils.js");
var import_Transaction = require("../../transactions/Transaction.js");
var import_sui_types = require("../../utils/sui-types.js");
var import_core = require("../core.js");
var import_errors = require("../errors.js");
var import_utils2 = require("./utils.js");
var _jsonRpcClient;
class JSONRpcTransport extends import_core.Experimental_CoreClient {
constructor(jsonRpcClient) {
super({ network: jsonRpcClient.network });
__privateAdd(this, _jsonRpcClient);
__privateSet(this, _jsonRpcClient, jsonRpcClient);
}
async getObjects(options) {
const batches = (0, import_utils.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(import_errors.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 import_errors.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 = import_Transaction.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: (0, import_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" ? (0, import_bcs.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 = import_bcs2.bcs.SenderSignedData.parse((0, import_bcs.fromBase64)(transaction.rawTransaction))[0];
const objectTypes = {};
transaction.objectChanges?.forEach((change) => {
if (change.type !== "published") {
objectTypes[change.objectId] = change.objectType;
}
});
return {
digest: transaction.digest,
effects: (0, import_utils2.parseTransactionEffects)({
effects: new Uint8Array(transaction.rawEffects),
objectTypes
}),
bcs: import_bcs2.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: (0, import_sui_types.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 = import_bcs2.bcs.struct("Balance", {
value: import_bcs2.bcs.u64()
});
const Coin = import_bcs2.bcs.struct("Coin", {
id: import_bcs2.bcs.Address,
balance: Balance
});
//# sourceMappingURL=jsonRPC.js.map