diff --git a/src/bondMediator.ts b/src/bondMediator.ts index a91212f..bb4cafa 100644 --- a/src/bondMediator.ts +++ b/src/bondMediator.ts @@ -13,6 +13,9 @@ import { } from '../generated/schema'; import { + // bind'able contract instance of the PerformanceBondMediator + PerformanceBondMediator, + // events types AddPerformanceBond, AddCollateralWhitelist, AdminChanged, @@ -25,6 +28,7 @@ import { ERC20Sweep, GrantDaoRole, GrantGlobalRole, + Initialized, Paused, RemoveCollateralWhitelist, RevokeDaoRole, @@ -288,6 +292,40 @@ export function handleGrantGlobalRole(event: GrantGlobalRole): void { role.save(); } +// - Initialized(uint8) +export function handleInitialized(event: Initialized): void { + const mediator = PerformanceBondMediator.bind(event.address); + const factory = mediator.bondCreator(); + + let bondMediator = BondMediator.load(event.address.toHex()); + bondMediator = + bondMediator === null ? new BondMediator(event.address.toHex()) : bondMediator; + + let bondFactory = BondFactory.load(factory.toHex()); + bondFactory = + bondFactory === null + ? new BondFactory(factory.toHex()) + : bondFactory; + + bondMediator.factory = bondFactory.id; + + bondMediator.createdAtTimestamp = + bondMediator.createdAtTimestamp || event.block.timestamp; + bondMediator.lastUpdatedTimestamp = event.block.timestamp; + + bondMediator.save(); + + bondFactory.mediator = bondMediator.id; + bondFactory.factory = factory; + bondFactory.createdAtTimestamp = event.block.timestamp; + bondFactory.lastUpdatedTimestamp = event.block.timestamp; + + bondFactory.save(); + + // Bind to new template to capture events on the new BondFactory + PerformanceBondFactoryTemplate.create(factory); +} + // - Paused(address) export function handlePaused(event: Paused): void { let bondMediator = BondMediator.load(event.address.toHex()); diff --git a/src/stakingPoolMediator.ts b/src/stakingPoolMediator.ts index a1a7a14..eabeb24 100644 --- a/src/stakingPoolMediator.ts +++ b/src/stakingPoolMediator.ts @@ -13,6 +13,9 @@ import { } from '../generated/schema'; import { + // bind'able contract instance of the StakingPoolMediator + StakingPoolMediator as StakingPoolMediatorContract, + // events types AddCollateralWhitelist, AddStakingPool, AdminChanged, @@ -24,6 +27,7 @@ import { ERC20Sweep, GrantDaoRole, GrantGlobalRole, + Initialized, Paused, RemoveCollateralWhitelist, RevokeDaoRole, @@ -280,6 +284,42 @@ export function handleGrantGlobalRole(event: GrantGlobalRole): void { role.save(); } +// - Initialized(uint8) +export function handleInitialized(event: Initialized): void { + const mediator = StakingPoolMediatorContract.bind(event.address); + const factory = mediator.stakingPoolCreator(); + + let stakingPoolMediator = StakingPoolMediator.load(event.address.toHex()); + stakingPoolMediator = + stakingPoolMediator === null + ? new StakingPoolMediator(event.address.toHex()) + : stakingPoolMediator; + + let stakingPoolFactory = StakingPoolFactory.load(factory.toHex()); + stakingPoolFactory = + stakingPoolFactory === null + ? new StakingPoolFactory(factory.toHex()) + : stakingPoolFactory; + + stakingPoolMediator.factory = stakingPoolFactory.id; + + stakingPoolMediator.createdAtTimestamp = + stakingPoolMediator.createdAtTimestamp || event.block.timestamp; + stakingPoolMediator.lastUpdatedTimestamp = event.block.timestamp; + + stakingPoolMediator.save(); + + stakingPoolFactory.mediator = stakingPoolMediator.id; + stakingPoolFactory.factory = factory; + stakingPoolFactory.createdAtTimestamp = event.block.timestamp; + stakingPoolFactory.lastUpdatedTimestamp = event.block.timestamp; + + stakingPoolFactory.save(); + + // Bind to new template to capture events on the new StakingPoolFactory + StakingPoolFactoryTemplate.create(factory); +} + // - Paused(address) export function handlePaused(event: Paused): void { let stakingPoolMediator = StakingPoolMediator.load(event.address.toHex()); diff --git a/subgraph.template.yaml b/subgraph.template.yaml index ec47c70..456bf73 100644 --- a/subgraph.template.yaml +++ b/subgraph.template.yaml @@ -51,6 +51,8 @@ dataSources: handler: handleGrantDaoRole - event: GrantGlobalRole(bytes32,address,indexed address) handler: handleGrantGlobalRole + - event: Initialized(uint8) + handler: handleInitialized - event: Paused(address) handler: handlePaused - event: RemoveCollateralWhitelist(indexed uint256,indexed address,indexed address) @@ -146,6 +148,8 @@ dataSources: handler: handleGrantDaoRole - event: GrantGlobalRole(bytes32,address,indexed address) handler: handleGrantGlobalRole + - event: Initialized(uint8) + handler: handleInitialized - event: Paused(address) handler: handlePaused - event: RemoveCollateralWhitelist(indexed uint256,indexed address,indexed address) diff --git a/tests/bondMediator.test.ts b/tests/bondMediator.test.ts index 17a8b92..02572b1 100644 --- a/tests/bondMediator.test.ts +++ b/tests/bondMediator.test.ts @@ -1,5 +1,5 @@ import { Address, Bytes, ethereum } from '@graphprotocol/graph-ts'; -import { assert, clearStore, test } from 'matchstick-as/assembly/index'; +import { assert, clearStore, createMockedFunction, test } from 'matchstick-as/assembly/index'; import { BOND_ADDRESS, @@ -36,6 +36,7 @@ import { ERC20Sweep, GrantDaoRole, GrantGlobalRole, + Initialized, Paused, RemoveCollateralWhitelist, RevokeDaoRole, @@ -57,6 +58,7 @@ import { handleERC20Sweep, handleGrantDaoRole, handleGrantGlobalRole, + handleInitialized, handlePaused, handleRemoveCollateralWhitelist, handleRevokeDaoRole, @@ -483,6 +485,46 @@ test('Will handle GrantGlobalRole event', () => { clearStore(); }); +// - Initialized(uint8) +test('Will handle Initialized event', () => { + const instigator = Address.fromString('0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266'); + const factory = Address.fromString('0x70997970c51812dc3a010c7d01b50e0d17dc79c8'); + + const contractAddress = Address.fromString(BOND_MEDIATOR_ADDRESS); + createMockedFunction(contractAddress, "bondCreator", "bondCreator():(address)") + .returns([ethereum.Value.fromAddress(factory)]); + + createPerformanceBondFactory(); + createPerformanceBondMediator(); + + assert.fieldEquals('BondMediator', BOND_MEDIATOR_ADDRESS, 'factory', ''); + + handleInitialized( + new Initialized( + Address.fromString(BOND_MEDIATOR_ADDRESS), + defaultBigInt, + defaultBigInt, + defaultLogType, + newBlock(), + newTransaction(instigator.toHex()), + [ + new ethereum.EventParam('version', ethereum.Value.fromI32(0)), + ], + null + ) + ); + + assert.fieldEquals('BondMediator', BOND_MEDIATOR_ADDRESS, 'factory', factory.toHex()); + assert.fieldEquals( + 'BondMediator', + BOND_MEDIATOR_ADDRESS, + 'factories', + `[${factory.toHex()}]` + ); + + clearStore(); +}); + // - Paused(address) test('Will handle Paused event', () => { const instigator = Address.fromString('0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266'); diff --git a/tests/stakingPoolMediator.test.ts b/tests/stakingPoolMediator.test.ts index 38dde9e..de9a206 100644 --- a/tests/stakingPoolMediator.test.ts +++ b/tests/stakingPoolMediator.test.ts @@ -1,5 +1,5 @@ import { Address, Bytes, ethereum } from '@graphprotocol/graph-ts'; -import { assert, clearStore, test } from 'matchstick-as/assembly/index'; +import { assert, clearStore, createMockedFunction, test } from 'matchstick-as/assembly/index'; import { createStakingPool, @@ -36,6 +36,7 @@ import { ERC20Sweep, GrantDaoRole, GrantGlobalRole, + Initialized, Paused, RemoveCollateralWhitelist, RevokeDaoRole, @@ -57,6 +58,7 @@ import { handleERC20Sweep, handleGrantDaoRole, handleGrantGlobalRole, + handleInitialized, handlePaused, handleRemoveCollateralWhitelist, handleRevokeDaoRole, @@ -546,6 +548,51 @@ test('Will handle GrantGlobalRole event', () => { clearStore(); }); +// - Initialized(uint8) +test('Will handle Initialized event', () => { + const instigator = Address.fromString('0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266'); + const factory = Address.fromString('0x70997970c51812dc3a010c7d01b50e0d17dc79c8'); + + const contractAddress = Address.fromString(STAKINGPOOL_MEDIATOR_ADDRESS); + createMockedFunction(contractAddress, "stakingPoolCreator", "stakingPoolCreator():(address)") + .returns([ethereum.Value.fromAddress(factory)]); + + createStakingPoolFactory(); + createStakingPoolMediator(); + + assert.fieldEquals('StakingPoolMediator', STAKINGPOOL_MEDIATOR_ADDRESS, 'factory', ''); + + handleInitialized( + new Initialized( + Address.fromString(STAKINGPOOL_MEDIATOR_ADDRESS), + defaultBigInt, + defaultBigInt, + defaultLogType, + newBlock(), + newTransaction(instigator.toHex()), + [ + new ethereum.EventParam('version', ethereum.Value.fromI32(0)), + ], + null + ) + ); + + assert.fieldEquals( + 'StakingPoolMediator', + STAKINGPOOL_MEDIATOR_ADDRESS, + 'factory', + factory.toHex() + ); + assert.fieldEquals( + 'StakingPoolMediator', + STAKINGPOOL_MEDIATOR_ADDRESS, + 'factories', + `[${factory.toHex()}]` + ); + + clearStore(); +}); + // - Paused(address) test('Will handle Paused event', () => { const instigator = Address.fromString('0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266');