mirror of
https://github.com/Such-Software/hash-wallet.git
synced 2026-11-19 10:04:17 +01:00
CW-1175-Implement-swaps.xyz-swap-provider (#2526)
* Integrate SwapsXYZ * Remove print * enhance SwapsXyz EVM token swap handling * Disable fixed rate support in SwapsXyz provider * enable fixed rate swap * fix timestamp parsing * remove sender and recipient address from fetchRate * Improve SwapsXYZ trade approval logic * fix trade ID overflow * add Base chain support for swaps xyz * Update lib/view_model/exchange/exchange_trade_view_model.dart Co-authored-by: Omar Hatem <omarh.ismail1@gmail.com> * add missing support for Base --------- Co-authored-by: Omar Hatem <omarh.ismail1@gmail.com>
This commit is contained in:
@@ -22,7 +22,9 @@ import 'package:cake_wallet/entities/transaction_description.dart';
|
||||
import 'package:cake_wallet/entities/wallet_contact.dart';
|
||||
import 'package:cake_wallet/ethereum/ethereum.dart';
|
||||
import 'package:cake_wallet/exchange/provider/exchange_provider.dart';
|
||||
import 'package:cake_wallet/exchange/provider/swapsxyz_exchange_provider.dart';
|
||||
import 'package:cake_wallet/exchange/provider/thorchain_exchange.provider.dart';
|
||||
import 'package:cake_wallet/exchange/trade.dart';
|
||||
import 'package:cake_wallet/generated/i18n.dart';
|
||||
import 'package:cake_wallet/monero/monero.dart';
|
||||
import 'package:cake_wallet/nano/nano.dart';
|
||||
@@ -46,6 +48,7 @@ import 'package:cake_wallet/view_model/unspent_coins/unspent_coins_list_view_mod
|
||||
import 'package:cake_wallet/wownero/wownero.dart';
|
||||
import 'package:cake_wallet/zano/zano.dart';
|
||||
import 'package:cw_core/crypto_currency.dart';
|
||||
import 'package:cw_core/erc20_token.dart';
|
||||
import 'package:cw_core/currency_for_wallet_type.dart';
|
||||
import 'package:cw_core/exceptions.dart';
|
||||
import 'package:cw_core/pending_transaction.dart';
|
||||
@@ -119,6 +122,9 @@ abstract class SendViewModelBase extends WalletChangeListenerViewModel with Stor
|
||||
});
|
||||
}
|
||||
|
||||
PendingTransaction? _pendingApprovalTx;
|
||||
bool _isSwapsXYZCallDataTx = false;
|
||||
|
||||
@observable
|
||||
ExecutionState state;
|
||||
|
||||
@@ -133,6 +139,9 @@ abstract class SendViewModelBase extends WalletChangeListenerViewModel with Stor
|
||||
|
||||
bool get isMwebEnabled => balanceViewModel.mwebEnabled;
|
||||
|
||||
bool get isEVMWallet => walletType == WalletType.ethereum || walletType == WalletType.polygon ||
|
||||
walletType == WalletType.base;
|
||||
|
||||
@action
|
||||
void setShowAddressBookPopup(bool value) {
|
||||
_settingsStore.showAddressBookPopupEnabled = value;
|
||||
@@ -490,7 +499,7 @@ abstract class SendViewModelBase extends WalletChangeListenerViewModel with Stor
|
||||
Timer? _ledgerTxStateTimer;
|
||||
|
||||
@action
|
||||
Future<PendingTransaction?> createTransaction({ExchangeProvider? provider}) async {
|
||||
Future<PendingTransaction?> createTransaction({ExchangeProvider? provider, Trade? trade}) async {
|
||||
try {
|
||||
if (!(state is IsExecutingState)) state = IsExecutingState();
|
||||
|
||||
@@ -505,6 +514,151 @@ abstract class SendViewModelBase extends WalletChangeListenerViewModel with Stor
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// Swaps.xyz (EVM) path
|
||||
|
||||
if (isEVMWallet && trade != null && provider is SwapsXyzExchangeProvider) {
|
||||
final routerTo = trade.inputAddress;
|
||||
final routerData = trade.routerData;
|
||||
final routerValueWei =
|
||||
BigInt.tryParse((trade.routerValue ?? '0').toString()) ?? BigInt.zero;
|
||||
|
||||
if (routerTo?.isNotEmpty == true && routerData?.isNotEmpty == true) {
|
||||
|
||||
// detect prepared ERC-20 transfer(...) (alt-vm deposit pattern)
|
||||
String _selector(String s) =>
|
||||
(s.startsWith('0x') && s.length >= 10) ? s.substring(0, 10) : '';
|
||||
const _transferSig = '0xa9059cbb';
|
||||
final _sel = _selector(routerData!);
|
||||
final _isPreparedTransfer = _sel == _transferSig &&
|
||||
(trade.sourceTokenAddress ?? '').toLowerCase() == (routerTo ?? '').toLowerCase();
|
||||
|
||||
_pendingApprovalTx = null;
|
||||
|
||||
// Optionally prebuild approval (SKIP for prepared transfer)
|
||||
final tokenContract = trade.sourceTokenAddress ?? '';
|
||||
final requiredAmount = BigInt.tryParse(
|
||||
(trade.sourceTokenAmountRaw ?? '0').replaceAll('n', ''),
|
||||
) ?? BigInt.zero;
|
||||
|
||||
// Only do approval when NOT a prepared transfer, and only if the API hinted we might need it
|
||||
final requiresTokenApproval = (trade.requiresTokenApproval ?? false) && !_isPreparedTransfer;
|
||||
|
||||
if (requiresTokenApproval && tokenContract.isNotEmpty && requiredAmount > BigInt.zero) {
|
||||
if (walletType == WalletType.ethereum) {
|
||||
final priority = _settingsStore.priority[WalletType.ethereum]!;
|
||||
_pendingApprovalTx = await buildApprovalIfNeeded(
|
||||
spender: routerTo!, // if API provides a specific spender, use that instead
|
||||
tokenContract: tokenContract,
|
||||
requiredAmount: requiredAmount,
|
||||
sourceTokenDecimals: trade.sourceTokenDecimals,
|
||||
);
|
||||
|
||||
// Build the callData tx
|
||||
pendingTransaction = await ethereum!.createRawCallDataTransaction(
|
||||
wallet,
|
||||
routerTo,
|
||||
routerData,
|
||||
routerValueWei,
|
||||
priority,
|
||||
);
|
||||
|
||||
_isSwapsXYZCallDataTx = true;
|
||||
state = ExecutedSuccessfullyState();
|
||||
return pendingTransaction; // do NOT fall back to regular flow
|
||||
}
|
||||
if (walletType == WalletType.polygon) {
|
||||
final priority = _settingsStore.priority[WalletType.polygon]!;
|
||||
_pendingApprovalTx = await buildApprovalIfNeeded(
|
||||
spender: routerTo!,
|
||||
tokenContract: tokenContract,
|
||||
requiredAmount: requiredAmount,
|
||||
sourceTokenDecimals: trade.sourceTokenDecimals,
|
||||
);
|
||||
|
||||
// Build the callData tx
|
||||
pendingTransaction = await polygon!.createRawCallDataTransaction(
|
||||
wallet,
|
||||
routerTo,
|
||||
routerData,
|
||||
routerValueWei,
|
||||
priority,
|
||||
);
|
||||
|
||||
_isSwapsXYZCallDataTx = true;
|
||||
state = ExecutedSuccessfullyState();
|
||||
return pendingTransaction; // do NOT fall back to regular flow
|
||||
}
|
||||
if (walletType == WalletType.base) {
|
||||
final priority = _settingsStore.priority[WalletType.base]!;
|
||||
_pendingApprovalTx = await buildApprovalIfNeeded(
|
||||
spender: routerTo!,
|
||||
tokenContract: tokenContract,
|
||||
requiredAmount: requiredAmount,
|
||||
sourceTokenDecimals: trade.sourceTokenDecimals,
|
||||
);
|
||||
|
||||
// Build the callData tx
|
||||
pendingTransaction = await base!.createRawCallDataTransaction(
|
||||
wallet,
|
||||
routerTo,
|
||||
routerData,
|
||||
routerValueWei,
|
||||
priority,
|
||||
);
|
||||
|
||||
_isSwapsXYZCallDataTx = true;
|
||||
state = ExecutedSuccessfullyState();
|
||||
return pendingTransaction; // do NOT fall back to regular flow
|
||||
}
|
||||
}
|
||||
|
||||
// No approval needed (or prepared transfer): send exactly what backend prepared
|
||||
if (walletType == WalletType.ethereum) {
|
||||
final priority = _settingsStore.priority[WalletType.ethereum]!;
|
||||
pendingTransaction = await ethereum!.createRawCallDataTransaction(
|
||||
wallet,
|
||||
routerTo!,
|
||||
routerData,
|
||||
routerValueWei,
|
||||
priority,
|
||||
);
|
||||
_isSwapsXYZCallDataTx = true;
|
||||
state = ExecutedSuccessfullyState();
|
||||
return pendingTransaction;
|
||||
}
|
||||
if (walletType == WalletType.polygon) {
|
||||
final priority = _settingsStore.priority[WalletType.polygon]!;
|
||||
pendingTransaction = await polygon!.createRawCallDataTransaction(
|
||||
wallet,
|
||||
routerTo!,
|
||||
routerData,
|
||||
routerValueWei,
|
||||
priority,
|
||||
);
|
||||
_isSwapsXYZCallDataTx = true;
|
||||
state = ExecutedSuccessfullyState();
|
||||
return pendingTransaction;
|
||||
}
|
||||
if (walletType == WalletType.base) {
|
||||
final priority = _settingsStore.priority[WalletType.polygon]!;
|
||||
pendingTransaction = await base!.createRawCallDataTransaction(
|
||||
wallet,
|
||||
routerTo!,
|
||||
routerData,
|
||||
routerValueWei,
|
||||
priority,
|
||||
);
|
||||
_isSwapsXYZCallDataTx = true;
|
||||
state = ExecutedSuccessfullyState();
|
||||
return pendingTransaction;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Regular flow
|
||||
|
||||
pendingTransaction = await wallet.createTransaction(_credentials(provider));
|
||||
|
||||
if (provider is ThorChainExchangeProvider) {
|
||||
@@ -602,6 +756,37 @@ abstract class SendViewModelBase extends WalletChangeListenerViewModel with Stor
|
||||
return;
|
||||
}
|
||||
|
||||
// Swaps.xyz approval (if any), then commit the prebuilt router tx
|
||||
if (_isSwapsXYZCallDataTx) {
|
||||
if (_pendingApprovalTx != null) {
|
||||
await _pendingApprovalTx!.commit();
|
||||
_pendingApprovalTx = null;
|
||||
// Small pause to ensure allowance is indexed
|
||||
await Future.delayed(const Duration(milliseconds: 300));
|
||||
}
|
||||
|
||||
await pendingTransaction!.commit();
|
||||
_isSwapsXYZCallDataTx = false;
|
||||
|
||||
state = TransactionCommitted();
|
||||
return; // skip the regular flow below
|
||||
}
|
||||
|
||||
// Regular flow (non-Swaps)
|
||||
if (pendingTransaction!.shouldCommitUR()) {
|
||||
final urstr = await pendingTransaction!.commitUR();
|
||||
final result = await Navigator.of(context).pushNamed(
|
||||
Routes.urqrAnimatedPage,
|
||||
arguments: urstr,
|
||||
);
|
||||
if (result == null) {
|
||||
state = FailureState("Canceled by user");
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
await pendingTransaction!.commit();
|
||||
}
|
||||
|
||||
String address = outputs.fold('', (acc, value) {
|
||||
return value.isParsedAddress
|
||||
? '$acc${value.address}\n${value.extractedAddress}\n\n'
|
||||
@@ -933,6 +1118,71 @@ abstract class SendViewModelBase extends WalletChangeListenerViewModel with Stor
|
||||
return false;
|
||||
}
|
||||
|
||||
Future<PendingTransaction?> buildApprovalIfNeeded({
|
||||
required String spender,
|
||||
required String tokenContract,
|
||||
required BigInt requiredAmount,
|
||||
int? sourceTokenDecimals,
|
||||
}) async {
|
||||
|
||||
// Only EVM chains support ERC20 approvals
|
||||
if (!isEVMWallet) return null;
|
||||
|
||||
const zero = '0x0000000000000000000000000000000000000000';
|
||||
const evmNative = '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE';
|
||||
|
||||
final tokenLc = tokenContract.toLowerCase();
|
||||
if (tokenLc == zero || tokenLc == evmNative.toLowerCase()) return null;
|
||||
if (requiredAmount <= BigInt.zero) return null;
|
||||
|
||||
bool needsApproval = false;
|
||||
if (walletType == WalletType.ethereum) {
|
||||
needsApproval = await ethereum!.isApprovalRequired(
|
||||
wallet, tokenContract, spender, requiredAmount,
|
||||
);
|
||||
} else if (walletType == WalletType.polygon) {
|
||||
needsApproval = await polygon!.isApprovalRequired(
|
||||
wallet, tokenContract, spender, requiredAmount,
|
||||
);
|
||||
} else if (walletType == WalletType.base) {
|
||||
needsApproval = await base!.isApprovalRequired(
|
||||
wallet, tokenContract, spender, requiredAmount,
|
||||
);
|
||||
}
|
||||
|
||||
if (!needsApproval) return null;
|
||||
|
||||
final erc20Token = wallet.balance.keys.whereType<Erc20Token>().firstWhere(
|
||||
(t) => t.contractAddress.toLowerCase() == tokenLc,
|
||||
orElse: () => Erc20Token(
|
||||
name: '',
|
||||
symbol: '',
|
||||
contractAddress: tokenContract,
|
||||
decimal: sourceTokenDecimals ?? 18,
|
||||
enabled: true,
|
||||
),
|
||||
);
|
||||
|
||||
if (walletType == WalletType.ethereum) {
|
||||
final priority = _settingsStore.priority[WalletType.ethereum]!;
|
||||
return await ethereum!.createTokenApproval(
|
||||
wallet, requiredAmount, spender, erc20Token, priority,
|
||||
);
|
||||
} else if (walletType == WalletType.polygon) {
|
||||
final priority = _settingsStore.priority[WalletType.polygon]!;
|
||||
return await polygon!.createTokenApproval(
|
||||
wallet, requiredAmount, spender, erc20Token, priority,
|
||||
);
|
||||
} else if (walletType == WalletType.base) {
|
||||
final priority = _settingsStore.priority[WalletType.base]!;
|
||||
return await base!.createTokenApproval(
|
||||
wallet, requiredAmount, spender, erc20Token, priority,
|
||||
);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@computed
|
||||
bool get usePayjoin => _settingsStore.usePayjoin;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user