Skip to content

Commit

Permalink
fix: modify the way query the mapped address (#1018)
Browse files Browse the repository at this point in the history
* fix: rollback getMappedNativeAddress and getMappedEvmAddress function

* fix: select correct wallet after switching wallet from EVM to Native

* Unified account select display fix

---------

Co-authored-by: Bobo <[email protected]>
  • Loading branch information
impelcrypto and bobo-k2 authored Nov 9, 2023
1 parent d21f9eb commit 1f421c5
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/components/header/modals/ModalAccount.vue
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ export default defineComponent({
const wallet = substrateAccounts.value.find((it) => it.address === substrateAccount);
wallet && localStorage.setItem(LOCAL_STORAGE.SELECTED_WALLET, wallet.source);
}
store.commit('general/setCurrentWallet', props.selectedWallet);
isSelected.value = true;
isClosing.value = false;
localStorage.removeItem(LOCAL_STORAGE.MULTISIG);
Expand Down
8 changes: 4 additions & 4 deletions src/hooks/transfer/useTokenTransfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ export function useTokenTransfer(selectedToken: Ref<Asset>) {
if (isH160.value) {
const receivingAddress = isValidEvmAddress(toAddress)
? toAddress
: await accountUnificationService.getMappedEvmAddress(toAddress);
: await accountUnificationService.getConvertedEvmAddress(toAddress);
const successMessage = t('assets.toast.completedMessage', {
symbol,
transferAmt,
Expand All @@ -211,7 +211,7 @@ export function useTokenTransfer(selectedToken: Ref<Asset>) {
});
} else {
const receivingAddress = isValidEvmAddress(toAddress)
? await accountUnificationService.getMappedNativeAddress(toAddress)
? await accountUnificationService.getConvertedNativeAddress(toAddress)
: toAddress;
const successMessage = t('assets.toast.completedMessage', {
symbol,
Expand Down Expand Up @@ -264,15 +264,15 @@ export function useTokenTransfer(selectedToken: Ref<Asset>) {

const isSendToH160 = isValidEvmAddress(toAddress.value);
const destAddress = isSendToH160
? await accountUnificationService.getMappedNativeAddress(toAddress.value)
? await accountUnificationService.getConvertedNativeAddress(toAddress.value)
: toAddress.value;
const srcChainId = evmNetworkIdx.value;

if (isTransferNativeToken.value && !isZkEvm.value) {
toAddressBalance.value = await getNativeTokenBalance(destAddress);
} else if (isH160.value) {
const address = isValidAddressPolkadotAddress(toAddress.value)
? await accountUnificationService.getMappedEvmAddress(toAddress.value)
? await accountUnificationService.getConvertedEvmAddress(toAddress.value)
: toAddress.value;

const balance = await getTokenBal({
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/wallet/useAccountUnification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export const useAccountUnification = () => {
let isPendingWithdrawal = false;
let stakingData: MyStakeInfo[] = [];

const mappedSS58Address = await accountUnificationService.getMappedNativeAddress(
const mappedSS58Address = await accountUnificationService.getConvertedNativeAddress(
selectedEvmAddress.value
);
const dappStakingService = container.get<IDappStakingService>(Symbols.DappStakingService);
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/xcm/useXcmBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ export function useXcmBridge(selectedToken: Ref<Asset>) {
// if: SS58 Deposit
const isSendToH160 = isValidEvmAddress(address);
const destAddress = isSendToH160
? await accountUnificationService.getMappedNativeAddress(address)
? await accountUnificationService.getConvertedNativeAddress(address)
: address;
if (isAstarNativeTransfer.value) {
const accountInfo = await $api?.query.system.account<SystemAccount>(address);
Expand Down
16 changes: 14 additions & 2 deletions src/v2/repositories/IAccountUnificationRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,29 @@ export interface IAccountUnificationRepository {
getClaimEvmAccountCall(evmAddress: string, signature: string): Promise<ExtrinsicPayload>;

/**
* Gets mapped native address for the given EVM address.
* Queries mapped native address for the given EVM address in runtime.
* @param evmAddress EVM address to get mapped native address.
*/
getMappedNativeAddress(evmAddress: string): Promise<string>;

/**
* Gets mapped EVM address for the given native address.
* Queries mapped EVM address for the given native address in runtime.
* @param nativeAddress Native address to get mapped EVM address.
*/
getMappedEvmAddress(nativeAddress: string): Promise<string>;

/**
* Gets mapped native address for the given EVM address.
* @param evmAddress EVM address to get mapped native address.
*/
getConvertedNativeAddress(evmAddress: string): Promise<string>;

/**
* Gets mapped EVM address for the given native address.
* @param nativeAddress Native address to get mapped EVM address.
*/
getConvertedEvmAddress(nativeAddress: string): Promise<string>;

/**
* check if the given address is unified account.
* @param address H160 or SS58 address.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ export class AccountUnificationRepository implements IAccountUnificationReposito
const api = await this.api.getApi();
const nativeAddress = api.query.hasOwnProperty('unifiedAccounts')
? await api.query.unifiedAccounts.evmToNative<AccountId32>(evmAddress)
: toSS58Address(evmAddress);
: '';

return nativeAddress.toString() !== '' ? nativeAddress.toString() : toSS58Address(evmAddress);
return nativeAddress.toString();
}

public async getMappedEvmAddress(nativeAddress: string): Promise<string> {
Expand All @@ -44,9 +44,21 @@ export class AccountUnificationRepository implements IAccountUnificationReposito
const api = await this.api.getApi();
const evmAddress = api.query.hasOwnProperty('unifiedAccounts')
? await api.query.unifiedAccounts.nativeToEvm<H160>(nativeAddress)
: buildEvmAddress(nativeAddress);
: '';

return evmAddress.toString() !== '' ? evmAddress.toString() : buildEvmAddress(nativeAddress);
return evmAddress.toString();
}

public async getConvertedNativeAddress(evmAddress: string): Promise<string> {
Guard.ThrowIfUndefined('evmAddress', evmAddress);
const nativeAddress = await this.getMappedNativeAddress(evmAddress);
return nativeAddress !== '' ? nativeAddress : toSS58Address(evmAddress);
}

public async getConvertedEvmAddress(nativeAddress: string): Promise<string> {
Guard.ThrowIfUndefined('nativeAddress', nativeAddress);
const evmAddress = await this.getMappedEvmAddress(nativeAddress);
return evmAddress !== '' ? evmAddress : buildEvmAddress(nativeAddress);
}

public async handleCheckIsUnifiedAccount(address: string): Promise<boolean> {
Expand Down
2 changes: 2 additions & 0 deletions src/v2/services/IAccountUnificationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ export interface IAccountUnificationService {
): Promise<boolean>;
getMappedNativeAddress(evmAddress: string): Promise<string>;
getMappedEvmAddress(nativeAddress: string): Promise<string>;
getConvertedNativeAddress(evmAddress: string): Promise<string>;
getConvertedEvmAddress(nativeAddress: string): Promise<string>;
checkIsUnifiedAccount(address: string): Promise<boolean>;
}
16 changes: 16 additions & 0 deletions src/v2/services/implementations/AccountUnificationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,22 @@ export class AccountUnificationService implements IAccountUnificationService {

return await this.unificationRepo.getMappedEvmAddress(nativeAddress);
}

// Memo: return mapped native address for evm address
// 1. query in API
// 2. convert the address by SDK in case the account hasn't been unified
public async getConvertedNativeAddress(evmAddress: string): Promise<string> {
Guard.ThrowIfUndefined('evmAddress', evmAddress);

return await this.unificationRepo.getConvertedNativeAddress(evmAddress);
}

// Memo: return mapped EVM address for native address
public async getConvertedEvmAddress(nativeAddress: string): Promise<string> {
Guard.ThrowIfUndefined('nativeAddress', nativeAddress);

return await this.unificationRepo.getConvertedEvmAddress(nativeAddress);
}
public async checkIsUnifiedAccount(address: string): Promise<boolean> {
Guard.ThrowIfUndefined('address', address);

Expand Down

0 comments on commit 1f421c5

Please sign in to comment.