mirror of
https://github.com/Such-Software/hash-wallet.git
synced 2026-12-01 12:04:21 +01:00
CW-1273: Implement USDT0 Bridge (#2855)
* feat: Implement USDT0 Bridging using OFT Standard by Layer Zero. POC available in apps menu. * feat: Implement USDT0 Bridging using OFT Standard by Layer Zero. POC available in apps menu. * feat: Implement USDT0 Bridging using OFT Standard by Layer Zero. POC available in apps menu. * Add transaction history and status polling for USDT0 bridging * Add Transaction history and status polling for USDT0 Bridging * USDT0 Bridging Implementation * Update Arbitrum USDT token symbol * fix: Merge conflicts * fix: Merge conflicts * chore: Rever formatting * feat: Implement new flow * feat: Implement new ui flow * Add currency to configure * feat: new ui flow * Update USDT0 implementation - Switch to sqlite for storage instead of hive - Switch bridge history and details to modals - Switch bridge transfer details page to new tx details ui * refactor: rearrange modal action buttons * enhance usdt0 bridge flows * Update lib/entities/wallet_manager.dart [skip ci] --------- Co-authored-by: Omar Hatem <omarh.ismail1@gmail.com>
This commit is contained in:
co-authored by
Omar Hatem
parent
162e0a5300
commit
586a040e4a
@@ -0,0 +1,174 @@
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:cw_evm/usdt0/usdt0_quote.dart';
|
||||
import 'package:hex/hex.dart' as hex;
|
||||
import 'package:web3dart/web3dart.dart' as web3;
|
||||
|
||||
/// Minimal OFT ABI for USDT0 (per docs.usdt0.to).
|
||||
/// quoteSend(SendParam, bool payInLzToken) returns MessagingFee.
|
||||
/// send(SendParam, MessagingFee, address refundAddress) payable.
|
||||
/// SendParam: dstEid, to (bytes32), amountLD (amount to send, token units),
|
||||
/// minAmountLD (min receive on destination), extraOptions, composeMsg, oftCmd.
|
||||
const String _oftAbiJson = '''
|
||||
[
|
||||
{
|
||||
"inputs": [
|
||||
{
|
||||
"components": [
|
||||
{"internalType": "uint32", "name": "dstEid", "type": "uint32"},
|
||||
{"internalType": "bytes32", "name": "to", "type": "bytes32"},
|
||||
{"internalType": "uint256", "name": "amountLD", "type": "uint256"},
|
||||
{"internalType": "uint256", "name": "minAmountLD", "type": "uint256"},
|
||||
{"internalType": "bytes", "name": "extraOptions", "type": "bytes"},
|
||||
{"internalType": "bytes", "name": "composeMsg", "type": "bytes"},
|
||||
{"internalType": "bytes", "name": "oftCmd", "type": "bytes"}
|
||||
],
|
||||
"internalType": "struct SendParam",
|
||||
"name": "sendParam",
|
||||
"type": "tuple"
|
||||
},
|
||||
{"internalType": "bool", "name": "payInLzToken", "type": "bool"}
|
||||
],
|
||||
"name": "quoteSend",
|
||||
"outputs": [
|
||||
{
|
||||
"components": [
|
||||
{"internalType": "uint256", "name": "nativeFee", "type": "uint256"},
|
||||
{"internalType": "uint256", "name": "lzTokenFee", "type": "uint256"}
|
||||
],
|
||||
"internalType": "struct MessagingFee",
|
||||
"name": "",
|
||||
"type": "tuple"
|
||||
}
|
||||
],
|
||||
"stateMutability": "view",
|
||||
"type": "function"
|
||||
},
|
||||
{
|
||||
"inputs": [
|
||||
{
|
||||
"components": [
|
||||
{"internalType": "uint32", "name": "dstEid", "type": "uint32"},
|
||||
{"internalType": "bytes32", "name": "to", "type": "bytes32"},
|
||||
{"internalType": "uint256", "name": "amountLD", "type": "uint256"},
|
||||
{"internalType": "uint256", "name": "minAmountLD", "type": "uint256"},
|
||||
{"internalType": "bytes", "name": "extraOptions", "type": "bytes"},
|
||||
{"internalType": "bytes", "name": "composeMsg", "type": "bytes"},
|
||||
{"internalType": "bytes", "name": "oftCmd", "type": "bytes"}
|
||||
],
|
||||
"internalType": "struct SendParam",
|
||||
"name": "_sendParam",
|
||||
"type": "tuple"
|
||||
},
|
||||
{
|
||||
"components": [
|
||||
{"internalType": "uint256", "name": "nativeFee", "type": "uint256"},
|
||||
{"internalType": "uint256", "name": "lzTokenFee", "type": "uint256"}
|
||||
],
|
||||
"internalType": "struct MessagingFee",
|
||||
"name": "_fee",
|
||||
"type": "tuple"
|
||||
},
|
||||
{"internalType": "address", "name": "_refundAddress", "type": "address"}
|
||||
],
|
||||
"name": "send",
|
||||
"outputs": [],
|
||||
"stateMutability": "payable",
|
||||
"type": "function"
|
||||
}
|
||||
]
|
||||
''';
|
||||
|
||||
final web3.ContractAbi oftContractAbi = web3.ContractAbi.fromJson(_oftAbiJson, 'OFT');
|
||||
|
||||
/// OFT contract wrapper for quoteSend (view) and send (payable).
|
||||
class OFT {
|
||||
OFT({
|
||||
required web3.EthereumAddress address,
|
||||
required web3.Web3Client client,
|
||||
}) : _address = address,
|
||||
_client = client;
|
||||
|
||||
final web3.EthereumAddress _address;
|
||||
final web3.Web3Client _client;
|
||||
|
||||
/// Calls quoteSend and returns MessagingFee (nativeFee, lzTokenFee).
|
||||
Future<USDT0Quote> quoteSend({
|
||||
required int dstEid,
|
||||
required List<int> toBytes32,
|
||||
required BigInt amountLD,
|
||||
required BigInt minAmountLD,
|
||||
Uint8List? extraOptions,
|
||||
Uint8List? composeMsg,
|
||||
Uint8List? oftCmd,
|
||||
bool payInLzToken = false,
|
||||
}) async {
|
||||
final contract = web3.DeployedContract(
|
||||
oftContractAbi,
|
||||
_address,
|
||||
);
|
||||
final fn = contract.function('quoteSend');
|
||||
final sendParam = [
|
||||
BigInt.from(dstEid),
|
||||
Uint8List.fromList(toBytes32),
|
||||
amountLD,
|
||||
minAmountLD,
|
||||
extraOptions ?? Uint8List(0),
|
||||
composeMsg ?? Uint8List(0),
|
||||
oftCmd ?? Uint8List(0),
|
||||
];
|
||||
final params = [sendParam, payInLzToken];
|
||||
|
||||
final result = await _client.call(
|
||||
contract: contract,
|
||||
function: fn,
|
||||
params: params,
|
||||
);
|
||||
|
||||
final msgFee = result.first as List<dynamic>;
|
||||
final nativeFee = msgFee[0] as BigInt;
|
||||
final lzTokenFee = msgFee[1] as BigInt;
|
||||
return USDT0Quote(nativeFee: nativeFee, lzTokenFee: lzTokenFee);
|
||||
}
|
||||
|
||||
/// Encodes send(SendParam, MessagingFee, refundAddress) for transaction.
|
||||
/// Returns (dataHex, valueWei) where valueWei is the native fee to send.
|
||||
({String dataHex, BigInt valueWei}) encodeSend({
|
||||
required int dstEid,
|
||||
required List<int> toBytes32,
|
||||
required BigInt amountLD,
|
||||
required BigInt minAmountLD,
|
||||
required BigInt nativeFee,
|
||||
required BigInt lzTokenFee,
|
||||
required String refundAddress,
|
||||
Uint8List? extraOptions,
|
||||
Uint8List? composeMsg,
|
||||
Uint8List? oftCmd,
|
||||
}) {
|
||||
final contract = web3.DeployedContract(oftContractAbi, _address);
|
||||
final fn = contract.function('send');
|
||||
final sendParam = [
|
||||
BigInt.from(dstEid),
|
||||
Uint8List.fromList(toBytes32),
|
||||
amountLD,
|
||||
minAmountLD,
|
||||
extraOptions ?? Uint8List(0),
|
||||
composeMsg ?? Uint8List(0),
|
||||
oftCmd ?? Uint8List(0),
|
||||
];
|
||||
final fee = [nativeFee, lzTokenFee];
|
||||
final refund = web3.EthereumAddress.fromHex(refundAddress);
|
||||
final encoded = fn.encodeCall([sendParam, fee, refund]);
|
||||
final dataHex = '0x${hex.HEX.encode(encoded)}';
|
||||
return (dataHex: dataHex, valueWei: nativeFee);
|
||||
}
|
||||
}
|
||||
|
||||
/// Converts EVM address (0x + 40 hex) to bytes32 (left-padded).
|
||||
List<int> addressToBytes32(String address) {
|
||||
final clean = address.startsWith('0x') ? address.substring(2) : address;
|
||||
|
||||
if (clean.length != 40) throw ArgumentError('Invalid address length');
|
||||
|
||||
return hex.HEX.decode('000000000000000000000000$clean');
|
||||
}
|
||||
@@ -1281,12 +1281,25 @@ abstract class EVMChainWalletBase
|
||||
} else if (newTxInfo.direction == TransactionDirection.incoming &&
|
||||
existingTxInfo.direction == TransactionDirection.outgoing) {
|
||||
result[transactionModel.hash] = newTxInfo;
|
||||
}
|
||||
|
||||
else if (newTxInfo.direction == TransactionDirection.outgoing &&
|
||||
existingTxInfo.direction == TransactionDirection.outgoing &&
|
||||
_hasEvmTokenContractAddress(newTxInfo) &&
|
||||
!_hasEvmTokenContractAddress(existingTxInfo)) {
|
||||
result[transactionModel.hash] = newTxInfo;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
bool _hasEvmTokenContractAddress(EVMChainTransactionInfo info) {
|
||||
final c = info.contractAddress;
|
||||
return c != null && c.isNotEmpty;
|
||||
}
|
||||
|
||||
String? analyzeTransaction(String? transactionInput) {
|
||||
if (transactionInput == '0x' || transactionInput == null || transactionInput.isEmpty) {
|
||||
return '';
|
||||
|
||||
@@ -12,6 +12,13 @@ class ArbitrumTokens {
|
||||
decimal: 18,
|
||||
enabled: true,
|
||||
),
|
||||
Erc20Token(
|
||||
name: "Tether USD (Omnichain)",
|
||||
symbol: "USDT",
|
||||
contractAddress: "0xFd086bC7CD5C481DCC9C85ebE478A1C0b69FCbb9",
|
||||
decimal: 6,
|
||||
enabled: true,
|
||||
),
|
||||
Erc20Token(
|
||||
name: "USD Coin",
|
||||
symbol: "USDC",
|
||||
@@ -68,8 +75,7 @@ class ArbitrumTokens {
|
||||
if (token.iconPath?.isEmpty ?? true) {
|
||||
try {
|
||||
iconPath = CryptoCurrency.all
|
||||
.firstWhere((element) =>
|
||||
element.title.toUpperCase() == token.symbol.toUpperCase())
|
||||
.firstWhere((element) => element.title.toUpperCase() == token.symbol.toUpperCase())
|
||||
.iconPath;
|
||||
} catch (_) {}
|
||||
} else {
|
||||
@@ -80,4 +86,3 @@ class ArbitrumTokens {
|
||||
}).toList();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
import 'package:cw_core/erc20_token.dart';
|
||||
|
||||
/// USDT0 (Omnichain USDT) config.
|
||||
/// Addresses and EIDs from https://docs.usdt0.to/technical-documentation/developer/usdt0-deployments
|
||||
class USDT0Config {
|
||||
USDT0Config._();
|
||||
|
||||
static const String ethereumOftAdapter =
|
||||
'0x6C96dE32CEa08842dcc4058c14d3aaAD7Fa41dee';
|
||||
|
||||
static const String ethereumNativeUsdt =
|
||||
'0xdac17f958d2ee523a2206206994597c13d831ec7';
|
||||
|
||||
static const Map<int, String> usdt0TokenAddressByChainId = {
|
||||
1: ethereumNativeUsdt,
|
||||
137: '0xc2132D05D31c914a87C6611C10748AEb04B58e8F',
|
||||
42161: '0xFd086bC7CD5C481DCC9C85ebE478A1C0b69FCbb9',
|
||||
};
|
||||
|
||||
static const Map<int, String> oftContractAddressByChainId = {
|
||||
1: ethereumOftAdapter,
|
||||
137: '0x6BA10300f0DC58B7a1e4c0e41f5daBb7D7829e13',
|
||||
42161: '0x14E4A1B13bf7F943c8ff7C51fb60FA964A298D92',
|
||||
};
|
||||
|
||||
static const Map<int, int> endpointIdByChainId = {
|
||||
1: 30101,
|
||||
137: 30109,
|
||||
42161: 30110,
|
||||
};
|
||||
|
||||
static const int ethereumChainId = 1;
|
||||
|
||||
static List<int> get supportedChainIds =>
|
||||
usdt0TokenAddressByChainId.keys.toList(growable: false);
|
||||
|
||||
static String? getOftAdapterAddress(int chainId) {
|
||||
if (chainId == ethereumChainId) return ethereumOftAdapter;
|
||||
return null;
|
||||
}
|
||||
|
||||
static String? getUsdt0TokenAddress(int chainId) =>
|
||||
usdt0TokenAddressByChainId[chainId];
|
||||
|
||||
static String? getOftContractAddress(int chainId) =>
|
||||
oftContractAddressByChainId[chainId];
|
||||
|
||||
static int? getEndpointId(int chainId) => endpointIdByChainId[chainId];
|
||||
|
||||
static bool isChainSupported(int chainId) =>
|
||||
usdt0TokenAddressByChainId.containsKey(chainId);
|
||||
|
||||
static bool isUSDT0Token(Erc20Token token, int chainId) {
|
||||
final address = getUsdt0TokenAddress(chainId);
|
||||
if (address == null) return false;
|
||||
return token.contractAddress.toLowerCase() == address.toLowerCase();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
class USDT0Quote {
|
||||
const USDT0Quote({
|
||||
required this.nativeFee,
|
||||
required this.lzTokenFee,
|
||||
});
|
||||
|
||||
/// Fee in native token (e.g. ETH, MATIC) in wei.
|
||||
final BigInt nativeFee;
|
||||
|
||||
/// Fee in LZ token (if paying with LZ token).
|
||||
final BigInt lzTokenFee;
|
||||
|
||||
@override
|
||||
String toString() => 'USDT0Quote(nativeFee: $nativeFee, lzTokenFee: $lzTokenFee)';
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
import 'package:cw_core/erc20_token.dart';
|
||||
import 'package:cw_core/pending_transaction.dart';
|
||||
import 'package:cw_evm/contract/erc20.dart';
|
||||
import 'package:cw_evm/contract/oft.dart';
|
||||
import 'package:cw_evm/evm_chain_transaction_priority.dart';
|
||||
import 'package:cw_evm/utils/evm_chain_utils.dart';
|
||||
import 'package:cw_evm/evm_chain_wallet.dart';
|
||||
import 'package:cw_evm/usdt0/usdt0_config.dart';
|
||||
import 'package:cw_evm/usdt0/usdt0_quote.dart';
|
||||
import 'package:web3dart/web3dart.dart' as web3;
|
||||
|
||||
class USDT0Service {
|
||||
static Future<USDT0Quote> quoteCrossChainTransfer({
|
||||
required web3.Web3Client client,
|
||||
required int sourceChainId,
|
||||
required int destinationChainId,
|
||||
required BigInt amount,
|
||||
required String recipientAddress,
|
||||
BigInt? minAmount,
|
||||
}) async {
|
||||
final min = minAmount ?? BigInt.zero;
|
||||
final oftAddress = USDT0Config.getOftContractAddress(sourceChainId);
|
||||
final dstEid = USDT0Config.getEndpointId(destinationChainId);
|
||||
|
||||
if (oftAddress == null || dstEid == null) {
|
||||
throw Exception(
|
||||
'USDT0 not supported for chain $sourceChainId -> $destinationChainId',
|
||||
);
|
||||
}
|
||||
|
||||
final toBytes32 = addressToBytes32(recipientAddress);
|
||||
final oft = OFT(
|
||||
address: web3.EthereumAddress.fromHex(oftAddress),
|
||||
client: client,
|
||||
);
|
||||
|
||||
return oft.quoteSend(
|
||||
dstEid: dstEid,
|
||||
toBytes32: toBytes32,
|
||||
amountLD: amount,
|
||||
minAmountLD: min,
|
||||
payInLzToken: false,
|
||||
);
|
||||
}
|
||||
|
||||
static Future<PendingTransaction> executeCrossChainTransfer({
|
||||
required EVMChainWallet wallet,
|
||||
required int sourceChainId,
|
||||
required int destinationChainId,
|
||||
required BigInt amount,
|
||||
required String recipientAddress,
|
||||
required USDT0Quote quote,
|
||||
required Erc20Token token,
|
||||
required EVMChainTransactionPriority priority,
|
||||
bool useBlinkProtection = true,
|
||||
}) async {
|
||||
final oftAddress = USDT0Config.getOftContractAddress(sourceChainId);
|
||||
final dstEid = USDT0Config.getEndpointId(destinationChainId);
|
||||
|
||||
if (oftAddress == null || dstEid == null) {
|
||||
throw Exception(
|
||||
'USDT0 not supported for chain $sourceChainId -> $destinationChainId',
|
||||
);
|
||||
}
|
||||
|
||||
if (sourceChainId == USDT0Config.ethereumChainId) {
|
||||
final adapter = USDT0Config.getOftAdapterAddress(sourceChainId);
|
||||
if (adapter != null) {
|
||||
final needsApproval = await _isApprovalRequired(
|
||||
wallet: wallet,
|
||||
token: token,
|
||||
spender: adapter,
|
||||
amount: amount,
|
||||
);
|
||||
|
||||
if (needsApproval) {
|
||||
final maxUint = BigInt.parse(
|
||||
'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff',
|
||||
radix: 16,
|
||||
);
|
||||
|
||||
final pendingApproval = await wallet.createApprovalTransaction(
|
||||
maxUint,
|
||||
adapter,
|
||||
token,
|
||||
priority,
|
||||
EVMChainUtils.getFeeCurrency(wallet.selectedChainId),
|
||||
useBlinkProtection: useBlinkProtection,
|
||||
);
|
||||
await pendingApproval.commit();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final client = wallet.getWeb3Client();
|
||||
if (client == null) {
|
||||
throw StateError('Wallet not connected to node');
|
||||
}
|
||||
|
||||
final toBytes32 = addressToBytes32(recipientAddress);
|
||||
final oft = OFT(
|
||||
address: web3.EthereumAddress.fromHex(oftAddress),
|
||||
client: client,
|
||||
);
|
||||
|
||||
final encoded = oft.encodeSend(
|
||||
dstEid: dstEid,
|
||||
toBytes32: toBytes32,
|
||||
amountLD: amount,
|
||||
minAmountLD: BigInt.zero,
|
||||
nativeFee: quote.nativeFee,
|
||||
lzTokenFee: quote.lzTokenFee,
|
||||
refundAddress: wallet.walletAddresses.primaryAddress,
|
||||
);
|
||||
|
||||
return wallet.createCallDataTransaction(
|
||||
oftAddress,
|
||||
encoded.dataHex,
|
||||
encoded.valueWei,
|
||||
priority,
|
||||
token.contractAddress,
|
||||
amount,
|
||||
useBlinkProtection: useBlinkProtection,
|
||||
);
|
||||
}
|
||||
|
||||
static Future<bool> _isApprovalRequired({
|
||||
required EVMChainWallet wallet,
|
||||
required Erc20Token token,
|
||||
required String spender,
|
||||
required BigInt amount,
|
||||
}) async {
|
||||
final client = wallet.getWeb3Client();
|
||||
if (client == null) return true;
|
||||
|
||||
final erc20 = ERC20(
|
||||
address: web3.EthereumAddress.fromHex(token.contractAddress),
|
||||
client: client,
|
||||
);
|
||||
final current = await erc20.allowance(
|
||||
web3.EthereumAddress.fromHex(wallet.walletAddresses.primaryAddress),
|
||||
web3.EthereumAddress.fromHex(spender),
|
||||
);
|
||||
return current < amount;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user