Browse Source

Use regular Option types for instruction data

COptions are only used for account data.
Loris Leiva 1 year ago
parent
commit
5f1425028c

+ 2 - 16
clients/js/src/generated/instructions/initializeMint.ts

@@ -26,8 +26,6 @@ import {
   getOptionEncoder,
   getStructDecoder,
   getStructEncoder,
-  getU32Decoder,
-  getU32Encoder,
   getU8Decoder,
   getU8Encoder,
   none,
@@ -82,13 +80,7 @@ export function getInitializeMintInstructionDataEncoder(): Encoder<InitializeMin
       ['discriminator', getU8Encoder()],
       ['decimals', getU8Encoder()],
       ['mintAuthority', getAddressEncoder()],
-      [
-        'freezeAuthority',
-        getOptionEncoder(getAddressEncoder(), {
-          prefix: getU32Encoder(),
-          fixed: true,
-        }),
-      ],
+      ['freezeAuthority', getOptionEncoder(getAddressEncoder())],
     ]),
     (value) => ({
       ...value,
@@ -103,13 +95,7 @@ export function getInitializeMintInstructionDataDecoder(): Decoder<InitializeMin
     ['discriminator', getU8Decoder()],
     ['decimals', getU8Decoder()],
     ['mintAuthority', getAddressDecoder()],
-    [
-      'freezeAuthority',
-      getOptionDecoder(getAddressDecoder(), {
-        prefix: getU32Decoder(),
-        fixed: true,
-      }),
-    ],
+    ['freezeAuthority', getOptionDecoder(getAddressDecoder())],
   ]);
 }
 

+ 2 - 16
clients/js/src/generated/instructions/setAuthority.ts

@@ -30,8 +30,6 @@ import {
   getOptionEncoder,
   getStructDecoder,
   getStructEncoder,
-  getU32Decoder,
-  getU32Encoder,
   getU8Decoder,
   getU8Encoder,
   transformEncoder,
@@ -84,13 +82,7 @@ export function getSetAuthorityInstructionDataEncoder(): Encoder<SetAuthorityIns
     getStructEncoder([
       ['discriminator', getU8Encoder()],
       ['authorityType', getAuthorityTypeEncoder()],
-      [
-        'newAuthority',
-        getOptionEncoder(getAddressEncoder(), {
-          prefix: getU32Encoder(),
-          fixed: true,
-        }),
-      ],
+      ['newAuthority', getOptionEncoder(getAddressEncoder())],
     ]),
     (value) => ({ ...value, discriminator: 6 })
   );
@@ -100,13 +92,7 @@ export function getSetAuthorityInstructionDataDecoder(): Decoder<SetAuthorityIns
   return getStructDecoder([
     ['discriminator', getU8Decoder()],
     ['authorityType', getAuthorityTypeDecoder()],
-    [
-      'newAuthority',
-      getOptionDecoder(getAddressDecoder(), {
-        prefix: getU32Decoder(),
-        fixed: true,
-      }),
-    ],
+    ['newAuthority', getOptionDecoder(getAddressDecoder())],
   ]);
 }
 

+ 45 - 0
clients/js/test/initializeMint.test.ts

@@ -64,3 +64,48 @@ test('it creates and initializes a new mint account', async (t) => {
     },
   });
 });
+
+test('it creates a new mint account with a freeze authority', async (t) => {
+  // Given an authority and a mint account.
+  const client = createDefaultSolanaClient();
+  const [payer, mintAuthority, freezeAuthority, mint] = await Promise.all([
+    generateKeyPairSignerWithSol(client),
+    generateKeyPairSigner(),
+    generateKeyPairSigner(),
+    generateKeyPairSigner(),
+  ]);
+
+  // When we create and initialize a mint account at this address.
+  const space = BigInt(getMintSize());
+  const rent = await client.rpc.getMinimumBalanceForRentExemption(space).send();
+  const instructions = [
+    getCreateAccountInstruction({
+      payer,
+      newAccount: mint,
+      lamports: rent,
+      space,
+      programAddress: TOKEN_PROGRAM_ADDRESS,
+    }),
+    getInitializeMintInstruction({
+      mint: mint.address,
+      decimals: 0,
+      mintAuthority: mintAuthority.address,
+      freezeAuthority: freezeAuthority.address,
+    }),
+  ];
+  await pipe(
+    await createDefaultTransaction(client, payer),
+    (tx) => appendTransactionMessageInstructions(instructions, tx),
+    (tx) => signAndSendTransaction(client, tx)
+  );
+
+  // Then we expect the mint account to exist and have the following data.
+  const mintAccount = await fetchMint(client.rpc, mint.address);
+  t.like(mintAccount, <Account<Mint>>{
+    address: mint.address,
+    data: {
+      mintAuthority: some(mintAuthority.address),
+      freezeAuthority: some(freezeAuthority.address),
+    },
+  });
+});

+ 4 - 4
program/idl.json

@@ -325,10 +325,10 @@
               "item": { "kind": "publicKeyTypeNode" },
               "prefix": {
                 "kind": "numberTypeNode",
-                "format": "u32",
+                "format": "u8",
                 "endian": "le"
               },
-              "fixed": true
+              "fixed": false
             },
             "defaultValue": {
               "kind": "noneValueNode"
@@ -776,10 +776,10 @@
               "item": { "kind": "publicKeyTypeNode" },
               "prefix": {
                 "kind": "numberTypeNode",
-                "format": "u32",
+                "format": "u8",
                 "endian": "le"
               },
-              "fixed": true
+              "fixed": false
             },
             "docs": ["The new authority"]
           }