-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add 'rewards query' subcommand
- Loading branch information
Showing
6 changed files
with
143 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters