Skip to content

Commit

Permalink
Fix display of rewards after stake withdrawal (#256)
Browse files Browse the repository at this point in the history
* add IDE folders to .gitignore

* fix subgraph dependencies

* don't remove Stake entity if it no longer has active share

* Remove unused dependency

* update package manager for subgraph test action

* Restore npm package manager

* add gluegun dependency

* upgrade graph-cli

* fix test workflow

* remove git install

* add schema

* fix rewardsDistributor tests

* fix rewardsDistributor tests
  • Loading branch information
kovart committed Feb 13, 2024
1 parent 5df3af5 commit f2f3484
Show file tree
Hide file tree
Showing 8 changed files with 1,671 additions and 1,330 deletions.
8 changes: 4 additions & 4 deletions .github/actions/testSubgraph/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ runs:
uses: actions/setup-node@v3
with:
node-version: "16"

- name: Install subgraph dependencies
working-directory: ./subgraph
shell: bash
run: npm install
run: yarn install

- name: Run test
working-directory: ./subgraph
shell: bash
run: npm run test
run: yarn run test
6 changes: 4 additions & 2 deletions subgraph/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
generated
old_subgraph
node_modules
schema.graphql
.bin
tests/.bin
tests/.bin

.idea
.vscode
20 changes: 10 additions & 10 deletions subgraph/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@
"name": "forta-subgraph",
"license": "UNLICENSED",
"scripts": {
"prepare:schema": "concat -o schema.graphql src/schema.gql node_modules/@amxx/graphprotocol-utils/generated/schema.graphql",
"prepare:codegen": "graph codegen subgraph.yaml --network matic",
"prepare:codegen": "graph codegen subgraph.yaml",
"prepare:compile": "graph build subgraph.yaml --network matic",
"prepare-dev:schema": "concat -o schema.graphql src/schema.gql node_modules/@amxx/graphprotocol-utils/generated/schema.graphql",
"prepare-dev:codegen": "graph codegen subgraph.yaml --network mumbai",
"prepare-dev:codegen": "graph codegen subgraph.yaml",
"prepare-dev:compile": "graph build subgraph.yaml --network mumbai",
"prepare": "yarn prepare:schema && yarn prepare:codegen && yarn prepare:compile",
"prepare-dev": "yarn prepare-dev:schema && yarn prepare-dev:codegen && yarn prepare-dev:compile",
"prepare": "yarn prepare:codegen && yarn prepare:compile",
"prepare-dev": "yarn prepare-dev:codegen && yarn prepare-dev:compile",
"deploy-dev": "graph deploy --node https://api.graph-eu.p2pify.com/765768b776199f59bb7f1bf37253e73f/deploy --ipfs https://api.graph-eu.p2pify.com/765768b776199f59bb7f1bf37253e73f/ipfs forta-dev",
"deploy": "graph deploy -l v0.0.1 --node https://api.graph-eu.p2pify.com/3e3485fa2703678549290ce52f45ef9c/deploy --ipfs https://api.graph-eu.p2pify.com/3e3485fa2703678549290ce52f45ef9c/ipfs forta-b",
"deploy-theGraph": "graph deploy --network matic --node https://api.thegraph.com/deploy/ forta-network/forta-network",
Expand All @@ -19,11 +17,13 @@
"test": "graph test"
},
"dependencies": {
"@amxx/graphprotocol-utils": "^1.1.0-alpha.0",
"@graphprotocol/graph-cli": "0.30.0",
"@amxx/graphprotocol-utils": "1.1.0",
"@graphprotocol/graph-cli": "0.49.0",
"@graphprotocol/graph-ts": "0.27.0",
"concat": "^1.0.3",
"matchstick-as": "^0.5.0",
"source-map-support": "^0.5.21"
},
"devDependencies": {
"typescript": "^4.9.5"
}
}
}
32 changes: 11 additions & 21 deletions subgraph/src/datasources/FortaStaking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ function findStakeInactiveShares(_subjectType: i32, _subject: BigInt, _staker: A

if(!previousStake) {
return null
}
}

return previousStake.inactiveShares;
}

Expand Down Expand Up @@ -136,7 +136,7 @@ function updateStake(
_subjectType: i32,
_subject: BigInt,
_staker: Address): string {

const _subjectId = formatSubjectId(_subject, _subjectType);
const _stakerId = addressToHex(_staker);
let subject = Subject.load(_subjectId);
Expand Down Expand Up @@ -266,12 +266,11 @@ export function handleStakeDeposited(event: StakeDepositedEvent): void {
}

export function handleWithdrawalInitiated(event: WithdrawalInitiated): void {

// Find number of inactive shares stored on stake entity before update
const _staker = event.params.account;
const _subjectType = event.params.subjectType;
const _subject = event.params.subject

const previousInactiveShares = findStakeInactiveShares(_subjectType, _subject, _staker)

const stakeId = updateStake(
Expand All @@ -292,7 +291,7 @@ export function handleWithdrawalInitiated(event: WithdrawalInitiated): void {
withdrawalInitiatedEvent.timestamp = event.block.timestamp;
withdrawalInitiatedEvent.stake = stakeId;
withdrawalInitiatedEvent.subject = formatSubjectId(event.params.subject, event.params.subjectType);

if(previousInactiveShares) {
withdrawalInitiatedEvent.amount = (currentInActiveShares as BigInt).minus(previousInactiveShares)
} else {
Expand All @@ -314,7 +313,7 @@ export function handleWithdrawalInitiated(event: WithdrawalInitiated): void {
currentStake.save()
}

export function handleWithdrawalExecuted(event: WithdrawalExecuted): void {
export function handleWithdrawalExecuted(event: WithdrawalExecuted): void {
const stakeId = updateStake(
event.address,
event.params.subjectType,
Expand All @@ -341,15 +340,6 @@ export function handleWithdrawalExecuted(event: WithdrawalExecuted): void {
withdrawalExecutedEvent.subject = formatSubjectId(event.params.subject, event.params.subjectType);
withdrawalExecutedEvent.amount = withdrawalInitiatedEvent.amount;
withdrawalExecutedEvent.save();

if (
currentStake.shares &&
currentStake.inactiveShares &&
(currentStake.shares as BigInt).isZero() &&
(currentStake.inactiveShares as BigInt).isZero()
) {
store.remove("Stake", currentStake.id);
}
}

export function handleRewarded(event: RewardedEvent): void {
Expand Down Expand Up @@ -408,7 +398,7 @@ export function handleTransferSingle(event: TransferSingleEvent): void {
// Update withdrawal executed event with value

if(event.params.to.toHex() != ZERO_ADDRESS) {
const account = Account.load(event.params.to.toString()) // Get account of receiver
const account = Account.load(event.params.to.toString()) // Get account of receiver


if(account && account.staker) {
Expand All @@ -435,12 +425,12 @@ export function handleTransferSingle(event: TransferSingleEvent): void {
}
}
}

if (sharesId) {
let subject = Subject.load(sharesId.subject);
if(subject && subject.subjectId) {
const _subjectId: BigInt | null = subject.subjectId;
const subjectId: BigInt = _subjectId ? _subjectId : BigInt.zero();
const subjectId: BigInt = _subjectId ? _subjectId : BigInt.zero();
if (
!subjectId.isZero() &&
event.params.from.toHex() != ZERO_ADDRESS &&
Expand All @@ -457,7 +447,7 @@ export function handleTransferSingle(event: TransferSingleEvent): void {
subject.subjectType,
subjectId,
event.params.to,
);
);
}
}
}
Expand Down Expand Up @@ -600,4 +590,4 @@ export function createFrozeEvent(
mockFrozeEvent.parameters.push(isFrozenParam);

return mockFrozeEvent;
}
}
35 changes: 35 additions & 0 deletions subgraph/src/schema.gql → subgraph/src/schema.graphql
Original file line number Diff line number Diff line change
@@ -1,3 +1,38 @@
# Entities from @amxx/graphprotocol-utils
# -------------------

type DecimalValue @entity {
id: ID!
value: BigDecimal!
exact: BigInt!
decimals: Int!
}

interface Event {
id: ID!
transaction: Transaction!
timestamp: BigInt!
}

type Transaction @entity(immutable: true) {
id: ID!
timestamp: BigInt!
blockNumber: BigInt!
events: [Event!]! @derivedFrom(field: "transaction")
}

type PersistentString @entity {
id: ID!
value: String!
}

type PersistentStringArray @entity {
id: ID!
values: [String!]!
}

# -----------------

type Account @entity {
id: ID!
bots: [Bot!]! @derivedFrom(field: "owner")
Expand Down
Loading

0 comments on commit f2f3484

Please sign in to comment.