Skip to content

Commit

Permalink
nft module
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkCherepovskyi committed Nov 1, 2024
1 parent e049870 commit 069138a
Show file tree
Hide file tree
Showing 15 changed files with 187 additions and 85 deletions.
80 changes: 42 additions & 38 deletions database/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ import (
// SaveChain allows to save new Chain
func (db *Db) SaveBridgeChain(id string, chainType int32, bridgeAddress string, operator string) error {
query := `
INSERT INTO chains(id, chain_type, bridge_address, operator)
VALUES ($1, $2, $3, $4) RETURNING id
INSERT INTO bridge_chains(id, chain_type, bridge_address, operator)
VALUES ($1, $2, $3, $4)
ON CONFLICT (id) DO UPDATE
SET chain_type = excluded.chain_type,
bridge_address = excluded.bridge_address,
operator = excluded.operator
WHERE chains.id <= excluded.id
SET chain_type = excluded.chain_type,
bridge_address = excluded.bridge_address,
operator = excluded.operator
`
_, err := db.SQL.Exec(query, id, chainType, bridgeAddress, operator)
if err != nil {
Expand All @@ -27,7 +26,7 @@ func (db *Db) SaveBridgeChain(id string, chainType int32, bridgeAddress string,
// RemoveChain allows to remove the Chain
func (db *Db) RemoveBridgeChain(id string) error {
query := `
DELETE FROM chains WHERE id = $1
DELETE FROM bridge_chains WHERE id = $1
`
_, err := db.SQL.Exec(query, id)
if err != nil {
Expand All @@ -40,29 +39,30 @@ func (db *Db) RemoveBridgeChain(id string) error {
// -------------------------------------------------------------------------------------------------------------------

// SaveTokenInfo allows to save new TokenInfo
func (db *Db) SaveBridgeTokenInfo(address string, decimals uint64, chaiID string, tokenID uint64, isWrapped bool) (int64, error) {
func (db *Db) SaveBridgeTokenInfo(address string, decimals uint64, chainID string, tokenID uint64, isWrapped bool) (int64, error) {
query := `
INSERT INTO tokens_info( address, decimals, chaiId, tokenId, isWrapped)
VALUES ($1, $2, $3, $4, $5, $6) RETURNING id
INSERT INTO bridge_tokens_info(address, decimals, chain_id, token_id, is_wrapped)
VALUES ($1, $2, $3, $4, $5)
ON CONFLICT (address) DO UPDATE
SET chaiId = excluded.chaiId,
tokenId = excluded.tokenId,
WHERE tokens_info.address <= excluded.address
SET chain_id = excluded.chain_id,
token_id = excluded.token_id,
is_wrapped = excluded.is_wrapped
RETURNING id
`

var id int64
err := db.SQL.QueryRow(query, address, decimals, chaiID, tokenID, isWrapped)
err := db.SQL.QueryRow(query, address, decimals, chainID, tokenID, isWrapped).Scan(&id)
if err != nil {
return emptyIndex, fmt.Errorf("error while storing token info: %s", err)
return 0, fmt.Errorf("error while storing token info: %s", err)
}

return id, nil
}

// RemoveTokenInfo allows to remove the TokenInfo
func (db *Db) RemoveBridgeTokenInfo(id string) error {
func (db *Db) RemoveBridgeTokenInfo(id int64) error {
query := `
DELETE FROM tokens_info WHERE id = $1
DELETE FROM bridge_tokens_info WHERE id = $1
`
_, err := db.SQL.Exec(query, id)
if err != nil {
Expand All @@ -75,28 +75,28 @@ func (db *Db) RemoveBridgeTokenInfo(id string) error {
// -------------------------------------------------------------------------------------------------------------------

// SaveTokenMetadata allows to save new TokenMetadata
func (db *Db) SaveBridgeTokenMetadata(tokenID uint64, name, symbol, uri string) (int64, error) {
func (db *Db) SaveBridgeTokenMetadata(tokenID uint64, name, symbol, uri string) error {
query := `
INSERT INTO token_metadata(tokenID, name, symbol, uri)
INSERT INTO bridge_token_metadata(token_id, name, symbol, uri)
VALUES ($1, $2, $3, $4)
ON CONFLICT (name) DO UPDATE
SET uri = excluded.uri
WHERE tokens_info.name <= excluded.name
ON CONFLICT (token_id) DO UPDATE
SET name = excluded.name,
symbol = excluded.symbol,
uri = excluded.uri
`

var id int64
err := db.SQL.QueryRow(query, tokenID, name, symbol, uri).Scan(&id)
_, err := db.SQL.Exec(query, tokenID, name, symbol, uri)
if err != nil {
return emptyIndex, fmt.Errorf("error while storing token metadata: %s", err)
return fmt.Errorf("error while storing token metadata: %s", err)
}

return id, nil
return nil
}

// RemoveTokenMetadata allows to remove the TokenMetadata
func (db *Db) RemoveBridgeTokenMetadata(id string) error {
func (db *Db) RemoveBridgeTokenMetadata(id int64) error {
query := `
DELETE FROM token_metadata WHERE id = $1
DELETE FROM bridge_token_metadata WHERE id = $1
`
_, err := db.SQL.Exec(query, id)
if err != nil {
Expand All @@ -108,24 +108,28 @@ func (db *Db) RemoveBridgeTokenMetadata(id string) error {

// -------------------------------------------------------------------------------------------------------------------

func (db *Db) SaveBridgeTokens(tokensInfoID, tokenMetadataID int64) error {
// SaveBridgeTokens allows to save new Tokens
func (db *Db) SaveBridgeToken(tokensInfoID int64, tokenMetadataID uint64) error {
query := `
INSERT INTO tokens(tokens_info_id, token_metadata_id)
VALUES ($1, $2) RETURNING id
INSERT INTO bridge_tokens(tokens_info_id, metadata_id)
VALUES ($1, $2)
ON CONFLICT (tokens_info_id, metadata_id) DO NOTHING
`

_, err := db.SQL.Exec(query, tokensInfoID, tokenMetadataID)
if err != nil {
return fmt.Errorf("error while removing token: %s", err)
return fmt.Errorf("error while storing token: %s", err)
}
return err
return nil
}

func (db *Db) RemoveBridgeTokens(tokenID uint64) error {
// RemoveBridgeTokens allows to remove the Tokens
func (db *Db) RemoveBridgeToken(tokenID uint64) error {
query := `
DELETE FROM tokens WHERE metadata_id = $1;
DELETE FORM tokens_info WHERE id = $1;
DELETE FORM token_metadata WHERE token_id = $1;
DELETE FROM bridge_tokens WHERE metadata_id = $1;
DELETE FROM bridge_tokens_info WHERE id = $1;
DELETE FROM bridge_token_metadata WHERE token_id = $1;
`
_, err := db.SQL.Exec(query, tokenID)
if err != nil {
Expand All @@ -141,7 +145,7 @@ func (db *Db) SaveBridgeTransaction(
tx types.Transaction,
) error {
query := `
INSERT INTO transactions(
INSERT INTO bridge_transactions(
deposit_chain_id,
deposit_tx_hash,
deposit_tx_index,
Expand Down
60 changes: 34 additions & 26 deletions database/schema/14-bridge.sql
Original file line number Diff line number Diff line change
@@ -1,31 +1,39 @@
-- +migrate Up

CREATE TABLE tokens_info
CREATE TABLE bridge_tokens_info
(
id SERIAL PRIMARY KEY,
address TEXT,
decimals INTEGER,
chain_id TEXT,
token_id INT,
is_wrapped BOOLEAN
address VARCHAR(255) NOT NULL UNIQUE ,
decimals INTEGER NOT NULL,
chain_id VARCHAR(255) NOT NULL,
token_id INTEGER NOT NULL,
is_wrapped BOOLEAN NOT NULL
);

CREATE TABLE token_metadata (
token_id TEXT UNIQUE PRIMARY KEY,
name TEXT,
symbol TEXT,
uri TEXT
CREATE INDEX idx_bridge_tokens_info_address ON bridge_tokens_info(address);
CREATE INDEX idx_bridge_tokens_info_chain_id ON bridge_tokens_info(chain_id);

CREATE TABLE bridge_token_metadata (
token_id VARCHAR(255) UNIQUE PRIMARY KEY,
name VARCHAR(255) NOT NULL,
symbol VARCHAR(50) NOT NULL,
uri TEXT
);

CREATE TABLE tokens (
metadata_id TEXT NOT NULL,
tokens_info_id BIGINT NOT NULL,
FOREIGN KEY (metadata_id) REFERENCES token_metadata(token_id) ON DELETE CASCADE,
FOREIGN KEY (tokens_info_id) REFERENCES tokens_info(id) ON DELETE CASCADE,
PRIMARY KEY (metadata_id, tokens_info_id)
CREATE INDEX idx_bridge_token_metadata_name ON bridge_token_metadata(name);
CREATE INDEX idx_bridge_token_metadata_symbol ON bridge_token_metadata(symbol);

CREATE TABLE bridge_tokens (
metadata_id VARCHAR(255) NOT NULL,
tokens_info_id BIGINT NOT NULL,
FOREIGN KEY (metadata_id) REFERENCES bridge_token_metadata(token_id) ON DELETE CASCADE,
FOREIGN KEY (tokens_info_id) REFERENCES bridge_tokens_info(id) ON DELETE CASCADE,
PRIMARY KEY (metadata_id, tokens_info_id)
);

CREATE TABLE transactions
CREATE INDEX idx_bridge_tokens_metadata_id ON bridge_tokens(metadata_id);
CREATE INDEX idx_bridge_tokens_tokens_info_id ON bridge_tokens(tokens_info_id);

CREATE TABLE bridge_transactions
(
id SERIAL PRIMARY KEY,
deposit_chain_id TEXT NOT NULL,
Expand All @@ -43,18 +51,18 @@ CREATE TABLE transactions
is_wrapped BOOLEAN NOT NULL
);

CREATE TABLE chains
CREATE TABLE bridge_chains
(
id TEXT,
id TEXT UNIQUE PRIMARY KEY ,
chain_type SMALLINT,
bridge_address TEXT,
operator TEXT
);

-- +migrate Down
DROP TABLE tokens;
DROP TABLE tokens_info;
DROP TABLE chains;
DROP TABLE transactions;
DROP TABLE token_metadata;
DROP TABLE bridge_tokens;
DROP TABLE bridge_tokens_info;
DROP TABLE bridge_chains;
DROP TABLE bridge_transactions;
DROP TABLE bridge_token_metadata;

14 changes: 14 additions & 0 deletions hasura/metadata/databases/bdjuno/tables/public_bridge_chains.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
table:
name: bridge_chains
schema: public
select_permissions:
- permission:
allow_aggregations: false
columns:
- id
- chain_type
- bridge_address
- operator
filter: {}
limit: 100
role: anonymous
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
table:
name: tokens_info
name: bridge_tokens_info
schema: public
select_permissions:
- permission:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
table:
name: token_metadata
name: bridge_token_metadata
schema: public
select_permissions:
- permission:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
table:
name: tokens
name: bridge_tokens
schema: public
object_relationships:
- name: tokens_info
- name: bridge_tokens_info
using:
foreign_key_constraint_on: id
- name: token_metadata
- name: bridge_token_metadata
using:
foreign_key_constraint_on: id
select_permissions:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
table:
name: transaction
name: bridge_transaction
schema: public
object_relationships:
- name: block
Expand Down
9 changes: 5 additions & 4 deletions hasura/metadata/databases/bdjuno/tables/tables.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
- "!include public_token_price.yaml"
- "!include public_token_price_history.yaml"
- "!include public_token_unit.yaml"
- "!include public_transaction.yaml"
- "!include public_bridge_transaction.yaml"
- "!include public_validator.yaml"
- "!include public_validator_commission.yaml"
- "!include public_validator_description.yaml"
Expand All @@ -43,9 +43,10 @@
- "!include public_vesting_period.yaml"
- "!include public_nft_events.yaml"
- "!include public_nfts.yaml"
- "!include public_tokens.yaml"
- "!include public_token_metadata.yaml"
- "!include public_token_info.yaml"
- "!include public_bridge_tokens.yaml"
- "!include public_bridge_token_metadata.yaml"
- "!include public_bridge_token_info.yaml"
- "!include public_admins_vesting.yaml"
- "!include public_accumulator_params.yaml"
- "!include public_bridge_chains.yaml"

52 changes: 52 additions & 0 deletions modules/bridge/handle_genesis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package bridge

import (
"encoding/json"
bridgetypes "github.com/hyle-team/bridgeless-core/v12/x/bridge/types"
"github.com/pkg/errors"
tmtypes "github.com/tendermint/tendermint/types"

"github.com/rs/zerolog/log"
)

// HandleGenesis implements modules.Module
func (m *Module) HandleGenesis(doc *tmtypes.GenesisDoc, appState map[string]json.RawMessage) error {
log.Debug().Str("module", "bridge").Msg("parsing genesis")

// Read the genesis state
var genState bridgetypes.GenesisState
err := m.cdc.UnmarshalJSON(appState[bridgetypes.ModuleName], &genState)
if err != nil {
return errors.Wrap(err, "error while reading bridge genesis data")
}

// Save tokens
for _, token := range genState.Tokens {
err = m.db.SaveBridgeTokenMetadata(token.Id, token.Metadata.Name, token.Metadata.Symbol, token.Metadata.Uri)
if err != nil {
return errors.Wrap(err, "error while storing genesis token metadata")
}
for _, tokenInfo := range token.Info {
tokenInfoId, err := m.db.SaveBridgeTokenInfo(tokenInfo.Address, tokenInfo.Decimals, tokenInfo.ChainId, tokenInfo.TokenId, tokenInfo.IsWrapped)
if err != nil {
return errors.Wrap(err, "error while storing genesis token info")
}
if err = m.db.SaveBridgeToken(tokenInfoId, token.Id); err != nil {
return errors.Wrap(err, "error while storing genesis token")
}
}
}

for _, chain := range genState.Chains {
if err = m.db.SaveBridgeChain(chain.Id, int32(chain.Type), chain.BridgeAddress, chain.Operator); err != nil {
return errors.Wrap(err, "error while storing genesis chain")
}
}

for _, tx := range genState.Transactions {
if err = m.db.SaveBridgeTransaction(tx); err != nil {
return errors.Wrap(err, "error while storing genesis transaction")
}
}
return nil
}
Loading

0 comments on commit 069138a

Please sign in to comment.