Merge pull request #270 from CosmWasm/fix-needsRefill

Test and fix needsRefill
This commit is contained in:
Simon Warta 2020-07-01 16:37:35 +02:00 committed by GitHub
commit 8e10dba528
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 5 deletions

View File

@ -1,5 +1,5 @@
import { TokenManager } from "./tokenmanager";
import { TokenConfiguration } from "./types";
import { MinimalAccount, TokenConfiguration } from "./types";
const dummyConfig: TokenConfiguration = {
bankTokens: [
@ -138,4 +138,39 @@ describe("TokenManager", () => {
});
});
});
describe("needsRefill", () => {
const tm = new TokenManager(dummyConfig);
it("works for sufficient/insufficient balance", () => {
const brokeAccount: MinimalAccount = {
address: "cosmos1rtfrpqt3yd7c8g73m9rsaen7fft0h52m3v9v5a",
balance: [
{
denom: "utokenz",
amount: "3",
},
],
};
const richAccount: MinimalAccount = {
address: "cosmos1rtfrpqt3yd7c8g73m9rsaen7fft0h52m3v9v5a",
balance: [
{
denom: "utokenz",
amount: "3456789000000", // 3456789 TOKENZ
},
],
};
expect(tm.needsRefill(brokeAccount, "TOKENZ")).toEqual(true);
expect(tm.needsRefill(richAccount, "TOKENZ")).toEqual(false);
});
it("works for missing balance", () => {
const emptyAccount: MinimalAccount = {
address: "cosmos1rtfrpqt3yd7c8g73m9rsaen7fft0h52m3v9v5a",
balance: [],
};
expect(tm.needsRefill(emptyAccount, "TOKENZ")).toEqual(true);
});
});
});

View File

@ -47,10 +47,7 @@ export class TokenManager {
const balanceAmount = account.balance.find((b) => b.denom === meta.denom);
const balance = balanceAmount
? Decimal.fromAtomics(balanceAmount.amount, meta.fractionalDigits)
: Decimal.fromAtomics("0", 0);
const balance = Decimal.fromAtomics(balanceAmount ? balanceAmount.amount : "0", meta.fractionalDigits);
const thresholdAmount = this.refillThreshold(tickerSymbol);
const threshold = Decimal.fromAtomics(thresholdAmount.amount, meta.fractionalDigits);