def mint(self,payer:Keypair,mintAuthority:str=None,owner:str=None,amount=1): """ :param pubkey: :param program_id: :param payer: :return: """ mintAuthority=payer.public_key if mintAuthority is None else PublicKey(mintAuthority) owner=payer.public_key if owner is None else PublicKey(owner) tk=Token(self.api,mintAuthority,program_id=TOKEN_PROGRAM_ID,payer=payer) token=tk.create_mint(self.api, mint_authority=mintAuthority, freeze_authority=mintAuthority, decimals=0, program_id=TOKEN_PROGRAM_ID, payer=payer, skip_confirmation=False) mint_info=token.get_mint_info() if amount>0: token.create_associated_token_account(owner=owner) tx_opts=TxOpts(skip_confirmation=False,max_retries=3) tk.mint_to(owner,mint_authority=mintAuthority,amount=amount,opts=tx_opts) return mint_info
def deploy(self): print('deploying solana SPL token') conn = SolanaInt(self.contract.network.name).connect() owner = PublicKey(str(self.admin_address)) key = Keypair.from_secret_key(bytes(SOLANA_KEYPAIR[0:32])) balance_needed = Token.get_min_balance_rent_for_exempt_for_mint(conn) token, txn, payer, mint_account, opts = Token._create_mint_args(conn, key, key.public_key, self.decimals, TOKEN_PROGRAM_ID, owner, False, balance_needed, Token) response = conn.send_transaction(txn, payer, mint_account, opts=opts) print(response) error = response['result']['meta']['err'] if error: raise Exception(f'error while deploying \n {error}') else: tx_hash = response['result']['transaction']['signatures'][0] contract_address = response['result']['transaction']['message']['accountKeys'][1] solana_contract = SolanaContract() solana_contract.contract = self.contract solana_contract.original_contract = self.contract solana_contract.tx_hash = tx_hash solana_contract.address = contract_address solana_contract.save() self.solana_contract = solana_contract self.save() self.msg_deployed({})
def create_token_mint(self): self.solana_client = SolanaClient(SOLANA_URL) with open("proxy/operator-keypairs/id.json") as f: d = json.load(f) self.mint_authority = SolanaAccount(d[0:32]) self.solana_client.request_airdrop(self.mint_authority.public_key(), 1000_000_000_000, Confirmed) while True: balance = self.solana_client.get_balance( self.mint_authority.public_key(), Confirmed)["result"]["value"] if balance > 0: break sleep(1) print('create_token_mint mint, SolanaAccount: ', self.mint_authority.public_key()) self.token = SplToken.create_mint( self.solana_client, self.mint_authority, self.mint_authority.public_key(), 9, TOKEN_PROGRAM_ID, )
def test_token(stubbed_sender, test_http_client) -> Token: """Test create mint.""" resp = test_http_client.request_airdrop(stubbed_sender.public_key(), 10000000) assert_valid_response(confirm_transaction(test_http_client, resp["result"])) expected_decimals = 6 expected_freeze_authority = Account() token_client = Token.create_mint( test_http_client, stubbed_sender, stubbed_sender.public_key(), expected_decimals, TOKEN_PROGRAM_ID, expected_freeze_authority.public_key(), ) assert token_client.pubkey assert token_client.program_id == TOKEN_PROGRAM_ID assert token_client.payer.public_key() == stubbed_sender.public_key() resp = test_http_client.get_account_info(token_client.pubkey) assert_valid_response(resp) assert resp["result"]["value"]["owner"] == str(TOKEN_PROGRAM_ID) mint_data = layouts.MINT_LAYOUT.parse( decode_byte_string(resp["result"]["value"]["data"][0])) assert mint_data.is_initialized assert mint_data.decimals == expected_decimals assert mint_data.supply == 0 assert PublicKey(mint_data.mint_authority) == stubbed_sender.public_key() assert PublicKey( mint_data.freeze_authority) == expected_freeze_authority.public_key() return token_client
def msg_deployed(self, message): print('msg_deployed method of the solana spl token') if self.contract.state != 'WAITING_FOR_DEPLOYMENT': take_off_blocking(self.contract.network.name) return else: conn = SolanaInt(self.contract.network.name).connect() key = Keypair.from_secret_key(bytes(SOLANA_KEYPAIR[0:32])) token_address = PublicKey(self.solana_contract.address) tok_int = Token(conn, token_address, TOKEN_PROGRAM_ID, key) holders = self.contract.tokenholder_set.all() if holders: print('transfering premint tokens') for th in holders: holder_addr = PublicKey(th.address) try: associated_address = tok_int.create_associated_token_account(holder_addr) print(f'created associated account {associated_address}') except RPCException: print('associated token account already created') associated_address = get_associated_token_address(holder_addr, tok_int.pubkey) response = tok_int.mint_to(associated_address, key, int(th.amount)) print(f'tx_hash = {response["result"]}') print('transferring of mint authority started') owner = PublicKey(self.admin_address) address = self.solana_contract.address tok_int.set_authority(address, key.public_key, 0, owner) print('successfully transferred mint authority') self.initialized({})
def get_audio_token(solana_client: Client): if WAUDIO_MINT_PUBKEY is None: logger.error( "cache_user_balance.py | Missing Required SPL Confirguration") return None waudio_token = Token( conn=solana_client, pubkey=WAUDIO_MINT_PUBKEY, program_id=SPL_TOKEN_PROGRAM_ID_PUBKEY, payer=Keypair.generate( ), # not making any txs so payer is not required ) return waudio_token
# Get private key and use that the create a new account #pubkey = "GKksmU6hSJ2X7zz1mcE3Qhr7DDEpvxqbygzb17SxygUD" f = open('./my-solana-wallet/my-keypair.json') private_key = json.load(f) mid = len(private_key) // 2 private_key = private_key[:mid] # to 32 bytes payer = Account(private_key) # Your account to pay fees print("Public Key is: {}".format(payer.public_key())) # %% cc = conn("https://api.mainnet-beta.solana.com/") quote_token = Token( cc, pubkey=PublicKey(quote_ccy_mint[quote_ccy]), # mint address of token program_id=TOKEN_PROGRAM_ID, payer=payer, ) quote_wallet = quote_token.create_account( payer.public_key(), skip_confirmation=False) # Make sure you send tokens to this address print("quote wallet: ", str(quote_wallet)) market_address = PublicKey(pair_mkt[pair]) market = Market.load(cc, market_address) tx_sig = market.place_order( payer=quote_wallet, #PublicKey(pubkey), # My public key owner=payer, side=side,
ek2 = bp.cipher.encrypt(a2.secret_key()) ek3 = bp.cipher.encrypt(a3.secret_key()) tu1 = json.loads(bp.topup(api_endpoint, str(a1.public_key()))) print(tu1) tu2 = json.loads(bp.topup(api_endpoint, str(a2.public_key()))) print(tu2) tu3 = json.loads(bp.topup(api_endpoint, str(a3.public_key()))) print(tu3) token = Token.create_mint( client, Account(bp.private_key), PublicKey(bp.public_key), 0, PublicKey(TOKEN_PROGRAM_ID), PublicKey(bp.public_key), skip_confirmation=False, ) mint = str(token.pubkey) res = json.loads(bp.initialize(api_endpoint, mint, skip_confirmation=False)) print(res) pool = res.get("binary_option") print(bp.mint_to(api_endpoint, pool, str(a1.public_key()), 1e6, skip_confirmation=False)) print(bp.mint_to(api_endpoint, pool, str(a2.public_key()), 1e6, skip_confirmation=False)) print(bp.mint_to(api_endpoint, pool, str(a3.public_key()), 1e6, skip_confirmation=False))