Skip to content

Commit

Permalink
fix sql schema and primary key
Browse files Browse the repository at this point in the history
  • Loading branch information
colindickson committed Nov 7, 2024
1 parent ad453e5 commit 09ee88f
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 909 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ num-bigint = "0.4"
substreams = "0.5"
substreams-ethereum = "0.9"
substreams-entity-change = "1.3"
substreams-database-change = "1.2"
substreams-database-change = "1.3.0"
num-traits = "0.2.16"

[build-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[toolchain]
channel = "1.64.0"
channel = "1.70"
components = [ "rustfmt" ]
targets = [ "wasm32-unknown-unknown" ]
20 changes: 11 additions & 9 deletions schema.sql
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
CREATE TABLE IF NOT EXISTS balance_changes (
`contract` TEXT NOT NULL,
`owner` TEXT NOT NULL,
`amount` INT NOT NULL,
`old_balance` INT NOT NULL,
`new_balance` INT NOT NULL,
`transaction_id` TEXT NOT NULL,
`block_num` INT NOT NULL,
`timestamp` TEXT NOT NULL,
`change_type` INT NOT NULL,
"contract" TEXT NOT NULL,
"owner" TEXT NOT NULL,
"amount" NUMERIC NOT NULL,
"old_balance" NUMERIC NOT NULL,
"new_balance" NUMERIC NOT NULL,
"transaction_id" TEXT NOT NULL,
"block_num" INT NOT NULL,
"timestamp" TEXT NOT NULL,
"change_type" INT NOT NULL,
"call_index" INT NOT NULL,
PRIMARY KEY ("block_num", "transaction_id", "call_index")
);
30 changes: 0 additions & 30 deletions src/pb/sf.ethereum.substreams.v1.rs

This file was deleted.

857 changes: 0 additions & 857 deletions src/pb/sf.ethereum.type.v2.rs

This file was deleted.

13 changes: 8 additions & 5 deletions src/sinks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,26 @@ pub fn graph_out(_clock: Clock, balance_changes: BalanceChanges) -> Result<Entit
#[substreams::handlers::map]
pub fn db_out(clock: Clock, balance_changes: BalanceChanges) -> Result<DatabaseChanges, Error> {
let mut tables = substreams_database_change::tables::Tables::new();
let block_num = clock.number.to_string();
let timestamp = clock.timestamp.unwrap().seconds.to_string();
let block_num = clock.clone().number.to_string();
let timestamp = clock.clone().timestamp.unwrap().seconds.to_string();

for balance_change in balance_changes.balance_changes {
if balance_change.change_type == 0 {
continue;
}

let key = format!("{}:{}:{}", balance_change.contract, balance_change.owner, balance_change.transaction);

tables.create_row("balance_changes", key)
tables.create_row("balance_changes", [
("block_num", (&clock).number.to_string()),
("transaction_id", (&balance_change).transaction.to_string()),
("call_index", (&balance_change).call_index.to_string())
])
.set("contract", balance_change.contract)
.set("owner", balance_change.owner)
.set("amount", balance_change.transfer_value)
.set("old_balance", balance_change.old_balance)
.set("new_balance", balance_change.new_balance)
.set("transaction_id", balance_change.transaction)
.set("call_index", balance_change.call_index)
.set("block_num", &block_num)
.set("timestamp", &timestamp)
.set("change_type", balance_change.change_type);
Expand Down
1 change: 1 addition & 0 deletions subgraph.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ description: ERC20 Ethereum Balance Changes (powered by Substreams)
repository: https://github.com/streamingfast/substreams-erc20-balanace-changes.git
schema:
file: ./schema.graphql
image: logo.png

dataSources:
- kind: substreams
Expand Down
19 changes: 15 additions & 4 deletions substreams.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
specVersion: v0.1.0
package:
name: erc20_balance_changes
version: v1.2.0
version: v1.3.0
url: https://github.com/streamingfast/erc20-balance-changes
image: logo.png
doc: |
This Substreams extracts all ERC20/ERC721 transfers from Ethereum events for the full chain.
imports:
entities: https://github.com/streamingfast/substreams-sink-entity-changes/releases/download/v1.3.0/substreams-sink-entity-changes-v1.3.0.spkg
sql: https://github.com/streamingfast/substreams-sink-sql/releases/download/protodefs-v1.0.7/substreams-sink-sql-protodefs-v1.0.7.spkg
entity: https://github.com/streamingfast/substreams-entity-change/releases/download/v1.1.0/substreams-entity-change-v1.1.0.spkg
database_change: https://github.com/streamingfast/substreams-sink-database-changes/releases/download/v1.2.0/substreams-database-change-v1.2.0.spkg

binaries:
Expand Down Expand Up @@ -79,4 +79,15 @@ modules:
- source: sf.substreams.v1.Clock
- store: store_valid_balance_changes
output:
type: proto:erc20.types.v1.BalanceChangeStats
type: proto:erc20.types.v1.BalanceChangeStats

network: mainnet

sink:
module: db_out
type: sf.substreams.sink.sql.v1.Service
config:
schema: "./schema.sql"
engine: postgres
postgraphile_frontend:
enabled: true

0 comments on commit 09ee88f

Please sign in to comment.