Skip to content

Commit

Permalink
Fix liberland adapter balance for custom tokens (#1378)
Browse files Browse the repository at this point in the history
* Fix liberland adapter balance for custom tokens

* liberland fixes

* update min balance amount & update config

---------

Co-authored-by: Stefan Popov <17688105+stefashkaa@users.noreply.github.com>
Co-authored-by: Nikita-Polyakov <polyakov@soramitsu.co.jp>
  • Loading branch information
3 people authored Apr 4, 2024
1 parent 7fc805f commit 7ce866b
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 38 deletions.
2 changes: 1 addition & 1 deletion env.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"KusamaSora": true,
"Polkadot": true,
"PolkadotSora": true,
"Liberland": true
"Liberland": false
},
"EVM_NETWORKS_IDS": [56, 8217],
"ETH_BRIDGE": {
Expand Down
6 changes: 1 addition & 5 deletions src/store/bridge/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,11 +302,7 @@ async function updateExternalMinBalance(context: ActionContext<any, any>): Promi
let minBalance = ZeroStringValue;

if (getters.isSubBridge && getters.asset && !state.isSoraToEvm) {
const minDepositCodec = await subBridgeConnector.network.getAssetMinDeposit(getters.asset.externalAddress);
// is greater than the minimum non-zero value
if (minDepositCodec > '1') {
minBalance = minDepositCodec;
}
minBalance = await subBridgeConnector.network.getAssetMinDeposit(getters.asset.externalAddress);
}

commit.setExternalMinBalance(minBalance);
Expand Down
8 changes: 1 addition & 7 deletions src/store/web3/getters.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { BridgeNetworkType } from '@sora-substrate/util/build/bridgeProxy/consts';
import { SubNetworkId } from '@sora-substrate/util/build/bridgeProxy/sub/consts';
import { WALLET_CONSTS } from '@soramitsu/soraneo-wallet-web';
import { defineGetters } from 'direct-vuex';

import { EVM_NETWORKS } from '@/consts/evm';
Expand Down Expand Up @@ -47,11 +45,7 @@ const getters = defineGetters<Web3State>()({
const data = SUB_NETWORKS[id];

if (data) {
let disabled = !(nodesOrFlag && state.supportedApps?.[BridgeNetworkType.Sub]?.includes(id as SubNetwork));

if (id === SubNetworkId.Liberland && rootState.wallet.settings.soraNetwork === WALLET_CONSTS.SoraNetwork.Prod) {
disabled = true; // TODO: [Liberland] Remove it when needed ready
}
const disabled = !(nodesOrFlag && state.supportedApps?.[BridgeNetworkType.Sub]?.includes(id as SubNetwork));

// override from config
if (Array.isArray(nodesOrFlag)) {
Expand Down
9 changes: 5 additions & 4 deletions src/utils/bridge/sub/classes/adapters/liberland.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ export class LiberlandAdapter extends SubAdapter {
if (result.isEmpty) return ZeroStringValue;

const data = result.unwrap();
const minBalance = data.minBalance.toString();

return data.minBalance.toString();
return minBalance > '1' ? minBalance : ZeroStringValue;
}, ZeroStringValue);
}

Expand All @@ -46,7 +47,7 @@ export class LiberlandAdapter extends SubAdapter {

const data = result.unwrap();

if (data.isFrozen.isTrue) return ZeroStringValue;
if (!data.status.isLiquid) return ZeroStringValue;

return data.balance.toString();
}, ZeroStringValue);
Expand Down Expand Up @@ -75,8 +76,8 @@ export class LiberlandAdapter extends SubAdapter {
try {
return await super.getNetworkFee(asset, sender, recipient);
} catch (error) {
// Hardcoded value for Liberland - 0.011153
return '11153000000';
// Hardcoded value for Liberland - 0.0106
return '10600000000';
}
}
}
39 changes: 18 additions & 21 deletions src/utils/bridge/sub/classes/reducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
getBridgeProxyHash,
getDepositedBalance,
getParachainBridgeAppMintedBalance,
getSubstrateBridgeAppMintedBalance,
getMessageAcceptedNonces,
isMessageDispatchedNonces,
isAssetAddedToChannel,
Expand Down Expand Up @@ -79,15 +80,17 @@ export class SubBridgeReducer extends BridgeReducer<SubHistory> {
this.updateTransactionParams(id, { payload: { ...prevPayload, ...params } });
}

async saveParachainBlock(id: string): Promise<void> {
const adapter = this.connector.soraParachain;
async saveStartBlock(id: string): Promise<void> {
const adapter =
this.transferType === SubTransferType.Standalone ? this.connector.network : this.connector.soraParachain;

if (!adapter) throw new Error(`[${this.constructor.name}]: Sora Parachain Adapter is not exists`);
if (!adapter) throw new Error(`[${this.constructor.name}]: Adapter is not exists`);

await adapter.connect();
// get current sora parachain block number
const parachainStartBlock = await adapter.getBlockNumber();
const startBlock = await adapter.getBlockNumber();
// update history data
this.updateTransactionPayload(id, { parachainStartBlock });
this.updateTransactionPayload(id, { startBlock });
}
}

Expand Down Expand Up @@ -141,11 +144,8 @@ export class SubBridgeIncomingReducer extends SubBridgeReducer {
await this.connector.start();
// sign transaction
await this.connector.network.transfer(asset, tx.to as string, tx.amount as string, id);

if (this.transferType !== SubTransferType.Standalone) {
// store sora parachain block number when tx was signed
await this.saveParachainBlock(id);
}
// save start block when tx was signed
await this.saveStartBlock(id);
}

private async updateTxSigningData(id: string): Promise<void> {
Expand Down Expand Up @@ -214,7 +214,7 @@ export class SubBridgeIncomingReducer extends SubBridgeReducer {

if (!adapter) throw new Error(`[${this.constructor.name}] adapter is not defined`);

const startBlockHeight: number = isStandalone ? tx.externalBlockHeight : tx.payload.parachainStartBlock;
const startBlockHeight: number = tx.payload.startBlock;

if (!startBlockHeight) throw new Error(`[${this.constructor.name}] startBlockHeight is not defined`);

Expand Down Expand Up @@ -322,6 +322,9 @@ export class SubBridgeIncomingReducer extends SubBridgeReducer {
let amount!: string;
let eventIndex!: number;

const isStandalone = this.transferType === SubTransferType.Standalone;
const getMintedBalance = isStandalone ? getSubstrateBridgeAppMintedBalance : getParachainBridgeAppMintedBalance;

try {
await new Promise<void>((resolve, reject) => {
const eventsObservable = api.system.getEventsObservable(subBridgeApi.apiRx);
Expand All @@ -338,11 +341,8 @@ export class SubBridgeIncomingReducer extends SubBridgeReducer {
const foundedEvents = events.slice(substrateDispatchEventIndex);

soraHash = getBridgeProxyHash(foundedEvents, subBridgeApi.api);
[amount, eventIndex] = getParachainBridgeAppMintedBalance(
foundedEvents,
tx.from as string,
subBridgeApi.api
);

[amount, eventIndex] = getMintedBalance(foundedEvents, tx.from as string, subBridgeApi.api);

resolve();
} catch (error) {
Expand Down Expand Up @@ -458,11 +458,8 @@ export class SubBridgeOutgoingReducer extends SubBridgeReducer {
await this.connector.start();
// sign transaction
await subBridgeApi.transfer(asset, tx.to as string, tx.amount as string, tx.externalNetwork as SubNetwork, id);

if (this.transferType !== SubTransferType.Standalone) {
// store sora parachain block number when tx was signed
await this.saveParachainBlock(id);
}
// save start block when tx was signed
await this.saveStartBlock(id);
}

private async waitForSendingExecution(id: string): Promise<void> {
Expand Down
18 changes: 18 additions & 0 deletions src/utils/bridge/sub/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,24 @@ export const getParachainBridgeAppMintedBalance = (

return [balance, index];
};
// for SORA from Liberland
export const getSubstrateBridgeAppMintedBalance = (
events: Array<any>,
to: string,
api: ApiPromise
): [string, number] => {
const index = events.findIndex((e) => {
if (!api.events.substrateBridgeApp.Minted.is(e.event)) return false;
return subBridgeApi.formatAddress(e.event.data[3].toString()) === subBridgeApi.formatAddress(to);
});

if (index === -1) throw new Error(`Unable to find "parachainBridgeApp.Minted" event`);

const event = events[index];
const balance = event.event.data[4].toString();

return [balance, index];
};

export const getReceivedAmount = (sendedAmount: string, receivedAmount: CodecString, decimals?: number) => {
const sended = new FPNumber(sendedAmount, decimals);
Expand Down

0 comments on commit 7ce866b

Please sign in to comment.