-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
353 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
{ | ||
"optimism": { | ||
"PositionRegistry": { | ||
"address": "0x447BEbdE07F6115b2714fd3a28e311b9d4a9E6f6", | ||
"startBlock": 128171866 | ||
"address": "0xeE156D8ea7b96a5524CcC3CF9283ab85E80E9534", | ||
"startBlock": 131291973 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,14 @@ | ||
import { AccountDeployed } from '../../generated/EntryPoint/EntryPoint' | ||
import { SmartAccount } from '../../generated/schema' | ||
import { ADDRESS_ZERO, BIGINT_ZERO } from '../utils/constants' | ||
import { EntryPoint } from '../../generated/schema' | ||
import { loadOrCreateSmartAccount } from '../utils/entity' | ||
|
||
export function handleAccountDeployed(event: AccountDeployed): void { | ||
const smartAccount = new SmartAccount(event.params.sender) | ||
smartAccount.positionCount = BIGINT_ZERO | ||
smartAccount.positionRegistry = ADDRESS_ZERO | ||
smartAccount.save() | ||
let entryPoint = EntryPoint.load(event.address) | ||
if (!entryPoint) { | ||
entryPoint = new EntryPoint(event.address) | ||
} | ||
entryPoint.save() | ||
|
||
// this call will always end up creating smartAccount entity and that's the goal. | ||
loadOrCreateSmartAccount(event.params.sender) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
import { Address, Bytes, ethereum } from '@graphprotocol/graph-ts' | ||
import { ERC20, Transfer } from '../../generated/ERC20/ERC20' | ||
import { Token, Balance, EntryPoint, SmartAccount } from '../../generated/schema' | ||
import { ENTRY_POINT } from '../utils/config' | ||
|
||
// load or create token entity | ||
function loadOrCreateToken(event: ethereum.Event): Token | null { | ||
let token = Token.load(event.address) | ||
if (!token) { | ||
// bind address to call contract method | ||
const erc20 = ERC20.bind(event.address) | ||
|
||
const nameResult = erc20.try_name() | ||
if (nameResult.reverted) { | ||
return null | ||
} | ||
|
||
const symbolResult = erc20.try_symbol() | ||
if (symbolResult.reverted) { | ||
return null | ||
} | ||
|
||
const decimalsResult = erc20.try_decimals() | ||
if (decimalsResult.reverted) { | ||
return null | ||
} | ||
|
||
token = new Token(event.address) | ||
token.name = nameResult.value | ||
token.symbol = symbolResult.value | ||
token.decimals = decimalsResult.value | ||
token.save() | ||
} | ||
return token | ||
} | ||
|
||
// create or update balance entity | ||
function createOrUpdateBalance(tokenId: Bytes, accountId: Address): Balance { | ||
const id = tokenId.toHex().concat('-').concat(accountId.toHex()) | ||
let balance = Balance.load(id) | ||
if (!balance) { | ||
balance = new Balance(id) | ||
balance.token = tokenId | ||
balance.smartAccount = accountId | ||
} | ||
// Read balance from contract | ||
balance.value = ERC20.bind(Address.fromBytes(tokenId)).balanceOf(accountId) | ||
balance.save() | ||
return balance as Balance | ||
} | ||
|
||
// function createBalance(tokenId: Bytes, accountId: Address): Balance | null { | ||
// const id = tokenId.toHex().concat('-').concat(accountId.toHex()) | ||
// const balance = new Balance(id) | ||
// balance.token = tokenId | ||
// balance.smartAccount = accountId | ||
// // First time read balance from contract | ||
// // const result = ERC20.bind(Address.fromBytes(tokenId)).try_balanceOf(accountId) | ||
// // if (result.reverted) { | ||
// // return null | ||
// // } | ||
// // FIXME if 2 transfer are in same block then this read is reading final state and next transfer will messed up value | ||
// balance.value = ERC20.bind(Address.fromBytes(tokenId)).balanceOf(accountId) | ||
// balance.save() | ||
// return balance | ||
// } | ||
|
||
// function loadBalance(tokenId: Bytes, accountId: Address): Balance | null { | ||
// const id = tokenId.toHex().concat('-').concat(accountId.toHex()) | ||
// return Balance.load(id) | ||
// } | ||
|
||
// returns true if account exist in array | ||
// function doesAccountExist(smartAccounts: SmartAccount[], accountId: Address): boolean { | ||
// for (let i = 0; i < smartAccounts.length; i++) { | ||
// if (smartAccounts[i].id == accountId) { | ||
// return true | ||
// } | ||
// } | ||
// return false | ||
// } | ||
|
||
// // returns true if account exist in array | ||
// function doesAccountsExist(smartAccounts: SmartAccount[], from: Address, to: Address): boolean[] { | ||
// let isFromExist = false | ||
// let isToExist = false | ||
|
||
// for (let i = 0; i < smartAccounts.length; i++) { | ||
// if (smartAccounts[i].id == from) { | ||
// isFromExist = true | ||
// } else if (smartAccounts[i].id == to) { | ||
// isToExist = true | ||
// } | ||
// if (isFromExist && isToExist) { | ||
// break | ||
// } | ||
// } | ||
// return [isFromExist, isToExist] | ||
// } | ||
|
||
// returns true if account exist in array | ||
function doesAccountsExist(smartAccounts: SmartAccount[], from: Address, to: Address): boolean[] { | ||
let isFromExist = false | ||
let isToExist = false | ||
|
||
for (let i = 0; i < smartAccounts.length; i++) { | ||
if (smartAccounts[i].id == from) { | ||
isFromExist = true | ||
} else if (smartAccounts[i].id == to) { | ||
isToExist = true | ||
} | ||
if (isFromExist && isToExist) { | ||
break | ||
} | ||
} | ||
return [isFromExist, isToExist] | ||
} | ||
|
||
// handle transfer event | ||
export function handleTransfer(event: Transfer): void { | ||
const entryPoint = EntryPoint.load(ENTRY_POINT) | ||
|
||
if (!entryPoint) { | ||
return | ||
} | ||
const smartAccounts = entryPoint.smartAccounts.load() | ||
|
||
const from = event.params.from | ||
const to = event.params.to | ||
|
||
// Check whether SmartAccount exist in storage | ||
const output = doesAccountsExist(smartAccounts, from, to) | ||
const isFromExist = output[0] | ||
const isToExist = output[1] | ||
|
||
// if any of the smartAccount exist in storage then handle token and balance creation | ||
if (isFromExist || isToExist) { | ||
const token = loadOrCreateToken(event) | ||
if (!token) { | ||
return | ||
} | ||
if (isFromExist) { | ||
createOrUpdateBalance(token.id, from) | ||
} | ||
if (isToExist) { | ||
createOrUpdateBalance(token.id, to) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { Address } from '@graphprotocol/graph-ts' | ||
|
||
export const POSITION_REGISTRY = Address.fromString('0xeE156D8ea7b96a5524CcC3CF9283ab85E80E9534') | ||
export const ENTRY_POINT = Address.fromString('0x0000000071727De22E5E9d8BAf0edAc6f37da032') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Address } from '@graphprotocol/graph-ts' | ||
import { SmartAccount } from '../../generated/schema' | ||
import { ENTRY_POINT } from './config' | ||
import { ADDRESS_ZERO, BIGINT_ZERO } from './constants' | ||
|
||
export function loadOrCreateSmartAccount(id: Address): SmartAccount { | ||
let smartAccount = SmartAccount.load(id) | ||
if (!smartAccount) { | ||
smartAccount = new SmartAccount(id) | ||
smartAccount.entryPoint = ENTRY_POINT | ||
smartAccount.positionCount = BIGINT_ZERO | ||
// PositionRegistry will be updated when smartAccount deploy a position. | ||
smartAccount.positionRegistry = ADDRESS_ZERO | ||
smartAccount.save() | ||
} | ||
return smartAccount | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.