Example code for interacting with solana anchor programs - candymachine

Overview

candypy

example code for interacting with solana anchor programs - candymachine

THIS IS PURELY SAMPLE CODE TO FORK, MODIFY, UNDERSTAND AND INTERACT WITH CANDYMACHINE USING ANCHORPY

I'll probably work on making this more resilient and fully featured, but at the moment, it's mainly to be used as an example of how to use anchorpy, and to understand candy machine better. the candymachine typescript client is amazing, but it couples together too many concerns - this is meant to interact with each instruction separately.

Interaction is primarily via command line to make it more explicit

some information about candymachine

  1. candymachine is an anchor program
  2. while a large portion of interacting with it is via anchorpy, there are still a few places where the interactions are directly through solanapy - mostly interactions with spl-token, spl-token-metadata

accounts used

There are 2 main accounts used by candymachine

  1. A config account which keeps track of total supply as well as rows [{ name: name, nft_metadata_uri: nft_metadata_uri}]
  2. A candymachine account which keeps track of the price and the date to go live

creating a candy machine involves the following steps at a high level

  1. creating the config account - using the create_config_account option. This is not an anchor interaction, but a vanilla interaction with the system program
  2. Initialization the config account (this is mainly allocating space for it based on the number of NFTs to be loaded, as well as paying rent) - using the initialize_config_account command (anchor)
  3. add NFTs into the config account (load the names and metadata urls for each nft as a separate row) - add_config_lines (anchor)
  4. create the main candymachine account (this is the second one). It will keep track of price, livedate, item count, treasury address which gets the payment
  5. update the candymachine account (this is optional, in case you want to change the date or the mint or the price)
  6. Mint. This is usually done through a browser client, but for illustration purposes the code also includes a mint command

step 1 - install the necessary stuff

pip install -r requirements.txt

also install the solana command line cli. its pretty useful

step 2 - configure solana cli for the devnet and get an airdrop of 5-10 sol

solana-keygen -o myfolder/wallet.json

solana config set -u d

solana airdrop 5 myfolder/wallet.json

run the last command a few times in case it fails. the key you just generted will serve the the payment key for configuring candymachine

step 3

create the config account

python main.py create_config_account myfolder/wallet.json 10

2yeCtaKgESShtnDWdH24EuhZLrfnkVoHk9t3WmmnJcaf

Note down the config accounts public address since you'll be using it for other commands

couple of things of note here-

  1. the space needed is calculated based on number of NFTs
  2. the rent exemption amount is in turn calculated by amount of space necesarry/ This can be optimized for a short term rent to make it easier, but i'm lazy for now
  3. the config accounts ownership is passed onto the CANDY_MACHINE_PROGRAM_ID

step 4

initialize the config account - store some basic data in it - authority, number of nfts, royalties, royalty split between creators

python main.py initialize_config_account myfolder/wallet.json ROBOTEST 10 2yeCtaKgESShtnDWdH24EuhZLrfnkVoHk9t3WmmnJcaf 100

here -

  1. ROBOTEST is the symbol
  2. royalties are basis points (1/100 * percentage)
  3. creator array which is an option json (do a -h to check it out) which has a % split of which address gets what % of the royalty. This should total to 100 and candymachine ts check for this but thats not the point of this tutorial for now.

step 5

Add config lines, i.e. the actual NFTs you're going to load into the machine

python main.py add_config_lines myfolder/wallet.json 2yeCtaKgESShtnDWdH24EuhZLrfnkVoHk9t3WmmnJcaf sample_files\nft_rows.json

These config lines are in the sample_files folder and the formation structure is a list of dicts-

[
  {"name": "rob #1",
 "uri": "https://gateway.pinata.cloud/ipfs/QmaPtzAKea1fcuaMhukiPsTVBEH7wwmMhDTxaN5Jz2zQq9/1.json"},
  {"name": "rob #2",
   "uri": "https://gateway.pinata.cloud/ipfs/QmaPtzAKea1fcuaMhukiPsTVBEH7wwmMhDTxaN5Jz2zQq9/2.json"},
  {"name": "rob #3",
   "uri": "https://gateway.pinata.cloud/ipfs/QmaPtzAKea1fcuaMhukiPsTVBEH7wwmMhDTxaN5Jz2zQq9/3.json"},
  {"name": "rob #4",
   "uri": "https://gateway.pinata.cloud/ipfs/QmaPtzAKea1fcuaMhukiPsTVBEH7wwmMhDTxaN5Jz2zQq9/4.json"},
  {"name": "rob #5",
   "uri": "https://gateway.pinata.cloud/ipfs/QmaPtzAKea1fcuaMhukiPsTVBEH7wwmMhDTxaN5Jz2zQq9/5.json"}
]

This sample has only 5 NFTs. It's possible to edit the code and upload the NFTs in batches, but since this is primarily for learning purposes, I'm keeping it simple (hardcoded index). becuase the index is always 0, it'll overwrite with the new set of files in the sample_files folder. Also, you'll notice this has the uri's already. Yes. This code expects you have your NFT uploaded to arweave or ipfs first. Another reason I went down this route besides learning was to keep a clear separation between code interating with solana and code interacting with arweave, file storage

step 6

create the actual cnady machine account

python main.py initialize_candy_machine myfolder/wallet.json 0.5 now 10 2yeCtaKgESShtnDWdH24EuhZLrfnkVoHk9t3WmmnJcaf ANAwyQU9HCZXaKkypAHkvTGzDEDGvVsHxto7jLhenp7q

"now" is s convenience ooption, but you want to pass the epoch timestamp of when you want the machine to go live 2yeCtaKgESShtnDWdH24EuhZLrfnkVoHk9t3WmmnJcaf as you know is the config account address ANAwyQU9HCZXaKkypAHkvTGzDEDGvVsHxto7jLhenp7q in this case is the wallet that should hold treasury funds

step 7

candy machine can be optionally updated. as many times as you like. Its mainly the price and the live date python main.py update_candy_machine myfolder/wallet.json 0.33 now 2yeCtaKgESShtnDWdH24EuhZLrfnkVoHk9t3WmmnJcaf

  • price
  • time (epoch)
  • config address

#step 8 This is actually the most complex part. Here is what's happening when you're minting an NFT

NON ANCHOR INSTRUCTIONS

  1. IMPORTANT: this is from a client side, so you would preferably want to configure another wallet + airdrop some sol in it.
  2. you create a new NFT token
  3. you create an associated token account derived from your main address to "hold" the NFT
  4. you allocate some default mint space for the account (based on some constants) - rent exempt amount because these need to be permanent
  5. you add approval for the candymachine to transfer the NFT out of your wallet, modify it and return the NFT token (now with metadata populated

Anchor insturctuions

  1. The ancor mint command doesn't take any args and just takes a list of accounts and signatures

python main.py mint myfolder/client-wallet.json 2yeCtaKgESShtnDWdH24EuhZLrfnkVoHk9t3WmmnJcaf ANAwyQU9HCZXaKkypAHkvTGzDEDGvVsHxto7jLhenp7q NAwyQU9HCZXaKkypAHkvTGzDEDGvVsHxto7jLhenp7q

myfolder/client-wallet.json is the new wallet creator for client side config address - same old, 2yeCtaKgESShtnDWdH24EuhZLrfnkVoHk9t3WmmnJcaf ANAwyQU9HCZXaKkypAHkvTGzDEDGvVsHxto7jLhenp7q - treaury address ANAwyQU9HCZXaKkypAHkvTGzDEDGvVsHxto7jLhenp7q - authority

step 8

once this is done, you can simply check the balance of your accont spl-token accounts --owner Token Balance

3W5RhXSBs5zyRpqeGUrC4xrN3ppNnRdBeYDYU3X1bhrp 1

Owner
dubbelosix
dubbelosix
dubbelosix
Translates English into Mandalorian (Mando'a) utilizing a "funtranslations" free API

Mandalorian Translator Translates English into Mandalorian (Mando'a) utilizing a "funtranslations" free API About I created this app to experiment wit

Hayden Covington 1 Dec 04, 2021
Python script to harvest tweets with the Twitter API V2 Academic Research Product Track

Tweet harvester Python script to scrape, collect, and/or harvest tweets with the Twitter API V2 Academic Research Product Track. Important note: In or

Thomas Frissen 2 Nov 11, 2021
RedFish API Toolkit

RedFish API Toolkit RedFish API Toolkit Build Status GitHub Actions Travis CI Requirements requirements.txt requirements-dev.txt Dependencies Document

Larry Smith Jr. 1 Nov 20, 2021
Async ShareX uploader written in python

Async ShareX uploader written in python

Jacob 2 Jan 07, 2022
NMux is the version of the NMscript in termux

NMscript-termux-version Termux-Version NMux is the termux version of NMscript which is NMscript? NMscript is a simple script written in Python that he

cabeson sin z 5 Apr 23, 2022
A discord bot written in python

arch-bot A discord bot written in python prefix: . help: .help Installation Requirements A discord bot token Your user id Python installed. For window

3 Jan 10, 2022
The Easy-to-use Dialogue Response Selection Toolkit for Researchers

Easy-to-use toolkit for retrieval-based Chatbot Our released data can be found at this link. Make sure the following steps are adopted to use our code

GMFTBY 32 Nov 13, 2022
Ulaavi for nuke, helps to keep our stocl elements organised.

Ulaavi Ulaavi for nuke, helps to keep our stock elements organised. Installation Downlaod ffmpeg from ffmpeg.org linux : https://johnvansickle.com/ffm

Arun Subramaniyam 17 Aug 24, 2022
This is Telegram Files Store Bot by @AbirHasan2005

PyroFilesStoreBot This is Telegram Parmanent Files Store Bot by @AbirHasan2005. Language: Python3 Library: Pyrogram Features: In PM Just Forward or Se

Abir Hasan 168 Dec 19, 2022
MCNameBot is a fast discord bot that is used to check the availability of a Minecraft name with a simple command.

MCNameBot MCNameBot is a fast discord bot that is used to check the availability of a Minecraft name with a simple command. If you would like to just

Killin 2 Oct 11, 2022
Quickly visualize docker networks with graphviz.

Docker Network Graph Visualize the relationship between Docker networks and containers as a neat graphviz graph. Example Usage usage: docker-net-graph

Leo Verto 43 Dec 12, 2022
Force-Subscribe-Bot - A Telegram Bot to force users to join a specific channel before sending messages in a group

Introduction A Telegram Bot to force users to join a specific channel before sen

LG Bot Updates 0 Jan 16, 2022
Chronocalc - Calculates the dates and times when the sun or moon is in a given position in the sky

Chronocalc I wrote this script after I was busy updating my article on chronoloc

16 Dec 13, 2022
Enigma simulator with python and clean code.

Enigma simulator with python and clean code.

Mohammad Dori 3 Jul 21, 2022
Telegram bot for stream music on telegram, powered by py-tgcalls and Pyrogram

Telegram Streamer Bot Telegram bot for stream music on telegram, powered by py-tgcalls and Pyrogram ✨ Features Coming soon, help me to improve it 🛠 C

Shohih Abdul 11 Oct 21, 2022
⛑ REDCap API interface in Python

REDCap API in Python Description Supports structured data extraction for REDCap projects. The API module d3b_redcap_api.redcap.REDCapStudy can be logi

D3b 1 Nov 21, 2022
PokemonGo-Bot - The Pokemon Go Bot, baking with community.

PokemonGo-Bot PokemonGo-Bot is a project created by the PokemonGoF team. Since no public API available for now, a patch to use HASH-Server was applied

3.8k Jan 08, 2023
Discord Token Generator - Python (Generates Tokens and Joins your Server Automatically) hCaptcha Bypass **FREE**

Best Discord Token Generator {hCaptcha bypass FREE Unlimited Memberboost} Install few requirements & run main.py it will redirect you to the Download

1 Oct 27, 2021
Simple Discord bot for snekbox (sandboxed Python code execution), self-host or use a global instance

snakeboxed Simple Discord bot for snekbox (sandboxed Python code execution), self-host or use a global instance

0 Jun 25, 2022
nuub-bot is a multi-purpose designed and developed in python3

nuub-bot About Nuub-Bot is an open source, fully customizable Discord bot that is constantly growing. You can invite it to your Discord server using t

Baneet Parmar 8 Jun 28, 2022