Mysten Labs SDKs

BCS

The @mysten/sui/bcs package extends @mysten/bcs with Sui specific scheme definitions.

To learn more about using BCS see the BCS documentation.

the bcs export of @mysten/sui/bcs contains all the same exports as bcs from @mysten/bcs plus the following pre-defined schemes:

  • U8
  • U16
  • U32
  • U64
  • U128
  • U256
  • ULEB128
  • Bool
  • String
  • Address
  • Argument
  • CallArg
  • CompressedSignature
  • GasData
  • MultiSig
  • MultiSigPkMap
  • MultiSigPublicKey
  • ObjectArg
  • ObjectDigest
  • ProgrammableMoveCall
  • ProgrammableTransaction
  • PublicKey
  • SenderSignedData
  • SharedObjectRef
  • StructTag
  • SuiObjectRef
  • Transaction
  • TransactionData
  • TransactionDataV1
  • TransactionExpiration
  • TransactionKind
  • TypeTag
  • Object - Complete object with data, owner, previousTransaction, and storageRebate
  • TransactionEffects - Transaction execution effects (supports both V1 and V2)
  • TransactionEffectsV1 - Legacy transaction effects format
  • TransactionEffectsV2 - Current transaction effects format with detailed object changes

All the upper-cased values are BcsType instances, and can be used directly to parse and serialize data.

import { bcs } from '@mysten/sui/bcs';

bcs.U8.serialize(1);
bcs.Address.serialize('0x1');
bcs.TypeTag.serialize({
	vector: {
		u8: true,
	},
});

Working with Objects

The bcs.Object schema can be used to serialize and deserialize complete Sui objects:

import { bcs } from '@mysten/sui/bcs';

// Parse a BCS-encoded object
const objectBytes = await client.getObjects({
  ids: [objectId],
  include: { objectBcs: true },
});

const parsed = bcs.Object.parse(objectBytes.objects[0].objectBcs);
console.log('Owner:', parsed.owner);
console.log('Previous Transaction:', parsed.previousTransaction);
console.log('Storage Rebate:', parsed.storageRebate);

// Serialize an object
const serialized = bcs.Object.serialize({
  data: {
    Move: {
      type: { GasCoin: null },
      hasPublicTransfer: true,
      version: '1',
      contents: new Uint8Array([...]),
    },
  },
  owner: { AddressOwner: '0x...' },
  previousTransaction: '...',
  storageRebate: '1000',
});

Working with Transaction Effects

The bcs.TransactionEffects schema can be used to parse transaction effects:

import { bcs } from '@mysten/sui/bcs';

// Parse transaction effects
const effects = bcs.TransactionEffects.parse(effectsBytes);

// Check execution status
if (effects.V2.status.$kind === 'Success') {
	console.log('Transaction succeeded');
} else {
	console.log('Transaction failed:', effects.V2.status.Failure.error);
}

// Access changed objects
for (const [objectId, change] of effects.V2.changedObjects) {
	console.log('Object:', objectId);
	console.log('Output state:', change.outputState.$kind);
}

On this page