Skip to content

Commit

Permalink
fix: add initialized events to pull factory details
Browse files Browse the repository at this point in the history
  • Loading branch information
grezle committed Jul 7, 2022
1 parent e7813c5 commit 5a9c8b1
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 2 deletions.
38 changes: 38 additions & 0 deletions src/bondMediator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import {
} from '../generated/schema';

import {
// bind'able contract instance of the PerformanceBondMediator
PerformanceBondMediator,
// events types
AddPerformanceBond,
AddCollateralWhitelist,
AdminChanged,
Expand All @@ -25,6 +28,7 @@ import {
ERC20Sweep,
GrantDaoRole,
GrantGlobalRole,
Initialized,
Paused,
RemoveCollateralWhitelist,
RevokeDaoRole,
Expand Down Expand Up @@ -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());
Expand Down
40 changes: 40 additions & 0 deletions src/stakingPoolMediator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import {
} from '../generated/schema';

import {
// bind'able contract instance of the StakingPoolMediator
StakingPoolMediator as StakingPoolMediatorContract,
// events types
AddCollateralWhitelist,
AddStakingPool,
AdminChanged,
Expand All @@ -24,6 +27,7 @@ import {
ERC20Sweep,
GrantDaoRole,
GrantGlobalRole,
Initialized,
Paused,
RemoveCollateralWhitelist,
RevokeDaoRole,
Expand Down Expand Up @@ -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());
Expand Down
4 changes: 4 additions & 0 deletions subgraph.template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
44 changes: 43 additions & 1 deletion tests/bondMediator.test.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -36,6 +36,7 @@ import {
ERC20Sweep,
GrantDaoRole,
GrantGlobalRole,
Initialized,
Paused,
RemoveCollateralWhitelist,
RevokeDaoRole,
Expand All @@ -57,6 +58,7 @@ import {
handleERC20Sweep,
handleGrantDaoRole,
handleGrantGlobalRole,
handleInitialized,
handlePaused,
handleRemoveCollateralWhitelist,
handleRevokeDaoRole,
Expand Down Expand Up @@ -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');
Expand Down
49 changes: 48 additions & 1 deletion tests/stakingPoolMediator.test.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -36,6 +36,7 @@ import {
ERC20Sweep,
GrantDaoRole,
GrantGlobalRole,
Initialized,
Paused,
RemoveCollateralWhitelist,
RevokeDaoRole,
Expand All @@ -57,6 +58,7 @@ import {
handleERC20Sweep,
handleGrantDaoRole,
handleGrantGlobalRole,
handleInitialized,
handlePaused,
handleRemoveCollateralWhitelist,
handleRevokeDaoRole,
Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit 5a9c8b1

Please sign in to comment.