Skip to content

Commit

Permalink
UA logic integration (#943)
Browse files Browse the repository at this point in the history
* Basic integration implemented

* Disable submit button on action
  • Loading branch information
bobo-k2 committed Oct 2, 2023
1 parent d1e5375 commit f4eed50
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 59 deletions.
42 changes: 1 addition & 41 deletions src/components/assets/Assets.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
<div class="separator" />
</div>
<div class="container--assets">
<astar-button @click="unify()">Unify accounts</astar-button>
<astar-button @click="setIdentity()">Set setIdentity</astar-button>
<div class="container--account">
<div class="title--account">
<span class="text--xl">
Expand Down Expand Up @@ -55,12 +53,8 @@ import { isValidEvmAddress } from '@astar-network/astar-sdk-core';
import { useAccount, useBalance, useDispatchGetDapps, useNetworkInfo } from 'src/hooks';
import { useStore } from 'src/store';
import { EvmAssets, XcmAssets, XvmAssets } from 'src/store/assets/state';
import { Asset, IdentityData } from 'src/v2/models';
import { Asset } from 'src/v2/models';
import { computed, defineComponent, ref, watch, watchEffect, onUnmounted } from 'vue';
import { container } from 'src/v2/common';
import { IAccountUnificationService, IIdentityService } from 'src/v2/services';
import { Symbols } from 'src/v2/symbols';
import { INftRepository } from 'src/v2/repositories';
export default defineComponent({
components: {
Expand Down Expand Up @@ -174,38 +168,6 @@ export default defineComponent({
window.removeEventListener(event, handler);
});
// Temp account unification test, TODO remove later
const unify = async () => {
const nativeAddress = 'XmSTidw9qbJJdC4ntotpzwCkR7iAgkMUnLv6rg29Qa3aoQa';
const evmAddress = '0x68F6F226c5D0C8124b62b98Ac797dD6208bAFE90';
const service = container.get<IAccountUnificationService>(Symbols.AccountUnificationService);
await service.unifyAccounts(nativeAddress, evmAddress, 'Bobo AU');
console.log('mapped native', await service.getMappedNativeAddress(evmAddress));
console.log('mapped evm', await service.getMappedEvmAddress(nativeAddress));
};
// Temp set identity test, TODO remove later
const setIdentity = async () => {
const tokenOwnerAddress = '0xe42A2ADF3BEe1c195f4D72410421ad7908388A6a';
const nftRepository = container.get<INftRepository>(Symbols.NftRepository);
const nfts = await nftRepository.getNftMetadata(
'astar',
'0x7b2152e51130439374672af463b735a59a47ea85',
'8893zß'
);
console.log(nfts);
const nativeAddress = 'XmSTidw9qbJJdC4ntotpzwCkR7iAgkMUnLv6rg29Qa3aoQa';
const service = container.get<IIdentityService>(Symbols.IdentityService);
await service.setIdentity(
nativeAddress,
new IdentityData('aaa', [
{ key: 'ContractAddress', value: '0x18F6F226c5D0C8124b62b98Ac797dD6208bAFE90' },
{ key: 'TokenId', value: '2000' },
])
);
};
return {
evmAssets,
isLoadingXcmAssetsAmount,
Expand All @@ -219,8 +181,6 @@ export default defineComponent({
accountData,
isModalXcmBridge,
isLoading,
unify,
setIdentity,
};
},
});
Expand Down
22 changes: 20 additions & 2 deletions src/components/header/modals/ModalAccountUnification.vue
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<step5
:account-name="accountName"
:selected-evm-address="selectedEvmAddress"
:is-busy="isLoading"
@next="updateSteps(6)"
/>
</div>
Expand All @@ -54,7 +55,7 @@
<script lang="ts">
import { useStore } from 'src/store';
import { wait } from '@astar-network/astar-sdk-core';
import { useAccountUnification, useBreakpoints } from 'src/hooks';
import { useAccount, useAccountUnification, useBreakpoints } from 'src/hooks';
import { computed, defineComponent, onUnmounted, ref } from 'vue';
import { useI18n } from 'vue-i18n';
import UserAccount from 'src/components/header/modals/account-unification/UserAccount.vue';
Expand Down Expand Up @@ -92,6 +93,8 @@ export default defineComponent({
const isSelected = ref<boolean>(false);
const isClosing = ref<boolean>(false);
const currentStep = ref<number>(0);
const { currentAccount } = useAccount();
const isLoading = computed<boolean>(() => store.getters['general/isLoading']);
const {
selectedEvmAddress,
Expand All @@ -103,6 +106,7 @@ export default defineComponent({
accountName,
setAccountName,
setWeb3,
unifyAccounts,
} = useAccountUnification();
const closeModal = async (): Promise<void> => {
Expand Down Expand Up @@ -133,7 +137,20 @@ export default defineComponent({
window.removeEventListener('resize', onHeightChange);
});
const updateSteps = (step: number): void => {
const updateSteps = async (step: number): Promise<void> => {
if (step === 6) {
// Make a call to unify accounts
const success = await unifyAccounts(
currentAccount.value,
selectedEvmAddress.value,
accountName.value
);
if (!success) {
return;
}
}
currentStep.value = step;
};
Expand Down Expand Up @@ -178,6 +195,7 @@ export default defineComponent({
isFetchingXc20Tokens,
isLoadingDappStaking,
accountName,
isLoading,
closeModal,
backModal,
updateSteps,
Expand Down
6 changes: 5 additions & 1 deletion src/components/header/modals/account-unification/Step5.vue
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

<!-- Action -->
<div>
<astar-button class="btn" @click="next()">
<astar-button :disabled="isBusy" class="btn" @click="next()">
{{ $t('dappStaking.modals.submit') }}
</astar-button>
</div>
Expand All @@ -55,6 +55,10 @@ export default defineComponent({
type: String,
required: true,
},
isBusy: {
type: Boolean,
required: true,
},
},
emits: ['next'],
setup(props, { emit }) {
Expand Down
15 changes: 14 additions & 1 deletion src/hooks/wallet/useAccountUnification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
import { MyStakeInfo, useCurrentEra } from 'src/hooks';
import { container } from 'src/v2/common';
import { DappCombinedInfo } from 'src/v2/models/DappsStaking';
import { IDappStakingService } from 'src/v2/services';
import { IAccountUnificationService, IDappStakingService } from 'src/v2/services';
import { Symbols } from 'src/v2/symbols';
import { BN } from '@polkadot/util';
import ABI from 'src/config/abi/ERC20.json';
Expand Down Expand Up @@ -260,6 +260,18 @@ export const useAccountUnification = () => {
}
};

const unifyAccounts = async (
nativeAddress: string,
evmAddress: string,
accountName: string
): Promise<boolean> => {
const unificationService = container.get<IAccountUnificationService>(
Symbols.AccountUnificationService
);

return unificationService.unifyAccounts(nativeAddress, evmAddress, accountName);
};

watch([currentAccount], setWeb3);
watch([web3], updateEvmProvider);
watch([selectedEvmAddress, dapps, era], checkStakerInfo);
Expand All @@ -276,5 +288,6 @@ export const useAccountUnification = () => {
accountName,
setAccountName,
setWeb3,
unifyAccounts,
};
};
2 changes: 1 addition & 1 deletion src/v2/services/IAccountUnificationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export interface IAccountUnificationService {
accountName: string,
avatarNftAddress?: string,
avatarNftId?: string
): Promise<void>;
): Promise<boolean>;
getMappedNativeAddress(evmAddress: string): Promise<string>;
getMappedEvmAddress(nativeAddress: string): Promise<string>;
}
40 changes: 28 additions & 12 deletions src/v2/services/implementations/AccountUnificationService.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import { injectable, inject } from 'inversify';
import { Symbols } from 'src/v2/symbols';
import { IWalletService, WalletType, IAccountUnificationService } from 'src/v2/services';
import { IAccountUnificationRepository, ISystemRepository } from 'src/v2/repositories';
import {
IWalletService,
WalletType,
IAccountUnificationService,
IGasPriceProvider,
} from 'src/v2/services';
import {
IAccountUnificationRepository,
IEthCallRepository,
ISystemRepository,
} from 'src/v2/repositories';
import { decodeAddress } from '@polkadot/util-crypto';
import { ExtrinsicStatusMessage, IEventAggregator } from 'src/v2/messaging';
import { Guard } from 'src/v2/common';
import { IdentityData } from 'src/v2/models';
import { MetamaskWalletService } from './MetamaskWalletService';

@injectable()
export class AccountUnificationService implements IAccountUnificationService {
Expand All @@ -15,7 +25,9 @@ export class AccountUnificationService implements IAccountUnificationService {
@inject(Symbols.SystemRepository) private systemRepo: ISystemRepository,
@inject(Symbols.AccountUnificationRepository)
private unificationRepo: IAccountUnificationRepository,
@inject(Symbols.EventAggregator) private eventAggregator: IEventAggregator
@inject(Symbols.EventAggregator) private eventAggregator: IEventAggregator,
@inject(Symbols.EthCallRepository) private ethCallRepository: IEthCallRepository,
@inject(Symbols.GasPriceProvider) private gasPriceProvider: IGasPriceProvider
) {}

public async unifyAccounts(
Expand All @@ -24,7 +36,7 @@ export class AccountUnificationService implements IAccountUnificationService {
accountName: string,
avatarNftAddress?: string,
avatarNftId?: string
): Promise<void> {
): Promise<boolean> {
try {
const chainId = await this.systemRepo.getChainId();
const genesisHash = await this.systemRepo.getBlockHash(0);
Expand All @@ -44,7 +56,15 @@ export class AccountUnificationService implements IAccountUnificationService {
};

// Sign payload with EVM account.
const evmWallet = this.walletFactory(WalletType.Metamask);
// TODO provide wallet name as the method parameter.
const evmWallet = new MetamaskWalletService(
this.systemRepo,
this.ethCallRepository,
this.eventAggregator,
'metamask',
this.gasPriceProvider
);
// const evmWallet = this.walletFactory(WalletType.Metamask);
const signedPayload = await evmWallet.signPayload(domain, types, value);

// Claim account with polkadot wallet.
Expand All @@ -60,14 +80,10 @@ export class AccountUnificationService implements IAccountUnificationService {
extrinsic: transaction,
senderAddress: nativeAddress,
});

return true;
} catch (error) {
const e = error as Error;
this.eventAggregator.publish(
new ExtrinsicStatusMessage({
success: false,
message: e.toString(),
})
);
return false;
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/v2/services/implementations/PolkadotWalletService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class PolkadotWalletService extends WalletService implements IWalletServi

let result: string | null = null;
try {
return new Promise<string>(async (resolve) => {
return new Promise<string>(async (resolve, reject) => {
!isMobileDevice && this.detectExtensionsAction(true);
await this.checkExtension();
let tip = transactionTip?.toString();
Expand Down Expand Up @@ -139,6 +139,7 @@ export class PolkadotWalletService extends WalletService implements IWalletServi
} catch (error) {
this.eventAggregator.publish(new BusyMessage(false));
unsub();
reject(error as Error);
}
}
);
Expand Down

0 comments on commit f4eed50

Please sign in to comment.