Skip to content

Commit

Permalink
devop: add token id for additional validation
Browse files Browse the repository at this point in the history
  • Loading branch information
kvhnuke committed Mar 27, 2024
1 parent a7f56fc commit d95ee88
Show file tree
Hide file tree
Showing 18 changed files with 109 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const NetworkEndpoints = {
[NetworkNames.Quartz]: "https://quartz.api.subscan.io/",
[NetworkNames.Unique]: "https://unique.api.subscan.io/",
[NetworkNames.Vara]: "https://vara.api.subscan.io/",
[NetworkNames.AssetHub]: "https://assethub-polkadot.api.subscan.io/",
};

export { NetworkEndpoints };
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { SubstrateNetwork } from "@/providers/polkadot/types/substrate-network";
import { hexToString } from "@polkadot/util";

import { toBN } from "web3-utils";
import { AstarToken, AstarTokenOptions } from "../types/astar-token";
import { AssetToken, AssetTokenOptions } from "./asset-token";

type AssetMetadata = {
name: `0x${string}`;
Expand All @@ -30,25 +30,19 @@ export default async (
const assetMetadatas = metadata.map(([key, value]) => {
const assetKey = (key.toHuman() as string[])[0].replaceAll(",", "");
const assetMetadata = value.toJSON() as AssetMetadata;

const info = {
key: assetKey,
name: hexToString(assetMetadata.name),
symbol: hexToString(assetMetadata.symbol),
decimals: assetMetadata.decimals,
};

return info;
});

const queries = assetMetadatas.map((metadata) => metadata.key);

const assetInfos = await apiPromise.query.assets.asset.multi(queries);

const tokenOptions = assetInfos
.map((info, index) => {
const infoHuman = info.toHuman() as AssetInfo;

if (infoHuman) {
const metadata = assetMetadatas[index];
return {
Expand All @@ -61,50 +55,44 @@ export default async (
})
.filter((asset) => asset !== null)
.map((asset) => {
const tokenOptions: AstarTokenOptions = {
const tokenOptions: AssetTokenOptions = {
name: asset!.name,
symbol: asset!.symbol,
decimals: asset!.decimals,
icon: network.icon,
id: asset!.key,
existentialDeposit: toBN(asset!.minBalance),
};

return tokenOptions;
})
.map((tokenOption) => {
if (knownTokens) {
const knownToken = knownTokens.find(
(knownToken) =>
knownToken.name === tokenOption.name &&
knownToken.symbol === tokenOption.symbol
knownToken.symbol === tokenOption.symbol &&
knownToken.id === tokenOption.id
);

if (knownToken) {
tokenOption.coingeckoID = knownToken.coingeckoID;
tokenOption.icon = knownToken.icon;
console.log(tokenOption);
}
}

return tokenOption;
});

if (address) {
const queries = tokenOptions.map((options) => {
return [options.id, address];
});

const balances = await apiPromise.query.assets.account.multi(queries);

balances.forEach((balanceInfo, index) => {
const data = balanceInfo.toJSON();

if (data) {
tokenOptions[index].balance = (data as any).balance.toString();
}
});
}

const nativeAsset = new SubstrateNativeToken({
name: network.currencyNameLong,
symbol: network.name,
Expand All @@ -113,6 +101,5 @@ export default async (
icon: network.icon,
coingeckoID: network.coingeckoID,
});

return [nativeAsset, ...tokenOptions.map((o) => new AstarToken(o))];
return [nativeAsset, ...tokenOptions.map((o) => new AssetToken(o))];
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import { ApiPromise } from "@polkadot/api";
import { SubmittableExtrinsic } from "@polkadot/api/types";
import { ISubmittableResult } from "@polkadot/types/types";

export interface AstarTokenOptions extends BaseTokenOptions {
export interface AssetTokenOptions extends BaseTokenOptions {
id: string;
}

export class AstarToken extends SubstrateToken {
export class AssetToken extends SubstrateToken {
private id: string;
constructor(options: AstarTokenOptions) {
constructor(options: AssetTokenOptions) {
super(options);
this.id = options.id;
}
Expand All @@ -26,7 +26,6 @@ export class AstarToken extends SubstrateToken {
this.balance = balance;
return balance;
}

return "0";
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { KnownTokenDisplay } from "@/providers/polkadot/types";

const assets: KnownTokenDisplay[] = [
// {
// name: "Acala",
// symbol: "ACA",
// coingeckoID: "acala",
// icon: require("./icons/ACA.png"),
// },
// {
// name: "Polkadot",
// symbol: "DOT",
// coingeckoID: "polkadot",
// icon: require("../../icons/polkadot.svg"),
// },
// {
// name: "Acala Dollar",
// symbol: "aUSD",
// icon: require("./icons/AUSD.png"),
// coingeckoID: "acala-dollar",
// },
// {
// name: "Liquid DOT",
// symbol: "LDOT",
// icon: require("./icons/LDOT.png"),
// },
];

export default assets;
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { NetworkNames } from "@enkryptcom/types";
import { toBN } from "web3-utils";
import {
SubstrateNetwork,
SubstrateNetworkOptions,
} from "@/providers/polkadot/types/substrate-network";
import { subscanActivity } from "@/providers/polkadot/libs/activity-handlers";
import wrapActivityHandler from "@/libs/activity-state/wrap-activity-handler";
import assetHandler from "@/providers/polkadot/libs/asset-handler";

const assetHubOptions: SubstrateNetworkOptions = {
name: NetworkNames.AssetHub,
name_long: "Asset Hub",
homePage: "https://polkadot.network",
blockExplorerTX: "https://assethub-polkadot.subscan.io/extrinsic/[[txHash]]",
blockExplorerAddr: "https://assethub-polkadot.subscan.io/account/[[address]]",
isTestNetwork: false,
currencyName: "DOT",
currencyNameLong: "Polkadot",
icon: require("../icons/assethub.png"),
decimals: 10,
prefix: 0,
node: "wss://statemint-rpc.dwellir.com/",
coingeckoID: "polkadot",
genesisHash:
"0x68d56f15f85d3136970ec16946040bc1752654e906147f7e43e9d539d7c3de2f",
activityHandler: wrapActivityHandler(subscanActivity),
existentialDeposit: toBN("10000000000"),
assetHandler,
knownTokens: [],
};

const assetHub = new SubstrateNetwork(assetHubOptions);

export default assetHub;
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,20 @@ const assets: KnownTokenDisplay[] = [
symbol: "ACA",
coingeckoID: "acala",
icon: require("./icons/ACA.png"),
id: "18446744073709551616",
},
{
name: "Polkadot",
symbol: "DOT",
coingeckoID: "polkadot",
icon: require("../../icons/polkadot.svg"),
},
{
name: "Acala Dollar",
symbol: "aUSD",
icon: require("./icons/AUSD.png"),
coingeckoID: "acala-dollar",
id: "340282366920938463463374607431768211455",
},
{
name: "Liquid DOT",
symbol: "LDOT",
icon: require("./icons/LDOT.png"),
id: "18446744073709551618",
},
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,19 @@ const assets: KnownTokenDisplay[] = [
symbol: "MOVR",
coingeckoID: "moonriver",
icon: require("./icons/MOVR.png"),
id: "18446744073709551620",
},
{
name: "Kusama",
symbol: "KSM",
icon: require("./icons/KSM.png"),
},
{
name: "Liquid Kusama",
symbol: "LKSM",
icon: require("./icons/LKSM.png"),
},
{
name: "Acala Dollar",
symbol: "aUSD",
icon: require("./icons/AUSD.png"),
coingeckoID: "acala-dollar",
id: "340282366920938463463374607431768211455",
},
{
name: "Karura",
symbol: "KAR",
icon: require("./icons/KAR.svg"),
id: "18446744073709551618",
},
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
SubstrateNetwork,
SubstrateNetworkOptions,
} from "../../types/substrate-network";
import assetHandler from "./libs//assetinfo";
import assetHandler from "@/providers/polkadot/libs/asset-handler";
import { toBN } from "web3-utils";

const astarOptions: SubstrateNetworkOptions = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
SubstrateNetwork,
SubstrateNetworkOptions,
} from "../../types/substrate-network";
import assetHandler from "./libs//assetinfo";
import assetHandler from "@/providers/polkadot/libs/asset-handler";
import { toBN } from "web3-utils";

const shidenOptions: SubstrateNetworkOptions = {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions packages/extension/src/providers/polkadot/networks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import uniqueNode from "./unique/unique";
import penNode from "./pendulum/pendulum";
import ampeNode from "./pendulum/amplitude";
import varaNode from "./vara";
import assetHubNode from "./assethub";

export default {
acala: acaNode,
Expand All @@ -32,4 +33,5 @@ export default {
pendulum: penNode,
amplitude: ampeNode,
vara: varaNode,
assetHub: assetHubNode,
};
1 change: 1 addition & 0 deletions packages/extension/src/providers/polkadot/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export interface KnownTokenDisplay {
symbol: string;
coingeckoID?: string;
icon: string;
id?: string;
}

export { InjectedProvider };
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export class SubstrateNetwork extends BaseNetwork {
} else {
const nativeToken = new SubstrateNativeToken({
name: this.currencyNameLong,
symbol: this.name,
symbol: this.currencyName,
coingeckoID: this.coingeckoID,
decimals: this.decimals,
icon: this.icon,
Expand All @@ -124,7 +124,7 @@ export class SubstrateNetwork extends BaseNetwork {
if (supported.length === 0) {
const nativeToken = new SubstrateNativeToken({
name: this.currencyNameLong,
symbol: this.name,
symbol: this.currencyName,
coingeckoID: this.coingeckoID,
decimals: this.decimals,
icon: this.icon,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
</a>
</div>
<hardware-wallet-msg :wallet-type="account?.walletType" />
<p
class="verify-transaction__description"
style="color: red"
:class="{ popup: isPopup }"
>
{{ errorMsg }}
</p>
<p class="verify-transaction__description" :class="{ popup: isPopup }">
Double check the information and confirm transaction
</p>
Expand All @@ -32,7 +39,6 @@
/>
<verify-transaction-amount :token="txData.toToken" />
<verify-transaction-fee :fee="txData.txFee" />
{{ errorMsg }}
</div>
</custom-scrollbar>

Expand Down Expand Up @@ -196,8 +202,22 @@ const sendAction = async () => {
address: network.value.displayAddress(txData.fromAddress),
network: network.value.name,
});
isSendDone.value = true;
if (getCurrentContext() === "popup") {
setTimeout(() => {
isProcessing.value = false;
router.go(-2);
}, 2500);
} else {
setTimeout(() => {
isProcessing.value = false;
window.close();
}, 1500);
}
})
.catch((error) => {
isProcessing.value = false;
errorMsg.value = error.message;
trackSendEvents(SendEventType.SendFailed, {
network: network.value.name,
error: error.message,
Expand All @@ -208,19 +228,6 @@ const sendAction = async () => {
network: network.value.name,
});
});
isSendDone.value = true;
if (getCurrentContext() === "popup") {
setTimeout(() => {
isProcessing.value = false;
router.go(-2);
}, 2500);
} else {
setTimeout(() => {
isProcessing.value = false;
window.close();
}, 1500);
}
} catch (error: any) {
isProcessing.value = false;
console.error(error);
Expand Down
1 change: 1 addition & 0 deletions packages/types/src/networks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export enum NetworkNames {
ArtheraTest = "AATest",
Arthera = "AA",
FormTestnet = "FormTestnet",
AssetHub = "AssetHub",
}

export enum CoingeckoPlatform {
Expand Down

1 comment on commit d95ee88

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.