Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Selected network per origin #17986

Merged
merged 15 commits into from
Apr 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.chromium.chrome.browser.util.Triple;
import org.chromium.mojo.bindings.Callbacks;
import org.chromium.mojo.system.MojoException;
import org.chromium.url.internal.mojom.Origin;

import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -123,10 +124,11 @@ public NetworkModel(JsonRpcService jsonRpcService, CryptoSharedData sharedData,
}
});
_mChainId.addSource(mSharedData.getCoinTypeLd(), coinType -> {
mJsonRpcService.getChainId(coinType, chainId -> {
mJsonRpcService.getDefaultChainId(coinType, chainId -> {
String id = BraveWalletConstants.MAINNET_CHAIN_ID;
if (TextUtils.isEmpty(chainId)) {
mJsonRpcService.setNetwork(id, mSharedData.getCoinType(), hasSetNetwork -> {});
mJsonRpcService.setNetwork(
id, mSharedData.getCoinType(), null, hasSetNetwork -> {});
} else {
id = chainId;
}
Expand Down Expand Up @@ -292,8 +294,8 @@ public void setNetworkWithAccountCheck(
if (isSameNetwork(networkToBeSetAsSelected, selectedNetwork)) return;
boolean hasAccountOfNetworkType = hasAccountOfNetworkType(networkToBeSetAsSelected);
if (hasAccountOfNetworkType) {
mJsonRpcService.setNetwork(
networkToBeSetAsSelected.chainId, networkToBeSetAsSelected.coin, isSelected -> {
mJsonRpcService.setNetwork(networkToBeSetAsSelected.chainId,
networkToBeSetAsSelected.coin, null, isSelected -> {
callback.call(isSelected);
mCryptoActions.updateCoinType();
init();
Expand All @@ -311,16 +313,16 @@ public void setNetworkWithAccountCheck(String chainId, Callbacks.Callback1<Boole

public void setNetwork(
NetworkInfo networkToBeSetAsSelected, Callbacks.Callback1<Boolean> callback) {
mJsonRpcService.setNetwork(
networkToBeSetAsSelected.chainId, networkToBeSetAsSelected.coin, isSelected -> {
mJsonRpcService.setNetwork(networkToBeSetAsSelected.chainId, networkToBeSetAsSelected.coin,
null, isSelected -> {
callback.call(isSelected);
mCryptoActions.updateCoinType();
init();
});
}

public void getNetwork(@CoinType.EnumType int coin, Callbacks.Callback1<NetworkInfo> callback) {
mJsonRpcService.getNetwork(coin, networkInfo -> { callback.call(networkInfo); });
mJsonRpcService.getNetwork(coin, null, networkInfo -> { callback.call(networkInfo); });
}

public void clearCreateAccountState() {
Expand Down Expand Up @@ -413,7 +415,7 @@ private boolean isSameNetwork(
}

@Override
public void chainChangedEvent(String chainId, int coin) {
public void chainChangedEvent(String chainId, int coin, Origin origin) {
_mChainId.postValue(chainId);
mCryptoActions.updateCoinAccountNetworkInfo(coin);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,16 @@ public void setTxToken(BlockchainToken mTxToken) {
// create and update balance and other corresponding details
}

public void sendSolanaToken(BlockchainToken token, String fromAddress, String toAddress,
long lamportsValue, Callbacks.Callback3<Boolean, String, String> callback) {
public void sendSolanaToken(String chainId, BlockchainToken token, String fromAddress,
String toAddress, long lamportsValue,
Callbacks.Callback3<Boolean, String, String> callback) {
if (token == null) return;

if (token.coin == CoinType.SOL && !TextUtils.isEmpty(token.contractAddress)) {
// process SPL (Solana Program Library) tokens
mSolanaTxManagerProxy.makeTokenProgramTransferTxData(token.contractAddress, fromAddress,
toAddress, lamportsValue, (solanaTxData, error, errorMessageMakeData) -> {
mSolanaTxManagerProxy.makeTokenProgramTransferTxData(chainId, token.contractAddress,
fromAddress, toAddress, lamportsValue,
(solanaTxData, error, errorMessageMakeData) -> {
if (error == 0) {
mTxService.addUnapprovedTransaction(
WalletUtils.toTxDataUnion(solanaTxData), fromAddress, null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ public void finishNativeInitialization() {
Log.e(TAG, "finishNativeInitialization " + e);
}
assert mJsonRpcService != null;
mJsonRpcService.getNetwork(mCoinType, selectedNetwork -> {
mJsonRpcService.getNetwork(mCoinType, null, selectedNetwork -> {
setUpAssetList(selectedNetwork);
fetchAccountInfo(selectedNetwork);
});
Expand All @@ -242,7 +242,7 @@ public void finishNativeInitialization() {
@Override
public void onAssetClick(BlockchainToken asset) {
assert mJsonRpcService != null;
mJsonRpcService.getChainId(mCoinType, chainId -> {
mJsonRpcService.getDefaultChainId(mCoinType, chainId -> {
Utils.openAssetDetailsActivity(AccountDetailActivity.this, chainId, asset);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ protected void triggerLayoutInflation() {

mEtCustomNonce = findViewById(R.id.activity_advance_setting_et_nonce);
String txId = getIntent().getStringExtra(WalletConstants.ADVANCE_TX_SETTING_INTENT_TX_ID);
String chainId =
getIntent().getStringExtra(WalletConstants.ADVANCE_TX_SETTING_INTENT_TX_CHAIN_ID);
String nonce =
getIntent().getStringExtra(WalletConstants.ADVANCE_TX_SETTING_INTENT_TX_NONCE);
mEtCustomNonce.setText(Utils.hexToIntString(nonce));
Expand All @@ -35,16 +37,18 @@ protected void triggerLayoutInflation() {
if (!TextUtils.isEmpty(newNonce)) {
newNonce = Utils.toHex(mEtCustomNonce.getText().toString());
}
getEthTxManagerProxy().setNonceForUnapprovedTransaction(txId, newNonce, isSet -> {
if (isSet) {
Intent result = new Intent().putExtra(
WalletConstants.ADVANCE_TX_SETTING_INTENT_RESULT_NONCE, newNonce);
setResult(Activity.RESULT_OK, result);
finish();
} else {
Log.e(TAG, "Unable to set nonce ");
}
});
getEthTxManagerProxy().setNonceForUnapprovedTransaction(
chainId, txId, newNonce, isSet -> {
if (isSet) {
Intent result = new Intent().putExtra(
WalletConstants.ADVANCE_TX_SETTING_INTENT_RESULT_NONCE,
newNonce);
setResult(Activity.RESULT_OK, result);
finish();
} else {
Log.e(TAG, "Unable to set nonce ");
}
});
});
onInitialLayoutInflationComplete();
}
Expand All @@ -54,4 +58,4 @@ private void setupBraveToolBar(@StringRes int title) {
toolbar.setTitle(title);
setSupportActionBar(toolbar);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public void onRejectAllTransactions() {
for (TransactionInfo transactionInfo : mPendingTxHelper.getPendingTransactions()) {
getTxService().rejectTransaction(
TransactionUtils.getCoinFromTxDataUnion(transactionInfo.txDataUnion),
transactionInfo.id, success -> {
transactionInfo.chainId, transactionInfo.id, success -> {
if (!success) {
Log.e(TAG, "Transaction failed " + transactionInfo.id);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ private void workWithSwapQuota(SwapResponse response, SwapErrorResponse errorRes

private void sendSwapTransaction(TxData data, String from) {
assert mEthTxManagerProxy != null;
mEthTxManagerProxy.getGasEstimation1559(estimation -> {
mEthTxManagerProxy.getGasEstimation1559(mSelectedNetwork.chainId, estimation -> {
String maxPriorityFeePerGas = "";
String maxFeePerGas = "";
if (estimation.fastMaxPriorityFeePerGas.equals(estimation.avgMaxPriorityFeePerGas)) {
Expand Down Expand Up @@ -587,8 +587,8 @@ private void checkAllowance(String contract, String spenderAddress, double amoun
assert mCurrentBlockchainToken != null;
String ownerAddress = mCustomAccountAdapter.getAccountAddressAtPosition(
mAccountSpinner.getSelectedItemPosition());
mJsonRpcService.getErc20TokenAllowance(
contract, ownerAddress, spenderAddress, (allowance, error, errorMessage) -> {
mJsonRpcService.getErc20TokenAllowance(contract, ownerAddress, spenderAddress,
mSelectedNetwork.chainId, (allowance, error, errorMessage) -> {
warnWhenError(TAG, "getErc20TokenAllowance", error, errorMessage);
if (error != ProviderError.SUCCESS
|| amountToSend <= Utils.fromHexWei(
Expand Down Expand Up @@ -1011,8 +1011,9 @@ public void onTextChanged(CharSequence s, int start, int before, int count) {
if (mCurrentBlockchainToken.isNft) {
amount = "1";
}
mSendModel.sendSolanaToken(mCurrentBlockchainToken, mSelectedAccount.address,
to, Utils.toDecimalLamport(amount, mCurrentBlockchainToken.decimals),
mSendModel.sendSolanaToken(mSelectedNetwork.chainId, mCurrentBlockchainToken,
mSelectedAccount.address, to,
Utils.toDecimalLamport(amount, mCurrentBlockchainToken.decimals),
(success, txMetaId, errorMessage) -> {
// Do nothing here when success as we will receive an
// unapproved transaction in TxServiceObserver.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ public void setupDialog(@NonNull Dialog dialog, int style) {
associatedSplTokenInfo.setText(associatedSPLTokenAccountInfo);
}
mCoinType = TransactionUtils.getCoinFromTxDataUnion(mTxInfo.txDataUnion);
jsonRpcService.getNetwork(mCoinType, selectedNetwork -> {
jsonRpcService.getNetwork(mCoinType, null, selectedNetwork -> {
networkName.setText(selectedNetwork.chainName);
keyringService.getKeyringInfo(
AssetUtils.getKeyringForCoinType(mCoinType), keyringInfo -> {
Expand Down Expand Up @@ -471,7 +471,7 @@ private void rejectTransaction(boolean dismiss) {
if (txService == null) {
return;
}
txService.rejectTransaction(mCoinType, mTxInfo.id, success -> {
txService.rejectTransaction(mCoinType, mTxInfo.chainId, mTxInfo.id, success -> {
assert success : "tx is not rejected";
if (!success || !dismiss) {
return;
Expand All @@ -489,34 +489,36 @@ private void approveTransaction() {
if (txService == null) {
return;
}
txService.approveTransaction(mCoinType, mTxInfo.id, (success, error, errorMessage) -> {
if (!success) {
int providerError = -1;
switch (error.which()) {
case ProviderErrorUnion.Tag.ProviderError:
providerError = error.getProviderError();
break;
case ProviderErrorUnion.Tag.SolanaProviderError:
providerError = error.getSolanaProviderError();
break;
case ProviderErrorUnion.Tag.FilecoinProviderError:
providerError = error.getFilecoinProviderError();
break;
default:
assert false : "unknown error " + errorMessage;
}
assert success : "tx is not approved error: " + providerError + ", " + errorMessage;
Utils.warnWhenError(ApproveTxBottomSheetDialogFragment.TAG_FRAGMENT,
"approveTransaction", providerError, errorMessage);
return;
}
reportTransactionForP3A();
mApproved = true;
if (mTransactionConfirmationListener != null) {
mTransactionConfirmationListener.onApproveTransaction();
}
dismiss();
});
txService.approveTransaction(
mCoinType, mTxInfo.chainId, mTxInfo.id, (success, error, errorMessage) -> {
if (!success) {
int providerError = -1;
switch (error.which()) {
case ProviderErrorUnion.Tag.ProviderError:
providerError = error.getProviderError();
break;
case ProviderErrorUnion.Tag.SolanaProviderError:
providerError = error.getSolanaProviderError();
break;
case ProviderErrorUnion.Tag.FilecoinProviderError:
providerError = error.getFilecoinProviderError();
break;
default:
assert false : "unknown error " + errorMessage;
}
assert success : "tx is not approved error: " + providerError + ", "
+ errorMessage;
Utils.warnWhenError(ApproveTxBottomSheetDialogFragment.TAG_FRAGMENT,
"approveTransaction", providerError, errorMessage);
return;
}
reportTransactionForP3A();
mApproved = true;
if (mTransactionConfirmationListener != null) {
mTransactionConfirmationListener.onApproveTransaction();
}
dismiss();
});
}

private void reportTransactionForP3A() {
Expand All @@ -528,7 +530,7 @@ private void reportTransactionForP3A() {
BraveWalletP3a braveWalletP3A = getBraveWalletP3A();
assert jsonRpcService != null && braveWalletP3A != null;

jsonRpcService.getNetwork(mCoinType, selectedNetwork -> {
jsonRpcService.getNetwork(mCoinType, null, selectedNetwork -> {
boolean countTestNetworks = CommandLine.getInstance().hasSwitch(
BraveWalletConstants.P3A_COUNT_TEST_NETWORKS_SWITCH);
if (countTestNetworks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ public void onMaybeShowTrashButton(
JsonRpcService jsonRpcService = getJsonRpcService();
if (jsonRpcService == null) return;
jsonRpcService.getNetwork(
walletListItemModel.getBlockchainToken().coin, selectedNetwork -> {
walletListItemModel.getBlockchainToken().coin, null, selectedNetwork -> {
TokenUtils.isCustomToken(getBlockchainRegistry(), selectedNetwork,
selectedNetwork.coin, walletListItemModel.getBlockchainToken(),
isCustom -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
new Intent(requireActivity(), AdvanceTxSettingActivity.class);
String nonce = mParsedTx.getNonce();
toAdvanceTxSetting.putExtra(WalletConstants.ADVANCE_TX_SETTING_INTENT_TX_ID, mTxInfo.id)
.putExtra(
WalletConstants.ADVANCE_TX_SETTING_INTENT_TX_CHAIN_ID, mTxInfo.chainId)
.putExtra(WalletConstants.ADVANCE_TX_SETTING_INTENT_TX_NONCE, nonce);
startActivityForResult(toAdvanceTxSetting, START_ADVANCE_SETTING_ACTIVITY_CODE);
});
Expand Down Expand Up @@ -163,7 +165,7 @@ public void onClick(View v) {
radioGroup.setOnCheckedChangeListener((group, checkedId) -> {
EthTxManagerProxy ethTxManagerProxy = getEthTxManagerProxy();
assert ethTxManagerProxy != null;
ethTxManagerProxy.getGasEstimation1559(estimation -> {
ethTxManagerProxy.getGasEstimation1559(mTxInfo.chainId, estimation -> {
mTxInfo.txDataUnion.getEthTxData1559().gasEstimation = estimation;
mCheckedPriorityId = checkedId;
String maxPriorityFeePerGas = mParsedTx.getMaxPriorityFeePerGas();
Expand Down Expand Up @@ -272,7 +274,7 @@ public void onClick(View v) {
mTxInfo.txDataUnion.getEthTxData1559().baseData.gasPrice =
Utils.toHexWei(gasFeeEdit.getText().toString(), 9);
ethTxManagerProxy.setGasPriceAndLimitForUnapprovedTransaction(
mTxInfo.id,
mTxInfo.chainId, mTxInfo.id,
mTxInfo.txDataUnion.getEthTxData1559().baseData.gasPrice,
mTxInfo.txDataUnion.getEthTxData1559().baseData.gasLimit,
success -> {
Expand Down Expand Up @@ -324,8 +326,9 @@ public void onClick(View v) {
mTxInfo.txDataUnion.getEthTxData1559().maxPriorityFeePerGas =
maxPriorityFeePerGas;
mTxInfo.txDataUnion.getEthTxData1559().maxFeePerGas = maxFeePerGas;
ethTxManagerProxy.setGasFeeAndLimitForUnapprovedTransaction(mTxInfo.id,
maxPriorityFeePerGas, maxFeePerGas, gasLimit, success -> {
ethTxManagerProxy.setGasFeeAndLimitForUnapprovedTransaction(
mTxInfo.chainId, mTxInfo.id, maxPriorityFeePerGas, maxFeePerGas,
gasLimit, success -> {
if (!success) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ private void fillOriginInfo(OriginInfo originInfo) {
}

private void updateNetwork(@CoinType.EnumType int coin) {
getJsonRpcService().getNetwork(
coin, selectedNetwork -> { mNetworkName.setText(selectedNetwork.chainName); });
getJsonRpcService().getNetwork(coin, null,
selectedNetwork -> { mNetworkName.setText(selectedNetwork.chainName); });
}

private void initToken() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ private void updateAccount(String address) {
}

private void updateNetwork(@CoinType.EnumType int coin) {
getJsonRpcService().getNetwork(
coin, selectedNetwork -> { mNetworkName.setText(selectedNetwork.chainName); });
getJsonRpcService().getNetwork(coin, null,
selectedNetwork -> { mNetworkName.setText(selectedNetwork.chainName); });
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ private void getBalance(AccountInfo selectedAccount) {
return;
}
mBraveWalletPanelServices.getJsonRpcService().getNetwork(
selectedAccount.coin, selectedNetwork -> {
selectedAccount.coin, null, selectedNetwork -> {
BlockchainToken asset = Utils.makeNetworkAsset(selectedNetwork);
AssetsPricesHelper.fetchPrices(mBraveWalletPanelServices.getAssetRatioService(),
new BlockchainToken[] {asset}, assetPrices -> {
Expand Down
Loading