Skip to content

Commit

Permalink
Fixes issue metaplex-foundation#8 and updates the solana.py library t…
Browse files Browse the repository at this point in the history
…o 0.20.0
  • Loading branch information
FFEvan committed Jan 3, 2022
1 parent 441c2ba commit c8fc8bf
Show file tree
Hide file tree
Showing 7 changed files with 292 additions and 118 deletions.
135 changes: 135 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# ignore idea files
.idea/

.DS_Store
.DS_Store/
92 changes: 52 additions & 40 deletions api/metaplex_api.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,39 @@
import json
from cryptography.fernet import Fernet
import base58
from solana.keypair import Keypair
from nacl.public import PrivateKey
from solana.keypair import Keypair
from metaplex.transactions import deploy, topup, mint, send, burn, update_token_metadata
from utils.execution_engine import execute

class MetaplexAPI():

def wallet():
""" Generate a wallet and return the address and private key. """
keypair = Keypair()
pub_key = keypair.public_key
private_key = list(keypair.seed)
return json.dumps(
{
'address': str(pub_key),
'private_key': private_key
}
)


class MetaplexAPI:

def __init__(self, cfg):
self.private_key = list(base58.b58decode(cfg["PRIVATE_KEY"]))[:32]
self.public_key = cfg["PUBLIC_KEY"]
self.keypair = Keypair(self.private_key)
self.keypair = Keypair(PrivateKey(bytes(self.private_key)))
self.cipher = Fernet(cfg["DECRYPTION_KEY"])

def wallet(self):
""" Generate a wallet and return the address and private key. """
keypair = Keypair()
pub_key = keypair.public_key
private_key = list(keypair.seed)
return json.dumps(
{
'address': str(pub_key),
'private_key': private_key
}
)

def deploy(self, api_endpoint, name, symbol, fees, max_retries=3, skip_confirmation=False, max_timeout=60, target=20, finalized=True):
def deploy(self, api_endpoint, name, symbol, fees, max_retries=3, skip_confirmation=False, max_timeout=60,
target=20, finalized=True):
"""
Deploy a contract to the blockchain (on network that support contracts). Takes the network ID and contract name, plus initialisers of name and symbol. Process may vary significantly between blockchains.
Returns status code of success or fail, the contract address, and the native transaction data.
Deploy a contract to the blockchain (on network that support contracts). Takes the network ID and contract
name, plus initializers of name and symbol. Process may vary significantly between blockchains. Returns
status code of success or fail, the contract address, and the native transaction data.
"""
try:
tx, signers, contract = deploy(api_endpoint, self.keypair, name, symbol, fees)
Expand All @@ -49,9 +54,11 @@ def deploy(self, api_endpoint, name, symbol, fees, max_retries=3, skip_confirmat
except:
return json.dumps({"status": 400})

def topup(self, api_endpoint, to, amount=None, max_retries=3, skip_confirmation=False, max_timeout=60, target=20, finalized=True):
def topup(self, api_endpoint, to, amount=None, max_retries=3, skip_confirmation=False, max_timeout=60, target=20,
finalized=True):
"""
Send a small amount of native currency to the specified wallet to handle gas fees. Return a status flag of success or fail and the native transaction data.
Send a small amount of native currency to the specified wallet to handle gas fees. Return a status flag of
success or fail and the native transaction data.
"""
try:
tx, signers = topup(api_endpoint, self.keypair, to, amount=amount)
Expand All @@ -70,7 +77,8 @@ def topup(self, api_endpoint, to, amount=None, max_retries=3, skip_confirmation=
except:
return json.dumps({"status": 400})

def mint(self, api_endpoint, contract_key, dest_key, link, max_retries=3, skip_confirmation=False, max_timeout=60, target=20, finalized=True, supply=1 ):
def mint(self, api_endpoint, contract_key, dest_key, link, max_retries=3, skip_confirmation=False, max_timeout=60,
target=20, finalized=True, supply=1):
"""
Mints an NFT to an account, updates the metadata and creates a master edition
"""
Expand All @@ -89,27 +97,30 @@ def mint(self, api_endpoint, contract_key, dest_key, link, max_retries=3, skip_c
return json.dumps(resp)
# except:
# return json.dumps({"status": 400})

def update_token_metadata(self, api_endpoint, mint_token_id, link, data, creators_addresses, creators_verified, creators_share,fee, max_retries=3, skip_confirmation=False, max_timeout=60, target=20, finalized=True, supply=1 ):
"""

def update_token_metadata(self, api_endpoint, mint_token_id, link, data, creators_addresses, creators_verified,
creators_share, fee, max_retries=3, skip_confirmation=False, max_timeout=60, target=20,
finalized=True, supply=1):
"""
Updates the json metadata for a given mint token id.
"""
tx, signers = update_token_metadata(api_endpoint, self.keypair, mint_token_id, link, data, fee, creators_addresses, creators_verified, creators_share)
resp = execute(
api_endpoint,
tx,
signers,
max_retries=max_retries,
skip_confirmation=skip_confirmation,
max_timeout=max_timeout,
target=target,
finalized=finalized,
)
resp["status"] = 200
return json.dumps(resp)

tx, signers = update_token_metadata(api_endpoint, self.keypair, mint_token_id, link, data, fee,
creators_addresses, creators_verified, creators_share)
resp = execute(
api_endpoint,
tx,
signers,
max_retries=max_retries,
skip_confirmation=skip_confirmation,
max_timeout=max_timeout,
target=target,
finalized=finalized,
)
resp["status"] = 200
return json.dumps(resp)

def send(self, api_endpoint, contract_key, sender_key, dest_key, encrypted_private_key, max_retries=3, skip_confirmation=False, max_timeout=60, target=20, finalized=True):
def send(self, api_endpoint, contract_key, sender_key, dest_key, encrypted_private_key, max_retries=3,
skip_confirmation=False, max_timeout=60, target=20, finalized=True):
"""
Transfer a token on a given network and contract from the sender to the recipient.
May require a private key, if so this will be provided encrypted using Fernet: https://cryptography.io/en/latest/fernet/
Expand All @@ -133,7 +144,8 @@ def send(self, api_endpoint, contract_key, sender_key, dest_key, encrypted_priva
except:
return json.dumps({"status": 400})

def burn(self, api_endpoint, contract_key, owner_key, encrypted_private_key, max_retries=3, skip_confirmation=False, max_timeout=60, target=20, finalized=True):
def burn(self, api_endpoint, contract_key, owner_key, encrypted_private_key, max_retries=3, skip_confirmation=False,
max_timeout=60, target=20, finalized=True):
"""
Burn a token, permanently removing it from the blockchain.
May require a private key, if so this will be provided encrypted using Fernet: https://cryptography.io/en/latest/fernet/
Expand Down
Loading

0 comments on commit c8fc8bf

Please sign in to comment.