Skip to content

Commit

Permalink
fix: disabled transaction speed ui for Shibuya EVM (#971)
Browse files Browse the repository at this point in the history
  • Loading branch information
impelcrypto authored Oct 12, 2023
1 parent 8f351c4 commit 72a8392
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/components/assets/transfer/LocalTransfer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
</div>
</div>

<div class="separator" />
<div v-if="isEnableSpeedConfiguration" class="separator" />

<speed-configuration
v-if="isEnableSpeedConfiguration"
Expand Down
1 change: 1 addition & 0 deletions src/config/web3/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export {
getTokenExplorer,
fetchErc20TokenInfo,
getTokenDetails,
checkIsSetGasByWallet,
} from 'src/config/web3/utils';

export { contractInstance, Staking } from 'src/config/web3/contracts';
Expand Down
9 changes: 9 additions & 0 deletions src/config/web3/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,12 @@ export const fetchErc20TokenInfo = async ({
return null;
}
};

export const checkIsSetGasByWallet = (chainId: EVM): boolean => {
switch (chainId) {
case EVM.SHIBUYA_TESTNET:
return true;
default:
return false;
}
};
8 changes: 3 additions & 5 deletions src/hooks/transfer/useTokenTransfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ export function useTokenTransfer(selectedToken: Ref<Asset>) {
const route = useRoute();
const router = useRouter();

const { nativeTokenSymbol, evmNetworkIdx, isSupportXvmTransfer, currentNetworkName } =
useNetworkInfo();
const { nativeTokenSymbol, evmNetworkIdx, isSupportXvmTransfer } = useNetworkInfo();
const isH160 = computed<boolean>(() => store.getters['general/isH160Formatted']);
const tokenSymbol = computed<string>(() => route.query.token as string);
const isLoading = computed<boolean>(() => store.getters['general/isLoading']);
Expand Down Expand Up @@ -127,13 +126,13 @@ export function useTokenTransfer(selectedToken: Ref<Asset>) {
if (isLoading.value) return;
const transferAmtRef = Number(transferAmt.value);
try {
if (transferAmtRef > fromAddressBalance.value) {
if (transferAmtRef && transferAmtRef > fromAddressBalance.value) {
errMsg.value = t('warning.insufficientBalance', {
token: selectedToken.value.metadata.symbol,
});
} else if (toAddress.value && !isValidDestAddress.value) {
errMsg.value = 'warning.inputtedInvalidDestAddress';
} else if (!transferableBalance.value && !isH160.value) {
} else if (transferAmtRef && !transferableBalance.value && !isH160.value) {
errMsg.value = t('warning.insufficientBalance', {
token: nativeTokenSymbol.value,
});
Expand Down Expand Up @@ -190,7 +189,6 @@ export function useTokenTransfer(selectedToken: Ref<Asset>) {
decimals,
finalizedCallback,
successMessage,
network: currentNetworkName.value.toLowerCase(),
});
} else {
const receivingAddress = isValidEvmAddress(toAddress)
Expand Down
13 changes: 11 additions & 2 deletions src/hooks/useGasPrice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export const useGasPrice = (isFetch = false) => {
const network = computed<string>(() => {
return isMainnet ? currentNetworkName.value.toLowerCase() : 'shibuya';
});
const isH160 = computed<boolean>(() => store.getters['general/isH160Formatted']);
const isShibuyaEvm = computed<boolean>(() => isH160.value && network.value === 'shibuya');

const setSelectedGas = (speed: Speed): void => {
selectedGas.value = {
Expand Down Expand Up @@ -86,14 +88,21 @@ export const useGasPrice = (isFetch = false) => {
return (
currentWallet !== SupportWallet.TalismanEvm &&
currentWallet !== SupportWallet.SubWalletEvm &&
currentWallet !== SupportWallet.OneKeyEvm
currentWallet !== SupportWallet.OneKeyEvm &&
!isShibuyaEvm.value
);
});

watch(
[network, $web3],
async () => {
if (isFetch && network.value && !gas.value && $web3.value) {
if (
isFetch &&
network.value &&
!gas.value &&
$web3.value &&
isEnableSpeedConfiguration.value
) {
// console.info('gas price', network.value, gas.value);
await dispatchGasPrice(network.value);
}
Expand Down
15 changes: 2 additions & 13 deletions src/v2/repositories/implementations/AssetsRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { IAssetsRepository } from './../IAssetsRepository';
import { BN } from '@polkadot/util';
import { TransactionConfig } from 'web3-eth';
import Web3 from 'web3';
import { buildEvmAddress, getEvmGas } from '@astar-network/astar-sdk-core';
import { buildEvmAddress } from '@astar-network/astar-sdk-core';
import { AbiItem } from 'web3-utils';
import ERC20_ABI from 'src/config/abi/ERC20.json';
import { IGasPriceProvider } from 'src/v2/services';
Expand Down Expand Up @@ -43,18 +43,9 @@ export class AssetsRepository implements IAssetsRepository {
param: ParamEvmTransfer;
web3: Web3;
}): Promise<TransactionConfig> {
const { contractAddress, senderAddress, toAddress, amount, decimals, network } = param;
// memo: don't use gas station for Shibuya, because it gives wrong gas price.
const gas = network === 'shibuya' ? '0' : this.gasPriceProvider.getGas().price;
const [nonce, gasPrice] = await Promise.all([
web3.eth.getTransactionCount(senderAddress),
// if gas === 0 the method will use gas value from RPC endpoint.
getEvmGas(web3, gas),
]);
const { contractAddress, senderAddress, toAddress, amount, decimals } = param;
if (contractAddress === astarNativeTokenErcAddr) {
return {
nonce,
gasPrice: web3.utils.toHex(gasPrice),
from: senderAddress,
to: toAddress,
value: web3.utils.toWei(String(amount), 'ether'),
Expand All @@ -63,8 +54,6 @@ export class AssetsRepository implements IAssetsRepository {
const contract = new web3.eth.Contract(ERC20_ABI as AbiItem[], contractAddress);
const amt = ethers.utils.parseUnits(String(amount), decimals).toString();
return {
nonce,
gasPrice: web3.utils.toHex(gasPrice),
from: senderAddress,
to: contractAddress,
value: '0x0',
Expand Down
1 change: 0 additions & 1 deletion src/v2/services/IAssetsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export interface ParamEvmTransfer {
contractAddress: string;
decimals: number;
successMessage: string;
network: string;
finalizedCallback: (hash: string) => void;
}

Expand Down
11 changes: 8 additions & 3 deletions src/v2/services/implementations/MetamaskWalletService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
import { WalletService } from 'src/v2/services/implementations';
import { Symbols } from 'src/v2/symbols';
import Web3 from 'web3';
import { checkIsSetGasByWallet } from 'src/config/web3';

@injectable()
export class MetamaskWalletService extends WalletService implements IWalletService {
Expand Down Expand Up @@ -127,17 +128,21 @@ export class MetamaskWalletService extends WalletService implements IWalletServi
web3.eth.getTransactionCount(from),
getEvmGas(web3, this.gasPriceProvider.getGas().price),
]);

const rawTx = {
nonce,
gasPrice: web3.utils.toHex(gasPrice),
from,
to,
value: value ? value : '0x0',
data,
};
const estimatedGas = await web3.eth.estimateGas(rawTx);

const connectedChainId = await web3.eth.net.getId();
const isSetGasByWallet = checkIsSetGasByWallet(connectedChainId);
const txParam = isSetGasByWallet ? rawTx : { ...rawTx, gasPrice: web3.utils.toHex(gasPrice) };
const estimatedGas = await web3.eth.estimateGas(txParam);
const transactionHash = await web3.eth
.sendTransaction({ ...rawTx, gas: estimatedGas })
.sendTransaction({ ...txParam, gas: estimatedGas })
.once('transactionHash', (transactionHash) => {
this.eventAggregator.publish(new BusyMessage(true));
})
Expand Down

0 comments on commit 72a8392

Please sign in to comment.