Skip to content

Latest commit

 

History

History
176 lines (151 loc) · 4.67 KB

bcs.mdx

File metadata and controls

176 lines (151 loc) · 4.67 KB
title BCS
description Binary Canonical Serialization for encoding Sui Move types.
keywords
BCS
binary canonical serialization
Sui
@mysten/sui/bcs
encoding
Move types
schemas
TypeScript
questions
How do I use BCS in the Sui SDK?
How do I serialize transaction data?
What BCS types are built into @mysten/sui?
answer The @mysten/sui package re-exports BCS utilities for serializing and deserializing Sui-specific types like addresses, objects, and transaction data.
goal
description requires
Reader can use the BCS module in @mysten/sui for serialization
has_frontmatter label
title
description
keywords
Has required frontmatter fields
min_words label
30
Needs content depth
has_questions label
true
Needs questions for AI search visibility
has_answer label
true
Needs answer summary for AI citation

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
  • 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

To parse onchain objects, fetch them with include: { content: true } and pass object.content to a generated BCS type or a manual struct definition. The content field contains only the inner Move struct bytes:

import { MyStruct } from './generated/my-module';

const { object } = await client.core.getObject({
	objectId: '0x123...',
	include: { content: true },
});

const parsed = MyStruct.parse(object.content);

bcs.Object: Full object envelope

The bcs.Object schema represents the complete onchain object, including metadata (type, owner, version, previous transaction, storage rebate) wrapping the inner struct bytes. This is what the objectBcs include option returns. Most of this metadata is already available as fields on the object response, so you typically only need content.

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

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

const parsed = bcs.Object.parse(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',
});
Do not pass `objectBcs` bytes to a Move struct parser. It contains wrapping metadata that will cause parsing to fail. Use `content` for parsing Move struct fields, as described under [objectBcs](/sui/clients/querying#objectbcs).

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);
}