UNPKG

@mysten/sui

Version:

Sui TypeScript API(Work in Progress)

304 lines (303 loc) 11.5 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 __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method); var _graphqlClient, _GraphQLTransport_instances, graphqlQuery_fn; import { Experimental_CoreClient } from "../core.js"; import { DryRunTransactionBlockDocument, ExecuteTransactionBlockDocument, GetAllBalancesDocument, GetBalanceDocument, GetCoinsDocument, GetDynamicFieldsDocument, GetOwnedObjectsDocument, GetReferenceGasPriceDocument, GetTransactionBlockDocument, MultiGetObjectsDocument, VerifyZkLoginSignatureDocument, ZkLoginIntentScope } from "../../graphql/generated/queries.js"; import { ObjectError } from "../errors.js"; import { fromBase64, toBase64 } from "@mysten/utils"; import { normalizeStructTag, normalizeSuiAddress } from "../../utils/sui-types.js"; import { deriveDynamicFieldID } from "../../utils/dynamic-fields.js"; import { parseTransactionEffects } from "./utils.js"; class GraphQLTransport extends Experimental_CoreClient { constructor(graphqlClient) { super({ network: graphqlClient.network }); __privateAdd(this, _GraphQLTransport_instances); __privateAdd(this, _graphqlClient); __privateSet(this, _graphqlClient, graphqlClient); } async getObjects(options) { const objects = []; let hasNextPage = true; let cursor = null; while (hasNextPage) { const objectsPage = await __privateMethod(this, _GraphQLTransport_instances, graphqlQuery_fn).call(this, { query: MultiGetObjectsDocument, variables: { objectIds: options.objectIds, cursor } }, (result) => result.objects); objects.push(...objectsPage.nodes); hasNextPage = objectsPage.pageInfo.hasNextPage; cursor = objectsPage.pageInfo.endCursor ?? null; } return { objects: options.objectIds.map((id) => normalizeSuiAddress(id)).map( (id) => objects.find((obj) => obj.address === id) ?? new ObjectError("notFound", `Object ${id} not found`) ).map((obj) => { if (obj instanceof ObjectError) { return obj; } return { id: obj.address, version: obj.version, digest: obj.digest, owner: mapOwner(obj.owner), type: obj.asMoveObject?.contents?.type?.repr, content: fromBase64(obj.asMoveObject?.contents?.bcs) }; }) }; } async getOwnedObjects(options) { const objects = await __privateMethod(this, _GraphQLTransport_instances, graphqlQuery_fn).call(this, { query: GetOwnedObjectsDocument, variables: { owner: options.address, limit: options.limit, cursor: options.cursor, filter: options.type ? { type: options.type } : void 0 } }, (result) => result.address?.objects); return { objects: objects.nodes.map((obj) => ({ id: obj.address, version: obj.version, digest: obj.digest, owner: mapOwner(obj.owner), type: obj.contents?.type?.repr, content: fromBase64(obj.contents?.bcs) })), hasNextPage: objects.pageInfo.hasNextPage, cursor: objects.pageInfo.endCursor ?? null }; } async getCoins(options) { const coins = await __privateMethod(this, _GraphQLTransport_instances, graphqlQuery_fn).call(this, { query: GetCoinsDocument, variables: { owner: options.address, cursor: options.cursor, first: options.limit, type: options.coinType } }, (result) => result.address?.coins); return { cursor: coins.pageInfo.endCursor ?? null, hasNextPage: coins.pageInfo.hasNextPage, objects: coins.nodes.map((coin) => ({ id: coin.address, version: coin.version, digest: coin.digest, owner: mapOwner(coin.owner), type: coin.contents?.type?.repr, balance: coin.coinBalance, content: fromBase64(coin.contents?.bcs) })) }; } async getBalance(options) { const result = await __privateMethod(this, _GraphQLTransport_instances, graphqlQuery_fn).call(this, { query: GetBalanceDocument, variables: { owner: options.address, type: options.coinType } }, (result2) => result2.address?.balance); return { balance: { coinType: result.coinType.repr, balance: result.totalBalance } }; } async getAllBalances(options) { const balances = await __privateMethod(this, _GraphQLTransport_instances, graphqlQuery_fn).call(this, { query: GetAllBalancesDocument, variables: { owner: options.address } }, (result) => result.address?.balances); return { cursor: balances.pageInfo.endCursor ?? null, hasNextPage: balances.pageInfo.hasNextPage, balances: balances.nodes.map((balance) => ({ coinType: balance.coinType.repr, balance: balance.totalBalance })) }; } async getTransaction(options) { const result = await __privateMethod(this, _GraphQLTransport_instances, graphqlQuery_fn).call(this, { query: GetTransactionBlockDocument, variables: { digest: options.digest } }, (result2) => result2.transactionBlock); return { transaction: parseTransaction(result) }; } async executeTransaction(options) { const result = await __privateMethod(this, _GraphQLTransport_instances, graphqlQuery_fn).call(this, { query: ExecuteTransactionBlockDocument, variables: { txBytes: toBase64(options.transaction), signatures: options.signatures } }, (result2) => result2.executeTransactionBlock); if (result.errors) { if (result.errors.length === 1) { throw new Error(result.errors[0]); } throw new AggregateError(result.errors.map((error) => new Error(error))); } return { transaction: parseTransaction(result.effects.transactionBlock) }; } async dryRunTransaction(options) { const result = await __privateMethod(this, _GraphQLTransport_instances, graphqlQuery_fn).call(this, { query: DryRunTransactionBlockDocument, variables: { txBytes: toBase64(options.transaction) } }, (result2) => result2.dryRunTransactionBlock); if (result.error) { throw new Error(result.error); } return { transaction: parseTransaction(result.transaction) }; } async getReferenceGasPrice() { const result = await __privateMethod(this, _GraphQLTransport_instances, graphqlQuery_fn).call(this, { query: GetReferenceGasPriceDocument }, (result2) => result2.epoch?.referenceGasPrice); return { referenceGasPrice: result.referenceGasPrice }; } async getDynamicFields(options) { const result = await __privateMethod(this, _GraphQLTransport_instances, graphqlQuery_fn).call(this, { query: GetDynamicFieldsDocument, variables: { parentId: options.parentId } }, (result2) => result2.owner?.dynamicFields); return { dynamicFields: result.nodes.map((dynamicField) => { const valueType = dynamicField.value?.__typename === "MoveObject" ? dynamicField.value.contents?.type?.repr : dynamicField.value?.type.repr; return { id: deriveDynamicFieldID( options.parentId, dynamicField.name?.type.repr, dynamicField.name?.bcs ), type: normalizeStructTag( dynamicField.value?.__typename === "MoveObject" ? `0x2::dynamic_field::Field<0x2::dynamic_object_field::Wrapper<${dynamicField.name?.type.repr}>,0x2::object::ID>` : `0x2::dynamic_field::Field<${dynamicField.name?.type.repr},${valueType}>` ), name: { type: dynamicField.name?.type.repr, bcs: fromBase64(dynamicField.name?.bcs) }, valueType }; }), cursor: result.pageInfo.endCursor ?? null, hasNextPage: result.pageInfo.hasNextPage }; } async verifyZkLoginSignature(options) { const intentScope = options.intentScope === "TransactionData" ? ZkLoginIntentScope.TransactionData : ZkLoginIntentScope.PersonalMessage; const result = await __privateMethod(this, _GraphQLTransport_instances, graphqlQuery_fn).call(this, { query: VerifyZkLoginSignatureDocument, variables: { bytes: options.bytes, signature: options.signature, intentScope, author: options.author } }, (result2) => result2.verifyZkloginSignature); return { success: result.success, errors: result.errors }; } } _graphqlClient = new WeakMap(); _GraphQLTransport_instances = new WeakSet(); graphqlQuery_fn = async function(options, getData) { const { data, errors } = await __privateGet(this, _graphqlClient).query(options); handleGraphQLErrors(errors); const extractedData = data && (getData ? getData(data) : data); if (extractedData == null) { throw new Error("Missing response data"); } return extractedData; }; function handleGraphQLErrors(errors) { if (!errors || errors.length === 0) return; const errorInstances = errors.map((error) => new GraphQLResponseError(error)); if (errorInstances.length === 1) { throw errorInstances[0]; } throw new AggregateError(errorInstances); } class GraphQLResponseError extends Error { constructor(error) { super(error.message); this.locations = error.locations; } } function mapOwner(owner) { switch (owner.__typename) { case "AddressOwner": return { $kind: "AddressOwner", AddressOwner: owner.owner?.asAddress?.address }; case "ConsensusV2": return { $kind: "ConsensusV2", ConsensusV2: owner.authenticator.address }; case "Immutable": return { $kind: "Immutable", Immutable: true }; case "Parent": return { $kind: "ObjectOwner", ObjectOwner: owner.parent?.address }; case "Shared": return { $kind: "Shared", Shared: owner.initialSharedVersion }; } } function parseTransaction(transaction) { const objectTypes = {}; transaction.effects?.unchangedSharedObjects.nodes.forEach((node) => { if (node.__typename === "SharedObjectRead") { const type = node.object?.asMoveObject?.contents?.type.repr; const address = node.object?.asMoveObject?.address; if (type && address) { objectTypes[address] = type; } } }); transaction.effects?.objectChanges.nodes.forEach((node) => { const address = node.address; const type = node.inputState?.asMoveObject?.contents?.type.repr ?? node.outputState?.asMoveObject?.contents?.type.repr; if (address && type) { objectTypes[address] = type; } }); return { digest: transaction.digest, effects: parseTransactionEffects({ effects: new Uint8Array(transaction.effects?.bcs), objectTypes }), bcs: transaction.bcs, signatures: transaction.signatures }; } export { GraphQLTransport }; //# sourceMappingURL=graphql.js.map