transfer static method

Future<TransferResult> transfer({
  1. required AgentFactory agent,
  2. required String to,
  3. required BigInt amount,
  4. SendOpts? sendOpts,
  5. SignIdentity? identity,
})

Implementation

static Future<TransferResult> transfer({
  required AgentFactory agent,
  required String to,
  required BigInt amount,
  SendOpts? sendOpts,
  SignIdentity? identity,
}) async {
  try {
    var ledgerInstance = Ledger.hook(agent)..setIdentity(identity);
    var defaultFee = BigInt.from(10000);
    var defaultMemo = getRandomValues(4).toBn(endian: Endian.big);

    var sendArgs = {
      "to": to,
      "fee": {
        "e8s": sendOpts?.fee ?? defaultFee,
      },
      "amount": {"e8s": amount},
      "memo": sendOpts?.memo ?? defaultMemo,
      "from_subaccount": null,
      "created_at_time": sendOpts?.created_at_time == null
          ? null
          : {
              "timestamp_nanos":
                  sendOpts?.created_at_time?.millisecondsSinceEpoch.toBn()
            },
    };

    var res = await ledgerInstance.agent.actor!.getFunc(
        LedgerMethods.transfer)!([TransferArgs.fromMap(sendArgs).toJson()]);

    if (res != null) {
      return TransferResult.fromMap(res as Map);
    }
    throw "Cannot get count but $res";
  } catch (e) {
    rethrow;
  }
}