Skip to content

Commit

Permalink
Merge pull request #238 from chainapsis/add-currencies-validation
Browse files Browse the repository at this point in the history
Add fee currencies, stake currency validate logic
  • Loading branch information
Thunnini authored Sep 15, 2023
2 parents 86bdd5a + 272b937 commit a6b37c9
Showing 1 changed file with 52 additions and 20 deletions.
72 changes: 52 additions & 20 deletions src/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ export const validateChainInfo = async (
);
}

// Check currencies
checkCurrencies(chainInfo);

for (const feature of chainInfo.features ?? []) {
if (!NonRecognizableChainFeatures.includes(feature)) {
throw new Error(
Expand All @@ -63,26 +66,6 @@ export const validateChainInfo = async (
}
}

for (const currency of chainInfo.currencies) {
if (new DenomHelper(currency.coinMinimalDenom).type !== "native") {
throw new Error(
`Do not provide not native token to currencies: ${currency.coinMinimalDenom}`,
);
}

if (currency.coinMinimalDenom.startsWith("ibc/")) {
throw new Error(
`Do not provide ibc currency to currencies: ${currency.coinMinimalDenom}`,
);
}

if (currency.coinMinimalDenom.startsWith("gravity0x")) {
throw new Error(
`Do not provide bridged currency to currencies: ${currency.coinMinimalDenom}`,
);
}
}

if (chainInfo.features?.includes("stargate")) {
throw new Error("'stargate' feature is deprecated");
}
Expand Down Expand Up @@ -161,3 +144,52 @@ const checkCoinGeckoId = async (coinGeckoId: string) => {
throw new Error(`Failed to fetch coinGeckoId ${coinGeckoId}`);
}
};

export const checkCurrencies = (chainInfo: ChainInfo) => {
// Check stake currency
if (
!chainInfo.currencies.some(
(currency) =>
currency.coinMinimalDenom === chainInfo.stakeCurrency.coinMinimalDenom,
)
) {
throw new Error(
`Stake Currency must be included in currencies. stakeCurrency: ${chainInfo.stakeCurrency.coinMinimalDenom}`,
);
}

// Check fee currency
if (
!chainInfo.feeCurrencies
.filter((feeCurrency) => !feeCurrency.coinMinimalDenom.startsWith("ibc/"))
.every((feeCurrency) =>
chainInfo.currencies.some(
(currency) =>
feeCurrency.coinMinimalDenom === currency.coinMinimalDenom,
),
)
) {
throw new Error(`Fee Currency must be included in currencies`);
}

// Check currencies
for (const currency of chainInfo.currencies) {
if (new DenomHelper(currency.coinMinimalDenom).type !== "native") {
throw new Error(
`Do not provide not native token to currencies: ${currency.coinMinimalDenom}`,
);
}

if (currency.coinMinimalDenom.startsWith("ibc/")) {
throw new Error(
`Do not provide ibc currency to currencies: ${currency.coinMinimalDenom}`,
);
}

if (currency.coinMinimalDenom.startsWith("gravity0x")) {
throw new Error(
`Do not provide bridged currency to currencies: ${currency.coinMinimalDenom}`,
);
}
}
};

1 comment on commit a6b37c9

@vercel
Copy link

@vercel vercel bot commented on a6b37c9 Sep 15, 2023

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.