Esempio n. 1
0
async def create_mint(client: AsyncClient, payer: Keypair, mint: Keypair,
                      mint_authority: PublicKey):
    mint_balance = await AsyncToken.get_min_balance_rent_for_exempt_for_mint(
        client)
    print(f"Creating pool token mint {mint.public_key}")
    txn = Transaction()
    txn.add(
        sys.create_account(
            sys.CreateAccountParams(
                from_pubkey=payer.public_key,
                new_account_pubkey=mint.public_key,
                lamports=mint_balance,
                space=MINT_LAYOUT.sizeof(),
                program_id=TOKEN_PROGRAM_ID,
            )))
    txn.add(
        spl_token.initialize_mint(
            spl_token.InitializeMintParams(
                program_id=TOKEN_PROGRAM_ID,
                mint=mint.public_key,
                decimals=9,
                mint_authority=mint_authority,
                freeze_authority=None,
            )))
    await client.send_transaction(txn,
                                  payer,
                                  mint,
                                  opts=TxOpts(skip_confirmation=False,
                                              preflight_commitment=Confirmed))
Esempio n. 2
0
    def create_mint(
        conn: Client,
        payer: Account,
        mint_authority: PublicKey,
        decimals: int,
        program_id: PublicKey,
        freeze_authority: Optional[PublicKey] = None,
        skip_confirmation: bool = False,
    ) -> Token:
        """Create and initialize a token.

        :param conn: RPC connection to a solana cluster.
        :param payer: Fee payer for transaction.
        :param mint_authority: Account or multisig that will control minting.
        :param decimals: Location of the decimal place.
        :param program_id: SPL Token program account.
        :param freeze_authority: (optional) Account or multisig that can freeze token accounts.
        :param skip_confirmation: (optional) Option to skip transaction confirmation.
        :return: Token object for the newly minted token.

        If skip confirmation is set to `False`, this method will block for at most 30 seconds
        or until the transaction is confirmed.
        """
        mint_account = Account()
        token = Token(conn, mint_account.public_key(), program_id, payer)
        # Allocate memory for the account
        balance_needed = Token.get_min_balance_rent_for_exempt_for_mint(conn)
        # Construct transaction
        txn = Transaction()
        txn.add(
            sp.create_account(
                sp.CreateAccountParams(
                    from_pubkey=payer.public_key(),
                    new_account_pubkey=mint_account.public_key(),
                    lamports=balance_needed,
                    space=MINT_LAYOUT.sizeof(),
                    program_id=program_id,
                )
            )
        )
        txn.add(
            spl_token.initialize_mint(
                spl_token.InitializeMintParams(
                    program_id=program_id,
                    mint=mint_account.public_key(),
                    decimals=decimals,
                    mint_authority=mint_authority,
                    freeze_authority=freeze_authority,
                )
            )
        )
        # Send the two instructions
        conn.send_transaction(
            txn, payer, mint_account, opts=TxOpts(skip_confirmation=skip_confirmation, skip_preflight=True)
        )
        return token
def test_initialize_mint(stubbed_sender):
    """Test initialize mint."""
    mint_authority, freeze_authority = PublicKey(0), PublicKey(1)
    params_with_freeze = spl_token.InitializeMintParams(
        decimals=18,
        program_id=TOKEN_PROGRAM_ID,
        mint=stubbed_sender.public_key(),
        mint_authority=mint_authority,
        freeze_authority=freeze_authority,
    )
    instruction = spl_token.initialize_mint(params_with_freeze)
    assert spl_token.decode_initialize_mint(instruction) == params_with_freeze

    params_no_freeze = spl_token.InitializeMintParams(
        decimals=18,
        program_id=TOKEN_PROGRAM_ID,
        mint=stubbed_sender.public_key(),
        mint_authority=mint_authority,
    )
    instruction = spl_token.initialize_mint(params_no_freeze)
    decoded_params = spl_token.decode_initialize_mint(instruction)
    assert not decoded_params.freeze_authority
    assert decoded_params == params_no_freeze
Esempio n. 4
0
 def _create_mint_args(
     conn: Union[Client, AsyncClient],
     payer: Keypair,
     mint_authority: PublicKey,
     decimals: int,
     program_id: PublicKey,
     freeze_authority: Optional[PublicKey],
     skip_confirmation: bool,
     balance_needed: int,
     cls: Union[Type[Token], Type[AsyncToken]],
 ) -> Tuple[Union[Token, AsyncToken], Transaction, Keypair, Keypair, TxOpts]:
     mint_keypair = Keypair()
     token = cls(conn, mint_keypair.public_key, program_id, payer)  # type: ignore
     # Construct transaction
     txn = Transaction()
     txn.add(
         sp.create_account(
             sp.CreateAccountParams(
                 from_pubkey=payer.public_key,
                 new_account_pubkey=mint_keypair.public_key,
                 lamports=balance_needed,
                 space=MINT_LAYOUT.sizeof(),
                 program_id=program_id,
             )
         )
     )
     txn.add(
         spl_token.initialize_mint(
             spl_token.InitializeMintParams(
                 program_id=program_id,
                 mint=mint_keypair.public_key,
                 decimals=decimals,
                 mint_authority=mint_authority,
                 freeze_authority=freeze_authority,
             )
         )
     )
     return token, txn, payer, mint_keypair, TxOpts(skip_confirmation=skip_confirmation, skip_preflight=True)