Skip to content

Commit

Permalink
Add Bounty-related events (#40)
Browse files Browse the repository at this point in the history
* Add Bounty todos to interfaces

* Add some bounty eventkinds to types.ts

* Add fetchTreasuryBounties to storageFetcher

* Import bounty to storageFetcher

* Add temp IEvents for Treasury approved/proposed/rejected

* Add bountyIndex to bounty events

* Fill in TreasuryBountyProposed

* Finish storageFetcher fetchTreasuryBounties

* Add fetcher method to fetch process, return with all events

* Add todos

* Add Bounty events to titler

* Add events to IEventData export

* Add bounty events to labeler.ts

* add bounty imports to enricher

* Add all Event types

* Export new events with IEventDat
a

* Add leftover types to labeler

* Add more event types to titler

* Comment todos for bounty events in enricher

* Add new interfaces to BountyStatus, BountyStatusCuratorProposed, BountyStatusActive, BountyStatusPendingPayout

* Clean up enricher, needs tests

* Add Bounty to entityToFieldName

* Add all bounty events to EventToEntity

* Add bounties to testutils api

* Add treasuryBounties to enricher synthetic api

* Modify api calls to bountyApprovals, for some reason not in api in storageFetcher.

* Add correct overrides

* Fix more overrides

* Fix bounty api overrides, tests passing

* Fix enricher and finish enricher tests

* Lint enricher

* Add bounty proposed event to processor.spec.ts

* Fix accidental compile error on enricher

* Process bounty event in processor.spec.ts

* Remove comments from labeler

* Remove comment from types.ts

* Fix enricher to build properly

* Export IBountyEventTypes

* Remove status from ITreasuryBountyProposed and from enricher

* Clean up comments and verify bounty event interfaces with pr review comments

* Start to fetch more than just proposed events

* Add comments to types

* Add more bounty lifecycle events to storageFetcher

* Fix build errors, still need to add all events

* More bounty events in storage fetcher.

* Add pendingPayout status event

* Add another event

* Add all events based on status

* Fix enricher tests to reflect new typing of proposed-event

* Fix bounty fetcher for new API

* Remove unnecessary comments in bounty fetcher

* update package.json

* Another iteration of versioning

* Try bounties query multi fix

* Try another fix.

* Rebuild bounty fetcher using derive instead of query. seems to be working better.

* Fetches all bounties and makes proposed events 💪 throws console warning messages but still successfully makes it through the api.derive.bounties. Will add more of the detailed events.

* Add better logs for bounty fetching

* Add more events to storageFetcher for bounties

* Clean up and add last 2 events

* Add bounty to fakeApi in tests, still throws undefined error in test

* Add bounties events to type_parser

* Fix enricher to use derive for compatibility

* Fix testUtil, move bounties overrides from query to derive

* Remove redundant code from storageFetcher

* Remove commented fetchTreasuryBounties

* Play with bounties reference on enricher api

* Fix some of the test issues, not all done yet

* Add fleshed out bounty to enricher test

* Fix storageFetcher test

* Remove logs

* Fix enricher test
  • Loading branch information
zakhap authored Feb 18, 2021
1 parent 18bcb37 commit b280ccf
Show file tree
Hide file tree
Showing 14 changed files with 1,626 additions and 884 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"@babel/core": "^7.10.3",
"@babel/polyfill": "^7.10.1",
"@babel/register": "^7.10.3",
"@istanbuljs/nyc-config-typescript": "^1.0.1",
"@istanbuljs/nyc-config-typescript": "^0.1.3",
"@openzeppelin/contracts": "^2.4.0",
"@typechain/ethers-v4": "^1.0.0",
"@types/bn.js": "^4.11.6",
Expand Down
1 change: 1 addition & 0 deletions scripts/listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ if (chainSupportedBy(network, SubstrateEvents.Types.EventChains)) {
try {
await fetcher.fetch();
} catch (err) {
console.log(err);
console.error(`Got error from fetcher: ${JSON.stringify(err, null, 2)}.`);
}
SubstrateEvents.subscribeEvents({
Expand Down
30 changes: 30 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ export function entityToFieldName(entity: IChainEntityKind): string | null {
case SubstrateTypes.EntityKind.TreasuryProposal: {
return 'proposalIndex';
}
case SubstrateTypes.EntityKind.TreasuryBounty: {
return 'bountyIndex';
}
case SubstrateTypes.EntityKind.CollectiveProposal: {
return 'proposalHash';
}
Expand All @@ -169,6 +172,7 @@ export function entityToFieldName(entity: IChainEntityKind): string | null {

export function eventToEntity(event: IChainEventKind): [ IChainEntityKind, EntityEventKind ] {
switch (event) {
// Democracy Events
case SubstrateTypes.EventKind.DemocracyProposed: {
return [ SubstrateTypes.EntityKind.DemocracyProposal, EntityEventKind.Create ];
}
Expand All @@ -188,6 +192,7 @@ export function eventToEntity(event: IChainEventKind): [ IChainEntityKind, Entit
return [ SubstrateTypes.EntityKind.DemocracyReferendum, EntityEventKind.Complete ];
}

// Preimage Events
case SubstrateTypes.EventKind.PreimageNoted: {
return [ SubstrateTypes.EntityKind.DemocracyPreimage, EntityEventKind.Create ];
}
Expand All @@ -197,6 +202,7 @@ export function eventToEntity(event: IChainEventKind): [ IChainEntityKind, Entit
return [ SubstrateTypes.EntityKind.DemocracyPreimage, EntityEventKind.Complete ];
}

// Treasury Events
case SubstrateTypes.EventKind.TreasuryProposed: {
return [ SubstrateTypes.EntityKind.TreasuryProposal, EntityEventKind.Create ];
}
Expand All @@ -205,6 +211,30 @@ export function eventToEntity(event: IChainEventKind): [ IChainEntityKind, Entit
return [ SubstrateTypes.EntityKind.TreasuryProposal, EntityEventKind.Complete ];
}

// Bounty Events
case SubstrateTypes.EventKind.TreasuryBountyProposed: {
return [ SubstrateTypes.EntityKind.TreasuryBounty, EntityEventKind.Create ];
}
case SubstrateTypes.EventKind.TreasuryBountyAwarded: {
return [ SubstrateTypes.EntityKind.TreasuryBounty, EntityEventKind.Update ];
}
case SubstrateTypes.EventKind.TreasuryBountyBecameActive: {
return [ SubstrateTypes.EntityKind.TreasuryBounty, EntityEventKind.Update ];
}
case SubstrateTypes.EventKind.TreasuryBountyCanceled: {
return [ SubstrateTypes.EntityKind.TreasuryBounty, EntityEventKind.Complete ];
}
case SubstrateTypes.EventKind.TreasuryBountyClaimed: {
return [ SubstrateTypes.EntityKind.TreasuryBounty, EntityEventKind.Complete ];
}
case SubstrateTypes.EventKind.TreasuryBountyExtended: {
return [ SubstrateTypes.EntityKind.TreasuryBounty, EntityEventKind.Update ];
}
case SubstrateTypes.EventKind.TreasuryBountyRejected: {
return [ SubstrateTypes.EntityKind.TreasuryBounty, EntityEventKind.Complete ];
}

// Collective Events
case SubstrateTypes.EventKind.CollectiveProposed: {
return [ SubstrateTypes.EntityKind.CollectiveProposal, EntityEventKind.Create ];
}
Expand Down
93 changes: 91 additions & 2 deletions src/substrate/filters/enricher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ApiPromise } from '@polkadot/api';
import {
Event, ReferendumInfoTo239, AccountId, TreasuryProposal, Balance, PropIndex, Proposal,
ReferendumIndex, ProposalIndex, VoteThreshold, Hash, BlockNumber, Extrinsic,
ReferendumInfo, ValidatorId, Exposure, AuthorityId, IdentificationTuple, AccountVote,
ReferendumInfo, ValidatorId, Exposure, AuthorityId, IdentificationTuple, AccountVote, BountyIndex, Bounty
} from '@polkadot/types/interfaces';
import { Option, bool, Vec, u32, u64, Compact, StorageKey } from '@polkadot/types';
import { Codec, AnyTuple } from '@polkadot/types/types';
Expand Down Expand Up @@ -474,7 +474,7 @@ export async function Enrich(
}

case EventKind.TreasuryRejected: {
const [ proposalIndex, slashedBond ] = event.data as unknown as [ ProposalIndex, Balance ] & Codec;
const [ proposalIndex, ] = event.data as unknown as [ ProposalIndex, ] & Codec;
return {
data: {
kind,
Expand All @@ -483,6 +483,95 @@ export async function Enrich(
};
}

/**
* Bounty Events
*/

case EventKind.TreasuryBountyProposed: {
const [ bountyIndex ] = event.data as unknown as [ BountyIndex ] & Codec;
const bounties = await api.derive.bounties.bounties();
if (!bounties) return;
const bounty = bounties.find((b) => +b.index === +bountyIndex);
if (!bounty) {
throw new Error(`could not fetch treasury proposals`);
}
return {
data: {
kind,
bountyIndex: +bountyIndex,
proposer: bounty.bounty.proposer.toString(),
value: bounty.bounty.value.toString(),
fee: bounty.bounty.fee.toString(),
curatorDeposit: bounty.bounty.curatorDeposit.toString(),
bond: bounty.bounty.bond.toString(),
}
};
}

case EventKind.TreasuryBountyAwarded: {
const [ bountyIndex, beneficiary ] = event.data as unknown as [ BountyIndex, AccountId ] & Codec;
return {
data: {
kind,
bountyIndex: +bountyIndex,
beneficiary: beneficiary.toString(),
}
};
}

case EventKind.TreasuryBountyRejected: {
const [ bountyIndex, bond ] = event.data as unknown as [ BountyIndex, Balance ] & Codec;
return {
data: {
kind,
bountyIndex: +bountyIndex,
bond: bond.toString(),
}
};
}

case EventKind.TreasuryBountyExtended: {
const [ bountyIndex ] = event.data as unknown as [ BountyIndex ] & Codec;
return {
data: {
kind,
bountyIndex: +bountyIndex,
}
};
}

case EventKind.TreasuryBountyClaimed: {
const [ bountyIndex, payout, beneficiary ] = event.data as unknown as [ BountyIndex, Balance, AccountId ] & Codec;
return {
data: {
kind,
bountyIndex: +bountyIndex,
payout: payout.toString(),
beneficiary: beneficiary?.toString(),
}
};
}

case EventKind.TreasuryBountyCanceled: {
const [ bountyIndex ] = event.data as unknown as [ BountyIndex ] & Codec;
return {
data: {
kind,
bountyIndex: +bountyIndex,
}
};
}

case EventKind.TreasuryBountyBecameActive: {
const [ bountyIndex ] = event.data as unknown as [ BountyIndex ] & Codec;
return {
data: {
kind,
bountyIndex: +bountyIndex,
}
};
}

/**
* Elections Events
*/
Expand Down
56 changes: 56 additions & 0 deletions src/substrate/filters/labeler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,62 @@ export const Label: LabelerFilter = (
linkUrl: chainId ? `/${chainId}/proposal/treasuryproposal/${proposalIndex}` : null,
};
}
case EventKind.TreasuryBountyProposed: {
const { bountyIndex, } = data;
return {
heading: 'Treasury Bounty Proposed',
label: `Treasury bounty ${bountyIndex} was proposed.`,
linkUrl: chainId ? `/${chainId}/proposal/treasurybounty/${bountyIndex}` : null,
};
}
case EventKind.TreasuryBountyAwarded: {
const { bountyIndex, beneficiary } = data;
return {
heading: 'Treasury Bounty Awarded',
label: `Treasury bounty ${bountyIndex} was awarded to ${beneficiary}.`,
linkUrl: chainId ? `/${chainId}/proposal/treasurybounty/${bountyIndex}` : null,
};
}
case EventKind.TreasuryBountyRejected: {
const { bountyIndex, bond} = data;
return {
heading: 'Treasury Bounty Rejected',
label: `Treasury bounty ${bountyIndex} with bond ${bond} was rejected.`,
linkUrl: chainId ? `/${chainId}/proposal/treasurybounty/${bountyIndex}` : null,
};
}
case EventKind.TreasuryBountyBecameActive: {
const { bountyIndex,} = data;
return {
heading: 'Treasury Bounty Became Active',
label: `Treasury bounty ${bountyIndex} became active.`,
linkUrl: chainId ? `/${chainId}/proposal/treasurybounty/${bountyIndex}` : null,
};
}
case EventKind.TreasuryBountyClaimed: {
const { bountyIndex, payout, beneficiary} = data;
return {
heading: 'Treasury Bounty Claimed',
label: `${beneficiary} claimed Treasury Bounty ${bountyIndex}, worth ${payout}.`,
linkUrl: chainId ? `/${chainId}/proposal/treasurybounty/${bountyIndex}` : null,
};
}
case EventKind.TreasuryBountyCanceled: {
const { bountyIndex,} = data;
return {
heading: 'Treasury Bounty Canceled',
label: `Treasury bounty ${bountyIndex} was canceled.`,
linkUrl: chainId ? `/${chainId}/proposal/treasurybounty/${bountyIndex}` : null,
};
}
case EventKind.TreasuryBountyExtended: {
const { bountyIndex,} = data;
return {
heading: 'Treasury Bounty Expiry Extended',
label: `Treasury bounty ${bountyIndex} expiry was extended.`,
linkUrl: chainId ? `/${chainId}/proposal/treasurybounty/${bountyIndex}` : null,
};
}

/**
* Elections Events
Expand Down
42 changes: 42 additions & 0 deletions src/substrate/filters/titler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,48 @@ export const Title: TitlerFilter = (kind: EventKind): IEventTitle => {
description: 'A treasury spend is rejected.',
};
}
case EventKind.TreasuryBountyProposed: {
return {
title: 'Treasury Bounty Proposed',
description: 'A treasury bounty is proposed.',
};
}
case EventKind.TreasuryBountyAwarded: {
return {
title: 'Treasury Bounty Awarded',
description: 'A treasury bounty is awarded.',
};
}
case EventKind.TreasuryBountyRejected: {
return {
title: 'Treasury Bounty Rejected',
description: 'A treasury bounty is rejected.',
};
}
case EventKind.TreasuryBountyBecameActive: {
return {
title: 'Treasury Bounty Became Active',
description: 'A treasury bounty became active.',
};
}
case EventKind.TreasuryBountyClaimed: {
return {
title: 'Treasury Bounty Claimed',
description: 'A treasury bounty is claimed.',
};
}
case EventKind.TreasuryBountyCanceled: {
return {
title: 'Treasury Bounty Canceled',
description: 'A treasury bounty is canceled.',
};
}
case EventKind.TreasuryBountyExtended: {
return {
title: 'Treasury Bounty Expiry Extended',
description: 'A treasury bounty\'s expiry is extended.',
};
}

/**
* Elections Events
Expand Down
11 changes: 11 additions & 0 deletions src/substrate/filters/type_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,17 @@ export function ParseType (
default: return null;
}
}
case 'bounties': {
switch (method) {
case 'BountyProposed': return EventKind.TreasuryBountyProposed;
case 'BountyRejected': return EventKind.TreasuryBountyRejected;
case 'BountyBecameActive': return EventKind.TreasuryBountyBecameActive;
case 'BountyAwarded': return EventKind.TreasuryBountyAwarded;
case 'BountyClaimed': return EventKind.TreasuryBountyClaimed;
case 'BountyCanceled': return EventKind.TreasuryBountyCanceled;
case 'BountyExtended': return EventKind.TreasuryBountyExtended;
}
}
default:
return null;
}
Expand Down
Loading

0 comments on commit b280ccf

Please sign in to comment.