• Stars
    star
    123
  • Rank 290,145 (Top 6 %)
  • Language
    Python
  • Created over 3 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Metaplex Python API

This modules allows you to create, mint, transfer and burn NFTs on the Solana blockchain using Python.

Setup

First, clone down the repository

Create a virtual environment and install the dependencies in requirements.txt. Be sure to use a version of Python >= 3.6

python3 -m virtualenv venv
source venv/bin/activate
pip install -r requirements.txt

At this point, you should be good to go.

Usage

To create a MetaplexAPI object, you need to pass a dicitonary to the constructor with the following keys:

cfg = {
    "PRIVATE_KEY": YOUR_PRIVATE_KEY,
    "PUBLIC_KEY": YOUR_PUBLIC_KEY,
    "DECRYPTION_KEY": SERVER_DECRYPTION_KEY
}
api = MetaplexAPI(cfg)

The keypair that is passed into the MetaplexAPI serves as the fee payer for all network transactions (creating new wallets, minting tokens, transferring tokens, etc). Both keys are base58 encoded.

The decryption key ensures that messages sent to a server that utilizes this API will not be receiving unencrypted private keys over the wire. These private keys are necessary for signing transactions. The client can encrypt their data by using the same decryption key as the server. This is the syntax:

from cryptography.fernet import Fernet
cipher = Fernet(DECRYPTION_KEY) # This is the same key that the server has
encrypted_key = cipher.encrypt(SECRET)

# Send `encrypted_key` to the downstream server to process

Methods

This section will go through the following story (if you look at the code snippets) and invoke each of the methods in the API along the way:

  1. Account A is created and airdropped 10 SOL
  2. A creates an NFT N
  3. Account B is created
  4. A pays B's rent fee
  5. N is minted to B associated token account
  6. Account C is created
  7. A pays C's rent fee
  8. B transfers N to C
  9. C destroys N

Let's get started:

$ python3 -i -m api.metaplex_api
>>> 

deploy

deploy will create a new NFT token by

  1. Creating a new account from a randomly generated address (invokes CreateAccount from the System Program)
  2. Invoking InitializeMint on the new account
  3. Initializing the metadata for this account by invoking the CreateMetatdata instruction from the Metaplex protocol

Args:

api_endpoint: (str) The RPC endpoint to connect the network. (devnet, mainnet)

name: (str) Name of the NFT Contract (32 bytes max)

symbol: (str) Symbol of the NFT Contract (10 bytes max)

skip_confirmation=True: A flag that tells you to wait for the transaction to be confirmed if set to False (only used for testing, because it requires synchronized/sequential calls)

Example:

>>> account = KeyPair()
>>> cfg = {"PRIVATE_KEY": base58.b58encode(account.seed).decode("ascii"), "PUBLIC_KEY": str(account.public_key), "DECRYPTION_KEY": Fernet.generate_key().decode("ascii")}
>>> api_endpoint = "https://api.devnet.solana.com/"
>>> Client(api_endpoint).request_airdrop(account.public_key, int(1e10))
{'jsonrpc': '2.0', 'result': '4ojKmAAesmKtqJkNLRtEjdgg4CkmowuTAjRSpp3K36UvQQvEXwhirV85E8cvWYAD42c3UyFdCtzydMgWokH2mbM', 'id': 1}
>>> metaplex_api = MetaplexAPI(cfg)
>>> seller_basis_fees = 0 # value in 10000 
>>> metaplex_api.deploy(api_endpoint, "A"*32, "A"*10, seller_basis_fees)
'{"status": 200, "contract": "7bxe7t1aGdum8o97bkuFeeBTcbARaBn9Gbv5sBd9DZPG", "msg": "Successfully created mint 7bxe7t1aGdum8o97bkuFeeBTcbARaBn9Gbv5sBd9DZPG", "tx": "2qmiWoVi2PNeAjppe2cNbY32zZCJLXMYgdS1zRVFiKJUHE41T5b1WfaZtR2QdFJUXadrqrjbkpwRN5aG2J3KQrQx"}'
>>> 

Note that when sending SOL to the newly generated account, that account will serve as the fee payer. You can check out this transaction on the Solana Block Exporer.

wallet

wallet creates a new random public/private keypair

>>> metaplex_api.wallet()
'{"address": "VtdBygLSt1EJF5M3nUk5CRxuNNTyZFUsKJ4yUVcC6hh", "private_key": [95, 46, 174, 145, 248, 101, 108, 111, 128, 44, 41, 212, 118, 145, 42, 242, 84, 6, 31, 115, 18, 126, 47, 230, 103, 202, 46, 7, 194, 149, 42, 213]}'
>>>

No network calls are made here

topup

topup sends a small amount of SOL to the destination account by invoking Transfer from the System Program

Args:

api_endpoint: (str) The RPC endpoint to connect the network. (devnet, mainnet)

to: (str) The base58 encoded public key of the destination address

amount: (Union[int, None]) This is the number of lamports to send to the destination address. If None (default), then the minimum rent exemption balance is transferred.

>>> metaplex_api.topup(api_endpoint, "VtdBygLSt1EJF5M3nUk5CRxuNNTyZFUsKJ4yUVcC6hh")
'{"status": 200, "msg": "Successfully sent 0.00203928 SOL to VtdBygLSt1EJF5M3nUk5CRxuNNTyZFUsKJ4yUVcC6hh", "tx": "32Dk647Fb6aKJyErVfxgtSfC4xbssoJprcB7BEmEAdYTFK96M5VEQ1z62QxCCC7tAPF1g9TNvMehoGNudLNaKTWE"}'
>>> 

tx link

mint

mint will mint a token to a designated user account by

  1. Fetching or creating an AssociatedTokenAccount from a Program Derived Address
  2. Invoking MintTo with the AssociatedTokenAccount as the destination
  3. Invoking the UpdateMetadata instruction from the Metaplex protocol to update the uri of the contract (containing the actual content)

Args:

api_endpoint: (str) The RPC endpoint to connect the network. (devnet, mainnet)

contract_key: (str) The base58 encoded public key of the mint address

dest_key: (str) The base58 encoded public key of the destinaion address (where the contract will be minted)

link: (str) The link to the content of the the NFT

>>> metaplex_api.mint(api_endpoint, "7bxe7t1aGdum8o97bkuFeeBTcbARaBn9Gbv5sBd9DZPG", "VtdBygLSt1EJF5M3nUk5CRxuNNTyZFUsKJ4yUVcC6hh", "https://arweave.net/1eH7bZS-6HZH4YOc8T_tGp2Rq25dlhclXJkoa6U55mM/")
'{"status": 200, "msg": "Successfully minted 1 token to DkrGGuqn183rNyYHQNo9NSDYKZB8FVsaPBGn3F6nG7iH", "tx": "5r4qY1LudNg49FXyduadoAm83cJDWVeypUX6dsGs91RJqSxzU5qTt9WXfXs3Lzs5ZGQsTDTRpDyiXorv1wCzrzsJ"}'
>>> 

tx link

send

send will send a token from one user account to another user account

  1. Fetching the AssociatedTokenAccount from a Program Derived Address for the sender
  2. Fetching or creatign the AssociatedTokenAccount from a Program Derived Address for the receiver
  3. Invoking Transfer (from the Token Program) with the receiver's AssociatedTokenAccount as the destination

Args:

api_endpoint: (str) The RPC endpoint to connect the network. (devnet, mainnet)

contract_key: (str) The base58 encoded public key of the mint address\

sender_key: (str) The base58 encoded public key of the source address (from which the contract will be transferred)

dest_key: (str) The base58 encoded public key of the destinaion address (to where the contract will be transferred)

encrypted_private_key: (bytes) The encrypted private key of the sender

>>> metaplex_api.wallet()
'{"address": "EnMb6ZntX43PFeX2NLcV4dtLhqsxF9hUr3tgF1Cwpwu8", "private_key": [172, 155, 209, 75, 226, 68, 91, 22, 199, 75, 148, 197, 143, 10, 211, 67, 5, 160, 101, 15, 139, 33, 208, 65, 59, 198, 5, 41, 167, 206, 85, 83]}'
>>> metaplex_api.topup(api_endpoint, "EnMb6ZntX43PFeX2NLcV4dtLhqsxF9hUr3tgF1Cwpwu8")
'{"status": 200, "msg": "Successfully sent 0.00203928 SOL to EnMb6ZntX43PFeX2NLcV4dtLhqsxF9hUr3tgF1Cwpwu8", "tx": "4erc1aPC8fSNV1kb41mgUSgMKMHhd8FdDd4gqFPQ9TmmS48QcaAi9zpNjzMG3UNr1dDw1mBxThZCgJyUPchiV3Jz"}'
>>> encrypted_key = metaplex_api.cipher.encrypt(bytes([95, 46, 174, 145, 248, 101, 108, 111, 128, 44, 41, 212, 118, 145, 42, 242, 84, 6, 31, 115, 18, 126, 47, 230, 103, 202, 46, 7, 194, 149, 42, 213]))
>>> metaplex_api.send(api_endpoint, "7bxe7t1aGdum8o97bkuFeeBTcbARaBn9Gbv5sBd9DZPG", "VtdBygLSt1EJF5M3nUk5CRxuNNTyZFUsKJ4yUVcC6hh", "EnMb6ZntX43PFeX2NLcV4dtLhqsxF9hUr3tgF1Cwpwu8", encrypted_key)
'{"status": 200, "msg": "Successfully transfered token from VtdBygLSt1EJF5M3nUk5CRxuNNTyZFUsKJ4yUVcC6hh to EnMb6ZntX43PFeX2NLcV4dtLhqsxF9hUr3tgF1Cwpwu8", "tx": "3ZsGcCfjUXviToSB4U6Wg1W1W4rm8bMT7wF8zfauTciK6PdszpLqcvmmYqqrz8mRGK8pQPABVewCk8EdsvNVhzp6"}'

tx link

burn

burn will remove a token from the blockchain

  1. Fetching the AssociatedTokenAccount from a Program Derived Address for the owner
  2. Invoking Burn (from the Token Program) with the owner's AssociatedTokenAccount as the destination

Args:

api_endpoint: (str) The RPC endpoint to connect the network. (devnet: https://api.devnet.solana.com/, mainnet: https://api.mainnet-beta.solana.com/)

contract_key: (str) The base58 encoded public key of the mint address

owner_key: (str) The base58 encoded public key of the owner address

encrypted_private_key: (bytes) The encrypted private key of the owner

>>> encrypted_key = metaplex_api.cipher.encrypt(bytes([172, 155, 209, 75, 226, 68, 91, 22, 199, 75, 148, 197, 143, 10, 211, 67, 5, 160, 101, 15, 139, 33, 208, 65, 59, 198, 5, 41, 167, 206, 85, 83]))
>>> metaplex_api.burn(api_endpoint, "7bxe7t1aGdum8o97bkuFeeBTcbARaBn9Gbv5sBd9DZPG", "EnMb6ZntX43PFeX2NLcV4dtLhqsxF9hUr3tgF1Cwpwu8", encrypted_key)
'{"status": 200, "msg": "Successfully burned token 7bxe7t1aGdum8o97bkuFeeBTcbARaBn9Gbv5sBd9DZPG on EnMb6ZntX43PFeX2NLcV4dtLhqsxF9hUr3tgF1Cwpwu8", "tx": "5kd5g4mNBSjoTVYwAasWZx6iB8ijaELfBukKrNYBeDvLomK7iTqFH1R29yniEGcfajakDxsqmYCDgDvukihRyZeZ"}'
>>> 

https://explorer.solana.com/tx/5kd5g4mNBSjoTVYwAasWZx6iB8ijaELfBukKrNYBeDvLomK7iTqFH1R29yniEGcfajakDxsqmYCDgDvukihRyZeZ?cluster=devnet

Full Example Code:

This is the sequential code from the previous section. These accounts will need to change if you want to do your own test.

account = KeyPair()
cfg = {"PRIVATE_KEY": base58.b58encode(account.seed).decode("ascii"), "PUBLIC_KEY": str(account.public_key), "DECRYPTION_KEY": Fernet.generate_key().decode("ascii")}
api_endpoint = "https://api.devnet.solana.com/"
Client(api_endpoint).request_airdrop(account.public_key, int(1e10))

# Create API
metaplex_api = MetaplexAPI(cfg)

# Deploy
metaplex_api.deploy(api_endpoint, "A"*32, "A"*10, 0)

# Topup VtdBygLSt1EJF5M3nUk5CRxuNNTyZFUsKJ4yUVcC6hh
metaplex_api.topup(api_endpoint, "VtdBygLSt1EJF5M3nUk5CRxuNNTyZFUsKJ4yUVcC6hh")

# Mint
metaplex_api.mint(api_endpoint, "7bxe7t1aGdum8o97bkuFeeBTcbARaBn9Gbv5sBd9DZPG", "VtdBygLSt1EJF5M3nUk5CRxuNNTyZFUsKJ4yUVcC6hh", "https://arweave.net/1eH7bZS-6HZH4YOc8T_tGp2Rq25dlhclXJkoa6U55mM/")

# Topup EnMb6ZntX43PFeX2NLcV4dtLhqsxF9hUr3tgF1Cwpwu8
metaplex_api.topup(api_endpoint, "EnMb6ZntX43PFeX2NLcV4dtLhqsxF9hUr3tgF1Cwpwu8")

# Send
encrypted_key = metaplex_api.cipher.encrypt(bytes([95, 46, 174, 145, 248, 101, 108, 111, 128, 44, 41, 212, 118, 145, 42, 242, 84, 6, 31, 115, 18, 126, 47, 230, 103, 202, 46, 7, 194, 149, 42, 213]))
metaplex_api.send(api_endpoint, "7bxe7t1aGdum8o97bkuFeeBTcbARaBn9Gbv5sBd9DZPG", "VtdBygLSt1EJF5M3nUk5CRxuNNTyZFUsKJ4yUVcC6hh", "EnMb6ZntX43PFeX2NLcV4dtLhqsxF9hUr3tgF1Cwpwu8", encrypted_key)

# Burn
encrypted_key = metaplex_api.cipher.encrypt(bytes([172, 155, 209, 75, 226, 68, 91, 22, 199, 75, 148, 197, 143, 10, 211, 67, 5, 160, 101, 15, 139, 33, 208, 65, 59, 198, 5, 41, 167, 206, 85, 83]))
metaplex_api.burn(api_endpoint, "7bxe7t1aGdum8o97bkuFeeBTcbARaBn9Gbv5sBd9DZPG", "EnMb6ZntX43PFeX2NLcV4dtLhqsxF9hUr3tgF1Cwpwu8", encrypted_key)
 

More Repositories

1

metaplex

A directory of what the Metaplex Foundation works on!
3,307
star
2

metaplex-program-library

Smart contracts maintained by the Metaplex team
Rust
579
star
3

js

A JavaScript SDK for interacting with Metaplex's programs
TypeScript
307
star
4

sugar

Candy Machine Rust CLI.
Rust
185
star
5

Solana.Swift

This is a open source library on pure swift for Solana protocol.
Swift
158
star
6

umi

A Solana Framework for JS Clients.
TypeScript
147
star
7

js-deprecated

Deprecated Metaplex JavaScript SDK
TypeScript
127
star
8

solita

Genrates an SDK API from solana contract IDL.
TypeScript
115
star
9

kinobi

Generate powerful clients for your Solana programs.
TypeScript
103
star
10

candy-machine-ui

Reference minting UI implementation for the Metaplex Candy Machine Program
TypeScript
70
star
11

amman

A modern mandatory toolbelt to help test solana SDK libraries and apps on a locally running validator.
TypeScript
67
star
12

js-examples

Examples and Starter Kits using the new JS SDK
JavaScript
67
star
13

SolanaKT

This is a open source library on kotlin for Solana protocol.
Kotlin
67
star
14

digital-asset-rpc-infrastructure

Reference implementation for Metaplex Digital Asset Standard API
Rust
66
star
15

mpl-bubblegum

Create and manage Metaplex compressed NFTs
Rust
65
star
16

shank

Extracts IDL from Solana Rust contracts
Rust
61
star
17

docs

docs.metaplex.com source code
JavaScript
43
star
18

digital-asset-validator-plugin

The NFT Geyser plugin that powers metaplex APIs
Rust
33
star
19

blockbuster

Canonical Program Parsing from Geyser Plugins
Rust
27
star
20

gumdrop

Gumdrop!
TypeScript
27
star
21

beet

Borsh compatible De/Serializer
TypeScript
26
star
22

mpl-token-auth-rules

A program that provides the ability to create and execute rules to restrict common token operations such as transferring and selling.
HTML
24
star
23

metaplex-ios

Metaplex Mobile IOS SDK
Swift
23
star
24

mpl-candy-guard

Access control logic for Metaplex Candy Machine.
21
star
25

get-collection

Rust and TypeScript example code for finding all members from a collection id.
Rust
21
star
26

metaplex-android

Metaplex Mobile Android SDK
Kotlin
18
star
27

compression-read-api-js-examples

TypeScript
16
star
28

aura

Rust
16
star
29

mip

14
star
30

mpl-candy-machine

Mint your NFT collection on Solana
TypeScript
12
star
31

mpl-toolbox

Essential programs and clients to build on top of.
TypeScript
10
star
32

solana-kmp

Kotlin
9
star
33

lut

A simple CLI for creating and managing Solana Lookup Tables.
Rust
8
star
34

mplex

Metaplex CLI to interact with the Metaplex SDK
TypeScript
7
star
35

arweave-cost

Calculates the cost of uploading files to Arweave
TypeScript
7
star
36

js-react-native

Java
6
star
37

mpl-trifle

The core composability contract for Metaplex Fusion
TypeScript
5
star
38

amman-explorer

Amman Solana Explorer
TypeScript
5
star
39

api-specifications

Public Documentation for our Written Specifications that have multi party implementations
5
star
40

token-entangler-ui

A UI for the token entangler program
TypeScript
5
star
41

mpl-token-metadata

Client library for the Token Metadata program
TypeScript
4
star
42

goose

A CLI for interacting with the mpl-migration-validator program.
Rust
4
star
43

themis

A CLI to allow our automated workflows to make proposals to spl-governance programs.
Rust
4
star
44

deprecated-storefront

The Metaplex Storefront frontend that is no longer maintained by the Metaplex Foundation
TypeScript
3
star
45

solana-project-template

A template for vanilla Solana programs and their clients
TypeScript
3
star
46

cusper

Resolves custom program errors from solana logs or error codes.
TypeScript
3
star
47

solita-swift

Sol ana I DL t o A PI generator.
Swift
3
star
48

digital-asset-protocol

Rust
3
star
49

florida

Where programs go to retire.
Rust
3
star
50

actions

A set of useful GitHub actions for Solana devs
3
star
51

deprecated-clis

A collection of JS CLIs that are no longer maintained by the Metaplex Foundation
TypeScript
3
star
52

fusion-dressing-room

Fusion user-facing UI forked from Solana dApp Scaffold
TypeScript
3
star
53

metaplex-incubating-programs

Programs that have not met the maturity, security and stability requirements live here, free of charge with community stewards
Rust
3
star
54

Metaplex-Deployer

2
star
55

shank-js

Generate IDL files via Anchor or Shank.
TypeScript
2
star
56

mpl-hydra

Client library for the Hydra program
TypeScript
2
star
57

umi-hotline

2
star
58

beet-swift

Swift
2
star
59

larry-discord-bot

The Larry Discord bots we use in our Discord server!
TypeScript
2
star
60

metaplex-swift-program-library

Generated Code for metaplex-ios
Swift
2
star
61

das-api-testing

Rust
2
star
62

listing-rewards-auctioneer

1
star
63

firewall

Solana Account Firewall
Rust
1
star
64

rooster

Three crows, and you're out. But there is always grace.
Rust
1
star
65

auctioneer-template

Template program for users to build their own Auctioneer
Rust
1
star
66

rustbin

Synchronizes a Rust binary version with the related Rust crate
TypeScript
1
star
67

mpl-migration-validator

Asset migration validator program, to be used in conjuction with Token Metadata to enable asset class migrations.
Rust
1
star
68

check-collections

Rust
1
star
69

mpl-core

Rust
1
star
70

fusion-ui

The Proof-of-Concept UI interfaces for Composable NFTs
TypeScript
1
star