registry-sdk/src/bond.test.ts
Prathamesh Musale 6a17b1b175 Add support for using auto fee calculation (#22)
Part of [Create a public laconicd testnet](https://www.notion.so/Create-a-public-laconicd-testnet-896a11bdd8094eff8f1b49c0be0ca3b8)

- Add an option for setting gas price
- Use fees as gas estimation multiplier when `gasPrice` is provided (following `cosmjs` pattern)
  - Default gas estimation multiplier: `2`
- Add a method to query bonds by owners and fix tests
- Add a test for inclusion of batch of txs sent at once in the same block

Co-authored-by: IshaVenikar <ishavenikar7@gmail.com>
Co-authored-by: Nabarun <nabarun@deepstacksoft.com>
Reviewed-on: cerc-io/registry-sdk#22
Co-authored-by: Prathamesh Musale <prathamesh.musale0@gmail.com>
Co-committed-by: Prathamesh Musale <prathamesh.musale0@gmail.com>
2024-09-06 09:07:57 +00:00

182 lines
7.3 KiB
TypeScript

import path from 'path';
import { Account, Registry } from './index';
import { ensureUpdatedConfig, getConfig } from './testing/helper';
import { DENOM } from './constants';
const WATCHER_YML_PATH = path.join(__dirname, './testing/data/watcher.yml');
const BOND_AMOUNT = '1000000000';
const { chainId, rpcEndpoint, gqlEndpoint, privateKey, fee } = getConfig();
jest.setTimeout(90 * 1000);
const bondTests = () => {
let registry: Registry;
let bond0: { id: string, owner: string };
const publishNewWatcherVersion = async (bondId: string) => {
let watcher = await ensureUpdatedConfig(WATCHER_YML_PATH);
await registry.setRecord({ privateKey, record: watcher.record, bondId }, privateKey, fee);
return watcher;
};
beforeAll(async () => {
registry = new Registry(gqlEndpoint, rpcEndpoint, { chainId });
});
test('Create bond.', async () => {
let bondId = await registry.getNextBondId(privateKey);
expect(bondId).toBeDefined();
await registry.createBond({ denom: DENOM, amount: BOND_AMOUNT }, privateKey, fee);
[bond0] = await registry.getBondsByIds([bondId]);
});
describe('With bond created', () => {
let bond1: { id: string, owner: string };
beforeAll(async () => {
let bondId1 = await registry.getNextBondId(privateKey);
expect(bondId1).toBeDefined();
await registry.createBond({ denom: DENOM, amount: BOND_AMOUNT }, privateKey, fee);
[bond1] = await registry.getBondsByIds([bondId1]);
expect(bond1).toBeDefined();
expect(bond1.id).toEqual(bondId1);
});
test('Get bond by ID.', async () => {
const [bond] = await registry.getBondsByIds([bond1.id]);
expect(bond).toBeDefined();
expect(bond.id).toBe(bond1.id);
expect(bond.balance).toHaveLength(1);
expect(bond.balance[0]).toEqual({ type: DENOM, quantity: BOND_AMOUNT });
});
test('Query bonds.', async () => {
const bonds = await registry.queryBonds();
expect(bonds).toBeDefined();
const filteredBonds = bonds.filter((bond: any) => bond.id === bond1.id);
expect(filteredBonds).toHaveLength(1);
expect(filteredBonds[0]).toMatchObject({ id: bond1.id, owner: bond1.owner });
});
test('Query bonds by owner.', async () => {
const mnenonic = Account.generateMnemonic();
const otherAccount = await Account.generateFromMnemonic(mnenonic);
await otherAccount.init();
await registry.sendCoins({ denom: DENOM, amount: '1000000000000', destinationAddress: otherAccount.address }, privateKey, fee);
const { id: bondId2 } = await registry.createBond({ denom: DENOM, amount: BOND_AMOUNT }, otherAccount.getPrivateKey(), fee);
const [owner1Bonds] = await registry.queryBondsByOwners([bond0.owner]);
const owner1Bond1 = owner1Bonds.bonds.filter((b: any) => b.id === bond0.id);
const owner1Bond2 = owner1Bonds.bonds.filter((b: any) => b.id === bond1.id);
expect(owner1Bond1).toBeDefined();
expect(owner1Bond2).toBeDefined();
const [bond2] = await registry.getBondsByIds([bondId2]);
const [owner2Bonds] = await registry.queryBondsByOwners([bond2.owner]);
expect(owner2Bonds.bonds).toHaveLength(1);
const owner2Bond = owner2Bonds.bonds.filter((b: any) => b.id === bondId2);
expect(owner2Bond).toBeDefined();
});
test('Refill bond.', async () => {
const refillAmount = '500';
const total = (parseInt(BOND_AMOUNT) + parseInt(refillAmount)).toString();
await registry.refillBond({ id: bond1.id, denom: DENOM, amount: refillAmount }, privateKey, fee);
const [bond] = await registry.getBondsByIds([bond1.id]);
expect(bond).toBeDefined();
expect(bond.id).toBe(bond1.id);
expect(bond.balance).toHaveLength(1);
expect(bond.balance[0]).toEqual({ type: DENOM, quantity: total });
});
test('Withdraw bond.', async () => {
await registry.withdrawBond({ id: bond1.id, denom: DENOM, amount: '500' }, privateKey, fee);
const [bond] = await registry.getBondsByIds([bond1.id]);
expect(bond).toBeDefined();
expect(bond.id).toBe(bond1.id);
expect(bond.balance).toHaveLength(1);
expect(bond.balance[0]).toEqual({ type: DENOM, quantity: BOND_AMOUNT });
});
test('Cancel bond.', async () => {
await registry.cancelBond({ id: bond1.id }, privateKey, fee);
const [bond] = await registry.getBondsByIds([bond1.id]);
expect(bond).toBe(null);
});
});
test('Associate/Dissociate bond.', async () => {
let bondId1: string;
bondId1 = await registry.getNextBondId(privateKey);
expect(bondId1).toBeDefined();
await registry.createBond({ denom: DENOM, amount: BOND_AMOUNT }, privateKey, fee);
// Create a new record.
let watcher = await publishNewWatcherVersion(bondId1);
let query = { type: watcher.record.type, url: watcher.record.url, version: watcher.record.version };
let [record1] = await registry.queryRecords(query, true);
expect(record1.bondId).toBe(bondId1);
// Dissociate record, query and confirm.
await registry.dissociateBond({ recordId: record1.id }, privateKey, fee);
[record1] = await registry.queryRecords(query, true);
expect(record1.bondId).toBe('');
// Associate record with bond, query and confirm.
await registry.associateBond({ recordId: record1.id, bondId: bondId1 }, privateKey, fee);
[record1] = await registry.queryRecords(query, true);
expect(record1.bondId).toBe(bondId1);
});
test('Reassociate/Dissociate records.', async () => {
let bondId1: string;
let bondId2: string;
bondId1 = await registry.getNextBondId(privateKey);
expect(bondId1).toBeDefined();
await registry.createBond({ denom: DENOM, amount: BOND_AMOUNT }, privateKey, fee);
// Create a new record version.
let watcher = await publishNewWatcherVersion(bondId1);
let queryv1 = { type: watcher.record.type, url: watcher.record.url, version: watcher.record.version };
let queryv2 = { type: watcher.record.type, url: watcher.record.url, version: watcher.record.version };
// Check version1, version2 as associated with bondId1.
let records;
records = await registry.queryRecords(queryv1, true);
expect(records[0].bondId).toBe(bondId1);
records = await registry.queryRecords(queryv2, true);
expect(records[0].bondId).toBe(bondId1);
// Create another bond.
bondId2 = await registry.getNextBondId(privateKey);
expect(bondId2).toBeDefined();
await registry.createBond({ denom: DENOM, amount: BOND_AMOUNT }, privateKey, fee);
const [bond] = await registry.getBondsByIds([bondId2]);
expect(bond.id).toBe(bondId2);
// Reassociate records from bondId1 to bondId2, verify change.
await registry.reassociateRecords({ oldBondId: bondId1, newBondId: bondId2 }, privateKey, fee);
records = await registry.queryRecords(queryv1, true);
expect(records[0].bondId).toBe(bondId2);
records = await registry.queryRecords(queryv2, true);
expect(records[0].bondId).toBe(bondId2);
// Dissociate all records from bond, verify change.
await registry.dissociateRecords({ bondId: bondId2 }, privateKey, fee);
records = await registry.queryRecords(queryv1, true);
expect(records[0].bondId).toBe('');
records = await registry.queryRecords(queryv2, true);
expect(records[0].bondId).toBe('');
});
};
describe('Bonds', bondTests);