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

[SUBGRAPH] approval mapping #1878

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 17 additions & 1 deletion packages/subgraph/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -1475,13 +1475,29 @@ type ApprovalEvent implements Event @entity(immutable: true) {
logIndex: BigInt!
order: BigInt!

"""
The address that will be granting allowance to transfer ERC20/NFT.
"""
owner: Account!

"""
The address that will be granted allowance to transfer the NFT.
The address that will be granted allowance to transfer ERC20/NFT.
"""
to: Account!

"""
Indicates whether the event was emitted for the approval of an NFT.
"""
isNFTApproval: Boolean!

"""
If `amount` is non-zero, this event was emitted for the approval of an ERC20.
Tne amount of ERC20 tokens that will be granted allowance to transfer.
"""
amount: BigInt!

"""
If `tokenId` is non-zero, this event was emitted for the approval of an NFT.
The id of the NFT that will be granted allowance to transfer.
The id is: uint256(keccak256(abi.encode(block.chainid, superToken, sender, receiver)))
"""
Expand Down
4 changes: 3 additions & 1 deletion packages/subgraph/src/mappings/flowNFT.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
MetadataUpdateEvent,
TransferEvent,
} from "../../generated/schema";
import { createEventID, initializeEventEntity } from "../utils";
import { BIG_INT_ONE, createEventID, initializeEventEntity } from "../utils";

export function handleApproval(event: Approval): void {
const eventId = createEventID("Approval", event);
Expand All @@ -19,6 +19,8 @@ export function handleApproval(event: Approval): void {
ev.owner = event.params.owner.toHex();
ev.to = event.params.approved.toHex();
ev.tokenId = event.params.tokenId;
ev.amount = BIG_INT_ONE.neg();
ev.isNFTApproval = true;

ev.save();
}
Expand Down
12 changes: 12 additions & 0 deletions packages/subgraph/src/mappings/superToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import {
AgreementLiquidatedByEvent,
AgreementLiquidatedV2Event,
ApprovalEvent,
BurnedEvent,
MintedEvent,
SentEvent,
Expand Down Expand Up @@ -452,6 +453,17 @@ function _createTransferEventEntity(event: Transfer): void {
}

export function handleApproval(event: Approval): void {
const eventId = createEventID("Approval", event);
const ev = new ApprovalEvent(eventId);
initializeEventEntity(ev, event, []);
kasparkallas marked this conversation as resolved.
Show resolved Hide resolved
ev.owner = event.params.owner.toHex();
ev.to = event.params.spender.toHex();
ev.tokenId = BIG_INT_ZERO;
ev.amount = event.params.value;
ev.isNFTApproval = false;

ev.save();

// The entity named `FlowOperators` which currently holds all the user access and approval settings will be renamed to `AccessSettings`.
const flowOperator = getOrInitFlowOperator(
event.block,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
} from "../../../src/mappings/superToken";
import { BIG_INT_ONE, BIG_INT_ZERO, encode, ZERO_ADDRESS } from "../../../src/utils";
import { assertEmptyTokenStatisticProperties, assertEventBaseProperties, assertTokenStatisticProperties } from "../../assertionHelpers";
import { alice, bob, cfaV1Address, charlie, DEFAULT_DECIMALS, delta, FAKE_INITIAL_BALANCE, maticXName, maticXSymbol } from "../../constants";
import { alice, bob, cfaV1Address, charlie, DEFAULT_DECIMALS, delta, FAKE_INITIAL_BALANCE, maticXName, maticXSymbol, TRUE } from "../../constants";
import { getETHAddress, getETHUnsignedBigInt, stringToBytes } from "../../converters";
import { createStream, createStreamRevision } from "../../mockedEntities";
import { mockedGetAppManifest, mockedGetHost, mockedHandleSuperTokenInitRPCCalls, mockedRealtimeBalanceOf } from "../../mockedFunctions";
Expand All @@ -28,6 +28,7 @@ import {
createBurnedEvent,
createMintedEvent,
createSentEvent,
createSuperTokenApprovalEvent,
createTokenDowngradedEvent,
createTokenUpgradedEvent,
createTransferEvent,
Expand Down Expand Up @@ -337,6 +338,28 @@ describe("SuperToken Mapper Unit Tests", () => {
assert.fieldEquals("TokenDowngradedEvent", id, "amount", amount.toString());
});

test("handleApproval() - Should create a new ApprovalEvent entity", () => {
const owner = alice;
const spender = bob;
const value = BigInt.fromI32(100);

const superTokenApprovalEvent = createSuperTokenApprovalEvent(
owner,
spender,
value
);

const id = assertEventBaseProperties(
superTokenApprovalEvent,
"TokenUpgraded"
);
assert.fieldEquals("ApprovalEvent", id, "owner", owner);
assert.fieldEquals("ApprovalEvent", id, "spender", spender);
assert.fieldEquals("ApprovalEvent", id, "amount", value.toString());
assert.fieldEquals("ApprovalEvent", id, "isNFTApproval", TRUE);
assert.fieldEquals("ApprovalEvent", id, "tokenId", "0");
});

test("handleTransfer() - Should create a new TransferEvent entity", () => {
const from = alice;
const to = bob;
Expand Down
14 changes: 14 additions & 0 deletions packages/subgraph/tests/superToken/superToken.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,20 @@ export function createTokenUpgradedEvent(
return newTokenUpgradedEvent;
}

export function createSuperTokenApprovalEvent(
owner: string,
spender: string,
value: BigInt
): Approval {
const newApprovalEvent = changetype<Approval>(newMockEvent());
newApprovalEvent.parameters = new Array();
newApprovalEvent.parameters.push(getAddressEventParam("owner", owner));
newApprovalEvent.parameters.push(getAddressEventParam("spender", spender));
newApprovalEvent.parameters.push(getBigIntEventParam("value", value));

return newApprovalEvent;
}

export function createTokenDowngradedEvent(
account: string,
amount: BigInt
Expand Down
Loading