Skip to content
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

feat: CUDOS-2110 nft marketplace auctions #97

Open
wants to merge 2 commits into
base: cudos-dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 59 additions & 7 deletions database/marketplace.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@ package database

import (
"fmt"
"time"

"github.com/forbole/bdjuno/v2/database/utils"
)

func (db *Db) CheckIfNftExists(tokenId uint64, denomId string) error {
func (db *Db) CheckIfNftExists(tokenID uint64, denomID string) error {
var rows []string

err := db.Sqlx.Select(&rows, `SELECT denom_id FROM marketplace_nft WHERE token_id=$1 AND denom_id=$2`, tokenId, denomId)
err := db.Sqlx.Select(&rows, `SELECT denom_id FROM marketplace_nft WHERE token_id=$1 AND denom_id=$2`, tokenID, denomID)
if err != nil {
return err
}

if len(rows) != 1 {
return fmt.Errorf("Not found.")
return fmt.Errorf("not found")
}

return nil
Expand All @@ -27,17 +28,17 @@ func (db *Db) SaveMarketplaceCollection(txHash string, id uint64, denomID, mintR
return err
}

func (tx *DbTx) ListNft(txHash string, id, tokenId uint64, denomID, price string) error {
func (tx *DbTx) ListNft(txHash string, id, tokenID uint64, denomID, price string) error {
_, err := tx.Exec(`UPDATE marketplace_nft SET transaction_hash=$1, id=$2, price=$3 WHERE token_id=$4 AND denom_id=$5`,
txHash, id, price, tokenId, denomID)
txHash, id, price, tokenID, denomID)
fmt.Println(err)
return err
}

func (tx *DbTx) SaveMarketplaceNft(txHash string, tokenId uint64, denomID, uid, price, creator string) error {
func (tx *DbTx) SaveMarketplaceNft(txHash string, tokenID uint64, denomID, uid, price, creator string) error {
_, err := tx.Exec(`INSERT INTO marketplace_nft (transaction_hash, uid, token_id, denom_id, price, creator, uniq_id)
VALUES($1, $2, $3, $4, $5, $6, $7) ON CONFLICT (token_id, denom_id) DO UPDATE SET price = EXCLUDED.price, id = EXCLUDED.id`,
txHash, uid, tokenId, denomID, price, creator, utils.FormatUniqID(tokenId, denomID))
txHash, uid, tokenID, denomID, price, creator, utils.FormatUniqID(tokenID, denomID))
return err
}

Expand Down Expand Up @@ -76,6 +77,52 @@ func (tx *DbTx) UnlistNft(id uint64) error {
return err
}

func (tx *DbTx) SaveMarketplaceAuction(auctionID uint64, tokenID uint64, denomID string, creator string, startTime time.Time, endTime time.Time, auctionInfo string) error {
_, err := tx.Exec(`INSERT INTO marketplace_auction (id, token_id, denom_id, creator, start_time, end_time, auction)
VALUES($1, $2, $3, $4, $5, $6, $7)`, auctionID, tokenID, denomID, creator, startTime, endTime, auctionInfo)
return err
}

func (tx *DbTx) SaveMarketplaceBid(auctionID uint64, bidder string, price string, timestamp time.Time, txHash string) error {
_, err := tx.Exec(`INSERT INTO marketplace_bid (auction_id, bidder, price, timestamp, transaction_hash)
VALUES($1, $2, $3, $4, $5)`, auctionID, bidder, price, timestamp, txHash)
return err
}

func (tx *DbTx) SaveMarketplaceAuctionSold(auctionID uint64, timestamp uint64, usdPrice string, btcPrice string, txHashAcceptBid string) error {
var tokenID uint64
var denomID, seller, buyer, price, txHashPlaceBid string

if err := tx.QueryRow(`SELECT token_id, denom_id, creator FROM marketplace_auction WHERE id = $1`, auctionID).Scan(&tokenID, &denomID, &seller); err != nil {
return err
}

if err := tx.QueryRow(`SELECT bidder, transaction_hash, price FROM marketplace_bid WHERE auction_id = $1 ORDER BY timestamp DESC`, auctionID).Scan(&buyer, &txHashPlaceBid, &price); err != nil {
return err
}

_, err := tx.Exec(`UPDATE marketplace_auction SET sold = true WHERE id = $1`, auctionID)
if err != nil {
return err
}

txHashBuyNft := txHashAcceptBid
if txHashBuyNft == "" {
txHashBuyNft = txHashPlaceBid
}

if err := tx.saveMarketplaceNftBuy(txHashBuyNft, buyer, timestamp, tokenID, denomID, price, seller, usdPrice, btcPrice); err != nil {
return err
}

return tx.UpdateNFTHistory(txHashBuyNft, tokenID, denomID, seller, buyer, timestamp)
}

func (tx *DbTx) UpdateMarketplaceAuctionInfo(auctionID uint64, auctionInfo string) error {
_, err := tx.Exec(`UPDATE marketplace_auction SET auction = $2 WHERE id = $1`, auctionID, auctionInfo)
return err
}

func (db *Db) UnlistNft(id uint64) error {
_, err := db.Sql.Exec(`UPDATE marketplace_nft SET price = '0', id = null WHERE id = $1`, id)
return err
Expand All @@ -95,3 +142,8 @@ func (db *Db) SetMarketplaceCollectionRoyalties(id uint64, mintRoyalties, resale
_, err := db.Sql.Exec(`UPDATE marketplace_collection SET mint_royalties = $1, resale_royalties = $2 WHERE id = $3`, mintRoyalties, resaleRoyalties, id)
return err
}

func (db *Db) UpdateMarketplaceAuctionInfo(auctionID uint64, auctionInfo string) error {
_, err := db.Sql.Exec(`UPDATE marketplace_auction SET auction = $2 WHERE id = $1`, auctionID, auctionInfo)
return err
}
3 changes: 3 additions & 0 deletions database/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ var expectedAppliedMigrations = []database.Migration{
{ID: int64(9), Name: "008-block_parsed_data.sql", CreatedAt: int64(0)},
{ID: int64(10), Name: "009-cw20token_update.sql", CreatedAt: int64(0)},
{ID: int64(11), Name: "010-nft-uniq-id.sql", CreatedAt: int64(0)},
{ID: int64(12), Name: "011-nft-migrate-uniq-id-values.sql", CreatedAt: int64(0)},
{ID: int64(13), Name: "012-marketplace-nft-id-column-unique.sql", CreatedAt: int64(0)},
{ID: int64(14), Name: "013-marketplace-auctions.sql", CreatedAt: int64(0)},
}

func (suite *DbTestSuite) TestExecuteMigrations() {
Expand Down
26 changes: 26 additions & 0 deletions database/scheme/013-marketplace-auctions.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
CREATE TABLE marketplace_auction (
id BIGINT NOT NULL PRIMARY KEY,
token_id BIGINT NOT NULL,
denom_id TEXT NOT NULL REFERENCES nft_denom (id),
creator TEXT NOT NULL,
start_time TIMESTAMP WITHOUT TIME ZONE NOT NULL,
end_time TIMESTAMP WITHOUT TIME ZONE NOT NULL,
auction TEXT NOT NULL,
sold BOOLEAN NOT NULL DEFAULT FALSE,
FOREIGN KEY (token_id, denom_id) REFERENCES nft_nft(id, denom_id)
);

CREATE INDEX marketplace_auction_token_id_denom_id_index ON marketplace_auction (token_id, denom_id);
CREATE INDEX marketplace_auction_creator_index ON marketplace_auction (creator);

CREATE TABLE marketplace_bid (
auction_id BIGINT NOT NULL REFERENCES marketplace_auction (id),
bidder TEXT NOT NULL,
price DECIMAL NOT NULL,
timestamp TIMESTAMP WITHOUT TIME ZONE NOT NULL,
transaction_hash TEXT NOT NULL REFERENCES transaction (hash)
);

CREATE INDEX marketplace_bid_auction_id_index ON marketplace_bid (auction_id);
CREATE INDEX marketplace_bid_bidder_index ON marketplace_bid (bidder);
CREATE INDEX marketplace_bid_timestamp_index ON marketplace_bid (timestamp desc);
37 changes: 37 additions & 0 deletions database/types/marketplace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package types

import (
"time"
)

type MarketplaceAuctionRow struct {
ID uint64 `db:"id"`
TokenID uint64 `db:"token_id"`
DenomID string `db:"denom_id"`
Creator string `db:"creator"`
StartTime time.Time `db:"start_time"`
EndTime time.Time `db:"end_time"`
Auction string `db:"auction"`
Sold bool `db:"sold"`
}

type MarketplaceBidRow struct {
AuctionID uint64 `db:"auction_id"`
Bidder string `db:"bidder"`
Price string `db:"price"`
Timestamp time.Time `db:"timestamp"`
TxHash string `db:"transaction_hash"`
}

type MarketplaceNftBuyHistory struct {
TxHash string `db:"transaction_hash"`
TokenID uint64 `db:"token_id"`
DenomID string `db:"denom_id"`
Price string `db:"price"`
Buyer string `db:"buyer"`
Seller string `db:"seller"`
UsdPrice string `db:"usd_price"`
BtcPrice string `db:"btc_price"`
Timestamp uint64 `db:"timestamp"`
UniqID string `db:"uniq_id"`
}
12 changes: 12 additions & 0 deletions database/types/nft.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package types

type NftTransferHistoryRow struct {
ID uint64 `db:"id"`
TxHash string `db:"transaction_hash"`
DenomID string `db:"denom_id"`
Price string `db:"price"`
OldOwner string `db:"old_owner"`
NewOwner string `db:"new_owner"`
Timestamp uint64 `db:"timestamp"`
UniqID string `db:"uniq_id"`
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.17
require (
github.com/CosmWasm/wasmd v0.25.0
github.com/CosmWasm/wasmvm v1.0.0-beta10
github.com/CudoVentures/cudos-node v1.0.1-0.20221205141302-2c6a9098d44b
github.com/CudoVentures/cudos-node v1.0.1-0.20230207102951-f1acfc21d5b9
github.com/althea-net/cosmos-gravity-bridge/module v0.0.0-00010101000000-000000000000
github.com/cosmos/cosmos-sdk v0.45.3
github.com/forbole/juno/v2 v2.0.0-20220223115732-dbb226a91ce9
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -405,10 +405,8 @@ github.com/CudoVentures/cosmos-gravity-bridge/module v0.0.0-20220908134551-c82e5
github.com/CudoVentures/cosmos-gravity-bridge/module v0.0.0-20220908134551-c82e5d5a6ac3/go.mod h1:yhB9hRM0nPHH8k0HN+066glD0xaZjfB9WjgYeijYNBc=
github.com/CudoVentures/cosmos-sdk v0.0.0-20220816082327-65532d606824 h1:6IkLkSVwMjH+qsyBZbOUwJRUHh72jXRzVNOMGKN8icI=
github.com/CudoVentures/cosmos-sdk v0.0.0-20220816082327-65532d606824/go.mod h1:Zlp+5q+oliIV6SFlfspqcd/28Ra7v51Bt6Wy6H1b8PI=
github.com/CudoVentures/cudos-node v1.0.1-0.20221124124559-4c2b4c80ba67 h1:2assj7VdMtkQpD394PQJaxrmlvm8hH6g0BXiWiZXrhE=
github.com/CudoVentures/cudos-node v1.0.1-0.20221124124559-4c2b4c80ba67/go.mod h1:NSSTwYXNsoRHQT+TtLPMRcuoTv1t1ngwY5MufiyA6Bo=
github.com/CudoVentures/cudos-node v1.0.1-0.20221205141302-2c6a9098d44b h1:e6wx/bp5G8CdVkLS1wxZq7eKdHIzIcDUBBDmpaq62dU=
github.com/CudoVentures/cudos-node v1.0.1-0.20221205141302-2c6a9098d44b/go.mod h1:NSSTwYXNsoRHQT+TtLPMRcuoTv1t1ngwY5MufiyA6Bo=
github.com/CudoVentures/cudos-node v1.0.1-0.20230207102951-f1acfc21d5b9 h1:fD72wHgKt5JkQikt8/og8FQYADuXWrx9Enbw/IZwE40=
github.com/CudoVentures/cudos-node v1.0.1-0.20230207102951-f1acfc21d5b9/go.mod h1:jp1iaVOhA+a78rw68Dpzr90Bk6vV1jjSHVysGysH0X8=
github.com/CudoVentures/juno/v2 v2.0.1-0.20220908075630-6618cc96377f h1:yWNnzclSePa0xJfKFCXDMSBdmFOPD84uwlE6zGaXreI=
github.com/CudoVentures/juno/v2 v2.0.1-0.20220908075630-6618cc96377f/go.mod h1:lSiplsjZl8aFajq+DnRph947q6hMPHJqfGEb1WH+pGA=
github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
table:
name: marketplace_auction
schema: public
object_relationships:
- name: nft_denom
using:
foreign_key_constraint_on: denom_id
- name: nft_nft
using:
foreign_key_constraint_on: [token_id, denom_id]
select_permissions:
- permission:
allow_aggregations: true
columns:
- id
- token_id
- denom_id
- creator
- start_time
- end_time
- auction
- sold
filter: {}
role: anonymous
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
table:
name: marketplace_bid
schema: public
object_relationships:
- name: marketplace_auction
using:
foreign_key_constraint_on: auction_id
- name: transaction
using:
foreign_key_constraint_on: transaction_hash
select_permissions:
- permission:
allow_aggregations: true
columns:
- auction_id
- bidder
- price
- timestamp
- transaction_hash
filter: {}
role: anonymous
2 changes: 2 additions & 0 deletions hasura/metadata/databases/bdjuno/tables/tables.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,5 @@
- "!include public_cw20token_info.yaml"
- "!include public_cw20token_code_id.yaml"
- "!include public_nft_transfer_history.yaml"
- "!include public_marketplace_auction.yaml"
- "!include public_marketplace_bid.yaml"
Loading