Skip to content

Commit

Permalink
Use the code from coral-xyz/anchor#537
Browse files Browse the repository at this point in the history
  • Loading branch information
TopDeveloper917 committed Aug 19, 2021
1 parent f153075 commit 60ae9de
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 253 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
# wumbo-programs
# wumbo-programs

## TODO

We should probably eventually change from @wum.bo/anchor, which is using code from https://github.com/project-serum/anchor/pull/537.
86 changes: 18 additions & 68 deletions app/idlToTs.js
Original file line number Diff line number Diff line change
@@ -1,88 +1,32 @@
import { watch, readdir, readFile, writeFile } from 'fs/promises';
import camelcase from "camelcase";

const idl = "./target/idl/";

const fieldToType = {
publicKey: "PublicKey",
u64: "BN",
u32: "number",
u16: "number",
u8: "number",
i64: "BN"
}

function primitiveFieldToTs(field) {
return `${field.name}: ${fieldToType[field.type]};`
}

function enumVariantToTs(variant) {
return `${variant.name}: { ${variant.name.toLowerCase()}: {} }`
}

function structToTs(struct) {
const structFields = (struct.type.fields || []).filter(f => !!f.type.defined)
const primitiveFields = (struct.type.fields || []).filter(f => typeof f.type === "string");
function enumToTs(struct) {
const enumVariants = struct.type.variants;
if (enumVariants) {
return `export type ${struct.name} = Record<string, Record<string, any>>
export const ${struct.name} = {
return `export type ${capitalizeFirstLetter(struct.name)} = Record<string, Record<string, any>>
export const ${capitalizeFirstLetter(struct.name)} = {
${enumVariants.map(enumVariantToTs).join(",\n ")}
}
`
}
return `export interface ${struct.name} {
${structFields.map(f => `${f.name}: ${f.type.defined};`).join("\n ")}
${primitiveFields.map(primitiveFieldToTs).join("\n ")}
}
`
}

function accountClient(acc) {
return `export class ${acc.name}AccountClient extends AccountClient {
override async fetch(address: Address): Promise<${acc.name}> {
return (await this.fetch(address)) as ${acc.name};
}
parse(accountInfo: AccountInfo<Buffer>): ${acc.name} {
return this.coder.accounts.decode("${acc.name}", accountInfo.data);
}
override async all(filter?: Buffer): Promise<ProgramAccount<${acc.name}>[]> {
return (await this.all(filter)).map(programAccount => ({
...programAccount,
account: programAccount.account as ${acc.name}
}))
}
}
function allEnumsToTs(idlDef) {
return `
${idlDef.types.map(enumToTs).filter(Boolean).join("\n\n")}
${idlDef.accounts.map(enumToTs).filter(Boolean).join("\n\n")}
`
}

function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
function lowerFirstLetter(string) {
return string.charAt(0).toLowerCase() + string.slice(1);
}

function toTs(name, idlDef) {
const camelName = capitalizeFirstLetter(name.replace(/-([a-z])/g, function (g) { return g[1].toUpperCase(); }));
return `import { PublicKey, AccountInfo } from "@solana/web3.js";
import { ProgramAccount , Address, Program, AccountNamespace, AccountClient } from "@project-serum/anchor";
import BN from "bn.js";
${idlDef.types.map(structToTs).join("\n\n")}
${idlDef.accounts.map(structToTs).join("\n\n")}
${idlDef.accounts.map(accountClient).join("\n\n")}
export interface ${camelName}AccountNamespace extends AccountNamespace {
${idlDef.accounts.map(acc => `${lowerFirstLetter(acc.name)}: ${acc.name}AccountClient`).join(";\n ")}
}
export interface ${camelName}Program extends Program {
account: ${camelName}AccountNamespace
}
`
function capitalizeFirstLetter(str) {
return str[0].toUpperCase() + str.slice(1);
}

(async () => {
Expand All @@ -94,10 +38,16 @@ export interface ${camelName}Program extends Program {
const watcher = watch(path);
async function generate() {
const rawdata = await readFile(path);
const idlDef = JSON.parse(rawdata.toString());
console.log(`Change in ${path}`)
const name = filename.replace(".json", "").replace(/\_/g, "-");
writeFile(`./packages/${name}/src/generated/${name}.ts`, toTs(name, idlDef));
let idlJson = JSON.parse(rawdata.toString());
for (let account of idlJson.accounts) {
account.name = camelcase(account.name);
}

const fileContents = `export type ${capitalizeFirstLetter(camelcase(name))}IDL = ${JSON.stringify(idlJson)};
${allEnumsToTs(idlJson)}`;
writeFile(`./packages/${name}/src/generated/${name}.ts`, fileContents);
}
await generate()
for await (const event of watcher) {
Expand Down
2 changes: 1 addition & 1 deletion migrations/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// single deploy script that's invoked from the CLI, injecting a provider
// configured from the workspace's Anchor.toml.

const anchor = require("@project-serum/anchor");
const anchor = require("@wum.bo/anchor");

module.exports = async function (provider) {
// Configure client to use the provider.
Expand Down
22 changes: 11 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
"type": "module",
"author": "Wum.bo Inc.",
"devDependencies": {
"@project-serum/anchor": "^0.13.2",
"@wum.bo/anchor": "^0.11.1",
"@types/mocha": "^9.0.0",
"@wum.bo/spl-token-staking": "^1.0.0",
"camelcase": "^6.2.0",
"chai": "^4.3.4",
"concurrently": "^6.2.1",
"mocha": "^9.0.3",
Expand Down
63 changes: 33 additions & 30 deletions packages/spl-token-staking/package.json
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
{
"name": "@wum.bo/spl-token-staking",
"version": "1.0.0",
"description": "Interface to the spl-token-staking smart contract",
"repository": {
"type": "git",
"url": "https://github.com/ChewingGlassFund/wumbo"
},
"main": "dist/lib/index.js",
"types": "dist/lib/index.d.ts",
"exports": {
".": "./dist/lib/index.js"
},
"files": [
"dist/lib/**/*"
],
"scripts": {
"build": "tsc --project tsconfig.json",
"watch": "tsc --watch --project tsconfig.json",
"style": "prettier --write \"src/**/*.{ts,tsx}\""
},
"dependencies": {
},
"devDependencies": {
"prettier": "2.3.1",
"style-loader": "^2.0.0",
"ts-loader": "^9.2.3",
"typescript": "^4.3.4"
},
"peerDependencies": {}
}
"name": "@wum.bo/spl-token-staking",
"version": "1.0.0",
"description": "Interface to the spl-token-staking smart contract",
"repository": {
"type": "git",
"url": "https://github.com/ChewingGlassFund/wumbo"
},
"main": "dist/lib/index.js",
"types": "dist/lib/index.d.ts",
"exports": {
".": "./dist/lib/index.js"
},
"files": [
"dist/lib/**/*"
],
"scripts": {
"build": "tsc --project tsconfig.json",
"watch": "tsc --watch --project tsconfig.json",
"style": "prettier --write \"src/**/*.{ts,tsx}\""
},
"dependencies": {
"@project-serum/common": "^0.0.1-beta.3",
"@solana/web3.js": "^1.24.1",
"bn.js": "^5.2.0"
},
"devDependencies": {
"prettier": "2.3.1",
"style-loader": "^2.0.0",
"ts-loader": "^9.2.3",
"typescript": "^4.3.4"
},
"peerDependencies": {}
}
Loading

0 comments on commit 60ae9de

Please sign in to comment.