toScVal method

  1. @override
XdrSCVal toScVal()
override

Returns the on-chain ScVal map encoding of the parameter shape. The managers call this internally when installing the policy.

Implementation

@override
XdrSCVal toScVal() {
  if (threshold <= 0) {
    throw SmartAccountValidationException.invalidInput(
      'threshold',
      'Threshold must be greater than zero',
    );
  }
  if (signerWeights.isEmpty) {
    throw SmartAccountValidationException.invalidInput(
      'signerWeights',
      'Weighted threshold policy requires at least one signer with weight',
    );
  }

  // Build signer weights inner map.
  final weightsEntries = <XdrSCMapEntry>[];
  for (final entry in signerWeights.entries) {
    final signerScVal = entry.key.toScVal();
    weightsEntries.add(
      XdrSCMapEntry(signerScVal, XdrSCVal.forU32(entry.value)),
    );
  }

  final sortedWeightsEntries =
      OZPolicyManager.sortMapByKeyXdr(weightsEntries);

  final topEntries = <XdrSCMapEntry>[
    XdrSCMapEntry(
      XdrSCVal.forSymbol('signer_weights'),
      XdrSCVal.forMap(sortedWeightsEntries),
    ),
    XdrSCMapEntry(
      XdrSCVal.forSymbol('threshold'),
      XdrSCVal.forU32(threshold),
    ),
  ];
  return XdrSCVal.forMap(topEntries);
}