Skip to content

Commit

Permalink
Merge pull request #3419 from Emurgo/fix/YOEXT-624/connector-sign-pag…
Browse files Browse the repository at this point in the history
…e-assets

fix incorrect info displayed on dApp-connector window for multiparty txs
  • Loading branch information
vsubhuman authored Feb 24, 2024
2 parents 5dee7b7 + 385394a commit 98782a3
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import type {
import type {
CardanoConnectorSignRequest,
SignSubmissionErrorType,
TxDataOutput,
} from '../../types';
import type LocalizableError from '../../../i18n/LocalizableError';
import { Component } from 'react';
Expand Down Expand Up @@ -215,7 +214,7 @@ class SignTxPage extends Component<Props, State> {
selectedExplorer={this.props.selectedExplorer}
hash={fingerprint}
light
linkType="address"
linkType="token"
>
<span>{truncateAddressShort(getTokenName(tokenInfo), 10)}</span> <ExternalLinkIcon />
</ExplorableHashContainer>
Expand Down Expand Up @@ -282,33 +281,6 @@ class SignTxPage extends Component<Props, State> {
return null;
};

getUniqueAssets: (
Array<TxDataOutput>
) => Array<{|
tokenInfo: ?$ReadOnly<TokenRow>,
amount: BigNumber,
|}> = assets => {
return assets.reduce((acc, curr) => {
const newAcc: Array<{|
tokenInfo: ?$ReadOnly<TokenRow>,
amount: BigNumber,
|}> = [].concat(acc);

const defaultEntry = curr.value.getDefaultEntry();

[defaultEntry].concat(curr.value.nonDefaultEntries()).forEach(e => {
if (!newAcc.some(a => a.tokenInfo?.Identifier === e.identifier) && e.identifier) {
const tokenInfo = this.props.getTokenInfo(e);
if (tokenInfo != null) {
newAcc.push({ tokenInfo, amount: e.amount });
};
}
});

return newAcc;
}, []);
};

getSummaryAssetsData: void => SummaryAssetsData = () => {
const { txData } = this.props;

Expand All @@ -323,10 +295,31 @@ class SignTxPage extends Component<Props, State> {
const defaultTokenId = txData.amount.defaults.defaultIdentifier;
const defaultTokenAmount = txData.amount.get(defaultTokenId) ?? new BigNumber('0');
const txFeeAmount = new BigNumber(txData.fee.amount);
const sentAssets = this.getUniqueAssets(txData.outputs.filter(o => !o.isForeign));
const receivedAssets = this.getUniqueAssets(
txData.inputs.map(i => ({ ...i, isForeign: false }))
);

const assets = txData.amount.nonDefaultEntries();
for (const asset of assets) {
const assetInfo = {
amount: asset.amount,
// todo: properly query the backend (but don't block the UI) instead of using a default
tokenInfo: this.props.getTokenInfo(asset) ?? {
Identifier: asset.identifier,
IsDefault: false,
Metadata: {
type: 'Cardano',
policyId: asset.identifier.split('.')[0],
assetName: asset.identifier.split('.')[1],
numberOfDecimals: 0,
ticker: null,
longName: null,
},
},
};
if (asset.amount.isPositive()) {
assetsData.received.push(assetInfo);
} else {
assetsData.sent.push(assetInfo);
}
}

// only tx fee (no sign) & one asset sent/received
assetsData.total = this.getDisplayAmount({
Expand All @@ -337,12 +330,6 @@ class SignTxPage extends Component<Props, State> {
});

assetsData.isOnlyTxFee = defaultTokenAmount.toNumber() === 0;

// More than 1 asset sent/received (rather is NFT or not)
if (sentAssets.length > 1 || receivedAssets.length > 1) {
assetsData.sent = sentAssets.filter(Boolean);
assetsData.received = receivedAssets.filter(Boolean);
}
}
return assetsData;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ const ExpandableAssetsPanel = ({
)}
</Box>

{(hasNativeToken || assets.length !== 0) && (
{((total.amount.startsWith('-') && action === 'sent') || (
!total.amount.startsWith('-') && action === 'received')) && (
<AsseetValueDisplay>
{total.amount} {total.ticker}
</AsseetValueDisplay>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,7 @@ export default class ConnectorStore extends Store<StoresMap, ActionsMap> {
ownAddresses
);

let foreignInputDetails = [];
if (foreignInputs.length) {
const foreignUtxos = await this.stores.substores.ada.stateFetchStore.fetcher.getUtxoData({
network: connectedWallet.publicDeriver.getParent().networkInfo,
Expand Down Expand Up @@ -682,7 +683,7 @@ export default class ConnectorStore extends Store<StoresMap, ActionsMap> {
return;
}
const value = multiTokenFromRemote(foreignUtxo.output, defaultToken.NetworkId);
inputs.push({
foreignInputDetails.push({
address: Buffer.from(
RustModule.WalletV4.Address.from_bech32(foreignUtxo.output.address).to_bytes()
).toString('hex'),
Expand Down Expand Up @@ -886,8 +887,7 @@ export default class ConnectorStore extends Store<StoresMap, ActionsMap> {
runInAction(() => {
this.adaTransaction = {
inputs,
// $FlowFixMe[prop-missing]
foreignInputs,
foreignInputs: foreignInputDetails,
outputs,
fee,
total,
Expand Down
21 changes: 10 additions & 11 deletions packages/yoroi-extension/chrome/extension/connector/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,16 @@ const initializeDappConnector: void => Promise<void> = async () => {
const history = syncHistoryWithStore(hashHistory, router);
const stores = createStores(api, actions);

// <TODO:PENDING_REMOVAL> ??? USAGE?
// window.ergo = {
// api,
// actions,
// translations,
// stores,
// reset: action(() => {
// Action.resetAllActions();
// createStores(api, actions);
// }),
// };
window.yoroi = {
api,
actions,
translations,
stores,
reset: action(() => {
Action.resetAllActions();
createStores(api, actions);
}),
};

const root = document.querySelector('#root-yoroi-connector');
if (root == null) {
Expand Down

0 comments on commit 98782a3

Please sign in to comment.