From 59ab7e3ea07b20f20999fe040121a915d3805e3b Mon Sep 17 00:00:00 2001 From: Sergey Chystiakov Date: Mon, 22 Jul 2024 02:43:01 +0200 Subject: [PATCH] confirmation messages --- class/wallets/mintlayer-wallet.js | 2 - loc/en.json | 4 +- screen/staking/confirm.js | 65 ++++++++++++-- screen/staking/delegationDetails.js | 134 ++++++++++++++++++---------- screen/staking/styles.js | 4 + 5 files changed, 150 insertions(+), 59 deletions(-) diff --git a/class/wallets/mintlayer-wallet.js b/class/wallets/mintlayer-wallet.js index d972bfd..fbac28d 100644 --- a/class/wallets/mintlayer-wallet.js +++ b/class/wallets/mintlayer-wallet.js @@ -803,8 +803,6 @@ export class MintLayerWallet extends AbstractHDWallet { tokenId, }); - console.log('txOutput', txOutput); - const changeAmountCoin = (totalUtxosAmount(requireUtxoCoin) - Number(amountToUseFinaleCoin)).toString(); const txChangeOutputCoin = await getTxOutput({ amount: changeAmountCoin, diff --git a/loc/en.json b/loc/en.json index 53091f3..1c01a3e 100644 --- a/loc/en.json +++ b/loc/en.json @@ -273,10 +273,12 @@ "stake": { "header": "Stake", "pool_id": "Pool ID", + "delegation_id": "Delegation ID", "add_funds": "Add Funds", "withdraw": "Withdraw", "add_delegation": "Add new delegation", - "owner_address": "Owner address" + "owner_address": "Owner address", + "new_balance": "New balance after transaction" }, "settings": { "about": "About", diff --git a/screen/staking/confirm.js b/screen/staking/confirm.js index 120f755..f669928 100644 --- a/screen/staking/confirm.js +++ b/screen/staking/confirm.js @@ -14,6 +14,7 @@ import alert from '../../components/Alert'; import { MintLayerWallet } from '../../class/wallets/mintlayer-wallet'; import { MintlayerUnit } from '../../models/mintlayerUnits'; import { ML_ATOMS_PER_COIN, TransactionType } from '../../blue_modules/Mintlayer'; +import { COLORS } from '../../theme/Colors'; const currency = require('../../blue_modules/currency'); const BlueElectrum = require('../../blue_modules/BlueElectrum'); const Bignumber = require('bignumber.js'); @@ -22,7 +23,8 @@ const Confirm = () => { const { wallets, fetchAndSaveWalletTransactions, isElectrumDisabled, isTorDisabled, isTestMode } = useContext(BlueStorageContext); const [isBiometricUseCapableAndEnabled, setIsBiometricUseCapableAndEnabled] = useState(false); const { params } = useRoute(); - const { recipients = [], walletID, fee, memo, tx, satoshiPerByte, psbt, requireUtxo, tokenInfo, action } = params; + const { recipients = [], walletID, fee, memo, tx, satoshiPerByte, psbt, requireUtxo, tokenInfo, action, delegation } = params; + console.log(tx); const [isLoading, setIsLoading] = useState(false); const wallet = wallets.find((wallet) => wallet.getID() === walletID); const isMintlayerWallet = wallet.type === MintLayerWallet.type; @@ -152,8 +154,22 @@ const Confirm = () => { <> {action === 'CreateDelegation' && ( <> - - Create delegation + + Create delegation + + + )} + {action === 'addFunds' && ( + <> + + Add funds to delegation + + + )} + {action === 'withdrawFunds' && ( + <> + + Withdraw from delegation )} @@ -169,12 +185,22 @@ const Confirm = () => { {toLocalCurrencyFn(item.value)} )} - - {loc.stake.owner_address} - - {item.address} - - + {delegation && delegation.delegation_id && ( + + {loc.stake.delegation_id} + + {delegation.delegation_id} + + + )} + {item.address && ( + + {loc.stake.owner_address} + + {item.address} + + + )} {item.poolId && ( {loc.stake.pool_id} @@ -183,6 +209,16 @@ const Confirm = () => { )} + + {action === 'addFunds' && ( + + {loc.stake.new_balance} + + {amount + delegation.balance / 1e11} ML + + + )} + {recipients.length > 1 && {loc.formatString(loc._.of, { number: index + 1, total: recipients.length })}} ); @@ -309,6 +345,17 @@ const styles = StyleSheet.create({ fontSize: 15, fontWeight: 'bold', }, + actionView: { + flexDirection: 'row', + justifyContent: 'center', + marginVertical: 16, + }, + actionText: { + color: COLORS.green, + fontSize: 22, + fontWeight: '500', + alignSelf: 'center', + }, }); Confirm.navigationOptions = navigationStyle({}, (opts) => ({ ...opts, title: loc.send.confirm_header })); diff --git a/screen/staking/delegationDetails.js b/screen/staking/delegationDetails.js index 8c676fe..4fb3938 100644 --- a/screen/staking/delegationDetails.js +++ b/screen/staking/delegationDetails.js @@ -46,7 +46,6 @@ const StakingDelegationDetails = () => { const [units, setUnits] = useState([MintlayerUnit.ML]); const [customFee, setCustomFee] = useState(null); - const [amountUnit, setAmountUnit] = useState(MintlayerUnit.ML); const [amount, setAmount] = useState(''); const [feeUnit, setFeeUnit] = useState(); const [addresses, setAddresses] = useState([]); @@ -58,6 +57,8 @@ const StakingDelegationDetails = () => { const [dumb, setDumb] = useState(false); + const amountUnit = MintlayerUnit.ML; + const balance = utxo ? utxo.reduce((prev, curr) => prev + curr.value, 0) : wallet?.getBalance(); const [poolId, setPoolId] = useState(''); @@ -121,7 +122,6 @@ const StakingDelegationDetails = () => { // check if we have a suitable wallet setUnits([MintlayerUnit.ML]); setFeeUnit(wallet.getPreferredBalanceUnit()); - setAmountUnit(wallet.preferredBalanceUnit); // default for whole screen // we are ready! setIsLoading(false); @@ -225,28 +225,38 @@ const StakingDelegationDetails = () => { return null; } - const utxo = wallet.getUtxo(); - const address = await wallet.getAddressAsync(); - const changeAddress = await wallet.getChangeAddressAsync(); - - const targets = [ - { - delegationId: delegation.delegation_id, - value: BigInt(amount * 100000000000), - }, - ]; - - const { tx, outputs, fee, requireUtxo } = await wallet.createTransaction(utxo, targets, 100000000, changeAddress); - - const recipients = outputs.filter(({ address }) => address !== changeAddress); + setIsLoading(true); - navigation.navigate('Confirm', { - fee: new BigNumber(fee).dividedBy(ML_ATOMS_PER_COIN).toNumber(), - walletID: wallet.getID(), - tx, - recipients, - requireUtxo, - }); + try { + const utxo = wallet.getUtxo(); + const address = await wallet.getAddressAsync(); + const changeAddress = await wallet.getChangeAddressAsync(); + + const targets = [ + { + delegationId: delegation.delegation_id, + value: BigInt(amount * 100000000000), + }, + ]; + + const { tx, outputs, fee, requireUtxo } = await wallet.createTransaction(utxo, targets, 100000000, changeAddress); + + const recipients = outputs.filter(({ address }) => address !== changeAddress); + + navigation.navigate('Confirm', { + action: 'addFunds', + fee: new BigNumber(fee).dividedBy(ML_ATOMS_PER_COIN).toNumber(), + walletID: wallet.getID(), + tx, + recipients, + requireUtxo, + delegation, + }); + setIsLoading(false); + } catch (e) { + console.log(e); + setIsLoading(false); + } }; const createWithdrawTransaction = async () => { @@ -254,24 +264,46 @@ const StakingDelegationDetails = () => { return null; } - const utxo = wallet.getUtxo(); - const address = await wallet.getAddressAsync(); - const changeAddress = await wallet.getChangeAddressAsync(); + setIsLoading(true); - const targets = [ - { - delegationId: delegation.delegation_id, - value: BigInt(amount * 100000000000), - }, - ]; + try { + const utxo = wallet.getUtxo(); + const address = await wallet.getAddressAsync(); + const changeAddress = await wallet.getChangeAddressAsync(); + + const targets = [ + { + delegationId: delegation.delegation_id, + value: BigInt(amount * 100000000000), + }, + ]; + + const { tx, fee } = await wallet.withdrawDelegation({ + delegation, + amount: BigInt(amount * 100000000000), + feeRate: 100000000, + }); - const { tx, fee } = await wallet.withdrawDelegation({ delegation, amount: BigInt(amount * 100000000000), feeRate: 100000000 }); + const recipients = [ + { + address: delegation.spend_destination, + value: BigInt(amount * 100000000000), + }, + ]; - navigation.navigate('Confirm', { - fee: new BigNumber(fee).dividedBy(ML_ATOMS_PER_COIN).toNumber(), - walletID: wallet.getID(), - tx, - }); + navigation.navigate('Confirm', { + delegation, + action: 'withdrawFunds', + fee: new BigNumber(fee).dividedBy(ML_ATOMS_PER_COIN).toNumber(), + walletID: wallet.getID(), + recipients, + tx, + }); + setIsLoading(false); + } catch (e) { + console.log(e); + setIsLoading(false); + } }; useRecalcFee({ wallet, networkTransactionFees, feeRate, utxo, addresses, changeAddress, dumb, feePrecalc, setFeePrecalc, balance }); @@ -282,7 +314,7 @@ const StakingDelegationDetails = () => { - Enter amount + Enter amount you want to delegate { // TODO: implement unit change }} onChangeText={(text) => { + console.log(text); setAmount(text); }} unit={amountUnit} @@ -313,7 +346,7 @@ const StakingDelegationDetails = () => { )} - {isLoading ? : } + {isLoading ? : } @@ -327,7 +360,7 @@ const StakingDelegationDetails = () => { - Enter amount + Enter amount you want to withdraw { )} - {isLoading ? : } + {isLoading ? : delegation.balance / 1e11 - feePrecalc.current / 1e11} onPress={createWithdrawTransaction} title={loc.send.details_next} testID="CreateTransactionButton" />} @@ -365,8 +398,6 @@ const StakingDelegationDetails = () => { ); }; - console.log('delegation', delegation); - return ( setWidth(e.nativeEvent.layout.width)}> @@ -383,13 +414,22 @@ const StakingDelegationDetails = () => { Pool Id: {delegation.pool_id} - + Pool Summary: - {JSON.stringify(delegation.pool_data)} + + + Cost per block: {delegation.pool_data.cost_per_block.decimal} ML + + + Margin ratio: {delegation.pool_data.margin_ratio_per_thousand} + + + Pool pledge: {delegation.pool_data.staker_balance.decimal} ML + - Balance: + Your balance: {delegation.balance / 1e11} ML diff --git a/screen/staking/styles.js b/screen/staking/styles.js index c9d88cb..2c01658 100644 --- a/screen/staking/styles.js +++ b/screen/staking/styles.js @@ -148,4 +148,8 @@ export const stakingStyles = { delegationPoolDetailsData: { paddingTop: 10, }, + + delegationPoolSummaryData: { + paddingTop: 10, + }, };