|
| 1 | +// Copyright (c) Mysten Labs, Inc. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import { fromBase64 } from '@mysten/bcs'; |
| 5 | + |
| 6 | +import { bcs } from '../../bcs/index.js'; |
| 7 | +import type { |
| 8 | + ObjectOwner, |
| 9 | + SuiClient, |
| 10 | + SuiObjectData, |
| 11 | + SuiTransactionBlockResponse, |
| 12 | +} from '../../client/index.js'; |
| 13 | +import { batch } from '../../transactions/plugins/utils.js'; |
| 14 | +import { Transaction } from '../../transactions/Transaction.js'; |
| 15 | +import { ObjectError } from '../errors.js'; |
| 16 | +import type { Experimental_SuiClientTypes } from '../types.js'; |
| 17 | + |
| 18 | +export class JSONRpcTransport implements Experimental_SuiClientTypes.TransportMethods { |
| 19 | + #jsonRpcClient: SuiClient; |
| 20 | + |
| 21 | + constructor(jsonRpcClient: SuiClient) { |
| 22 | + this.#jsonRpcClient = jsonRpcClient; |
| 23 | + } |
| 24 | + |
| 25 | + async getObjects(options: Experimental_SuiClientTypes.GetObjectsOptions) { |
| 26 | + const batches = batch(options.objectIds, 50); |
| 27 | + const results: Experimental_SuiClientTypes.GetObjectsResponse['objects'] = []; |
| 28 | + |
| 29 | + for (const batch of batches) { |
| 30 | + const objects = await this.#jsonRpcClient.multiGetObjects({ |
| 31 | + ids: batch, |
| 32 | + options: { |
| 33 | + showOwner: true, |
| 34 | + showType: true, |
| 35 | + }, |
| 36 | + }); |
| 37 | + |
| 38 | + for (const [idx, object] of objects.entries()) { |
| 39 | + if (object.error) { |
| 40 | + results.push(ObjectError.fromResponse(object.error, batch[idx])); |
| 41 | + } else { |
| 42 | + results.push(parseObject(object.data!)); |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + return { |
| 48 | + objects: results, |
| 49 | + }; |
| 50 | + } |
| 51 | + async getOwnedObjects(options: Experimental_SuiClientTypes.GetOwnedObjectsOptions) { |
| 52 | + const objects = await this.#jsonRpcClient.getOwnedObjects({ |
| 53 | + owner: options.address, |
| 54 | + limit: options.limit, |
| 55 | + cursor: options.cursor, |
| 56 | + }); |
| 57 | + |
| 58 | + return { |
| 59 | + objects: objects.data.map((result) => { |
| 60 | + if (result.error) { |
| 61 | + throw ObjectError.fromResponse(result.error); |
| 62 | + } |
| 63 | + |
| 64 | + return parseObject(result.data!); |
| 65 | + }), |
| 66 | + hasNextPage: objects.hasNextPage, |
| 67 | + cursor: objects.nextCursor ?? null, |
| 68 | + }; |
| 69 | + } |
| 70 | + async getBalance(options: Experimental_SuiClientTypes.GetBalanceOptions) { |
| 71 | + const balance = await this.#jsonRpcClient.getBalance({ |
| 72 | + owner: options.address, |
| 73 | + coinType: options.coinType, |
| 74 | + }); |
| 75 | + |
| 76 | + return { |
| 77 | + balance: { |
| 78 | + coinType: balance.coinType, |
| 79 | + balance: BigInt(balance.totalBalance), |
| 80 | + }, |
| 81 | + }; |
| 82 | + } |
| 83 | + async getAllBalances(options: Experimental_SuiClientTypes.GetAllBalancesOptions) { |
| 84 | + const balances = await this.#jsonRpcClient.getAllBalances({ |
| 85 | + owner: options.address, |
| 86 | + }); |
| 87 | + |
| 88 | + return { |
| 89 | + balances: balances.map((balance) => ({ |
| 90 | + coinType: balance.coinType, |
| 91 | + balance: BigInt(balance.totalBalance), |
| 92 | + })), |
| 93 | + hasNextPage: false, |
| 94 | + cursor: null, |
| 95 | + }; |
| 96 | + } |
| 97 | + async getTransaction(options: Experimental_SuiClientTypes.GetTransactionOptions) { |
| 98 | + const transaction = await this.#jsonRpcClient.getTransactionBlock({ |
| 99 | + digest: options.digest, |
| 100 | + options: { |
| 101 | + showRawInput: true, |
| 102 | + showObjectChanges: true, |
| 103 | + showRawEffects: true, |
| 104 | + showEvents: true, |
| 105 | + }, |
| 106 | + }); |
| 107 | + |
| 108 | + return { |
| 109 | + transaction: parseTransaction(transaction), |
| 110 | + }; |
| 111 | + } |
| 112 | + async executeTransaction(options: Experimental_SuiClientTypes.ExecuteTransactionOptions) { |
| 113 | + const transaction = await this.#jsonRpcClient.executeTransactionBlock({ |
| 114 | + transactionBlock: options.transaction, |
| 115 | + signature: options.signatures, |
| 116 | + options: { |
| 117 | + showEffects: true, |
| 118 | + showEvents: true, |
| 119 | + }, |
| 120 | + }); |
| 121 | + |
| 122 | + return { |
| 123 | + transaction: parseTransaction(transaction), |
| 124 | + }; |
| 125 | + } |
| 126 | + async dryRunTransaction(options: Experimental_SuiClientTypes.DryRunTransactionOptions) { |
| 127 | + const tx = Transaction.from(options.transaction); |
| 128 | + const result = await this.#jsonRpcClient.dryRunTransactionBlock({ |
| 129 | + transactionBlock: options.transaction, |
| 130 | + }); |
| 131 | + |
| 132 | + return { |
| 133 | + transaction: { |
| 134 | + digest: await tx.getDigest(), |
| 135 | + // TODO: Effects aren't returned as bcs from dryRun, once we define structured effects we can return those instead |
| 136 | + effects: result.effects as never, |
| 137 | + signatures: [], |
| 138 | + bcs: options.transaction, |
| 139 | + }, |
| 140 | + }; |
| 141 | + } |
| 142 | + async getReferenceGasPrice() { |
| 143 | + const referenceGasPrice = await this.#jsonRpcClient.getReferenceGasPrice(); |
| 144 | + return { |
| 145 | + referenceGasPrice, |
| 146 | + }; |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +function parseObject(object: SuiObjectData): Experimental_SuiClientTypes.ObjectResponse { |
| 151 | + return { |
| 152 | + id: object.objectId, |
| 153 | + version: object.version, |
| 154 | + digest: object.digest, |
| 155 | + type: object.type!, |
| 156 | + content: |
| 157 | + object.bcs?.dataType === 'moveObject' ? fromBase64(object.bcs.bcsBytes) : new Uint8Array(), |
| 158 | + owner: parseOwner(object.owner!), |
| 159 | + }; |
| 160 | +} |
| 161 | + |
| 162 | +function parseOwner(owner: ObjectOwner): Experimental_SuiClientTypes.ObjectOwner { |
| 163 | + if (owner === 'Immutable') { |
| 164 | + return { |
| 165 | + $kind: 'Immutable', |
| 166 | + Immutable: true, |
| 167 | + }; |
| 168 | + } |
| 169 | + |
| 170 | + if ('ConsensusV2' in owner) { |
| 171 | + return { |
| 172 | + $kind: 'ConsensusV2', |
| 173 | + ConsensusV2Owner: { |
| 174 | + authenticator: { |
| 175 | + $kind: 'SingleOwner', |
| 176 | + SingleOwner: owner.ConsensusV2.authenticator.SingleOwner, |
| 177 | + }, |
| 178 | + startVersion: owner.ConsensusV2.start_version, |
| 179 | + }, |
| 180 | + }; |
| 181 | + } |
| 182 | + |
| 183 | + if ('AddressOwner' in owner) { |
| 184 | + return { |
| 185 | + $kind: 'AddressOwner', |
| 186 | + AddressOwner: owner.AddressOwner, |
| 187 | + }; |
| 188 | + } |
| 189 | + |
| 190 | + if ('ObjectOwner' in owner) { |
| 191 | + return { |
| 192 | + $kind: 'ObjectOwner', |
| 193 | + ObjectOwner: owner.ObjectOwner, |
| 194 | + }; |
| 195 | + } |
| 196 | + |
| 197 | + if ('Shared' in owner) { |
| 198 | + return { |
| 199 | + $kind: 'Shared', |
| 200 | + Shared: { |
| 201 | + initialSharedVersion: owner.Shared.initial_shared_version, |
| 202 | + }, |
| 203 | + }; |
| 204 | + } |
| 205 | + |
| 206 | + throw new Error(`Unknown owner type: ${JSON.stringify(owner)}`); |
| 207 | +} |
| 208 | + |
| 209 | +function parseTransaction( |
| 210 | + transaction: SuiTransactionBlockResponse, |
| 211 | +): Experimental_SuiClientTypes.TransactionResponse { |
| 212 | + const parsedTx = bcs.SenderSignedData.parse(fromBase64(transaction.rawTransaction!))[0]; |
| 213 | + |
| 214 | + return { |
| 215 | + digest: transaction.digest, |
| 216 | + effects: new Uint8Array(transaction.rawEffects!), |
| 217 | + bcs: bcs.TransactionData.serialize(parsedTx.intentMessage.value).toBytes(), |
| 218 | + signatures: parsedTx.txSignatures, |
| 219 | + }; |
| 220 | +} |
0 commit comments