├── LICENSE ├── README.md ├── lambda_function.py └── mint.py /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 BedrosovaYulia 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # NFT-minting-using-python 2 | Minting Solana NFT using candy-machine-cli is quite expensive, so I searched and I have found a cheaper option: it is 20% cheaper!!! 3 | 4 | Using: 5 | 6 | arweave deploy --key-file /Users/BedrosovaYulia/Documents/Projects/metaplex-api/python-api/arweave-keyfile-rpLKsmy-xTFUyFezwRi3vQWuHghA96cuWbAkLhpRtbY.json /Users/BedrosovaYulia/Documents/Projects/metaplex-api/python-api/images/bedrosova1.png 7 | 8 | arweave deploy --key-file /Users/BedrosovaYulia/Documents/Projects/metaplex-api/python-api/arweave-keyfile-rpLKsmy-xTFUyFezwRi3vQWuHghA96cuWbAkLhpRtbY.json /Users/BedrosovaYulia/Documents/Projects/metaplex-api/python-api/images/bedrosova-test-nft-1.json 9 | 10 | python3 mint.py -j 'https://arweave.net/S0Eo5QsC2yS9svD7denUaYa36JvSxYFAG9D4DwxUWGE' 11 | 12 | You can put these commands in the pipeline and use them for your NFT batch generation. 13 | 14 | # AWS Lambda version: 15 | To prepare the layer for the Lambda, I first deployed amazonlinux on my machine in docker and installed the necessary libraries (solana, cryptography) in the folder python/lib/python3.7/site-packages/ 16 | -------------------------------------------------------------------------------- /lambda_function.py: -------------------------------------------------------------------------------- 1 | import json 2 | import base58 3 | from solana.keypair import Keypair 4 | from solana.rpc.api import Client 5 | from metaplex.metadata import get_metadata 6 | from cryptography.fernet import Fernet 7 | import api.metaplex_api as metaplex_api 8 | 9 | import boto3 10 | import base64 11 | from botocore.exceptions import ClientError 12 | import datetime 13 | import time 14 | 15 | 16 | 17 | def get_secret(secret_name,region_name): 18 | session = boto3.session.Session() 19 | client = session.client( 20 | service_name='secretsmanager', 21 | region_name=region_name 22 | ) 23 | try: 24 | get_secret_value_response = client.get_secret_value( 25 | SecretId=secret_name 26 | ) 27 | except ClientError as e: 28 | print("Can't find solana secret key") 29 | raise e 30 | else: 31 | if 'SecretString' in get_secret_value_response: 32 | secret = get_secret_value_response['SecretString'] 33 | return secret 34 | else: 35 | decoded_binary_secret = base64.b64decode(get_secret_value_response['SecretBinary']) 36 | return decoded_binary_secret 37 | 38 | 39 | 40 | 41 | def lambda_handler(event, context): 42 | 43 | key=event['Records'][0]['s3']['object']['key'] 44 | 45 | if (key): 46 | 47 | secret = json.loads(get_secret("solana","us-east-1"))['solana_key']; 48 | #print(secret) 49 | 50 | lines = secret.split(',') 51 | key_from_file = [int(x) for x in lines] 52 | 53 | keypair = Keypair(key_from_file[:32]) 54 | print(keypair.public_key) 55 | cfg = { 56 | "PRIVATE_KEY": base58.b58encode(keypair.seed).decode("ascii"), 57 | "PUBLIC_KEY": str(keypair.public_key), 58 | "DECRYPTION_KEY": Fernet.generate_key().decode("ascii"), 59 | } 60 | api = metaplex_api.MetaplexAPI(cfg) 61 | 62 | api_endpoint = "https://api.devnet.solana.com/" 63 | 64 | 65 | # deploy a contract. will return a contract key. 66 | 67 | now = datetime.datetime.now() 68 | 69 | json_file_template={ 70 | "name": "Bedrosova"+now.strftime("%H-%M-%S"), 71 | "description": "Yuliya Bedrosova Test NFT from AWS Lambda", 72 | "seller_fee_basis_points": 500, 73 | "image": "https://solana-nft-minter.s3.amazonaws.com/"+str(key), 74 | "collection": { 75 | "name": "Bedrosova Test NFT from AWS Lambda", 76 | "family": "Bedrosova Test NFT" 77 | }, 78 | "properties": { 79 | "files": [ 80 | { 81 | "uri": "https://solana-nft-minter.s3.amazonaws.com/"+str(key), 82 | "type": "image/jpg" 83 | } 84 | ], 85 | "category": "image", 86 | "creators": [ 87 | { 88 | "address": str(keypair.public_key), 89 | "share": 100 90 | } 91 | ] 92 | } 93 | } 94 | 95 | s3_client = boto3.client('s3') 96 | bucket_name = "solana-nft-minter" 97 | 98 | 99 | response = s3_client.put_object( 100 | ACL='public-read', 101 | Body=json.dumps(json_file_template, indent=2), 102 | ContentType='application/json', 103 | Bucket=bucket_name, 104 | Key="json/test-nft-"+now.strftime("%d-%m-%Y-%H-%M-%S")+".json", 105 | ) 106 | 107 | if(response): 108 | print("Deploy:") 109 | result = api.deploy(api_endpoint, "TestNFT", "TNF", fees=300) 110 | print("Deploy completed. Result: %s",result) 111 | print("Load contract key:") 112 | contract_key = json.loads(result).get('contract') 113 | print("Contract key loaded. Conract key: %s", contract_key) 114 | print("Mint:") 115 | # conduct a mint, and send to a recipient, e.g. wallet_2 116 | divinity_json_file="https://solana-nft-minter.s3.amazonaws.com/json/test-nft-"+now.strftime("%d-%m-%Y-%H-%M-%S")+".json" 117 | print(api_endpoint, contract_key, keypair.public_key,divinity_json_file) 118 | 119 | try: 120 | mint_res = api.mint(api_endpoint, contract_key, keypair.public_key,divinity_json_file) 121 | print("Mint completed. Result: %s", mint_res) 122 | except: 123 | print("try to mint another time:") 124 | #time.sleep(60) 125 | mint_res = api.mint(api_endpoint, contract_key, keypair.public_key,divinity_json_file) 126 | print("Mint completed. Result: %s", mint_res) 127 | 128 | 129 | return { 130 | 'statusCode': 200, 131 | 'body': json.dumps('Hello from Lambda!') 132 | } 133 | -------------------------------------------------------------------------------- /mint.py: -------------------------------------------------------------------------------- 1 | import json 2 | import base58 3 | from solana.keypair import Keypair 4 | from solana.rpc.api import Client 5 | from metaplex.metadata import get_metadata 6 | from cryptography.fernet import Fernet 7 | import api.metaplex_api as metaplex_api 8 | import argparse 9 | 10 | 11 | def parse_commandline_arguments(): 12 | 13 | global divinity_json_file 14 | 15 | parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter, 16 | description='Specify the json file') 17 | 18 | parser.add_argument("-j", "--jsonf", dest="jsonf", 19 | type=str, help="Specify the json file from https://arweave.net") 20 | 21 | args = parser.parse_args() 22 | divinity_json_file = str(args.jsonf) 23 | 24 | if __name__ == '__main__': 25 | parse_commandline_arguments() 26 | 27 | if (divinity_json_file): 28 | 29 | text_file = open("/Users/BedrosovaYulia/.config/solana/id.json", "r") 30 | lines = text_file.read()[1:-1].split(',') 31 | key_from_file = [int(x) for x in lines] 32 | 33 | keypair = Keypair(key_from_file[:32]) 34 | print(keypair.public_key) 35 | cfg = { 36 | "PRIVATE_KEY": base58.b58encode(keypair.seed).decode("ascii"), 37 | "PUBLIC_KEY": str(keypair.public_key), 38 | "DECRYPTION_KEY": Fernet.generate_key().decode("ascii"), 39 | } 40 | api = metaplex_api.MetaplexAPI(cfg) 41 | 42 | api_endpoint = "https://api.devnet.solana.com/" 43 | 44 | # requires a JSON file with metadata. best to publish on Arweave 45 | #divinity_json_file = "https://arweave.net/KMqr5IowiHhfxhd_Yo1y6tnF2cc6sryeD3cU9IIxpFk" 46 | print(divinity_json_file) 47 | # deploy a contract. will return a contract key. 48 | 49 | #print(api.wallet()) 50 | print("Deploy:") 51 | result = api.deploy(api_endpoint, "Test NFT deploy", "TNF", fees=300) 52 | print("Deploy completed. Result: %s",result) 53 | 54 | print("Load contract key:") 55 | contract_key = json.loads(result).get('contract') 56 | print("Contract key loaded. Conract key: %s", contract_key) 57 | print("Mint:") 58 | # conduct a mint, and send to a recipient, e.g. wallet_2 59 | mint_res = api.mint(api_endpoint, contract_key, keypair.public_key, divinity_json_file) 60 | print("Mint completed. Result: %s", mint_res) 61 | 62 | else: 63 | print("Specify the json file from https://arweave.net") 64 | --------------------------------------------------------------------------------