|
| 1 | + |
| 2 | +import "./spl_token.sol"; |
| 3 | +import "solana"; |
| 4 | + |
| 5 | +@program_id("J2eUKE878XKXJZaP7vXwxgWnWnNQMqHSkMPoRFQwa86b") |
| 6 | +contract pda_mint_authority { |
| 7 | + bytes1 bump; // stores the bump for the pda address |
| 8 | + |
| 9 | + @payer(payer) |
| 10 | + @seed("mint_authority") // hard-coded seed |
| 11 | + @bump(_bump) // bump for the pda address |
| 12 | + constructor(address payer, bytes1 _bump) { |
| 13 | + // Independently derive the PDA address from the seeds, bump, and programId |
| 14 | + (address pda, bytes1 pdaBump) = try_find_program_address(["mint_authority"], type(pda_mint_authority).program_id); |
| 15 | + |
| 16 | + // Verify that the bump passed to the constructor matches the bump derived from the seeds and programId |
| 17 | + // This ensures that only the canonical pda address can be used to create the account (first bump that generates a valid pda address) |
| 18 | + require(pdaBump == _bump, 'INVALID_BUMP'); |
| 19 | + |
| 20 | + bump = _bump; |
| 21 | + } |
| 22 | + |
| 23 | + function createTokenMint( |
| 24 | + address payer, // payer account |
| 25 | + address mint, // mint account to be created |
| 26 | + address mintAuthority, // mint authority for the mint account |
| 27 | + address freezeAuthority, // freeze authority for the mint account |
| 28 | + address metadata, // metadata account to be created |
| 29 | + uint8 decimals, // decimals for the mint account |
| 30 | + string name, // name for the metadata account |
| 31 | + string symbol, // symbol for the metadata account |
| 32 | + string uri // uri for the metadata account |
| 33 | + ) public view { |
| 34 | + // Invoke System Program to create a new account for the mint account and, |
| 35 | + // Invoke Token Program to initialize the mint account |
| 36 | + // Set mint authority, freeze authority, and decimals for the mint account |
| 37 | + SplToken.create_mint( |
| 38 | + payer, // payer account |
| 39 | + mint, // mint account |
| 40 | + mintAuthority, // mint authority |
| 41 | + freezeAuthority, // freeze authority |
| 42 | + decimals // decimals |
| 43 | + ); |
| 44 | + |
| 45 | + // Invoke Metadata Program to create a new account for the metadata account |
| 46 | + _createMetadataAccount( |
| 47 | + metadata, // metadata account |
| 48 | + mint, // mint account |
| 49 | + mintAuthority, // mint authority |
| 50 | + payer, // payer |
| 51 | + payer, // update authority (of the metadata account) |
| 52 | + name, // name |
| 53 | + symbol, // symbol |
| 54 | + uri // uri (off-chain metadata json) |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + // Create metadata account, must reimplement manually to sign with PDA, which is the mint authority |
| 59 | + function _createMetadataAccount( |
| 60 | + address metadata, // metadata account address |
| 61 | + address mint, // mint account address |
| 62 | + address mintAuthority, // mint authority |
| 63 | + address payer, // payer |
| 64 | + address updateAuthority, // update authority for the metadata account |
| 65 | + string name, // token name |
| 66 | + string symbol, // token symbol |
| 67 | + string uri // token uri |
| 68 | + ) private view { |
| 69 | + // // Independently derive the PDA address from the seeds, bump, and programId |
| 70 | + (address pda, bytes1 _bump) = try_find_program_address(["mint_authority"], type(pda_mint_authority).program_id); |
| 71 | + |
| 72 | + require(address(this) == pda, 'INVALID_PDA'); |
| 73 | + |
| 74 | + DataV2 data = DataV2({ |
| 75 | + name: name, |
| 76 | + symbol: symbol, |
| 77 | + uri: uri, |
| 78 | + sellerFeeBasisPoints: 0, |
| 79 | + creatorsPresent: false, |
| 80 | + collectionPresent: false, |
| 81 | + usesPresent: false |
| 82 | + }); |
| 83 | + |
| 84 | + CreateMetadataAccountArgsV3 args = CreateMetadataAccountArgsV3({ |
| 85 | + data: data, |
| 86 | + isMutable: true, |
| 87 | + collectionDetailsPresent: false |
| 88 | + }); |
| 89 | + |
| 90 | + AccountMeta[7] metas = [ |
| 91 | + AccountMeta({pubkey: metadata, is_writable: true, is_signer: false}), |
| 92 | + AccountMeta({pubkey: mint, is_writable: false, is_signer: false}), |
| 93 | + AccountMeta({pubkey: mintAuthority, is_writable: false, is_signer: true}), |
| 94 | + AccountMeta({pubkey: payer, is_writable: true, is_signer: true}), |
| 95 | + AccountMeta({pubkey: updateAuthority, is_writable: false, is_signer: false}), |
| 96 | + AccountMeta({pubkey: address"11111111111111111111111111111111", is_writable: false, is_signer: false}), |
| 97 | + AccountMeta({pubkey: address"SysvarRent111111111111111111111111111111111", is_writable: false, is_signer: false}) |
| 98 | + ]; |
| 99 | + |
| 100 | + bytes1 discriminator = 33; |
| 101 | + bytes instructionData = abi.encode(discriminator, args); |
| 102 | + |
| 103 | + address"metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s".call{accounts: metas, seeds: [["mint_authority", abi.encode(_bump)]]}(instructionData); |
| 104 | + } |
| 105 | + |
| 106 | + struct CreateMetadataAccountArgsV3 { |
| 107 | + DataV2 data; |
| 108 | + bool isMutable; |
| 109 | + bool collectionDetailsPresent; // To handle Rust Option<> in Solidity |
| 110 | + } |
| 111 | + |
| 112 | + struct DataV2 { |
| 113 | + string name; |
| 114 | + string symbol; |
| 115 | + string uri; |
| 116 | + uint16 sellerFeeBasisPoints; |
| 117 | + bool creatorsPresent; // To handle Rust Option<> in Solidity |
| 118 | + bool collectionPresent; // To handle Rust Option<> in Solidity |
| 119 | + bool usesPresent; // To handle Rust Option<> in Solidity |
| 120 | + } |
| 121 | + |
| 122 | + function mintTo(address payer, address tokenAccount, address mint, address owner) public view { |
| 123 | + // Create an associated token account for the owner to receive the minted token |
| 124 | + SplToken.create_associated_token_account( |
| 125 | + payer, // payer account |
| 126 | + tokenAccount, // associated token account address |
| 127 | + mint, // mint account |
| 128 | + owner // owner account |
| 129 | + ); |
| 130 | + |
| 131 | + // Mint 1 token to the associated token account |
| 132 | + _mintTo( |
| 133 | + mint, // mint account |
| 134 | + tokenAccount, // token account |
| 135 | + 1 // amount |
| 136 | + ); |
| 137 | + |
| 138 | + // Remove mint authority from mint account |
| 139 | + _removeMintAuthority( |
| 140 | + mint // mint |
| 141 | + ); |
| 142 | + } |
| 143 | + |
| 144 | + // Invoke the token program to mint tokens to a token account, using a PDA as the mint authority |
| 145 | + function _mintTo(address mint, address account, uint64 amount) private view { |
| 146 | + // Independently derive the PDA address from the seeds, bump, and programId |
| 147 | + (address pda, bytes1 _bump) = try_find_program_address(["mint_authority"], type(pda_mint_authority).program_id); |
| 148 | + require(address(this) == pda, 'INVALID_PDA'); |
| 149 | + |
| 150 | + // Prepare instruction data |
| 151 | + bytes instructionData = new bytes(9); |
| 152 | + instructionData[0] = uint8(7); // MintTo instruction index |
| 153 | + instructionData.writeUint64LE(1, 1); // Amount to mint |
| 154 | + |
| 155 | + // Prepare accounts required by instruction |
| 156 | + AccountMeta[3] metas = [ |
| 157 | + AccountMeta({pubkey: mint, is_writable: true, is_signer: false}), |
| 158 | + AccountMeta({pubkey: account, is_writable: true, is_signer: false}), |
| 159 | + AccountMeta({pubkey: pda, is_writable: true, is_signer: true}) // mint authority |
| 160 | + ]; |
| 161 | + |
| 162 | + // Invoke the token program with prepared accounts and instruction data |
| 163 | + SplToken.tokenProgramId.call{accounts: metas, seeds: [["mint_authority", abi.encode(_bump)]]}(instructionData); |
| 164 | + } |
| 165 | + |
| 166 | + function _removeMintAuthority(address mintAccount) private view { |
| 167 | + // Independently derive the PDA address from the seeds, bump, and programId |
| 168 | + (address pda, bytes1 _bump) = try_find_program_address(["mint_authority"], type(pda_mint_authority).program_id); |
| 169 | + require(address(this) == pda, 'INVALID_PDA'); |
| 170 | + |
| 171 | + AccountMeta[2] metas = [ |
| 172 | + AccountMeta({pubkey: mintAccount, is_signer: false, is_writable: true}), |
| 173 | + AccountMeta({pubkey: pda, is_signer: true, is_writable: false}) // mint authority |
| 174 | + ]; |
| 175 | + |
| 176 | + bytes instructionData = new bytes(9); |
| 177 | + instructionData[0] = uint8(6); // SetAuthority instruction index |
| 178 | + instructionData[1] = uint8(0); // AuthorityType::MintTokens |
| 179 | + instructionData[3] = 0; |
| 180 | + |
| 181 | + // Invoke the token program with prepared accounts and instruction data |
| 182 | + SplToken.tokenProgramId.call{accounts: metas, seeds: [["mint_authority", abi.encode(_bump)]]}(instructionData); |
| 183 | + } |
| 184 | +} |
0 commit comments