Browse Source

ts/account: use account names from IDL for keys in AccountNamespace

Chris Heaney 4 years ago
parent
commit
c377fbd9c3

+ 1 - 1
.travis.yml

@@ -75,7 +75,7 @@ jobs:
       script:
         - pushd ts && yarn && yarn build && npm link && popd
         - pushd examples/escrow && npm link @project-serum/anchor && yarn && popd
-        - pushd examples/escrow && anchor build && sh ./create-idl-type.sh && anchor test --skip-build && popd
+        - pushd examples/escrow && anchor build && npx ts-node createIDLType.ts && anchor test --skip-build && popd
         - pushd examples/pyth && yarn && anchor test && popd
         - pushd examples/tutorial/basic-0 && anchor test && popd
         - pushd examples/tutorial/basic-1 && anchor test && popd

+ 0 - 11
examples/escrow/create-idl-type.sh

@@ -1,11 +0,0 @@
-#!/usr/bin/env bash
-
-TYPES_DIR="./tests/types"
-rm -rf $TYPES_DIR
-mkdir -p $TYPES_DIR
-OUT_PATH="$TYPES_DIR/escrow.ts"
-
-typename="EscrowIDL"
-echo "export type $typename =" >>$OUT_PATH
-cat target/idl/escrow.json >>$OUT_PATH
-echo ";" >>$OUT_PATH

+ 18 - 0
examples/escrow/createIDLType.ts

@@ -0,0 +1,18 @@
+const fs = require('fs')
+import camelcase from "camelcase";
+
+fs.rmdirSync("tests/types", { recursive: true });
+fs.mkdir("tests/types", { recursive: true }, (err) => {
+    if (err) {
+        throw err;
+    }
+});
+
+let escrowIDLJSON = JSON.parse(fs.readFileSync('./target/idl/escrow.json'));
+for (let account of escrowIDLJSON.accounts) {
+    account.name = camelcase(account.name);
+}
+
+const fileContents = `export type EscrowIDL = ${JSON.stringify(escrowIDLJSON)};`;
+fs.writeFileSync("tests/types/escrow.ts", fileContents);
+

+ 4 - 3
examples/escrow/package.json

@@ -2,11 +2,12 @@
   "dependencies": {
     "@project-serum/anchor": "../../ts",
     "@project-serum/serum": "latest",
-    "@solana/web3.js": "latest",
     "@solana/spl-token": "latest",
+    "@solana/web3.js": "latest",
     "@types/mocha": "^8.2.3",
+    "bn.js": "^5.2.0",
+    "camelcase": "^6.2.0",
     "@types/node": "^14.14.37",
-    "chai": "^4.3.4",
-    "bn.js": "^5.2.0"
+    "chai": "^4.3.4"
   }
 }

+ 1 - 0
examples/escrow/tests/types/escrow.ts

@@ -0,0 +1 @@
+export type EscrowIDL = {"version":"0.0.0","name":"escrow","instructions":[{"name":"initializeEscrow","accounts":[{"name":"initializer","isMut":false,"isSigner":true},{"name":"initializerDepositTokenAccount","isMut":true,"isSigner":false},{"name":"initializerReceiveTokenAccount","isMut":false,"isSigner":false},{"name":"escrowAccount","isMut":true,"isSigner":false},{"name":"tokenProgram","isMut":false,"isSigner":false},{"name":"rent","isMut":false,"isSigner":false}],"args":[{"name":"initializerAmount","type":"u64"},{"name":"takerAmount","type":"u64"}]},{"name":"cancelEscrow","accounts":[{"name":"initializer","isMut":false,"isSigner":false},{"name":"pdaDepositTokenAccount","isMut":true,"isSigner":false},{"name":"pdaAccount","isMut":false,"isSigner":false},{"name":"escrowAccount","isMut":true,"isSigner":false},{"name":"tokenProgram","isMut":false,"isSigner":false}],"args":[]},{"name":"exchange","accounts":[{"name":"taker","isMut":false,"isSigner":true},{"name":"takerDepositTokenAccount","isMut":true,"isSigner":false},{"name":"takerReceiveTokenAccount","isMut":true,"isSigner":false},{"name":"pdaDepositTokenAccount","isMut":true,"isSigner":false},{"name":"initializerReceiveTokenAccount","isMut":true,"isSigner":false},{"name":"initializerMainAccount","isMut":true,"isSigner":false},{"name":"escrowAccount","isMut":true,"isSigner":false},{"name":"pdaAccount","isMut":false,"isSigner":false},{"name":"tokenProgram","isMut":false,"isSigner":false}],"args":[]}],"accounts":[{"name":"escrowAccount","type":{"kind":"struct","fields":[{"name":"initializerKey","type":"publicKey"},{"name":"initializerDepositTokenAccount","type":"publicKey"},{"name":"initializerReceiveTokenAccount","type":"publicKey"},{"name":"initializerAmount","type":"u64"},{"name":"takerAmount","type":"u64"}]}}],"metadata":{"address":"orwyukmpT9ZKxq78aQCkM75xJDgV1VXgAktFgX6PZob"}};

+ 1 - 1
examples/escrow/tsconfig.json

@@ -1,6 +1,6 @@
 {
   "compilerOptions": {
-    "types": ["mocha", "chai"],
+    "types": ["mocha", "chai", "node"],
     "typeRoots": ["./node_modules/@types"],
     "lib": ["es2015"],
     "module": "commonjs",

+ 3 - 3
ts/src/program/namespace/account.ts

@@ -17,7 +17,7 @@ import Coder, {
 } from "../../coder";
 import { Subscription, Address, translateAddress } from "../common";
 import { getProvider } from "../../";
-import { IdlTypes, TypeDef } from "./types";
+import { AllAccountsMap, IdlTypes, TypeDef } from "./types";
 import * as pubkeyUtil from "../../utils/pubkey";
 
 export default class AccountFactory {
@@ -64,8 +64,8 @@ export default class AccountFactory {
  *
  * For the full API, see the [[AccountClient]] reference.
  */
-export interface AccountNamespace<IDL extends Idl = Idl> {
-  [key: string]: AccountClient<IDL>;
+export type AccountNamespace<IDL extends Idl = Idl> = {
+  [M in keyof AllAccountsMap<IDL>]: AccountClient<IDL>
 }
 
 export class AccountClient<

+ 1 - 3
ts/src/program/namespace/index.ts

@@ -72,9 +72,7 @@ export default class NamespaceFactory {
       simulate[name] = simulateItem;
     });
 
-    const account = idl.accounts
-      ? AccountFactory.build(idl, coder, programId, provider)
-      : {};
+    const account = AccountFactory.build(idl, coder, programId, provider);
 
     return [
       rpc as RpcNamespace<IDL>,

+ 19 - 0
ts/src/program/namespace/types.ts

@@ -24,6 +24,25 @@ export type AllInstructionsMap<IDL extends Idl> = InstructionMap<
   AllInstructions<IDL>
 >;
 
+/**
+ * All accounts for an IDL.
+ */
+export type AllAccounts<IDL extends Idl> = IDL["accounts"][number];
+
+/**
+ * Returns a type of instruction name to the IdlInstruction.
+ */
+export type AccountMap<I extends IdlTypeDef> = {
+  [K in I["name"]]: I & { name: K };
+};
+
+/**
+ * Returns a type of instruction name to the IdlInstruction.
+ */
+export type AllAccountsMap<IDL extends Idl> = AccountMap<
+    AllAccounts<IDL>
+    >;
+
 export type MakeInstructionsNamespace<
   IDL extends Idl,
   I extends IdlInstruction,