codama.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. import { renderJavaScriptUmiVisitor, renderJavaScriptVisitor, renderRustVisitor } from '@codama/renderers';
  2. import { accountLinkNode, accountNode, booleanTypeNode, booleanValueNode, constantDiscriminatorNode, constantPdaSeedNodeFromString, constantValueNode, createFromRoot, instructionAccountLinkNode, instructionAccountNode, instructionArgumentNode, instructionNode, numberTypeNode, numberValueNode, optionTypeNode, pdaLinkNode, pdaNode, pdaSeedValueNode, pdaValueNode, programNode, publicKeyTypeNode, publicKeyValueNode, resolverValueNode, rootNode, sizeDiscriminatorNode, sizePrefixTypeNode, stringTypeNode, stringValueNode, structFieldTypeNode, structTypeNode, variablePdaSeedNode } from "codama"
  3. import path from "path";
  4. import fs from "fs";
  5. const rustClientsDir = path.join(__dirname, "..", "sdk", "rust");
  6. const typescriptClientsDir = path.join(
  7. __dirname,
  8. "..",
  9. "sdk",
  10. "ts",
  11. );
  12. const root = rootNode(
  13. programNode({
  14. name: "block-list",
  15. publicKey: "BLoCKLSG2qMQ9YxEyrrKKAQzthvW4Lu8Eyv74axF6mf",
  16. version: "1.0.0",
  17. accounts: [
  18. accountNode({
  19. name: "config",
  20. discriminators: [
  21. constantDiscriminatorNode(constantValueNode(numberTypeNode("u8"), numberValueNode(0))),
  22. ],
  23. size: 41,
  24. pda: pdaLinkNode("config"),
  25. docs: ["The config PDA account"],
  26. data: structTypeNode([
  27. structFieldTypeNode({
  28. name: "discriminator",
  29. type: numberTypeNode("u8"),
  30. defaultValueStrategy: "omitted",
  31. }),
  32. structFieldTypeNode({
  33. name: "authority",
  34. type: publicKeyTypeNode(),
  35. }),
  36. structFieldTypeNode({
  37. name: "blocked_wallets_count",
  38. type: numberTypeNode("u64"),
  39. }),
  40. ]),
  41. }),
  42. accountNode({
  43. name: "walletBlock",
  44. discriminators: [
  45. constantDiscriminatorNode(constantValueNode(numberTypeNode("u8"), numberValueNode(1))),
  46. ],
  47. size: 33,
  48. pda: pdaLinkNode("walletBlock"),
  49. docs: ["The config PDA account"],
  50. data: structTypeNode([
  51. structFieldTypeNode({
  52. name: "authority",
  53. type: publicKeyTypeNode(),
  54. }),
  55. ])
  56. }),
  57. accountNode({
  58. name: "extraMetas",
  59. pda: pdaLinkNode("extraMetas"),
  60. docs: ["The extra metas PDA account"],
  61. })
  62. ],
  63. instructions: [
  64. instructionNode({
  65. name: "init",
  66. arguments: [
  67. instructionArgumentNode({
  68. name: 'discriminator',
  69. type: numberTypeNode('u8'),
  70. defaultValue: numberValueNode(0xF1),
  71. defaultValueStrategy: 'omitted',
  72. }),
  73. ],
  74. accounts: [
  75. instructionAccountNode({
  76. name: "authority",
  77. isSigner: true,
  78. isWritable: true,
  79. }),
  80. instructionAccountNode({
  81. name: "config",
  82. isSigner: false,
  83. isWritable: true,
  84. defaultValue: pdaValueNode(pdaLinkNode("config"))
  85. }),
  86. instructionAccountNode({
  87. name: "systemProgram",
  88. defaultValue: publicKeyValueNode("11111111111111111111111111111111", "systemProgram"),
  89. isSigner: false,
  90. isWritable: false,
  91. })
  92. ],
  93. discriminators: [
  94. constantDiscriminatorNode(constantValueNode(numberTypeNode("u8"), numberValueNode(0xF1))),
  95. ],
  96. docs: ["Initialize the config PDA account"],
  97. }),
  98. instructionNode({
  99. name: "blockWallet",
  100. arguments: [
  101. instructionArgumentNode({
  102. name: 'discriminator',
  103. type: numberTypeNode('u8'),
  104. defaultValue: numberValueNode(0xF2),
  105. defaultValueStrategy: 'omitted',
  106. }),
  107. ],
  108. accounts: [
  109. instructionAccountNode({
  110. name: "authority",
  111. isSigner: true,
  112. isWritable: true,
  113. }),
  114. instructionAccountNode({
  115. name: "config",
  116. isSigner: false,
  117. isWritable: true,
  118. defaultValue: pdaValueNode(pdaLinkNode("config"))
  119. }),
  120. instructionAccountNode({
  121. name: "wallet",
  122. isSigner: false,
  123. isWritable: false,
  124. }),
  125. instructionAccountNode({
  126. name: "walletBlock",
  127. isSigner: false,
  128. isWritable: true,
  129. }),
  130. instructionAccountNode({
  131. name: "systemProgram",
  132. defaultValue: publicKeyValueNode("11111111111111111111111111111111", "systemProgram"),
  133. isSigner: false,
  134. isWritable: false,
  135. })
  136. ],
  137. discriminators: [
  138. constantDiscriminatorNode(constantValueNode(numberTypeNode("u8"), numberValueNode(0xF2))),
  139. ],
  140. docs: ["Block a wallet"],
  141. }),
  142. instructionNode({
  143. name: "unblockWallet",
  144. arguments: [
  145. instructionArgumentNode({
  146. name: 'discriminator',
  147. type: numberTypeNode('u8'),
  148. defaultValue: numberValueNode(0xF3),
  149. defaultValueStrategy: 'omitted',
  150. }),
  151. ],
  152. accounts: [
  153. instructionAccountNode({
  154. name: "authority",
  155. isSigner: true,
  156. isWritable: true,
  157. }),
  158. instructionAccountNode({
  159. name: "config",
  160. isSigner: false,
  161. isWritable: true,
  162. defaultValue: pdaValueNode(pdaLinkNode("config"))
  163. }),
  164. instructionAccountNode({
  165. name: "walletBlock",
  166. isSigner: false,
  167. isWritable: true,
  168. }),
  169. instructionAccountNode({
  170. name: "systemProgram",
  171. defaultValue: publicKeyValueNode("11111111111111111111111111111111", "systemProgram"),
  172. isSigner: false,
  173. isWritable: false,
  174. })
  175. ],
  176. discriminators: [
  177. constantDiscriminatorNode(constantValueNode(numberTypeNode("u8"), numberValueNode(0xF3))),
  178. ],
  179. docs: ["Unblock a wallet"],
  180. }),
  181. instructionNode({
  182. name: "setupExtraMetas",
  183. arguments: [
  184. instructionArgumentNode({
  185. name: 'discriminator',
  186. type: numberTypeNode('u8'),
  187. defaultValue: numberValueNode(0x6A),
  188. defaultValueStrategy: 'omitted',
  189. }),
  190. instructionArgumentNode({
  191. name: 'checkBothWallets',
  192. type: booleanTypeNode(),
  193. defaultValue: booleanValueNode(false),
  194. defaultValueStrategy: 'optional',
  195. }),
  196. ],
  197. accounts: [
  198. instructionAccountNode({
  199. name: "authority",
  200. isSigner: true,
  201. isWritable: true,
  202. }),
  203. instructionAccountNode({
  204. name: "config",
  205. isSigner: false,
  206. isWritable: false,
  207. defaultValue: pdaValueNode(pdaLinkNode("config"))
  208. }),
  209. instructionAccountNode({
  210. name: "mint",
  211. isSigner: false,
  212. isWritable: false,
  213. }),
  214. instructionAccountNode({
  215. name: "extraMetas",
  216. isSigner: false,
  217. isWritable: true,
  218. defaultValue: pdaValueNode(pdaLinkNode("extraMetas"))
  219. }),
  220. instructionAccountNode({
  221. name: "systemProgram",
  222. defaultValue: publicKeyValueNode("11111111111111111111111111111111", "systemProgram"),
  223. isSigner: false,
  224. isWritable: false,
  225. })
  226. ],
  227. discriminators: [
  228. constantDiscriminatorNode(constantValueNode(numberTypeNode("u8"), numberValueNode(0x6A))),
  229. ],
  230. docs: ["Unblock a wallet"],
  231. }),
  232. ],
  233. pdas: [
  234. pdaNode({
  235. name: "config",
  236. seeds: [constantPdaSeedNodeFromString("utf8", "config")],
  237. docs: ["The config PDA account"],
  238. }),
  239. pdaNode({
  240. name: "walletBlock",
  241. seeds: [
  242. constantPdaSeedNodeFromString("utf8", "wallet_block"),
  243. variablePdaSeedNode("wallet", publicKeyTypeNode()),
  244. ],
  245. docs: ["The wallet block PDA account"],
  246. }),
  247. pdaNode({
  248. name: "extraMetas",
  249. seeds: [
  250. constantPdaSeedNodeFromString("utf8", "extra-account-metas"),
  251. variablePdaSeedNode("mint", publicKeyTypeNode()),
  252. ],
  253. docs: ["The extra metas PDA account"],
  254. }),
  255. ]
  256. })
  257. );
  258. function preserveConfigFiles() {
  259. const filesToPreserve = ['package.json', 'tsconfig.json', '.npmignore', 'pnpm-lock.yaml', 'Cargo.toml'];
  260. const preservedFiles = new Map();
  261. filesToPreserve.forEach(filename => {
  262. const filePath = path.join(typescriptClientsDir, filename);
  263. const tempPath = path.join(typescriptClientsDir, `${filename}.temp`);
  264. if (fs.existsSync(filePath)) {
  265. fs.copyFileSync(filePath, tempPath);
  266. preservedFiles.set(filename, tempPath);
  267. }
  268. });
  269. return {
  270. restore: () => {
  271. preservedFiles.forEach((tempPath, filename) => {
  272. const filePath = path.join(typescriptClientsDir, filename);
  273. if (fs.existsSync(tempPath)) {
  274. fs.copyFileSync(tempPath, filePath);
  275. fs.unlinkSync(tempPath);
  276. }
  277. });
  278. }
  279. };
  280. }
  281. const codama = createFromRoot(root)
  282. const configPreserver = preserveConfigFiles();
  283. codama.accept(renderJavaScriptVisitor('sdk/ts/src', { formatCode: true }));
  284. codama.accept(renderRustVisitor('sdk/rust/src/client', { crateFolder: 'sdk/rust/', formatCode: true }));