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

Bridge balances refactoring #1100

Merged
merged 4 commits into from
Jul 14, 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
12 changes: 10 additions & 2 deletions src/components/SoraCard/SoraCardIntroPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@

<script lang="ts">
import { FPNumber } from '@sora-substrate/math';
import { XOR } from '@sora-substrate/util/build/assets/consts';
import { mixins } from '@soramitsu/soraneo-wallet-web';
import { Component, Mixins, Watch } from 'vue-property-decorator';
import { Component, Mixins } from 'vue-property-decorator';

import TranslationMixin from '@/components/mixins/TranslationMixin';
import { PageNames, Components } from '@/consts';
Expand Down Expand Up @@ -146,7 +147,14 @@ export default class SoraCardIntroPage extends Mixins(mixins.LoadingMixin, Trans

private bridgeTokens(): void {
if (!this.isEuroBalanceEnough) {
router.push({ name: PageNames.Bridge, params: { xorToDeposit: this.xorToDeposit.toString() } });
router.push({
name: PageNames.Bridge,
params: {
address: XOR.address,
amount: this.xorToDeposit.toString(),
isIncoming: 'true',
},
});
}
}

Expand Down
16 changes: 7 additions & 9 deletions src/components/mixins/SelectAssetMixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { getter } from '@/store/decorators';

import type { Asset, AccountAsset, RegisteredAccountAsset } from '@sora-substrate/util/build/assets/types';

const isEmpty = (a): boolean => isNil(a.balance) || !+a.balance.transferable;

@Component
export default class SelectAsset extends Mixins(mixins.DialogMixin, AssetsSearchMixin) {
@getter.assets.assetDataByAddress private getAsset!: (addr?: string) => Nullable<RegisteredAccountAsset>;
Expand All @@ -19,17 +21,13 @@ export default class SelectAsset extends Mixins(mixins.DialogMixin, AssetsSearch
this.clearAndFocusSearch();
}

public sortByBalance(external = false) {
const isEmpty = (a): boolean => (external ? !+a.externalBalance : isNil(a.balance) || !+a.balance.transferable);

return (a: AccountAsset | RegisteredAccountAsset, b: AccountAsset | RegisteredAccountAsset): number => {
const emptyABalance = isEmpty(a);
const emptyBBalance = isEmpty(b);
public sortByBalance(a: AccountAsset | RegisteredAccountAsset, b: AccountAsset | RegisteredAccountAsset): number {
const emptyABalance = isEmpty(a);
const emptyBBalance = isEmpty(b);

if (emptyABalance === emptyBBalance) return 0;
if (emptyABalance === emptyBBalance) return 0;

return emptyABalance && !emptyBBalance ? 1 : -1;
};
return emptyABalance && !emptyBBalance ? 1 : -1;
}

public getAssetsWithBalances(addresses: string[], excludeAddress = ''): Array<RegisteredAccountAsset> {
Expand Down
2 changes: 1 addition & 1 deletion src/components/pages/Bridge/SelectAsset.vue
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export default class BridgeSelectAsset extends Mixins(TranslationMixin, SelectAs
const assetsAddresses = Object.keys(this.registeredAssets);
const excludeAddress = this.asset?.address;

return this.getAssetsWithBalances(assetsAddresses, excludeAddress).sort(this.sortByBalance(!this.isSoraToEvm));
return this.getAssetsWithBalances(assetsAddresses, excludeAddress).sort(this.sortByBalance);
}

get filteredAssets(): Array<RegisteredAccountAsset> {
Expand Down
4 changes: 2 additions & 2 deletions src/components/shared/SelectAsset/SelectToken.vue
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export default class SelectToken extends Mixins(TranslationMixin, SelectAssetMix
const assetsAddresses = whiteList.map((asset) => asset.address);
const excludeAddress = this.asset?.address;

return this.getAssetsWithBalances(assetsAddresses, excludeAddress).sort(this.sortByBalance());
return this.getAssetsWithBalances(assetsAddresses, excludeAddress).sort(this.sortByBalance);
}

get filteredWhitelistTokens(): Array<AccountAsset> {
Expand Down Expand Up @@ -196,7 +196,7 @@ export default class SelectToken extends Mixins(TranslationMixin, SelectAssetMix
// TODO: we already have balances in nonWhitelistAccountAssets.
// Need to improve that logic
return this.getAssetsWithBalances(Object.keys(this.nonWhitelistAccountAssets), excludeAsset?.address).sort(
this.sortByBalance()
this.sortByBalance
);
}

Expand Down
88 changes: 52 additions & 36 deletions src/store/bridge/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,28 +181,35 @@ async function updateEvmBalances(context: ActionContext<any, any>): Promise<void
const { sender, recipient, asset } = getters;
const { isSoraToEvm } = state;

let recipientBalance = ZeroStringValue;
let senderBalance = ZeroStringValue;
let nativeBalance = ZeroStringValue;

if (asset?.address) {
if (sender) {
senderBalance = isSoraToEvm
? (await getAssetBalance(api.api, sender, asset.address, asset.decimals)).transferable
: (await ethersUtil.getAccountAssetBalance(sender, asset?.externalAddress)).value;
}
if (recipient) {
recipientBalance = isSoraToEvm
? (await ethersUtil.getAccountAssetBalance(recipient, asset?.externalAddress)).value
: (await getAssetBalance(api.api, recipient, asset.address, asset.decimals)).transferable;
}
const spender = isSoraToEvm ? recipient : sender;

const spender = isSoraToEvm ? recipient : sender;
const getSenderBalance = async () => {
if (!(asset?.address && sender)) return ZeroStringValue;

if (spender) {
nativeBalance = await ethersUtil.getAccountBalance(isSoraToEvm ? recipient : sender);
}
}
return isSoraToEvm
? (await getAssetBalance(api.api, sender, asset.address, asset.decimals)).transferable
: (await ethersUtil.getAccountAssetBalance(sender, asset?.externalAddress)).value;
};

const getRecipientBalance = async () => {
if (!(asset?.address && recipient)) return ZeroStringValue;

return isSoraToEvm
? (await ethersUtil.getAccountAssetBalance(recipient, asset?.externalAddress)).value
: (await getAssetBalance(api.api, recipient, asset.address, asset.decimals)).transferable;
};

const getSpenderBalance = async () => {
if (!(asset?.address && spender)) return ZeroStringValue;

return await ethersUtil.getAccountBalance(spender);
};

const [senderBalance, recipientBalance, nativeBalance] = await Promise.all([
getSenderBalance(),
getRecipientBalance(),
getSpenderBalance(),
]);

commit.setAssetSenderBalance(senderBalance);
commit.setAssetRecipientBalance(recipientBalance);
Expand All @@ -214,24 +221,33 @@ async function updateSubBalances(context: ActionContext<any, any>): Promise<void
const { sender, recipient, asset } = getters;
const { isSoraToEvm } = state;

let recipientBalance = ZeroStringValue;
let senderBalance = ZeroStringValue;
let nativeBalance = ZeroStringValue;
const getSenderBalance = async () => {
if (!(asset?.address && sender)) return ZeroStringValue;

if (asset?.address) {
if (sender) {
senderBalance = isSoraToEvm
? (await getAssetBalance(api.api, sender, asset.address, asset.decimals)).transferable
: await subConnector.adapter.getTokenBalance(sender, asset?.externalAddress);
return isSoraToEvm
? (await getAssetBalance(api.api, sender, asset.address, asset.decimals)).transferable
: await subConnector.adapter.getTokenBalance(sender, asset?.externalAddress);
};

nativeBalance = await subConnector.adapter.getTokenBalance(sender);
}
if (recipient) {
recipientBalance = isSoraToEvm
? await subConnector.adapter.getTokenBalance(recipient, asset?.externalAddress)
: (await getAssetBalance(api.api, recipient, asset.address, asset.decimals)).transferable;
}
}
const getRecipientBalance = async () => {
if (!(asset?.address && recipient)) return ZeroStringValue;

return isSoraToEvm
? await subConnector.adapter.getTokenBalance(recipient, asset?.externalAddress)
: (await getAssetBalance(api.api, recipient, asset.address, asset.decimals)).transferable;
};

const getSpenderBalance = async () => {
if (!(asset?.address && sender)) return ZeroStringValue;

return await subConnector.adapter.getTokenBalance(sender);
};

const [senderBalance, recipientBalance, nativeBalance] = await Promise.all([
getSenderBalance(),
getRecipientBalance(),
getSpenderBalance(),
]);

commit.setAssetSenderBalance(senderBalance);
commit.setAssetRecipientBalance(recipientBalance);
Expand Down
6 changes: 3 additions & 3 deletions src/store/bridge/mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ const mutations = defineMutations<BridgeState>()({
state.assetAddress = address || '';
},

setAssetSenderBalance(state, balance: CodecString = ZeroStringValue): void {
setAssetSenderBalance(state, balance: Nullable<CodecString> = null): void {
state.assetSenderBalance = balance;
},

setAssetRecipientBalance(state, balance: CodecString = ZeroStringValue): void {
setAssetRecipientBalance(state, balance: Nullable<CodecString> = null): void {
state.assetRecipientBalance = balance;
},

Expand All @@ -30,7 +30,7 @@ const mutations = defineMutations<BridgeState>()({
state.assetLockedBalanceFetching = flag;
},

setExternalBalance(state, balance: CodecString = ZeroStringValue): void {
setExternalBalance(state, balance: Nullable<CodecString> = null): void {
state.externalNativeBalance = balance;
},
setExternalBalancesFetching(state, flag: boolean): void {
Expand Down
4 changes: 2 additions & 2 deletions src/store/bridge/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ function initialState(): BridgeState {
return {
isSoraToEvm: true,
assetAddress: '',
assetSenderBalance: ZeroStringValue, // balance for sora
assetRecipientBalance: ZeroStringValue, // balance for bridge network
assetSenderBalance: null, // balance for sora
assetRecipientBalance: null, // balance for bridge network
assetLockedBalance: null, // asset balance locked on bridge
assetLockedBalanceFetching: false,
amount: '',
Expand Down
6 changes: 3 additions & 3 deletions src/store/bridge/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import type { CodecString, IBridgeTransaction } from '@sora-substrate/util';
export type BridgeState = {
isSoraToEvm: boolean;
assetAddress: string;
assetSenderBalance: CodecString;
assetRecipientBalance: CodecString;
assetSenderBalance: Nullable<CodecString>;
assetRecipientBalance: Nullable<CodecString>;
assetLockedBalance: Nullable<CodecString>;
assetLockedBalanceFetching: boolean;
amount: string;
externalNetworkFee: CodecString;
externalNetworkFeeFetching: boolean;
externalNativeBalance: CodecString;
externalNativeBalance: Nullable<CodecString>;
externalBalancesFetching: boolean;
externalBlockNumber: number;
// history sources (unsynced localstorage & network)
Expand Down
11 changes: 6 additions & 5 deletions src/views/Bridge.vue
Original file line number Diff line number Diff line change
Expand Up @@ -543,13 +543,14 @@ export default class Bridge extends Mixins(
}

async created(): Promise<void> {
if (this.$route.params.xorToDeposit) {
await this.selectAsset(this.xor);
const { address, amount, isIncoming } = this.$route.params;
if (address) {
this.setAssetAddress(address);
}
if (isIncoming) {
this.setSoraToEvm(false);
this.setAmount(this.$route.params.xorToDeposit);
} else {
this.setAmount();
}
this.setAmount(amount);
}

getBridgeItemTitle(isSoraNetwork = false): string {
Expand Down
4 changes: 1 addition & 3 deletions src/views/Wallet.vue
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export default class Wallet extends Mixins(TranslationMixin) {

@action.swap.setTokenFromAddress private setSwapFromAsset!: (address?: string) => Promise<void>;
@action.swap.setTokenToAddress private setSwapToAsset!: (address?: string) => Promise<void>;
@action.bridge.setAssetAddress private setBridgeAsset!: (address?: string) => Promise<void>;
@action.addLiquidity.setFirstTokenAddress private setAddliquidityAssetA!: (address: string) => Promise<void>;
@action.addLiquidity.setSecondTokenAddress private setAddliquidityAssetB!: (address: string) => Promise<void>;

Expand Down Expand Up @@ -72,8 +71,7 @@ export default class Wallet extends Mixins(TranslationMixin) {
router.push({ name: PageNames.AddLiquidity, params });
}

async handleBridge(asset: AccountAsset): Promise<void> {
await this.setBridgeAsset(asset.address);
handleBridge(asset: AccountAsset): void {
router.push({ name: PageNames.Bridge, params: { address: asset.address } });
}

Expand Down