浏览代码

ts: Make workspace metadata optional (#310)

Armani Ferrante 4 年之前
父节点
当前提交
5b807d7804
共有 3 个文件被更改,包括 17 次插入6 次删除
  1. 4 0
      CHANGELOG.md
  2. 1 1
      ts/package.json
  3. 12 5
      ts/src/workspace.ts

+ 4 - 0
CHANGELOG.md

@@ -11,6 +11,10 @@ incremented for features.
 
 
 ## [Unreleased]
 ## [Unreleased]
 
 
+## Features
+
+* ts: Address metadata is now optional for `anchor.workspace` clients ([#310](https://github.com/project-serum/anchor/pull/310)).
+
 ## [0.6.0] - 2021-05-23
 ## [0.6.0] - 2021-05-23
 
 
 ## Features
 ## Features

+ 1 - 1
ts/package.json

@@ -1,6 +1,6 @@
 {
 {
   "name": "@project-serum/anchor",
   "name": "@project-serum/anchor",
-  "version": "0.6.0",
+  "version": "0.6.1-beta.1",
   "description": "Anchor client",
   "description": "Anchor client",
   "main": "dist/cjs/index.js",
   "main": "dist/cjs/index.js",
   "module": "dist/esm/index.js",
   "module": "dist/esm/index.js",

+ 12 - 5
ts/src/workspace.ts

@@ -2,7 +2,7 @@ import camelCase from "camelcase";
 import * as toml from "toml";
 import * as toml from "toml";
 import { PublicKey } from "@solana/web3.js";
 import { PublicKey } from "@solana/web3.js";
 import { Program } from "./program";
 import { Program } from "./program";
-import { getProvider } from "./";
+import { Idl } from "./idl";
 
 
 let _populatedWorkspace = false;
 let _populatedWorkspace = false;
 
 
@@ -40,11 +40,14 @@ const workspace = new Proxy({} as any, {
         throw new Error("Could not find workspace root.");
         throw new Error("Could not find workspace root.");
       }
       }
 
 
+      const idlMap = new Map<string, Idl>();
+
       find
       find
         .fileSync(/target\/idl\/.*\.json/, projectRoot)
         .fileSync(/target\/idl\/.*\.json/, projectRoot)
         .reduce((programs: any, path: string) => {
         .reduce((programs: any, path: string) => {
           const idlStr = fs.readFileSync(path);
           const idlStr = fs.readFileSync(path);
           const idl = JSON.parse(idlStr);
           const idl = JSON.parse(idlStr);
+          idlMap.set(idl.name, idl);
           const name = camelCase(idl.name, { pascalCase: true });
           const name = camelCase(idl.name, { pascalCase: true });
           if (idl.metadata && idl.metadata.address) {
           if (idl.metadata && idl.metadata.address) {
             programs[name] = new Program(
             programs[name] = new Program(
@@ -61,7 +64,11 @@ const workspace = new Proxy({} as any, {
       );
       );
       const clusterId = anchorToml.provider.cluster;
       const clusterId = anchorToml.provider.cluster;
       if (anchorToml.clusters && anchorToml.clusters[clusterId]) {
       if (anchorToml.clusters && anchorToml.clusters[clusterId]) {
-        attachWorkspaceOverride(workspaceCache, anchorToml.clusters[clusterId]);
+        attachWorkspaceOverride(
+          workspaceCache,
+          anchorToml.clusters[clusterId],
+          idlMap
+        );
       }
       }
 
 
       _populatedWorkspace = true;
       _populatedWorkspace = true;
@@ -73,14 +80,14 @@ const workspace = new Proxy({} as any, {
 
 
 function attachWorkspaceOverride(
 function attachWorkspaceOverride(
   workspaceCache: { [key: string]: Program },
   workspaceCache: { [key: string]: Program },
-  overrideConfig: { [key: string]: string }
+  overrideConfig: { [key: string]: string },
+  idlMap: Map<string, Idl>
 ) {
 ) {
   Object.keys(overrideConfig).forEach((programName) => {
   Object.keys(overrideConfig).forEach((programName) => {
     const wsProgramName = camelCase(programName, { pascalCase: true });
     const wsProgramName = camelCase(programName, { pascalCase: true });
-    const oldProgram = workspaceCache[wsProgramName];
     const overrideAddress = new PublicKey(overrideConfig[programName]);
     const overrideAddress = new PublicKey(overrideConfig[programName]);
     workspaceCache[wsProgramName] = new Program(
     workspaceCache[wsProgramName] = new Program(
-      oldProgram.idl,
+      idlMap.get(programName),
       overrideAddress
       overrideAddress
     );
     );
   });
   });