Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: refact alliance api #85

Merged
merged 4 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@terra-money/feather.js",
"version": "2.1.0-beta.1",
"version": "3.0.0-beta.0",
"description": "The JavaScript SDK for Terra and Feather chains",
"license": "MIT",
"author": "Terraform Labs, PTE.",
Expand Down Expand Up @@ -86,7 +86,7 @@
},
"dependencies": {
"@terra-money/legacy.proto": "npm:@terra-money/terra.proto@^0.1.7",
"@terra-money/terra.proto": "^4.0.10",
"@terra-money/terra.proto": "5.2.0",
"assert": "^2.0.0",
"axios": "^0.27.2",
"bech32": "^2.0.0",
Expand Down
143 changes: 143 additions & 0 deletions src/client/lcd/api/AllianceAPI.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import { AllianceAPI } from './AllianceAPI';
import { LCDClient } from '../LCDClient';
import { Dec } from '../../../core';
import { RewardWeightRange } from '../../../core/alliance';

const lcd = LCDClient.fromDefaultConfig('testnet');
const alliance = new AllianceAPI(lcd);

describe('AllianceAPI', () => {
it('params', async () => {
const params = await alliance.params('pisco-1');

expect(params.rewardDelayTime).toBeDefined();
expect(params.takeRateClaimInterval).toBeDefined();
expect(params.lastTakeRateClaimTime.getDate()).toBeLessThanOrEqual(
Date.now()
);
});

describe('alliance assets', () => {
it('query an alliance by denom', async () => {
const res = await alliance.queryAlliance(
'pisco-1',
'factory/terra1zdpgj8am5nqqvht927k3etljyl6a52kwqup0je/utest766e'
);

expect(res).toBeDefined();
expect(res.denom).toBe(
'factory/terra1zdpgj8am5nqqvht927k3etljyl6a52kwqup0je/utest766e'
);
expect(res.rewardWeight.eq(new Dec(0.01))).toBeTruthy();
expect(res.takeRate.equals(new Dec(0))).toBeTruthy();
expect(res.totalTokens.greaterThan(new Dec(1))).toBeTruthy();
expect(res.totalValidatorShares.greaterThan(new Dec(1))).toBeTruthy;
expect(res.rewardStartTime.getTime()).toBeLessThanOrEqual(Date.now());

expect(res.rewardChangeRate.equals(new Dec('1'))).toBeTruthy();
expect(res.rewardChangeInterval).toStrictEqual('6000s');
expect(res.lastRewardChangeTime.getTime()).toBeLessThanOrEqual(
Date.now()
);
expect(res.rewardWeightRange).toStrictEqual(
new RewardWeightRange(new Dec(0), new Dec(1))
);
expect(res.isInitialized).toBeTruthy();
});

it('query all alliances', async () => {
const res = await alliance.queryAlliances('pisco-1');
expect(res.pagination).toBeDefined();
expect(res.alliances.length).toBeGreaterThan(0);

const allianceAsset = res.alliances[0];
expect(allianceAsset).toBeDefined();
expect(allianceAsset.denom).toBe(
'factory/terra1zdpgj8am5nqqvht927k3etljyl6a52kwqup0je/utest766e'
);
expect(allianceAsset.rewardWeight.eq(new Dec(0.01))).toBeTruthy();
expect(allianceAsset.takeRate.equals(new Dec(0))).toBeTruthy();
expect(allianceAsset.totalTokens.greaterThan(new Dec(1))).toBeTruthy();
expect(allianceAsset.totalValidatorShares.greaterThan(new Dec(1)))
.toBeTruthy;
expect(allianceAsset.rewardStartTime.getTime()).toBeLessThanOrEqual(
Date.now()
);

expect(allianceAsset.rewardChangeRate.equals(new Dec('1'))).toBeTruthy();
expect(allianceAsset.rewardChangeInterval).toStrictEqual('6000s');
expect(allianceAsset.lastRewardChangeTime.getTime()).toBeLessThanOrEqual(
Date.now()
);
expect(allianceAsset.rewardWeightRange).toStrictEqual(
new RewardWeightRange(new Dec(0), new Dec(1))
);
expect(allianceAsset.isInitialized).toBeTruthy();
});
});

describe('delegations', () => {
it('query all', async () => {
const res = await alliance.queryAllianceDelegations('pisco-1');
expect(res.delegations.length).toBeGreaterThan(10);
expect(res.pagination).toBeDefined();
});

it('query all by delegator', async () => {
const res = await alliance.queryAllianceDelegations(
'pisco-1',
'terra1eaxcahzxp0x8wqejqjlqaey53tp06l728qad6z395lyzgl026qkq20xj43'
);
expect(res.delegations.length).toBeGreaterThan(0);
expect(res.pagination).toBeDefined();
});

it('query all by delegator and validator', async () => {
const res = await alliance.queryAllianceDelegations(
'pisco-1',
'terra1eaxcahzxp0x8wqejqjlqaey53tp06l728qad6z395lyzgl026qkq20xj43',
'terravaloper1zdpgj8am5nqqvht927k3etljyl6a52kwqndjz2'
);
expect(res.delegations.length).toBeGreaterThan(0);
expect(res.pagination).toBeDefined();
});

it('query all by delegator, validator and denom', async () => {
const res = await alliance.queryAllianceDelegations(
'pisco-1',
'terra1eaxcahzxp0x8wqejqjlqaey53tp06l728qad6z395lyzgl026qkq20xj43',
'terravaloper1zdpgj8am5nqqvht927k3etljyl6a52kwqndjz2',
'factory/terra1eaxcahzxp0x8wqejqjlqaey53tp06l728qad6z395lyzgl026qkq20xj43/ualliance'
);
expect(res.delegations.length).toBeGreaterThan(0);
expect(res.pagination).toBeDefined();
});

it('with missing incremental params', async () => {
await alliance
.queryAllianceDelegations(
'pisco-1',
undefined,
'terra1eaxcahzxp0x8wqejqjlqaey53tp06l728qad6z395lyzgl026qkq20xj43'
)
.catch((e: Error) => {
expect(e.message).toStrictEqual(
'DELEGATOR ADDRESS must be provided when VALIDATOR ADDRESS is provided!!'
);
});

await alliance
.queryAllianceDelegations(
'pisco-1',
'terra1eaxcahzxp0x8wqejqjlqaey53tp06l728qad6z395lyzgl026qkq20xj43',
undefined,
'factory/terra1eaxcahzxp0x8wqejqjlqaey53tp06l728qad6z395lyzgl026qkq20xj43/ualliance'
)
.catch((e: Error) => {
expect(e.message).toStrictEqual(
'VALIDATOR ADDRESS must be provided when ALLIANCE DENOM is provided!!'
);
});
});
});
});
Loading
Loading