borshSpace method

int borshSpace()

The serialized byte length required to store any instance of this class (i.e. max size).

Example

class Data extends BorshObjectSized {

  const Data(this.counter);

  final BigInt? counter;

  factory Data.deserialize(final Map<String, dynamic> json) 
    => Data(json['counter']);

  static final BorshStructSizedCodec borshCodec = BorshStructSizedCodec({ 
    'counter': borsh.u64.option(), 
  });

  @override
  BorshSchemaSized get borshSchema => borshCodec.schema;

  @override
  Map<String, dynamic> toJson() => { 
    'counter': counter, 
  };
}

const Data data0 = Data(null);
print('Optional type with `null` value:');      // Optional type with `null` value:
print(' - Buffer length ${data0.borshSize()}'); //  - Buffer length 1
print(' - Class size ${data0.borshSpace()}');   //  - Class size 9

final Data data1 = Data(BigInt.zero);
print('Optional type with `set` value:');       // Optional type with `set` value:
print(' - Buffer length ${data1.borshSize()}'); //  - Buffer length 9
print(' - Class size ${data1.borshSpace()}');   //  - Class size 9

Implementation

int borshSpace() => borshSchema.values.fold(0, (total, codec) => codec.byteLength);