withdraw method

Future<WithdrawResponse> withdraw(
  1. WithdrawRequest request
)

A withdraw is when a user redeems an asset currently on the Stellar network for its equivalent off-chain asset via the Anchor. For instance, a user redeeming their NGNT in exchange for fiat NGN.

If the anchor supports SEP-38 quotes, it can also provide a bridge between non-equivalent tokens. For example, the anchor can receive USDC from the Stellar network and in return send the equivalent value (minus fees) as NGN to the user's bank account. That kind of withdrawal is covered in GET /withdraw-exchange.

The /withdraw endpoint allows a wallet to get withdrawal information from an anchor, so a user has all the information needed to initiate a withdrawal. It also lets the anchor specify additional information (if desired) that the user must submit via SEP-12 to be able to withdraw.

Implementation

Future<WithdrawResponse> withdraw(WithdrawRequest request) async {
  Uri serverURI =
      Util.appendEndpointToUrl(_transferServiceAddress, 'withdraw');

  _WithdrawRequestBuilder requestBuilder =
      _WithdrawRequestBuilder(httpClient, serverURI);

  final Map<String, String> queryParams = {
    "asset_code": request.assetCode,
    "type": request.type,
  };

  if (request.dest != null) {
    queryParams["dest"] = request.dest!;
  }
  if (request.destExtra != null) {
    queryParams["dest_extra"] = request.destExtra!;
  }
  if (request.account != null) {
    queryParams["account"] = request.account!;
  }
  if (request.memo != null) {
    queryParams["memo"] = request.memo!;
  }
  if (request.memoType != null) {
    queryParams["memo_type"] = request.memoType!;
  }
  if (request.walletName != null) {
    queryParams["wallet_name"] = request.walletName!;
  }
  if (request.walletUrl != null) {
    queryParams["wallet_url"] = request.walletUrl!;
  }
  if (request.lang != null) {
    queryParams["lang"] = request.lang!;
  }
  if (request.onChangeCallback != null) {
    queryParams["on_change_callback"] = request.onChangeCallback!;
  }
  if (request.amount != null) {
    queryParams["amount"] = request.amount!;
  }
  if (request.countryCode != null) {
    queryParams["country_code"] = request.countryCode!;
  }
  if (request.refundMemo != null) {
    queryParams["refund_memo"] = request.refundMemo!;
  }
  if (request.refundMemoType != null) {
    queryParams["refund_memo_type"] = request.refundMemoType!;
  }
  if (request.customerId != null) {
    queryParams["customer_id"] = request.customerId!;
  }
  if (request.locationId != null) {
    queryParams["location_id"] = request.locationId!;
  }

  try {
    return await requestBuilder
        .forQueryParameters(queryParams)
        .execute(request.jwt);
  } on ErrorResponse catch (e) {
    if (e.code == 403) {
      _handleForbiddenResponse(e);
    }
    throw e;
  }
}