Skip to content

Commit

Permalink
confirmation messages
Browse files Browse the repository at this point in the history
  • Loading branch information
anyxem committed Jul 22, 2024
1 parent 2699fdb commit 59ab7e3
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 59 deletions.
2 changes: 0 additions & 2 deletions class/wallets/mintlayer-wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion loc/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
65 changes: 56 additions & 9 deletions screen/staking/confirm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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;
Expand Down Expand Up @@ -152,8 +154,22 @@ const Confirm = () => {
<>
{action === 'CreateDelegation' && (
<>
<View style={styles.valueWrap}>
<Text style={[styles.valueValue, stylesHook.valueValue]}>Create delegation</Text>
<View style={styles.actionView}>
<Text style={[styles.actionText]}>Create delegation</Text>
</View>
</>
)}
{action === 'addFunds' && (
<>
<View style={styles.actionView}>
<Text style={styles.actionText}>Add funds to delegation</Text>
</View>
</>
)}
{action === 'withdrawFunds' && (
<>
<View style={styles.actionView}>
<Text style={[styles.actionText]}>Withdraw from delegation</Text>
</View>
</>
)}
Expand All @@ -169,12 +185,22 @@ const Confirm = () => {
<Text style={[styles.transactionAmountFiat, stylesHook.transactionAmountFiat]}>{toLocalCurrencyFn(item.value)}</Text>
</>
)}
<BlueCard>
<Text style={[styles.transactionDetailsTitle, stylesHook.transactionDetailsTitle]}>{loc.stake.owner_address}</Text>
<Text testID="TransactionAddress" style={[styles.transactionDetailsSubtitle, stylesHook.transactionDetailsSubtitle]}>
{item.address}
</Text>
</BlueCard>
{delegation && delegation.delegation_id && (
<BlueCard>
<Text style={[styles.transactionDetailsTitle, stylesHook.transactionDetailsTitle]}>{loc.stake.delegation_id}</Text>
<Text testID="TransactionAddress" style={[styles.transactionDetailsSubtitle, stylesHook.transactionDetailsSubtitle]}>
{delegation.delegation_id}
</Text>
</BlueCard>
)}
{item.address && (
<BlueCard>
<Text style={[styles.transactionDetailsTitle, stylesHook.transactionDetailsTitle]}>{loc.stake.owner_address}</Text>
<Text testID="TransactionAddress" style={[styles.transactionDetailsSubtitle, stylesHook.transactionDetailsSubtitle]}>
{item.address}
</Text>
</BlueCard>
)}
{item.poolId && (
<BlueCard>
<Text style={[styles.transactionDetailsTitle, stylesHook.transactionDetailsTitle]}>{loc.stake.pool_id}</Text>
Expand All @@ -183,6 +209,16 @@ const Confirm = () => {
</Text>
</BlueCard>
)}

{action === 'addFunds' && (
<BlueCard>
<Text style={[styles.transactionDetailsTitle, stylesHook.transactionDetailsTitle]}>{loc.stake.new_balance}</Text>
<Text testID="TransactionAddress" style={[styles.transactionDetailsSubtitle, stylesHook.transactionDetailsSubtitle]}>
{amount + delegation.balance / 1e11} ML
</Text>
</BlueCard>
)}

{recipients.length > 1 && <BlueText style={styles.valueOf}>{loc.formatString(loc._.of, { number: index + 1, total: recipients.length })}</BlueText>}
</>
);
Expand Down Expand Up @@ -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 }));
134 changes: 87 additions & 47 deletions screen/staking/delegationDetails.js
Original file line number Diff line number Diff line change
Expand Up @@ -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([]);
Expand All @@ -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('');
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -225,53 +225,85 @@ 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 () => {
if (!wallet) {
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 });
Expand All @@ -282,7 +314,7 @@ const StakingDelegationDetails = () => {
<KeyboardAvoidingView enabled={!Platform.isPad} behavior={Platform.OS === 'ios' ? 'position' : null}>
<View style={styles.addDelegationModal}>
<View style={styles.addDelegationModalContent}>
<Text>Enter amount</Text>
<Text>Enter amount you want to delegate</Text>
</View>

<AmountInputML
Expand All @@ -292,6 +324,7 @@ const StakingDelegationDetails = () => {
// TODO: implement unit change
}}
onChangeText={(text) => {
console.log(text);
setAmount(text);
}}
unit={amountUnit}
Expand All @@ -313,7 +346,7 @@ const StakingDelegationDetails = () => {
)}
</TouchableOpacity>

<View style={styles.createButton}>{isLoading ? <ActivityIndicator /> : <BlueButton onPress={createAddFundsTransaction} title={loc.send.details_next} testID="CreateTransactionButton" />}</View>
<View style={styles.createButton}>{isLoading ? <ActivityIndicator /> : <BlueButton disabled={amount < 1} onPress={createAddFundsTransaction} title={loc.send.details_next} testID="CreateTransactionButton" />}</View>
</View>
</View>
</KeyboardAvoidingView>
Expand All @@ -327,7 +360,7 @@ const StakingDelegationDetails = () => {
<KeyboardAvoidingView enabled={!Platform.isPad} behavior={Platform.OS === 'ios' ? 'position' : null}>
<View style={styles.addDelegationModal}>
<View style={styles.addDelegationModalContent}>
<Text>Enter amount</Text>
<Text>Enter amount you want to withdraw</Text>
</View>
<AmountInputML
isLoading={isLoading}
Expand Down Expand Up @@ -357,16 +390,14 @@ const StakingDelegationDetails = () => {
)}
</TouchableOpacity>

<View style={styles.createButton}>{isLoading ? <ActivityIndicator /> : <BlueButton onPress={createWithdrawTransaction} title={loc.send.details_next} testID="CreateTransactionButton" />}</View>
<View style={styles.createButton}>{isLoading ? <ActivityIndicator /> : <BlueButton disabled={amount > delegation.balance / 1e11 - feePrecalc.current / 1e11} onPress={createWithdrawTransaction} title={loc.send.details_next} testID="CreateTransactionButton" />}</View>
</View>
</View>
</KeyboardAvoidingView>
</BottomModal>
);
};

console.log('delegation', delegation);

return (
<View style={[styles.root]} onLayout={(e) => setWidth(e.nativeEvent.layout.width)}>
<StatusBar barStyle="light-content" />
Expand All @@ -383,13 +414,22 @@ const StakingDelegationDetails = () => {
<Text>Pool Id:</Text>
<Text>{delegation.pool_id}</Text>
</View>
<View style={styles.delegationPoolDetailsData}>
<View style={styles.delegationPoolSummaryData}>
<Text>Pool Summary:</Text>
<Text>{JSON.stringify(delegation.pool_data)}</Text>

<View>
<Text>Cost per block: {delegation.pool_data.cost_per_block.decimal} ML</Text>
</View>
<View>
<Text>Margin ratio: {delegation.pool_data.margin_ratio_per_thousand}</Text>
</View>
<View>
<Text>Pool pledge: {delegation.pool_data.staker_balance.decimal} ML</Text>
</View>
</View>

<View style={styles.balance}>
<Text style={styles.balanceLabel}>Balance:</Text>
<Text style={styles.balanceLabel}>Your balance:</Text>
<Text style={styles.balanceValue}>{delegation.balance / 1e11} ML</Text>
</View>
</View>
Expand Down
4 changes: 4 additions & 0 deletions screen/staking/styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,8 @@ export const stakingStyles = {
delegationPoolDetailsData: {
paddingTop: 10,
},

delegationPoolSummaryData: {
paddingTop: 10,
},
};

0 comments on commit 59ab7e3

Please sign in to comment.