Remove BankTokenMeta
This commit is contained in:
parent
6bb5dcc221
commit
546eb95613
@ -31,7 +31,7 @@ export async function start(args: readonly string[]): Promise<void> {
|
||||
const chainTokens = faucet.configuredTokens();
|
||||
console.info("Chain tokens:", chainTokens);
|
||||
const accounts = await faucet.loadAccounts();
|
||||
logAccountsState(accounts, constants.tokenConfig);
|
||||
logAccountsState(accounts);
|
||||
let availableTokens = await faucet.availableTokens();
|
||||
console.info("Available tokens:", availableTokens);
|
||||
setInterval(async () => {
|
||||
|
||||
@ -1,38 +1,35 @@
|
||||
import { Coin } from "@cosmjs/launchpad";
|
||||
|
||||
import { TokenConfiguration } from "./tokenmanager";
|
||||
import { MinimalAccount, SendJob } from "./types";
|
||||
|
||||
/** A string representation of a coin in a human-readable format that can change at any time */
|
||||
function debugCoin(coin: Coin, tokens: TokenConfiguration): string {
|
||||
const meta = tokens.bankTokens.find((token) => token.denom == coin.denom);
|
||||
if (!meta) throw new Error(`No token configuration found for denom ${coin.denom}`);
|
||||
return `${coin.amount} ${meta?.denom}`;
|
||||
function debugCoin(coin: Coin): string {
|
||||
return `${coin.amount} ${coin.denom}`;
|
||||
}
|
||||
|
||||
/** A string representation of a balance in a human-readable format that can change at any time */
|
||||
export function debugBalance(data: readonly Coin[], tokens: TokenConfiguration): string {
|
||||
return `[${data.map((b) => debugCoin(b, tokens)).join(", ")}]`;
|
||||
export function debugBalance(data: readonly Coin[]): string {
|
||||
return `[${data.map((b) => debugCoin(b)).join(", ")}]`;
|
||||
}
|
||||
|
||||
/** A string representation of an account in a human-readable format that can change at any time */
|
||||
export function debugAccount(account: MinimalAccount, tokens: TokenConfiguration): string {
|
||||
return `${account.address}: ${debugBalance(account.balance, tokens)}`;
|
||||
export function debugAccount(account: MinimalAccount): string {
|
||||
return `${account.address}: ${debugBalance(account.balance)}`;
|
||||
}
|
||||
|
||||
export function logAccountsState(accounts: readonly MinimalAccount[], tokens: TokenConfiguration): void {
|
||||
export function logAccountsState(accounts: readonly MinimalAccount[]): void {
|
||||
if (accounts.length < 2) {
|
||||
throw new Error("List of accounts must contain at least one token holder and one distributor");
|
||||
}
|
||||
const holder = accounts[0];
|
||||
const distributors = accounts.slice(1);
|
||||
console.info("Holder:\n" + ` ${debugAccount(holder, tokens)}`);
|
||||
console.info("Distributors:\n" + distributors.map((r) => ` ${debugAccount(r, tokens)}`).join("\n"));
|
||||
console.info("Holder:\n" + ` ${debugAccount(holder)}`);
|
||||
console.info("Distributors:\n" + distributors.map((r) => ` ${debugAccount(r)}`).join("\n"));
|
||||
}
|
||||
|
||||
export function logSendJob(job: SendJob, tokens: TokenConfiguration): void {
|
||||
export function logSendJob(job: SendJob): void {
|
||||
const from = job.sender;
|
||||
const to = job.recipient;
|
||||
const amount = debugCoin(job.amount, tokens);
|
||||
const amount = debugCoin(job.amount);
|
||||
console.info(`Sending ${amount} from ${from} to ${to} ...`);
|
||||
}
|
||||
|
||||
@ -14,14 +14,7 @@ function pendingWithoutWasmd(): void {
|
||||
|
||||
const httpUrl = "http://localhost:1317";
|
||||
const defaultTokenConfig: TokenConfiguration = {
|
||||
bankTokens: [
|
||||
{
|
||||
denom: "ucosm",
|
||||
},
|
||||
{
|
||||
denom: "ustake",
|
||||
},
|
||||
],
|
||||
bankTokens: ["ucosm", "ustake"],
|
||||
};
|
||||
const defaultAddressPrefix = "cosmos";
|
||||
|
||||
|
||||
@ -80,9 +80,8 @@ export class Faucet {
|
||||
|
||||
return balance
|
||||
.filter((b) => b.amount !== "0")
|
||||
.map((b) => this.tokenConfig.bankTokens.find((token) => token.denom == b.denom))
|
||||
.filter(isDefined)
|
||||
.map((token) => token.denom);
|
||||
.map((b) => this.tokenConfig.bankTokens.find((token) => token == b.denom))
|
||||
.filter(isDefined);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -103,13 +102,13 @@ export class Faucet {
|
||||
recipient: recipient,
|
||||
amount: this.tokenManager.creditAmount(denom),
|
||||
};
|
||||
if (this.logging) logSendJob(job, this.tokenConfig);
|
||||
if (this.logging) logSendJob(job);
|
||||
await this.send(job);
|
||||
}
|
||||
|
||||
/** Returns a list to token denoms which are configured */
|
||||
public configuredTokens(): readonly string[] {
|
||||
return this.tokenConfig.bankTokens.map((token) => token.denom);
|
||||
public configuredTokens(): string[] {
|
||||
return Array.from(this.tokenConfig.bankTokens);
|
||||
}
|
||||
|
||||
public async loadAccounts(): Promise<readonly MinimalAccount[]> {
|
||||
@ -139,7 +138,7 @@ export class Faucet {
|
||||
}
|
||||
|
||||
const accounts = await this.loadAccounts();
|
||||
if (this.logging) logAccountsState(accounts, this.tokenConfig);
|
||||
if (this.logging) logAccountsState(accounts);
|
||||
const [_, ...distributorAccounts] = accounts;
|
||||
|
||||
const availableTokenDenoms = await this.availableTokens();
|
||||
@ -155,7 +154,7 @@ export class Faucet {
|
||||
console.info(`Refilling ${denom} of:`);
|
||||
console.info(
|
||||
refillDistibutors.length
|
||||
? refillDistibutors.map((r) => ` ${debugAccount(r, this.tokenConfig)}`).join("\n")
|
||||
? refillDistibutors.map((r) => ` ${debugAccount(r)}`).join("\n")
|
||||
: " none",
|
||||
);
|
||||
}
|
||||
@ -169,7 +168,7 @@ export class Faucet {
|
||||
}
|
||||
if (jobs.length > 0) {
|
||||
for (const job of jobs) {
|
||||
if (this.logging) logSendJob(job, this.tokenConfig);
|
||||
if (this.logging) logSendJob(job);
|
||||
// don't crash faucet when one send fails
|
||||
try {
|
||||
await this.send(job);
|
||||
@ -181,7 +180,7 @@ export class Faucet {
|
||||
|
||||
if (this.logging) {
|
||||
console.info("Done refilling accounts.");
|
||||
logAccountsState(await this.loadAccounts(), this.tokenConfig);
|
||||
logAccountsState(await this.loadAccounts());
|
||||
}
|
||||
} else {
|
||||
if (this.logging) {
|
||||
|
||||
@ -2,7 +2,7 @@ import { TokenConfiguration, TokenManager } from "./tokenmanager";
|
||||
import { MinimalAccount } from "./types";
|
||||
|
||||
const dummyConfig: TokenConfiguration = {
|
||||
bankTokens: [{ denom: "utokenz" }, { denom: "mtrash" }],
|
||||
bankTokens: ["utokenz", "mtrash"],
|
||||
};
|
||||
|
||||
describe("TokenManager", () => {
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
import { Coin } from "@cosmjs/launchpad";
|
||||
import { Decimal, Uint53 } from "@cosmjs/math";
|
||||
|
||||
import { BankTokenMeta } from "./tokens";
|
||||
import { MinimalAccount } from "./types";
|
||||
|
||||
const defaultCreditAmount = 10_000_000;
|
||||
@ -14,7 +13,7 @@ const defaultRefillThresholdFactor = 8;
|
||||
|
||||
export interface TokenConfiguration {
|
||||
/** Supported tokens of the Cosmos SDK bank module */
|
||||
readonly bankTokens: readonly BankTokenMeta[];
|
||||
readonly bankTokens: readonly string[];
|
||||
}
|
||||
|
||||
export class TokenManager {
|
||||
|
||||
@ -3,33 +3,29 @@ import { parseBankToken, parseBankTokens } from "./tokens";
|
||||
describe("tokens", () => {
|
||||
describe("parseBankToken", () => {
|
||||
it("works", () => {
|
||||
expect(parseBankToken("ucosm")).toEqual({
|
||||
denom: "ucosm",
|
||||
});
|
||||
expect(parseBankToken("ucosm")).toEqual("ucosm");
|
||||
});
|
||||
|
||||
it("allows using whitespace", () => {
|
||||
expect(parseBankToken(" ucosm\n")).toEqual({
|
||||
denom: "ucosm",
|
||||
});
|
||||
expect(parseBankToken(" ucosm\n")).toEqual("ucosm");
|
||||
});
|
||||
});
|
||||
|
||||
describe("parseBankTokens", () => {
|
||||
it("works for one", () => {
|
||||
expect(parseBankTokens("ucosm")).toEqual([{ denom: "ucosm" }]);
|
||||
expect(parseBankTokens("ucosm")).toEqual(["ucosm"]);
|
||||
});
|
||||
|
||||
it("works for two", () => {
|
||||
expect(parseBankTokens("ucosm,mstake")).toEqual([{ denom: "ucosm" }, { denom: "mstake" }]);
|
||||
expect(parseBankTokens("ucosm,mstake")).toEqual(["ucosm", "mstake"]);
|
||||
});
|
||||
|
||||
it("ignores whitespace", () => {
|
||||
expect(parseBankTokens("ucosm, mstake\n")).toEqual([{ denom: "ucosm" }, { denom: "mstake" }]);
|
||||
expect(parseBankTokens("ucosm, mstake\n")).toEqual(["ucosm", "mstake"]);
|
||||
});
|
||||
|
||||
it("ignores empty elements", () => {
|
||||
expect(parseBankTokens("ucosm,mstake,")).toEqual([{ denom: "ucosm" }, { denom: "mstake" }]);
|
||||
expect(parseBankTokens("ucosm,mstake,")).toEqual(["ucosm", "mstake"]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@ -1,20 +1,14 @@
|
||||
export interface BankTokenMeta {
|
||||
readonly denom: string;
|
||||
}
|
||||
|
||||
const parseBankTokenPattern = /^([a-zA-Z]{2,20})$/;
|
||||
|
||||
export function parseBankToken(input: string): BankTokenMeta {
|
||||
export function parseBankToken(input: string): string {
|
||||
const match = input.replace(/\s/g, "").match(parseBankTokenPattern);
|
||||
if (!match) {
|
||||
throw new Error("Token could not be parsed. Format: {DISPLAY}=10^{DIGITS}{base}, e.g. ATOM=10^6uatom");
|
||||
}
|
||||
return {
|
||||
denom: match[1],
|
||||
};
|
||||
return match[1];
|
||||
}
|
||||
|
||||
export function parseBankTokens(input: string): BankTokenMeta[] {
|
||||
export function parseBankTokens(input: string): string[] {
|
||||
return input
|
||||
.trim()
|
||||
.split(",")
|
||||
|
||||
Loading…
Reference in New Issue
Block a user