unstakeToken method

Future<DC<Exception, Stream<SmartWalletEvent>>> unstakeToken(
  1. EthPrivateKey credentials,
  2. UnstakeRequestBody unstakeRequestBody,
  3. String unStakeTokenAddress
)

Implementation

Future<DC<Exception, Stream<SmartWalletEvent>>> unstakeToken(
  EthPrivateKey credentials,
  UnstakeRequestBody unstakeRequestBody,
  String unStakeTokenAddress,
) async {
  final response = await _stakingModule.unstake(unstakeRequestBody);
  if (response.hasError) {
    return DC.error(response.error!);
  }
  final tokenDetailsRes = await ContractsUtils.getERC20TokenDetails(
    web3client,
    EthereumAddress.fromHex(
      unstakeRequestBody.tokenAddress,
    ),
  );

  final BigInt amount = AmountFormat.toBigInt(
    unstakeRequestBody.tokenAmount,
    tokenDetailsRes.decimals,
  );
  final Map<String, dynamic> transactionBody = {
    "status": 'pending',
    "from": smartWallet.smartWalletAddress,
    'value': amount.toString(),
  };
  final String data = strip0x(response.data!.encodedABI);

  if (unStakeTokenAddress.toLowerCase() ==
      Variables.NATIVE_TOKEN_ADDRESS.toLowerCase()) {
    return callContract(
      credentials,
      response.data!.contractAddress,
      data,
      value: amount,
      transactionBody: transactionBody,
    );
  } else {
    return approveTokenAndCallContract(
      credentials,
      unStakeTokenAddress,
      response.data!.contractAddress,
      amount.toString(),
      data,
      transactionBody: transactionBody,
    );
  }
}