bigUIntBcsType function

BcsType<BigInt, dynamic> bigUIntBcsType({
  1. required String name,
  2. required int size,
  3. required String readMethod,
  4. required String writeMethod,
  5. required BigInt maxValue,
  6. void validate(
    1. BigInt
    )?,
})

Implementation

BcsType<BigInt, dynamic> bigUIntBcsType({
  required String name,
  required int size,
  required String readMethod,
  required String writeMethod,
  required BigInt maxValue,
  void Function(BigInt)? validate,
}) {
  return fixedSizeBcsType<BigInt, dynamic>(
    name: name,
    size: size,
    read: (reader) {
      switch (readMethod) {
        case 'read64':
          return reader.read64();
        case 'read128':
          return reader.read128();
        case 'read256':
          return reader.read256();
        default:
          throw ArgumentError('Invalid read type $readMethod');
      }
    },
    write: (value, writer) {
      final val = BigInt.parse(value.toString());
      switch (writeMethod) {
        case 'write64':
          writer.write64(val);
          break;
        case 'write128':
          writer.write128(val);
          break;
        case 'write256':
          writer.write256(val);
          break;
        default:
          throw ArgumentError('Invalid read type $readMethod');
      }
    },
    validate: (val) {
      final value = BigInt.parse(val.toString());
      if (value < BigInt.zero || value > maxValue) {
        throw ArgumentError('Invalid $name value: $value. Expected value in range 0-$maxValue');
      }
      validate?.call(value);
    },
  );
}