Skip to content

Commit

Permalink
multiple things idk what precisely tbh
Browse files Browse the repository at this point in the history
  • Loading branch information
0xLucqs committed Nov 7, 2024
1 parent 6354cd7 commit 2baff74
Show file tree
Hide file tree
Showing 20 changed files with 451 additions and 22 deletions.
1 change: 0 additions & 1 deletion backend/lib/peach/event.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ defmodule Peach.Event do
field :description, :string
field :location, :string
field :cover, :string
field :onchain, :boolean, default: false
field :treasury, :string

has_many :ticket_tiers, Peach.TicketTier
Expand Down
17 changes: 13 additions & 4 deletions backend/lib/peach/events.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule Peach.Events do
alias Peach.CalldataBuilder
alias Peach.Config
alias Peach.Event
alias Peach.OnchainEvent
alias Peach.Repo
alias Peach.TicketTiers
import Ecto.Query
Expand Down Expand Up @@ -67,7 +68,9 @@ defmodule Peach.Events do
{:ok,
Repo.all(
from e in Event,
where: e.end >= ^datetime and e.id > ^event_id,
join: oe in OnchainEvent,
on: oe.event_id == e.id,
where: e.end >= ^datetime and e.id > ^event_id and oe.onchain,
order_by: [asc: e.start, asc: e.id],
limit: ^first
)}
Expand All @@ -89,7 +92,9 @@ defmodule Peach.Events do
{:ok,
Repo.all(
from e in Event,
where: e.end >= ^datetime and e.id > ^event_id,
join: oe in OnchainEvent,
on: oe.event_id == e.id,
where: e.end >= ^datetime and e.id > ^event_id and oe.onchain,
order_by: [asc: e.start, asc: e.id],
limit: ^@default_limit
)}
Expand All @@ -108,7 +113,9 @@ defmodule Peach.Events do
{:ok,
Repo.all(
from e in Event,
where: e.end >= ^datetime and e.id > ^@default_event_id,
join: oe in OnchainEvent,
on: oe.event_id == e.id,
where: e.end >= ^datetime and e.id > @default_event_id and oe.onchain,
order_by: [asc: e.start, asc: e.id],
limit: ^first
)}
Expand All @@ -127,7 +134,9 @@ defmodule Peach.Events do
{:ok,
Repo.all(
from e in Event,
where: e.end >= ^datetime and e.id > @default_event_id,
join: oe in OnchainEvent,
on: oe.event_id == e.id,
where: e.end >= ^datetime and e.id > @default_event_id and oe.onchain,
order_by: [asc: e.start, asc: e.id],
limit: @default_limit
)}
Expand Down
22 changes: 22 additions & 0 deletions backend/lib/peach/onchain_event.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
defmodule Peach.OnchainEvent do
@moduledoc """
Defines an onchain event object for the peach app
"""
use Ecto.Schema
import Ecto.Changeset

@derive {Jason.Encoder, only: [:onchain, :event_id]}
@primary_key {:id, :string, autogenerate: false}
schema "onchain_events" do
field :onchain, :boolean, default: false
field :_cursor, :integer
belongs_to :event, Peach.Event, foreign_key: :event_id
end

@doc false
def changeset(onchain_event, attrs) do
onchain_event
|> cast(attrs, [:onchain])
|> validate_required([:onchain])
end
end
13 changes: 11 additions & 2 deletions backend/lib/peach/ticket.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,21 @@ defmodule Peach.Ticket do
use Ecto.Schema
import Ecto.Changeset

@primary_key {:id, :string, autogenerate: false}
schema "tickets" do
field :block_hash, :string
field :block_number, :integer
field :block_timestamp, :utc_datetime_usec
field :transaction_hash, :string
field :transfer_id, :string
field :index_in_block, :integer
field :owner, :string
field :balance, :string
field :created_at, :utc_datetime
field :_cursor, :integer

belongs_to :ticket_tier, Peach.TicketTier

timestamps(type: :utc_datetime)
belongs_to :event, Peach.Event
end

@doc false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ defmodule Peach.Repo.Migrations.CreateEvents do
add :start, :utc_datetime
add :end, :utc_datetime
add :cover, :string
add :onchain, :boolean, default: false
add :treasury, :string

timestamps(type: :utc_datetime)
Expand Down
18 changes: 14 additions & 4 deletions backend/priv/repo/migrations/20241009142013_create_tickets.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,23 @@ defmodule Peach.Repo.Migrations.CreateTickets do
use Ecto.Migration

def change do
create table(:tickets) do
add :owner, :string
create table(:tickets, primary_key: false) do
add :id, :string, primary_key: true
add :ticket_tier_id, references(:ticket_tiers, on_delete: :nothing)

timestamps(type: :utc_datetime)
add :event_id, references(:events, on_delete: :nothing)
add :block_hash, :string
add :block_number, :integer
add :block_timestamp, :utc_datetime_usec
add :transaction_hash, :string
add :transfer_id, :string
add :index_in_block, :integer
add :owner, :string
add :balance, :string
add :created_at, :utc_datetime
add :_cursor, :bigint
end

create index(:tickets, [:ticket_tier_id])
create index(:tickets, [:event_id])
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
defmodule Peach.Repo.Migrations.CreateOnchainEvents do
use Ecto.Migration

def change do
create table(:onchain_events, primary_key: false) do
add :id, :string, primary_key: true
add :onchain, :boolean, default: false
add :_cursor, :bigint
add :event_id, references(:events, on_delete: :nothing)
end
end
end
1 change: 0 additions & 1 deletion backend/test/peach_web/controllers/create_event_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ defmodule PeachWeb.EventCreateControllerTest do
assert event.description == "A conference about blockchain technology."
assert event.location == "San Francisco, CA"
assert event.cover == "https://example.com/cover.jpg"
assert not event.onchain

# Check that the ticket tiers were created
ticket_tiers = Repo.all(from tt in TicketTier, where: tt.event_id == ^event.id)
Expand Down
83 changes: 82 additions & 1 deletion backend/test/peach_web/controllers/get_events_test.exs
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
defmodule PeachWeb.GetEventControllerTest do
use PeachWeb.ConnCase, async: true
alias Peach.Event
alias Peach.OnchainEvent
alias Peach.Repo
alias Peach.TicketTier

setup do
# Insert sample events
event1 =
Repo.insert!(%Event{
description: "A conference about blockchain technology.",
location: "San Francisco, CA",
cover: "https://example.com/cover.jpg",
ticket_tiers: [
%TicketTier{
name: "General Admission",
description: "Access to all sessions",
price: 5,
max_supply: 100
},
%TicketTier{
name: "VIP",
description: "Access to VIP sessions and perks",
price: 10,
max_supply: 20
}
],
name: "Past Event",
start: ~N[2024-11-05 09:00:00],
end: ~N[2024-11-06 17:00:00],
Expand All @@ -15,19 +34,81 @@ defmodule PeachWeb.GetEventControllerTest do

event2 =
Repo.insert!(%Event{
description: "A conference about blockchain technology.",
location: "San Francisco, CA",
cover: "https://example.com/cover.jpg",
ticket_tiers: [
%TicketTier{
name: "General Admission",
description: "Access to all sessions",
price: 5,
max_supply: 100
},
%TicketTier{
name: "VIP",
description: "Access to VIP sessions and perks",
price: 10,
max_supply: 20
}
],
name: "Current Event",
start: ~N[2024-11-10 09:00:00],
end: ~N[2024-11-12 17:00:00]
end: ~N[2024-11-12 17:00:00],
treasury: "0xbeef"
})

event3 =
Repo.insert!(%Event{
description: "A conference about blockchain technology.",
location: "San Francisco, CA",
cover: "https://example.com/cover.jpg",
ticket_tiers: [
%TicketTier{
name: "General Admission",
description: "Access to all sessions",
price: 5,
max_supply: 100
},
%TicketTier{
name: "VIP",
description: "Access to VIP sessions and perks",
price: 10,
max_supply: 20
}
],
name: "Future Event",
start: ~N[2024-11-15 09:00:00],
end: ~N[2024-11-17 17:00:00],
treasury: "0xbeef"
})

Repo.insert!(%Event{
description: "A conference about blockchain technology.",
location: "San Francisco, CA",
cover: "https://example.com/cover.jpg",
ticket_tiers: [
%TicketTier{
name: "General Admission",
description: "Access to all sessions",
price: 5,
max_supply: 100
},
%TicketTier{
name: "VIP",
description: "Access to VIP sessions and perks",
price: 10,
max_supply: 20
}
],
name: "Not onchain Event",
start: ~N[2024-11-15 09:00:00],
end: ~N[2024-11-17 17:00:00],
treasury: "0xbeef"
})

Repo.insert(%OnchainEvent{id: "0x01_0x02", event_id: event1.id, onchain: true})
Repo.insert(%OnchainEvent{id: "0x02_0x03", event_id: event2.id, onchain: true})
Repo.insert(%OnchainEvent{id: "0x03_0x04", event_id: event3.id, onchain: true})
{:ok, event1: event1, event2: event2, event3: event3}
end

Expand Down
12 changes: 8 additions & 4 deletions backend/test/peach_web/controllers/ticket_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,30 @@ defmodule PeachWeb.TicketControllerTest do
Repo.insert!(%Ticket{
owner: "0xdead",
# Using tier_id from the association
ticket_tier_id: vip_tier.id
ticket_tier_id: vip_tier.id,
event_id: event.id
})

# Create tickets for the user address associated with the tiers
vip_ticket =
Repo.insert!(%Ticket{
owner: "0xdead",
# Using tier_id from the association
ticket_tier_id: vip_tier.id
ticket_tier_id: vip_tier.id,
event_id: event.id
})

Repo.insert!(%Ticket{
owner: "0xdead",
ticket_tier_id: standard_tier.id
ticket_tier_id: standard_tier.id,
event_id: event.id
})

standard_ticket =
Repo.insert!(%Ticket{
owner: "0xdead2",
ticket_tier_id: standard_tier.id
ticket_tier_id: standard_tier.id,
event_id: event.id
})

# Make the created data available for all tests
Expand Down
3 changes: 0 additions & 3 deletions backend/test/peach_web/controllers/update_event_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ defmodule PeachWeb.EventUpdateControllertest do
description: "Original description",
location: "Original location",
cover: "https://example.com/original_cover.jpg",
onchain: false,
treasury: "0xdead"
}

Expand Down Expand Up @@ -48,7 +47,6 @@ defmodule PeachWeb.EventUpdateControllertest do
assert event.description == acc.description
assert event.location == acc.location
assert event.cover == acc.cover
assert not event.onchain
acc
end)
end
Expand Down Expand Up @@ -79,7 +77,6 @@ defmodule PeachWeb.EventUpdateControllertest do
assert event.description == expected_event.description
assert event.location == expected_event.location
assert event.cover == expected_event.cover
assert not event.onchain
end)
end
end
1 change: 1 addition & 0 deletions indexer/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vscode
18 changes: 18 additions & 0 deletions indexer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Notice that the base image is build from scratch (not an OS like Ubuntu),
# so the binary is in a location that depends on the build.
# For this reason we stick to a specific version and architecture.
#
# When updating the image you also need to update the entrypoint below.
#
# - docker image pull quay.io/apibara/sink-postgres:0.7.0-x86_64
# - docker image inspect quay.io/apibara/sink-postgres:0.7.0-x86_64 | jq '.[].Config.Entrypoint'
FROM quay.io/apibara/sink-postgres:0.7.0-x86_64

WORKDIR /app
COPY ./src/* /app/
ENV SN_NETWORK=SN_SEPOLIA
ENV STARTING_BLOCK=287896
ENV POSTGRES_CONNECTION_STRING=postgresql://postgres:[email protected]:5432/peach_dev
ENV AUTH_TOKEN=dna_MTNdSFU3zwM9tdQBdg1s

ENTRYPOINT ["/nix/store/rh1g8pb7wfnyr527jfmkkc5lm3sa1f0l-apibara-sink-postgres-0.7.0/bin/apibara-sink-postgres"]
21 changes: 21 additions & 0 deletions indexer/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Indexers Setup

Current Status :

| Indexer | Dev | Prod |
| --------------- | --- | ---- |
| Events creation | | |
| Events transfer | | |

To run the indexer :

```sh
AUTH_TOKEN=<AUTH_TOKEN> docker-compose up
```

To configure the k8s indexer :

```sh
cp envs.example.yaml envs.yaml
kubectl apply -f envs.yaml
```
Loading

0 comments on commit 2baff74

Please sign in to comment.