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:
U8U16U32U64U128U256ULEB128BoolStringAddressArgumentCallArgCompressedSignatureGasDataMultiSigMultiSigPkMapMultiSigPublicKeyObjectArgObjectDigestProgrammableMoveCallProgrammableTransactionPublicKeySenderSignedDataSharedObjectRefStructTagSuiObjectRefTransactionTransactionDataTransactionDataV1TransactionExpirationTransactionKindTypeTagObject- Complete object with data, owner, previousTransaction, and storageRebateTransactionEffects- Transaction execution effects (supports both V1 and V2)TransactionEffectsV1- Legacy transaction effects formatTransactionEffectsV2- 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);
}