Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ts: Make workspace metadata optional #310

Merged
merged 2 commits into from
May 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ incremented for features.

## [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

## Features
Expand Down
2 changes: 1 addition & 1 deletion ts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@project-serum/anchor",
"version": "0.6.0",
"version": "0.6.1-beta.1",
"description": "Anchor client",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
Expand Down
17 changes: 12 additions & 5 deletions ts/src/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import camelCase from "camelcase";
import * as toml from "toml";
import { PublicKey } from "@solana/web3.js";
import { Program } from "./program";
import { getProvider } from "./";
import { Idl } from "./idl";

let _populatedWorkspace = false;

Expand Down Expand Up @@ -40,11 +40,14 @@ const workspace = new Proxy({} as any, {
throw new Error("Could not find workspace root.");
}

const idlMap = new Map<string, Idl>();

find
.fileSync(/target\/idl\/.*\.json/, projectRoot)
.reduce((programs: any, path: string) => {
const idlStr = fs.readFileSync(path);
const idl = JSON.parse(idlStr);
idlMap.set(idl.name, idl);
const name = camelCase(idl.name, { pascalCase: true });
if (idl.metadata && idl.metadata.address) {
programs[name] = new Program(
Expand All @@ -61,7 +64,11 @@ const workspace = new Proxy({} as any, {
);
const clusterId = anchorToml.provider.cluster;
if (anchorToml.clusters && anchorToml.clusters[clusterId]) {
attachWorkspaceOverride(workspaceCache, anchorToml.clusters[clusterId]);
attachWorkspaceOverride(
workspaceCache,
anchorToml.clusters[clusterId],
idlMap
);
}

_populatedWorkspace = true;
Expand All @@ -73,14 +80,14 @@ const workspace = new Proxy({} as any, {

function attachWorkspaceOverride(
workspaceCache: { [key: string]: Program },
overrideConfig: { [key: string]: string }
overrideConfig: { [key: string]: string },
idlMap: Map<string, Idl>
) {
Object.keys(overrideConfig).forEach((programName) => {
const wsProgramName = camelCase(programName, { pascalCase: true });
const oldProgram = workspaceCache[wsProgramName];
const overrideAddress = new PublicKey(overrideConfig[programName]);
workspaceCache[wsProgramName] = new Program(
oldProgram.idl,
idlMap.get(programName),
overrideAddress
);
});
Expand Down