public async Task HandleSwap(WebWalletBeginSwapTLYRAction action, IDispatcher dispatcher) { bool IsSuccess = false; try { if (action.fromToken == "LYR" && action.toToken == "TLYR") { var syncResult = await action.wallet.Sync(null); if (syncResult == APIResultCodes.Success) { var sendResult = await action.wallet.Send(action.fromAmount, action.options.lyrPub, "LYR"); if (sendResult.ResultCode == APIResultCodes.Success) { logger.LogInformation($"TokenSwap: first stage is ok for {action.fromAddress}"); var(txHash, result) = await SwapUtils.SendEthContractTokenAsync( action.options.ethUrl, action.options.ethContract, action.options.ethPub, action.options.ethPvk, action.toAddress, new BigInteger(action.toAmount * 100000000), // 10^8 action.gasPrice, action.gasLimit, null); logger.LogInformation($"TokenSwap: second stage for {action.fromAddress} eth tx hash is {txHash} IsSuccess: {result}"); if (!result) { throw new Exception("Eth sending failed."); } IsSuccess = true; } else { throw new Exception("Unable to send from your Lyra wallet."); } } else { throw new Exception("Unable to sync Lyra Wallet."); } } if (action.fromToken == "TLYR" && action.toToken == "LYR") { var(txHash, result) = await SwapUtils.SendEthContractTokenAsync( action.options.ethUrl, action.options.ethContract, action.fromAddress, null, action.options.ethPub, new BigInteger(action.fromAmount * 100000000), // 10^8 action.gasPrice, action.gasLimit, action.metamask); logger.LogInformation($"TokenSwap: first stage for {action.fromAddress} eth tx hash {result} IsSuccess: {result}"); if (result) // test if success transfer { var store = new AccountInMemoryStorage(); var wallet = Wallet.Create(store, "default", "", config["network"], action.options.lyrPvk); var syncResult = await wallet.Sync(client); if (syncResult == APIResultCodes.Success) { var sendResult = await wallet.Send(action.toAmount, action.toAddress, "LYR"); if (sendResult.ResultCode == Lyra.Core.Blocks.APIResultCodes.Success) { IsSuccess = true; } else { throw new Exception("Unable to send from your wallet."); } } else { throw new Exception("Unable to sync Lyra Wallet."); } } else { throw new Exception("Eth sending failed."); } } logger.LogInformation($"TokenSwap: Swapping {action.fromAmount} from {action.fromAddress} to {action.toAddress} is succeed."); dispatcher.Dispatch(new WebWalletSwapTLYRResultAction { Success = IsSuccess }); } catch (Exception ex) { logger.LogInformation($"TokenSwap: Swapping {action.fromAmount} from {action.fromAddress} to {action.toAddress} is failed. Error: {ex}"); dispatcher.Dispatch(new WebWalletSwapTLYRResultAction { Success = false, errMessage = ex.ToString() }); } }
public static WebWalletState ReductSwapTLYRHandler(WebWalletState state, WebWalletBeginSwapTLYRAction action) => state.With(new { IsLoading = true });