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

split community #83

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
16 changes: 8 additions & 8 deletions generated/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,37 +112,37 @@ export class CommunityEntity extends Entity {
this.set("state", Value.fromI32(value));
}

get previous(): Bytes | null {
get previous(): string | null {
let value = this.get("previous");
if (!value || value.kind == ValueKind.NULL) {
return null;
} else {
return value.toBytes();
return value.toString();
}
}

set previous(value: Bytes | null) {
set previous(value: string | null) {
if (!value) {
this.unset("previous");
} else {
this.set("previous", Value.fromBytes(<Bytes>value));
this.set("previous", Value.fromString(<string>value));
}
}

get next(): Bytes | null {
get next(): Array<string> | null {
let value = this.get("next");
if (!value || value.kind == ValueKind.NULL) {
return null;
} else {
return value.toBytes();
return value.toStringArray();
}
}

set next(value: Bytes | null) {
set next(value: Array<string> | null) {
if (!value) {
this.unset("next");
} else {
this.set("next", Value.fromBytes(<Bytes>value));
this.set("next", Value.fromStringArray(<Array<string>>value));
}
}

Expand Down
12 changes: 5 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "subgraph",
"name": "@impact-market/subgraph",
"license": "Apache-2.0",
"version": "1.0.0",
"files": ["dist"],
Expand All @@ -8,7 +8,7 @@
},
"scripts": {
"types": "graphql-codegen --config codegen.ts",
"package": "rimraf dist && tsc -p tsconfig.package.json",
"package": "rimraf dist generated-types && yarn types && tsc -p tsconfig.package.json",
"codegen": "graph codegen",
"build": "graph build",
"test": "graph test --version 0.5.4",
Expand All @@ -28,18 +28,16 @@
"prepare:alfajores:addresses": "mustache config/alfajores.json src/common/addresses.template > src/common/addresses.ts",
"prepare:alfajores": "yarn prepare:alfajores:subgraph && yarn prepare:alfajores:addresses"
},
"dependencies": {
"@graphprotocol/graph-cli": "0.37.1",
"@graphprotocol/graph-ts": "0.29.1",
"matchstick-as": "0.5.0"
},
"devDependencies": {
"@babel/core": "7.20.7",
"@graphql-codegen/cli": "3.2.2",
"@graphprotocol/graph-cli": "0.37.1",
"@graphprotocol/graph-ts": "0.29.1",
"@graphql-codegen/typescript": "3.0.2",
"@graphql-codegen/typescript-resolvers": "3.1.1",
"eslint": "8.30.0",
"eslint-config-impact-market": "2.0.3",
"matchstick-as": "0.5.0",
"mustache": "4.2.0",
"prettier": "2.8.1",
"rimraf": "4.4.0",
Expand Down
9 changes: 3 additions & 6 deletions schema.graphql
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
scalar Bytes
scalar BigDecimal

"""
Contributions of an asset to an entity
"""
Expand Down Expand Up @@ -41,11 +38,11 @@ type CommunityEntity @entity {
"""
address of the previous community, if migrated/copied.
"""
previous: Bytes
previous: CommunityEntity
"""
address of the community it was migrated/copied to
address of communities it was migrated/copied to
"""
next: Bytes
next: [CommunityEntity!]
"""
UBI parameter claim amount (normalized)
"""
Expand Down
2 changes: 1 addition & 1 deletion src/common/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export function genericHandleManagerAdded(
_community.previous !== null &&
Address.fromString(manager.community).equals(
// wasm still thinks it's null, so need to force
_community.previous!
Address.fromString(_community.previous!)
)
) {
// this is just migrating
Expand Down
5 changes: 3 additions & 2 deletions src/mappings/community.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,12 @@ export function handleMaxBeneficiariesUpdated(event: MaxBeneficiariesUpdated): v
export function handleBeneficiaryCopied(event: BeneficiaryCopied): void {
const newCommunity = CommunityEntity.load(event.address.toHex())!;
const beneficiary = BeneficiaryEntity.load(event.params.beneficiary.toHex())!;
const oldCommunity = CommunityEntity.load(beneficiary.community)!;
const beneficiaryCommunity = beneficiary.community;
const oldCommunity = CommunityEntity.load(beneficiaryCommunity)!;

newCommunity.beneficiaries += 1;
oldCommunity.beneficiaries -= 1;
beneficiary.community = event.address.toHex();
beneficiary.community = newCommunity.id;
beneficiary.addedBy = event.params.manager;

// save
Expand Down
57 changes: 41 additions & 16 deletions src/mappings/communityAdmin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,14 @@ export function handleCommunityMigrated(event: CommunityMigrated): void {
const totalNewManagers = event.params.managers.length;

// update previous community
previousCommunity.next = event.params.communityAddress;
let arrayNext = previousCommunity.next;

if (!arrayNext) {
arrayNext = new Array<string>();
}

arrayNext.push(event.params.communityAddress.toHex());
previousCommunity.next = arrayNext;
// create new community
community.startDayId = previousCommunity.startDayId;
community.state = previousCommunity.state;
Expand Down Expand Up @@ -133,7 +140,7 @@ export function handleCommunityMigrated(event: CommunityMigrated): void {
community.contributors = previousCommunity.contributors;
community.contributions = previousCommunity.contributions;
community.estimatedFunds = BigDecimal.zero();
community.previous = event.params.previousCommunityAddress;
community.previous = event.params.previousCommunityAddress.toHex();
community.managerList = new Array<string>();
community.minTranche = previousCommunity.minTranche;
community.maxTranche = previousCommunity.maxTranche;
Expand Down Expand Up @@ -189,13 +196,26 @@ export function handleCommunityMigrated(event: CommunityMigrated): void {
export function handleCommunityCopied(event: CommunityCopied): void {
const originalCommunity = CommunityEntity.load(event.params.originalCommunity.toHex())!;
const newCommunity = new CommunityEntity(event.params.copyCommunity.toHex());
const ubi = UBIDailyEntity.load('0')!;
// update daily ubi
const ubiDaily = loadOrCreateDailyUbi(event.block.timestamp);

newCommunity.merge([originalCommunity]);
// can't merge because it will override the some null fields
// newCommunity.merge([originalCommunity]);

// update previous community
let arrayNext = originalCommunity.next;

if (!arrayNext) {
arrayNext = new Array<string>();
}

originalCommunity.next = event.params.copyCommunity;
arrayNext.push(event.params.copyCommunity.toHex());
originalCommunity.next = arrayNext;

// new community
newCommunity.id = event.params.copyCommunity.toHex();
newCommunity.previous = event.params.originalCommunity;
newCommunity.previous = event.params.originalCommunity.toHex();
newCommunity.startDayId = event.block.timestamp.toI32() / 86400;
newCommunity.beneficiaries = 0;
newCommunity.removedBeneficiaries = 0;
Expand All @@ -211,19 +231,24 @@ export function handleCommunityCopied(event: CommunityCopied): void {
newCommunity.estimatedFunds = BigDecimal.zero();
newCommunity.lastActivity = event.block.timestamp.toI32();
// remaining properties are the same
// newCommunity.state = originalCommunity.state;
newCommunity.state = originalCommunity.state;
// newCommunity.previous = event.params.originalCommunity;
// newCommunity.claimAmount = originalCommunity.claimAmount;
// newCommunity.originalClaimAmount = originalCommunity.originalClaimAmount;
// newCommunity.maxClaim = originalCommunity.maxClaim;
// newCommunity.maxTotalClaim = originalCommunity.maxTotalClaim;
// newCommunity.decreaseStep = originalCommunity.decreaseStep;
// newCommunity.baseInterval = originalCommunity.baseInterval;
// newCommunity.incrementInterval = originalCommunity.incrementInterval;
// newCommunity.maxBeneficiaries = originalCommunity.maxBeneficiaries;
// newCommunity.minTranche = originalCommunity.minTranche;
// newCommunity.maxTranche = originalCommunity.maxTranche;
newCommunity.claimAmount = originalCommunity.claimAmount;
newCommunity.originalClaimAmount = originalCommunity.originalClaimAmount;
newCommunity.maxClaim = originalCommunity.maxClaim;
newCommunity.maxTotalClaim = originalCommunity.maxTotalClaim;
newCommunity.decreaseStep = originalCommunity.decreaseStep;
newCommunity.baseInterval = originalCommunity.baseInterval;
newCommunity.incrementInterval = originalCommunity.incrementInterval;
newCommunity.maxBeneficiaries = originalCommunity.maxBeneficiaries;
newCommunity.minTranche = originalCommunity.minTranche;
newCommunity.maxTranche = originalCommunity.maxTranche;

ubi.communities += 1;
ubiDaily.communities += 1;

originalCommunity.save();
newCommunity.save();
ubi.save();
ubiDaily.save();
}
4 changes: 4 additions & 0 deletions tests/beneficiary.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,8 @@ test('split community and copy beneficiaries', () => {
handleBeneficiaryAdded(beneficiaryAddedEvent2);

assert.fieldEquals('CommunityEntity', communityAddress[0], 'beneficiaries', '2');
assert.fieldEquals('BeneficiaryEntity', beneficiaryAddress[0], 'community', communityAddress[0]);
assert.fieldEquals('BeneficiaryEntity', beneficiaryAddress[1], 'community', communityAddress[0]);

const copiedCommunity = createCommunityCopiedEvent(communityAddress[0], communityAddress[1]);

Expand All @@ -530,4 +532,6 @@ test('split community and copy beneficiaries', () => {

assert.fieldEquals('CommunityEntity', communityAddress[0], 'beneficiaries', '1');
assert.fieldEquals('CommunityEntity', communityAddress[1], 'beneficiaries', '1');
assert.fieldEquals('BeneficiaryEntity', beneficiaryAddress[0], 'community', communityAddress[1]);
assert.fieldEquals('BeneficiaryEntity', beneficiaryAddress[1], 'community', communityAddress[0]);
});
19 changes: 14 additions & 5 deletions tests/community.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
handleCommunityRemoved
} from '../src/mappings/communityAdmin';
import { handleCommunityEdited } from '../src/mappings/old/community';
import { log, store } from '@graphprotocol/graph-ts';

export {
handleCommunityAdded,
Expand Down Expand Up @@ -271,17 +272,25 @@ test('lock/unlock community', () => {
test('split community without users', () => {
clearStore();

const community = createCommunityAddedEvent(communityAddress[0], [managerAddress[0]], communityProps[0]);
const community0 = createCommunityAddedEvent(communityAddress[0], [managerAddress[0]], communityProps[0]);

handleCommunityAdded(community);
handleCommunityAdded(community0);

assert.fieldEquals('CommunityEntity', communityAddress[0], 'state', '0');

const copiedCommunity = createCommunityCopiedEvent(communityAddress[0], communityAddress[1]);
const copiedCommunity0 = createCommunityCopiedEvent(communityAddress[0], communityAddress[1]);
const copiedCommunity1 = createCommunityCopiedEvent(communityAddress[0], communityAddress[2]);

handleCommunityCopied(copiedCommunity);
handleCommunityCopied(copiedCommunity0);
handleCommunityCopied(copiedCommunity1);

assert.fieldEquals('CommunityEntity', communityAddress[1], 'state', '0');
assert.fieldEquals('CommunityEntity', communityAddress[0], 'next', communityAddress[1]);
assert.fieldEquals('CommunityEntity', communityAddress[2], 'state', '0');
assert.fieldEquals('CommunityEntity', communityAddress[0], 'next', `[${communityAddress[1]}, ${communityAddress[2]}]`);
assert.fieldEquals('CommunityEntity', communityAddress[1], 'previous', communityAddress[0]);
log.info('{}', [(store.get('CommunityEntity', communityAddress[2])!.get('next') === null).toString()]);
assert.fieldEquals('CommunityEntity', communityAddress[1], 'previous', communityAddress[0]);
assert.assertNull(store.get('CommunityEntity', communityAddress[1])!.get('next'));
assert.fieldEquals('CommunityEntity', communityAddress[2], 'previous', communityAddress[0]);
assert.assertNull(store.get('CommunityEntity', communityAddress[2])!.get('next'));
});
4 changes: 3 additions & 1 deletion tests/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { BigDecimal, BigInt } from '@graphprotocol/graph-ts';

const communityAddress: string[] = [
'0x1cad798788568098e51c5751fe03a8daa0c7eac6',
'0x6a15bd0445e00b1705e0f02ad2e60902c1cbcc3e'
'0x6a15bd0445e00b1705e0f02ad2e60902c1cbcc3e',
'0x3b0c1a3d5a9f8d7a4abaa41ab82f819b5b71937e',
'0x1c4b70a24eccb9b505b2a4cf3a2e5a006e25b7b2',
];
const managerAddress: string[] = [
'0x270ec2751810dae7a1e7377dc77f9098ac9b5bce',
Expand Down