-
Notifications
You must be signed in to change notification settings - Fork 2
feat: add gas used and transactions fee #1
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
base: main
Are you sure you want to change the base?
Changes from all commits
997697e
8bc0fe7
8c3b943
b672303
066b327
8cb0458
82d2ce9
9413931
600a8b6
77acc2e
d21bfd7
42bf0df
9fa1230
96b2c42
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| .gitignore | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| # .dockerignore is a symbolic link to .gitignore | ||
| .bin | ||
| .vscode | ||
| build | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| version: '3.4' | ||
| services: | ||
| matchstick: | ||
| image: cartesi/subgraph-matchstick:dev | ||
| build: | ||
| dockerfile: tests/.docker | ||
| target: base | ||
| container_name: cartesi-subgraph-matchstick | ||
| volumes: | ||
| - .:/matchstick |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,75 +11,11 @@ | |
| // under the License. | ||
|
|
||
| import { Rewarded } from "../generated/PoS-1.0/PoS" | ||
| import { BlockProduced } from "../generated/BlockSelector-1.0/BlockSelector" | ||
| import { Block } from "../generated/schema" | ||
| import * as chains from "./chain" | ||
| import * as nodes from "./node" | ||
| import * as summary from "./summary" | ||
| import * as users from "./user" | ||
| import { handleRewardedInner, handleBlockProduced } from "./block-common" | ||
|
|
||
| export function handleRewarded(event: Rewarded): void { | ||
| let reward = event.params.userReward.plus(event.params.beneficiaryReward) | ||
|
|
||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Refacto: similar code from |
||
| // handle user | ||
| let user = users.loadOrCreate(event.params.user) | ||
| user.totalBlocks++ | ||
| user.totalReward = user.totalReward.plus(reward) | ||
| user.save() | ||
|
|
||
| // handle node | ||
| let node = nodes.loadOrCreate( | ||
| event.params.user, | ||
| event.params.worker, | ||
| event.block.timestamp | ||
| ) | ||
| node.totalBlocks++ | ||
| node.status = "Authorized" | ||
| node.totalReward = node.totalReward.plus(reward) | ||
| node.save() | ||
|
|
||
| // handle chain | ||
| let posAddress = event.address.toHex() | ||
| let chain = chains.loadOrCreate( | ||
| posAddress, | ||
| event.params.index.toI32(), | ||
| event.block.timestamp | ||
| ) | ||
| chain.totalBlocks++ | ||
| chain.totalReward = chain.totalReward.plus(reward) | ||
| chain.save() | ||
|
|
||
| // Rewarded is always called before BlockProduced, so create Block here | ||
| let block = Block.load(event.transaction.hash.toHex()) | ||
| if (block == null) { | ||
| block = new Block(event.transaction.hash.toHex()) | ||
| block.timestamp = event.block.timestamp | ||
| block.gasPrice = event.transaction.gasPrice | ||
| block.gasLimit = event.transaction.gasLimit | ||
| } | ||
| block.chain = chain.id | ||
| block.reward = reward | ||
| block.producer = event.params.user.toHex() | ||
| block.node = event.params.worker.toHex() | ||
| block.save() | ||
|
|
||
| // handle global summary | ||
| let s = summary.loadOrCreate() | ||
| s.totalBlocks++ | ||
| s.totalReward = s.totalReward.plus(reward) | ||
| s.save() | ||
| handleRewardedInner<Rewarded>(event, reward) | ||
| } | ||
|
|
||
| export function handleBlockProduced(event: BlockProduced): void { | ||
| // load Block and fill other properties | ||
| let block = Block.load(event.transaction.hash.toHex()) | ||
| if (block == null) { | ||
| block = new Block(event.transaction.hash.toHex()) | ||
| block.timestamp = event.block.timestamp | ||
| block.gasPrice = event.transaction.gasPrice | ||
| block.gasLimit = event.transaction.gasLimit | ||
| } | ||
| block.number = event.params.blockNumber.toI32() | ||
| block.difficulty = event.params.difficulty | ||
| block.save() | ||
| } | ||
| export { handleBlockProduced } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| import { Address, BigInt, ethereum, log } from "@graphprotocol/graph-ts" | ||
| import { Block } from "../generated/schema" | ||
| import * as nodes from "./node" | ||
| import * as chains from "./chain" | ||
| import * as users from "./user" | ||
| import * as summary from "./summary" | ||
| import { BlockProduced } from "../generated/BlockSelector/BlockSelector" | ||
|
|
||
| function createBlock(event: ethereum.Event): Block { | ||
| const block = new Block(event.transaction.hash.toHex()) | ||
| block.timestamp = event.block.timestamp | ||
| block.gasPrice = event.transaction.gasPrice | ||
| block.gasLimit = event.transaction.gasLimit | ||
| block.gasUsed = (event.receipt as ethereum.TransactionReceipt).gasUsed | ||
| block.transactionFee = block.gasUsed.times(block.gasPrice) | ||
| return block | ||
| } | ||
|
|
||
| abstract class RewardedCommonParams { | ||
| abstract get index(): BigInt | ||
| abstract get worker(): Address | ||
| abstract get user(): Address | ||
| } | ||
|
|
||
| abstract class RewardedCommon extends ethereum.Event { | ||
| abstract get params(): RewardedCommonParams | ||
| } | ||
|
|
||
| export function handleRewardedInner<T extends RewardedCommon>( | ||
| event: T, | ||
| reward: BigInt | ||
| ): void { | ||
| // handle node | ||
| let node = nodes.loadOrCreate( | ||
| event.params.user, | ||
| event.params.worker, | ||
| event.block.timestamp | ||
| ) | ||
| node.totalBlocks++ | ||
| node.status = "Authorized" | ||
| node.totalReward = node.totalReward.plus(reward) | ||
| node.save() | ||
|
|
||
| // handle chain | ||
| let posAddress = event.address.toHex() | ||
| let chain = chains.loadOrCreate( | ||
| posAddress, | ||
| event.params.index.toI32(), | ||
| event.block.timestamp | ||
| ) | ||
| chain.totalBlocks++ | ||
| chain.totalReward = chain.totalReward.plus(reward) | ||
| chain.save() | ||
|
|
||
| // Rewarded is always called before BlockProduced, so create Block here | ||
| let block = Block.load(event.transaction.hash.toHex()) | ||
| if (block == null) { | ||
| block = createBlock(event) | ||
| } | ||
| block.chain = chain.id | ||
| block.reward = reward | ||
| block.producer = event.params.user.toHex() | ||
| block.node = event.params.worker.toHex() | ||
| block.save() | ||
|
|
||
| // handle user | ||
| let user = users.loadOrCreate(event.params.user) | ||
| user.totalBlocks++ | ||
| user.totalReward = user.totalReward.plus(reward) | ||
| user.totalTransactionFee = user.totalTransactionFee.plus( | ||
| block.transactionFee | ||
| ) | ||
|
Comment on lines
+70
to
+72
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this big code block that was moved, those 3 lines were added |
||
| user.save() | ||
|
|
||
| // handle global summary | ||
| let s = summary.loadOrCreate() | ||
| s.totalBlocks++ | ||
| s.totalReward = s.totalReward.plus(reward) | ||
| s.totalTransactionFee = s.totalTransactionFee.plus(block.transactionFee) | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this big code block that was moved, this line was also added |
||
| s.save() | ||
| } | ||
|
|
||
| export function handleBlockProduced(event: BlockProduced): void { | ||
| // load Block and fill other properties | ||
| let block = Block.load(event.transaction.hash.toHex()) | ||
| if (block == null) { | ||
| block = createBlock(event) | ||
| } | ||
| block.number = event.params.blockNumber.toI32() | ||
| block.difficulty = event.params.difficulty | ||
| block.save() | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a symlink to
.gitignore