diff --git a/generated/schema.ts b/generated/schema.ts index 07a0f85..d7db018 100644 --- a/generated/schema.ts +++ b/generated/schema.ts @@ -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(value)); + this.set("previous", Value.fromString(value)); } } - get next(): Bytes | null { + get next(): Array | 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 | null) { if (!value) { this.unset("next"); } else { - this.set("next", Value.fromBytes(value)); + this.set("next", Value.fromStringArray(>value)); } } diff --git a/package.json b/package.json index 0f7b5de..bb5fb03 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "subgraph", + "name": "@impact-market/subgraph", "license": "Apache-2.0", "version": "1.0.0", "files": ["dist"], @@ -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", @@ -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", diff --git a/schema.graphql b/schema.graphql index ba8847e..980df1a 100644 --- a/schema.graphql +++ b/schema.graphql @@ -1,6 +1,3 @@ -scalar Bytes -scalar BigDecimal - """ Contributions of an asset to an entity """ @@ -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) """ diff --git a/src/common/manager.ts b/src/common/manager.ts index c272101..436aaca 100644 --- a/src/common/manager.ts +++ b/src/common/manager.ts @@ -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 diff --git a/src/mappings/community.ts b/src/mappings/community.ts index 9ada941..04ff255 100644 --- a/src/mappings/community.ts +++ b/src/mappings/community.ts @@ -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 diff --git a/src/mappings/communityAdmin.ts b/src/mappings/communityAdmin.ts index f1b1f85..1897979 100644 --- a/src/mappings/communityAdmin.ts +++ b/src/mappings/communityAdmin.ts @@ -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(); + } + + arrayNext.push(event.params.communityAddress.toHex()); + previousCommunity.next = arrayNext; // create new community community.startDayId = previousCommunity.startDayId; community.state = previousCommunity.state; @@ -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(); community.minTranche = previousCommunity.minTranche; community.maxTranche = previousCommunity.maxTranche; @@ -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(); + } - 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; @@ -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(); } diff --git a/tests/beneficiary.test.ts b/tests/beneficiary.test.ts index e49bc8e..b42deb7 100644 --- a/tests/beneficiary.test.ts +++ b/tests/beneficiary.test.ts @@ -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]); @@ -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]); }); diff --git a/tests/community.test.ts b/tests/community.test.ts index 025c766..89ba1f0 100644 --- a/tests/community.test.ts +++ b/tests/community.test.ts @@ -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, @@ -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')); }); diff --git a/tests/utils/constants.ts b/tests/utils/constants.ts index ec35aaa..c7a103b 100644 --- a/tests/utils/constants.ts +++ b/tests/utils/constants.ts @@ -2,7 +2,9 @@ import { BigDecimal, BigInt } from '@graphprotocol/graph-ts'; const communityAddress: string[] = [ '0x1cad798788568098e51c5751fe03a8daa0c7eac6', - '0x6a15bd0445e00b1705e0f02ad2e60902c1cbcc3e' + '0x6a15bd0445e00b1705e0f02ad2e60902c1cbcc3e', + '0x3b0c1a3d5a9f8d7a4abaa41ab82f819b5b71937e', + '0x1c4b70a24eccb9b505b2a4cf3a2e5a006e25b7b2', ]; const managerAddress: string[] = [ '0x270ec2751810dae7a1e7377dc77f9098ac9b5bce',