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

Fairminter v2 #3070

Open
wants to merge 21 commits into
base: migration
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 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
6,251 changes: 3,782 additions & 2,469 deletions apiary.apib

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions counterparty-core/counterpartycore/lib/ledger/issuances.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,20 @@
return (sums["quantity"] or 0) + (sums["commission"] or 0), (sums["paid_quantity"] or 0)


def get_fairmint_by_address(db, fairminter_tx_hash, source):
cursor = db.cursor()
query = """

Check warning on line 517 in counterparty-core/counterpartycore/lib/ledger/issuances.py

View check run for this annotation

Codecov / codecov/patch

counterparty-core/counterpartycore/lib/ledger/issuances.py#L516-L517

Added lines #L516 - L517 were not covered by tests
SELECT
SUM(earn_quantity) AS quantity
FROM fairmints
WHERE fairminter_tx_hash = ? AND status = ? AND source = ?
"""
bindings = (fairminter_tx_hash, "valid", source)
cursor.execute(query, bindings)
sums = cursor.fetchone()
return sums["quantity"] or 0

Check warning on line 526 in counterparty-core/counterpartycore/lib/ledger/issuances.py

View check run for this annotation

Codecov / codecov/patch

counterparty-core/counterpartycore/lib/ledger/issuances.py#L523-L526

Added lines #L523 - L526 were not covered by tests


def get_fairminters_by_soft_cap_deadline(db, block_index):
cursor = db.cursor()
query = """
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE fairminters ADD COLUMN max_mint_per_address INTEGER;
48 changes: 35 additions & 13 deletions counterparty-core/counterpartycore/lib/messages/fairmint.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from counterpartycore.lib import config, exceptions, ledger
from counterpartycore.lib.messages import fairminter as fairminter_mod
from counterpartycore.lib.parser import protocol
from counterpartycore.lib.utils import helpers

logger = logging.getLogger(config.LOGGER_NAME)

Expand Down Expand Up @@ -37,6 +38,15 @@

asset_supply = ledger.supplies.asset_supply(db, fairminter["asset"])

if fairminter["max_mint_per_address"] is not None and fairminter["max_mint_per_address"] > 0:
alread_minted = ledger.issuances.get_fairmint_by_address(db, fairminter["tx_hash"], source)
if fairminter["price"] > 0:
if alread_minted + quantity > fairminter["max_mint_per_address"]:
problems.append("quantity exceeds maximum allowed by address")

Check warning on line 45 in counterparty-core/counterpartycore/lib/messages/fairmint.py

View check run for this annotation

Codecov / codecov/patch

counterparty-core/counterpartycore/lib/messages/fairmint.py#L42-L45

Added lines #L42 - L45 were not covered by tests
else:
if alread_minted + fairminter["max_mint_per_tx"] > fairminter["max_mint_per_address"]:
problems.append("quantity exceeds maximum allowed by address")

Check warning on line 48 in counterparty-core/counterpartycore/lib/messages/fairmint.py

View check run for this annotation

Codecov / codecov/patch

counterparty-core/counterpartycore/lib/messages/fairmint.py#L47-L48

Added lines #L47 - L48 were not covered by tests

if fairminter["price"] > 0:
# if the fairminter is not free the quantity is mandatory
if quantity <= 0:
Expand Down Expand Up @@ -82,25 +92,37 @@

# create message
data = struct.pack(config.SHORT_TXTYPE_FORMAT, ID)
# to optimize the data size (avoiding fixed sizes per parameter) we use a simple
# string of characters separated by `|`.
data_content = "|".join(
[
str(value)
for value in [
asset,
quantity,

if protocol.enabled("fairminter_v2"):
asset_id = ledger.issuances.generate_asset_id(asset)
data += helpers.encode_data(asset_id, quantity or "")

Check warning on line 98 in counterparty-core/counterpartycore/lib/messages/fairmint.py

View check run for this annotation

Codecov / codecov/patch

counterparty-core/counterpartycore/lib/messages/fairmint.py#L96-L98

Added lines #L96 - L98 were not covered by tests
else:
data_content = "|".join(

Check warning on line 100 in counterparty-core/counterpartycore/lib/messages/fairmint.py

View check run for this annotation

Codecov / codecov/patch

counterparty-core/counterpartycore/lib/messages/fairmint.py#L100

Added line #L100 was not covered by tests
[
str(value)
for value in [
asset,
quantity,
]
]
]
).encode("utf-8")
data += struct.pack(f">{len(data_content)}s", data_content)
).encode("utf-8")
data += struct.pack(f">{len(data_content)}s", data_content)

Check warning on line 109 in counterparty-core/counterpartycore/lib/messages/fairmint.py

View check run for this annotation

Codecov / codecov/patch

counterparty-core/counterpartycore/lib/messages/fairmint.py#L109

Added line #L109 was not covered by tests

return (source, [], data)


def unpack(message, return_dict=False):
try:
data_content = struct.unpack(f">{len(message)}s", message)[0].decode("utf-8").split("|")
(asset, quantity) = data_content
if protocol.enabled("fairminter_v2"):
asset_id, quantity = helpers.decode_data(message)
print(asset_id, quantity)
asset = ledger.issuances.generate_asset_name(helpers.bytes_to_int(asset_id))
print(asset)
quantity = helpers.bytes_to_int(quantity or b"\x00")
else:
data_content = struct.unpack(f">{len(message)}s", message)[0].decode("utf-8").split("|")
(asset, quantity) = data_content

if return_dict:
return {"asset": asset, "quantity": int(quantity)}

Expand Down
166 changes: 118 additions & 48 deletions counterparty-core/counterpartycore/lib/messages/fairminter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from counterpartycore.lib import config, exceptions, ledger
from counterpartycore.lib.parser import protocol
from counterpartycore.lib.utils import assetnames
from counterpartycore.lib.utils import assetnames, helpers

logger = logging.getLogger(config.LOGGER_NAME)
D = decimal.Decimal
Expand All @@ -20,6 +20,7 @@
price=0,
quantity_by_price=1,
max_mint_per_tx=0,
max_mint_per_address=0,
hard_cap=0,
premint_quantity=0,
start_block=0,
Expand All @@ -40,6 +41,7 @@
"price": price,
"quantity_by_price": quantity_by_price,
"max_mint_per_tx": max_mint_per_tx,
"max_mint_per_address": max_mint_per_address,
"hard_cap": hard_cap,
"premint_quantity": premint_quantity,
"start_block": start_block,
Expand Down Expand Up @@ -74,6 +76,9 @@
"`minted_asset_commission` must be less than 0 or greater than or equal to 1"
)

if max_mint_per_tx > max_mint_per_address > 0:
problems.append("max_mint_per_tx must be <= max_mint_per_address.")

Check warning on line 80 in counterparty-core/counterpartycore/lib/messages/fairminter.py

View check run for this annotation

Codecov / codecov/patch

counterparty-core/counterpartycore/lib/messages/fairminter.py#L80

Added line #L80 was not covered by tests

# check asset name format
try:
ledger.issuances.generate_asset_id(asset)
Expand Down Expand Up @@ -143,8 +148,12 @@
if start_block > end_block > 0:
problems.append("Start block must be <= end block.") # could be one block fair minter

if soft_cap >= hard_cap > 0:
problems.append("Soft cap must be < hard cap.")
if protocol.enabled("fairminter_v2"):
if soft_cap > hard_cap > 0:
problems.append("Soft cap must be <= hard cap.")

Check warning on line 153 in counterparty-core/counterpartycore/lib/messages/fairminter.py

View check run for this annotation

Codecov / codecov/patch

counterparty-core/counterpartycore/lib/messages/fairminter.py#L153

Added line #L153 was not covered by tests
else:
if soft_cap >= hard_cap > 0:
problems.append("Soft cap must be < hard cap.")

Check warning on line 156 in counterparty-core/counterpartycore/lib/messages/fairminter.py

View check run for this annotation

Codecov / codecov/patch

counterparty-core/counterpartycore/lib/messages/fairminter.py#L156

Added line #L156 was not covered by tests
if soft_cap > 0:
if not soft_cap_deadline_block:
problems.append("Soft cap deadline block must be specified if soft cap is specified.")
Expand All @@ -164,6 +173,7 @@
price: int = 0,
quantity_by_price: int = 1,
max_mint_per_tx: int = 0,
max_mint_per_address: int = 0,
hard_cap: int = 0,
premint_quantity: int = 0,
start_block: int = 0,
Expand All @@ -187,6 +197,7 @@
price,
quantity_by_price,
max_mint_per_tx,
max_mint_per_address,
hard_cap,
premint_quantity,
start_block,
Expand All @@ -207,42 +218,35 @@

# create message
data = struct.pack(config.SHORT_TXTYPE_FORMAT, ID)
# to optimize the data size (avoiding fixed sizes per parameter) we use a simple
# string of characters separated by `|`.
# The description is placed last to be able to contain `|`.
data_content = "|".join(
[
str(value)
for value in [
asset,
asset_parent,
price,
quantity_by_price,
max_mint_per_tx,
hard_cap,
premint_quantity,
start_block,
end_block,
soft_cap,
soft_cap_deadline_block,
minted_asset_commission_int,
int(burn_payment),
int(lock_description),
int(lock_quantity),
int(divisible),
description,
]
]
).encode("utf-8")
data += struct.pack(f">{len(data_content)}s", data_content)
return (source, [], data)


def unpack(message, return_dict=False):
try:
data_content = struct.unpack(f">{len(message)}s", message)[0].decode("utf-8").split("|")
arg_count = len(data_content)
(
packed_value = []
if protocol.enabled("fairminter_v2"):
asset_id = ledger.issuances.generate_asset_id(asset)
asset_parent_id = (

Check warning on line 224 in counterparty-core/counterpartycore/lib/messages/fairminter.py

View check run for this annotation

Codecov / codecov/patch

counterparty-core/counterpartycore/lib/messages/fairminter.py#L221-L224

Added lines #L221 - L224 were not covered by tests
ledger.issuances.generate_asset_id(asset_parent) if asset_parent != "" else 0
)
data += helpers.encode_data(

Check warning on line 227 in counterparty-core/counterpartycore/lib/messages/fairminter.py

View check run for this annotation

Codecov / codecov/patch

counterparty-core/counterpartycore/lib/messages/fairminter.py#L227

Added line #L227 was not covered by tests
asset_id,
asset_parent_id or "",
price or "",
quantity_by_price or "",
max_mint_per_tx or "",
max_mint_per_address or "",
hard_cap or "",
premint_quantity or "",
start_block or "",
end_block or "",
soft_cap or "",
soft_cap_deadline_block or "",
minted_asset_commission_int or "",
int(burn_payment) or "",
int(lock_description) or "",
int(lock_quantity) or "",
int(divisible) or "",
description,
)
else:
packed_value += []
packed_value += [

Check warning on line 249 in counterparty-core/counterpartycore/lib/messages/fairminter.py

View check run for this annotation

Codecov / codecov/patch

counterparty-core/counterpartycore/lib/messages/fairminter.py#L248-L249

Added lines #L248 - L249 were not covered by tests
asset,
asset_parent,
price,
Expand All @@ -255,13 +259,73 @@
soft_cap,
soft_cap_deadline_block,
minted_asset_commission_int,
burn_payment,
lock_description,
lock_quantity,
divisible,
) = data_content[0 : arg_count - 1]
# The description is placed last to be able to contain `|`.
description = "|".join(data_content[arg_count - 1 :])
int(burn_payment),
int(lock_description),
int(lock_quantity),
int(divisible),
description,
]
data_content = "|".join([str(value) for value in packed_value]).encode("utf-8")
data += struct.pack(f">{len(data_content)}s", data_content)

Check warning on line 269 in counterparty-core/counterpartycore/lib/messages/fairminter.py

View check run for this annotation

Codecov / codecov/patch

counterparty-core/counterpartycore/lib/messages/fairminter.py#L268-L269

Added lines #L268 - L269 were not covered by tests

return (source, [], data)

Check warning on line 271 in counterparty-core/counterpartycore/lib/messages/fairminter.py

View check run for this annotation

Codecov / codecov/patch

counterparty-core/counterpartycore/lib/messages/fairminter.py#L271

Added line #L271 was not covered by tests


def unpack(message, return_dict=False):
try:
if protocol.enabled("fairminter_v2"):
decoded_data = helpers.decode_data(message)
description = decoded_data.pop().decode("utf-8")
(
asset_id,
asset_parent_id,
price,
quantity_by_price,
max_mint_per_tx,
max_mint_per_address,
hard_cap,
premint_quantity,
start_block,
end_block,
soft_cap,
soft_cap_deadline_block,
minted_asset_commission_int,
burn_payment,
lock_description,
lock_quantity,
divisible,
) = [helpers.bytes_to_int(value or b"\x00") for value in decoded_data]

asset = ledger.issuances.generate_asset_name(asset_id)
asset_parent = (
ledger.issuances.generate_asset_name(asset_parent_id)
if asset_parent_id != 0
else ""
)
else:
data_content = struct.unpack(f">{len(message)}s", message)[0].decode("utf-8").split("|")
arg_count = len(data_content)
(
asset,
asset_parent,
price,
quantity_by_price,
max_mint_per_tx,
hard_cap,
premint_quantity,
start_block,
end_block,
soft_cap,
soft_cap_deadline_block,
minted_asset_commission_int,
burn_payment,
lock_description,
lock_quantity,
divisible,
) = data_content[0 : arg_count - 1]
# The description is placed last to be able to contain `|`.
description = "|".join(data_content[arg_count - 1 :])
max_mint_per_address = 0

minted_asset_commission = D(minted_asset_commission_int) / D(1e8)

Expand All @@ -272,6 +336,7 @@
"price": int(price),
"quantity_by_price": int(quantity_by_price),
"max_mint_per_tx": int(max_mint_per_tx),
"max_mint_per_address": int(max_mint_per_address),
"hard_cap": int(hard_cap),
"premint_quantity": int(premint_quantity),
"start_block": int(start_block),
Expand All @@ -292,6 +357,7 @@
int(price),
int(quantity_by_price),
int(max_mint_per_tx),
int(max_mint_per_address),
int(hard_cap),
int(premint_quantity),
int(start_block),
Expand All @@ -305,8 +371,9 @@
bool(int(divisible)),
description,
)
except Exception: # pylint: disable=broad-exception-caught
return "", "", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.0, False, False, False, False, ""
except Exception as e: # pylint: disable=broad-exception-caught
logger.error("fairminter unpack error: %s", e, exc_info=True)
return "", "", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.0, False, False, False, False, ""

Check warning on line 376 in counterparty-core/counterpartycore/lib/messages/fairminter.py

View check run for this annotation

Codecov / codecov/patch

counterparty-core/counterpartycore/lib/messages/fairminter.py#L374-L376

Added lines #L374 - L376 were not covered by tests


def parse(db, tx, message):
Expand All @@ -316,6 +383,7 @@
price,
quantity_by_price,
max_mint_per_tx,
max_mint_per_address,
hard_cap,
premint_quantity,
start_block,
Expand All @@ -338,6 +406,7 @@
price,
quantity_by_price,
max_mint_per_tx,
max_mint_per_address,
hard_cap,
premint_quantity,
start_block,
Expand Down Expand Up @@ -428,6 +497,7 @@
"hard_cap": hard_cap,
"burn_payment": burn_payment,
"max_mint_per_tx": max_mint_per_tx,
"max_mint_per_address": max_mint_per_address,
"premint_quantity": premint_quantity,
"start_block": start_block,
"end_block": end_block,
Expand Down
Loading
Loading