parent
0ca1daf2ba
commit
9f3dc8d92b
@ -0,0 +1,43 @@ |
||||
defmodule Explorer.Chain.Token.Instance do |
||||
@moduledoc """ |
||||
Represents an ERC 721 token instance and stores metadata defined in https://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md. |
||||
""" |
||||
|
||||
use Explorer.Schema |
||||
|
||||
alias Explorer.Chain.{Hash, Token} |
||||
alias Explorer.Chain.Token.Instance |
||||
|
||||
@typedoc """ |
||||
* `token_id` - ID of the token |
||||
* `token_contract_address_hash` - Address hash foreign key |
||||
* `metadata` - Token instance metadata |
||||
""" |
||||
|
||||
@type t :: %Instance{ |
||||
token_id: non_neg_integer(), |
||||
token_contract_address_hash: Hash.Address.t(), |
||||
metadata: Map.t() |
||||
} |
||||
|
||||
schema "token_instances" do |
||||
field(:token_id, :decimal, primary_key: true) |
||||
field(:metadata, :map) |
||||
|
||||
belongs_to( |
||||
:token, |
||||
Token, |
||||
foreign_key: :token_contract_address_hash, |
||||
references: :contract_address_hash, |
||||
type: Hash.Address, |
||||
primary_key: true |
||||
) |
||||
end |
||||
|
||||
# def changeset(%Instance{} = instance, params \\ %{}) do |
||||
# instance |
||||
# |> cast([:token_id, :metadata, :token_contract_address_hash]) |
||||
# |> validate_required([:token_id, :token_contract_address_hash]) |
||||
# |> foreign_key_constraint(:token_contract_address_hash) |
||||
# end |
||||
end |
@ -0,0 +1,18 @@ |
||||
defmodule Explorer.Repo.Migrations.CreateTokenInstances do |
||||
use Ecto.Migration |
||||
|
||||
def change do |
||||
create table(:token_instances, primary_key: false) do |
||||
# ERC-721 tokens have IDs |
||||
# 10^x = 2^256, x ~ 77.064, so 78 decimal digits will store the full 256-bits of a native EVM type |
||||
add(:token_id, :numeric, precision: 78, scale: 0, null: false, primary_key: true) |
||||
|
||||
add(:token_contract_address_hash, references(:tokens, column: :contract_address_hash, type: :bytea), |
||||
null: false, |
||||
primary_key: true |
||||
) |
||||
|
||||
add(:metadata, :jsonb) |
||||
end |
||||
end |
||||
end |
Loading…
Reference in new issue