Skip to content

Commit

Permalink
Listen events
Browse files Browse the repository at this point in the history
  • Loading branch information
imstar15 committed Nov 27, 2023
1 parent d798fa1 commit 8d3cdbf
Show file tree
Hide file tree
Showing 4 changed files with 237 additions and 164 deletions.
15 changes: 8 additions & 7 deletions packages/adapter/src/chains/oak.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,13 +272,8 @@ export class OakAdapter extends ChainAdapter {
const autoCompoundingDelegationsCodec =
await api.query.parachainStaking.autoCompoundingDelegations(collator);
const autoCompoundingDelegations =
autoCompoundingDelegationsCodec as unknown as Option<any>;
let autoCompoundingDelegationsLength = 0;
if (autoCompoundingDelegations.isSome) {
const { delegations } = autoCompoundingDelegations.unwrap();
autoCompoundingDelegationsLength = delegations.length;
}
return autoCompoundingDelegationsLength;
autoCompoundingDelegationsCodec as unknown as any[];
return autoCompoundingDelegations.length;
}

/**
Expand Down Expand Up @@ -330,8 +325,14 @@ export class OakAdapter extends ChainAdapter {
const { delegationCount: candidateDelegationCount } =
candidateInfo.unwrap();

console.log("collator: ", collator);

const autoCompoundingDelegationsLength =
await this.getAutoCompoundingDelegationsLength(collator);
console.log(
"autoCompoundingDelegationsLength: ",
autoCompoundingDelegationsLength,
);

// Delegate to collator
const delegateExtrinsic = api.tx.parachainStaking.delegateWithAutoCompound(
Expand Down
83 changes: 39 additions & 44 deletions test/compound/compound.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// import _ from "lodash";
import BN from "bn.js";
import { u8aToHex } from "@polkadot/util";
import { ApiPromise, WsProvider } from "@polkadot/api";
Expand All @@ -10,9 +9,9 @@ import {
DEFAULT_TIMEOUT_INITIALIZE,
DEFAULT_TIMEOUT_PER_TEST,
} from "../utils/constants";
import { getKeyringPair } from "../utils/helpFn";
import { findEvent, getKeyringPair } from "../utils/helpFn";

describe("test-compound", () => {
describe("compound", () => {
let keyringPair: KeyringPair | undefined;
let turingApi: ApiPromise | undefined;
let turingAdapter: OakAdapter | undefined;
Expand All @@ -22,7 +21,6 @@ describe("test-compound", () => {
const {
DevChains: { turingLocal: turingConfig },
} = chains;
console.log("turingConfig.endpoint: ", turingConfig.endpoint);

// Initialize adapters
turingApi = await ApiPromise.create({
Expand All @@ -39,22 +37,26 @@ describe("test-compound", () => {
await turingApi?.disconnect();
});

it("get-auto-compounding-delegation-percentage", async () => {
expect(turingApi).toBeDefined();
expect(keyringPair).toBeDefined();
const pools = await turingApi.query.parachainStaking.candidatePool();
const collatorWalletAddress = pools[0].owner.toHex();
const result = await turingAdapter?.getAutoCompoundingDelegationPercentage(
collatorWalletAddress,
u8aToHex(keyringPair?.addressRaw),
);
console.log(result);
expect(result).toBeDefined();
});
it(
"ensure-balance",
async () => {
expect(turingAdapter).toBeDefined();
expect(keyringPair).toBeDefined();
expect(keyringPair).toBeDefined();
await expect(
turingAdapter?.ensureBalance(
u8aToHex(keyringPair?.addressRaw),
new BN("10000000000"),
),
).resolves.toBeUndefined();
},
DEFAULT_TIMEOUT_PER_TEST,
);

it("get-delegation", async () => {
expect(turingApi).toBeDefined();
expect(keyringPair).toBeDefined();
expect(turingAdapter).toBeDefined();
const pools = await turingApi.query.parachainStaking.candidatePool();
const collatorWalletAddress = pools[0].owner.toHex();
const result = await turingAdapter?.getDelegation(
Expand All @@ -64,24 +66,20 @@ describe("test-compound", () => {
expect(result).toBeDefined();
});

it(
"delegate-with-auto-compound",
async () => {
expect(turingApi).toBeDefined();
expect(keyringPair).toBeDefined();
const pools = await turingApi.query.parachainStaking.candidatePool();
const collatorWalletAddress = pools[0].owner.toHex();
const { minDelegation } = turingApi.consts.parachainStaking;
const result = await turingAdapter?.delegateWithAutoCompound(
it("get-auto-compounding-delegation-percentage", async () => {
expect(turingApi).toBeDefined();
expect(keyringPair).toBeDefined();
expect(turingAdapter).toBeDefined();
const pools = await turingApi.query.parachainStaking.candidatePool();
const collatorWalletAddress = pools[0].owner.toHex();
const percentage =
await turingAdapter?.getAutoCompoundingDelegationPercentage(
collatorWalletAddress,
minDelegation,
50,
keyringPair,
u8aToHex(keyringPair?.addressRaw),
);
expect(result).toBeDefined();
},
DEFAULT_TIMEOUT_PER_TEST,
);
console.log(percentage);
expect(typeof percentage).toBe("number");
});

it(
"set-auto-compound",
Expand All @@ -96,6 +94,9 @@ describe("test-compound", () => {
keyringPair,
);
expect(result).toBeDefined();
const { events } = result;
const event = findEvent(events, "parachainStaking", "AutoCompoundSet");
expect(event).toBeDefined();
},
DEFAULT_TIMEOUT_PER_TEST,
);
Expand All @@ -113,19 +114,13 @@ describe("test-compound", () => {
keyringPair,
);
expect(result).toBeDefined();
},
DEFAULT_TIMEOUT_PER_TEST,
);

it(
"ensure-balance",
async () => {
expect(turingAdapter).toBeDefined();
expect(keyringPair).toBeDefined();
await turingAdapter?.ensureBalance(
u8aToHex(keyringPair?.addressRaw),
new BN("10000000000"),
const { events } = result;
const event = findEvent(
events,
"parachainStaking",
"DelegationIncreased",
);
expect(event).toBeDefined();
},
DEFAULT_TIMEOUT_PER_TEST,
);
Expand Down
61 changes: 61 additions & 0 deletions test/compound/delegate.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { ApiPromise, WsProvider } from "@polkadot/api";
import type { KeyringPair } from "@polkadot/keyring/types";
import { chains } from "@oak-network/config";
import { OakAdapter } from "@oak-network/adapter";
import { rpc, types, runtime } from "@oak-network/types";
import {
DEFAULT_TIMEOUT_INITIALIZE,
DEFAULT_TIMEOUT_PER_TEST,
} from "../utils/constants";
import { findEvent, getKeyringPair } from "../utils/helpFn";

// delegate-with-auto-compound test can only be run once on a fresh parachain
describe("delegate-with-auto-compound", () => {
let keyringPair: KeyringPair | undefined;
let turingApi: ApiPromise | undefined;
let turingAdapter: OakAdapter | undefined;
beforeAll(async () => {
// Create keyringPair
keyringPair = await getKeyringPair();
const {
DevChains: { turingLocal: turingConfig },
} = chains;

// Initialize adapters
turingApi = await ApiPromise.create({
provider: new WsProvider(turingConfig.endpoint),
rpc,
runtime,
types,
});
turingAdapter = new OakAdapter(turingApi, turingConfig);
await turingAdapter.initialize();
}, DEFAULT_TIMEOUT_INITIALIZE);

afterAll(async () => {
await turingApi?.disconnect();
});

it(
"delegate-with-auto-compound",
async () => {
expect(turingApi).toBeDefined();
expect(keyringPair).toBeDefined();
expect(turingAdapter).toBeDefined();
const pools = await turingApi.query.parachainStaking.candidatePool();
const collatorWalletAddress = pools[0].owner.toHex();
const { minDelegation } = turingApi.consts.parachainStaking;
const result = await turingAdapter?.delegateWithAutoCompound(
collatorWalletAddress,
minDelegation,
50,
keyringPair,
);
expect(result).toBeDefined();
const { events } = result;
const event = findEvent(events, "parachainStaking", "Delegation");
expect(event).toBeDefined();
},
DEFAULT_TIMEOUT_PER_TEST,
);
});
Loading

0 comments on commit 8d3cdbf

Please sign in to comment.