Skip to content

Commit

Permalink
feat: Add 'rewards query' subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
eliasmpw authored and aelesbao committed Aug 29, 2023
1 parent 4efcd0f commit 0e6c993
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 5 deletions.
49 changes: 49 additions & 0 deletions src/commands/rewards/query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { BaseCommand } from '@/lib/base';
import { accountRequired } from '@/arguments';
import { Accounts, Config } from '@/domain';
import { KeyringFlags } from '@/flags';
import { bold, darkGreen, green, yellow } from '@/utils';
import { showSpinner } from '@/ui';

import { BackendType } from '@/types/Account';

/**
* Command 'rewards query'
* Queries the outstanding rewards for a specific account or address.
*/
export default class RewardsQuery extends BaseCommand<typeof RewardsQuery> {
static summary = 'Queries the outstanding rewards for a specific account or address';
static args = {
account: accountRequired,
};

static flags = {
...KeyringFlags,
};

/**
* Runs the command.
*
* @returns Empty promise
*/
public async run(): Promise<void> {
const accountsDomain = await Accounts.init(this.flags['keyring-backend'] as BackendType, { filesPath: this.flags['keyring-path'] });
const account = await accountsDomain.accountBaseFromAddress(this.args.account!);

const config = await Config.open();

const result = await showSpinner(async () => {
const client = await config.getArchwayClient();

return client.getOutstandingRewards(account.address);
}, 'Querying rewards');

if (this.jsonEnabled()) {
this.logJson(result);
} else {
this.log(`Outstanding rewards for ${account.name ? `${green(account.name)} (${darkGreen(account.address)})` : green(account.address)}\n`);
if (result.totalRewards.length === 0) this.log(`- ${yellow('No outstanding rewards')}`);
for (const item of result.totalRewards) this.log(`- ${bold(item.amount)}${item.denom}`);
}
}
}
13 changes: 12 additions & 1 deletion src/domain/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'node:path';
import _ from 'lodash';
import ow from 'ow';
import { StargateClient } from '@cosmjs/stargate';
import { SigningArchwayClient } from '@archwayhq/arch3.js';
import { ArchwayClient, SigningArchwayClient } from '@archwayhq/arch3.js';
import { DirectSecp256k1HdWallet } from '@cosmjs/proto-signing';

import { getWorkspaceRoot, mergeCustomizer, writeFileWithDir, sanitizeDirName, prettyPrintTransaction, bold } from '@/utils';
Expand Down Expand Up @@ -293,6 +293,17 @@ export class Config {
return StargateClient.connect(rpcEndpoint);
}

/**
* Get an Archway client of the currently active chain in the project
*
* @returns Promise containing the {@link ArchwayClient}
*/
async getArchwayClient(): Promise<ArchwayClient> {
const rpcEndpoint = await this.activeChainRpcEndpoint();

return ArchwayClient.connect(rpcEndpoint);
}

/**
* Get a Stargate client of the currently active chain in the project
*
Expand Down
4 changes: 0 additions & 4 deletions test/commands/contracts/query/balance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ describe('contracts query balance', () => {
let keyringListStub: SinonStub;
let metadataStub: SinonStub;
let validWorkspaceStub: SinonStub;
let validateSchemaStub: SinonStub;
let findInstantiateStub: SinonStub;
let stargateClientStub: SinonStub;
before(() => {
Expand All @@ -41,8 +40,6 @@ describe('contracts query balance', () => {
metadataStub = sinon.stub(Cargo.prototype, 'projectMetadata').callsFake(async () => contractProjectMetadata);
// eslint-disable-next-line @typescript-eslint/no-empty-function
validWorkspaceStub = sinon.stub(Contracts.prototype, 'assertValidWorkspace').callsFake(async () => {});
// eslint-disable-next-line @typescript-eslint/no-empty-function
validateSchemaStub = sinon.stub(Contracts.prototype, <any>'assertValidJSONSchema').callsFake(async () => {});
findInstantiateStub = sinon
.stub(Contracts.prototype, 'findInstantiateDeployment')
.callsFake(() => instantiateDeployment as InstantiateDeployment);
Expand All @@ -59,7 +56,6 @@ describe('contracts query balance', () => {
keyringListStub.restore();
metadataStub.restore();
validWorkspaceStub.restore();
validateSchemaStub.restore();
findInstantiateStub.restore();
stargateClientStub.restore();
});
Expand Down
12 changes: 12 additions & 0 deletions test/commands/rewards/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { expect, test } from '@oclif/test';

import Rewards from '../../../src/commands/rewards';

describe('rewards help', () => {
test
.stdout()
.command(['rewards'])
.it('displays command info', ctx => {
expect(ctx.stdout).to.contain(Rewards.summary);
});
});
64 changes: 64 additions & 0 deletions test/commands/rewards/query.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { expect, test } from '@oclif/test';
import fs from 'node:fs/promises';
import sinon, { SinonStub } from 'sinon';
import keyring from '@archwayhq/keyring-go';
import { ArchwayClient } from '@archwayhq/arch3.js';

import { Cargo } from '../../../src/domain';
import {
aliceAccountName,
aliceAddress,
aliceStoreEntry,
aliceStoredAccount,
configString,
contractProjectMetadata,
dummyRewardsQueryResult,
} from '../../dummies';
import * as FilesystemUtils from '../../../src/utils/filesystem';

import { expectOutputJSON } from '../../helpers/expect';

describe('rewards query', () => {
let readStub: SinonStub;
let writeStub: SinonStub;
let mkdirStub: SinonStub;
let readSubDirStub: SinonStub;
let keyringGetStub: SinonStub;
let keyringListStub: SinonStub;
let metadataStub: SinonStub;
let archwayClientStub: SinonStub;
before(() => {
readStub = sinon.stub(fs, 'readFile').callsFake(async () => configString);
writeStub = sinon.stub(fs, 'writeFile');
mkdirStub = sinon.stub(fs, 'mkdir');
readSubDirStub = sinon.stub(FilesystemUtils, 'readSubDirectories').callsFake(async () => [contractProjectMetadata.name]);
keyringGetStub = sinon.stub(keyring.OsStore, 'get').callsFake(() => aliceStoredAccount);
keyringListStub = sinon.stub(keyring.OsStore, 'list').callsFake(() => [aliceStoreEntry]);
metadataStub = sinon.stub(Cargo.prototype, 'projectMetadata').callsFake(async () => contractProjectMetadata);
archwayClientStub = sinon
.stub(ArchwayClient, 'connect')
.callsFake(async () => ({ getOutstandingRewards: async () => dummyRewardsQueryResult } as any));
});
after(() => {
readStub.restore();
writeStub.restore();
mkdirStub.restore();
readSubDirStub.restore();
keyringGetStub.restore();
keyringListStub.restore();
metadataStub.restore();
archwayClientStub.restore();
});

test
.stdout()
.command(['rewards query', aliceAddress])
.it('Query outstanding rewards balance of a contract', ctx => {
expect(ctx.stdout).to.contain('Outstanding rewards for');
expect(ctx.stdout).to.contain(aliceAddress);
expect(ctx.stdout).to.contain(dummyRewardsQueryResult.totalRewards[0].amount);
expect(ctx.stdout).to.contain(dummyRewardsQueryResult.totalRewards[0].denom);
});

test.stdout().command(['rewards query', aliceAccountName, '--json']).it('Prints json output', expectOutputJSON);
});
6 changes: 6 additions & 0 deletions test/dummies/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -646,3 +646,9 @@ export const dummyMigrateTransaction = {
export const dummyQueryResult = {
msg: 'Hello, Archway!',
};

export const dummyRewardsQueryResult = {
rewardsAddress: 'archway1l3n05jjyrku0my3ahyg66q95jvstpjnn2xfkyw9xemz5zvl5rssqmnlr0q',
totalRewards: [{ denom: 'aconst', amount: '2' }],
totalRecords: 1,
};

0 comments on commit 0e6c993

Please sign in to comment.