Skip to content

Commit 36e1da8

Browse files
committed
Move generated IDL to ts directory (#437)
* Move generated IDL to ts directory * update github action with new idl path * Update to use the ts exported IDL instead of the json * rebase and regenerate bindings * remove generics from generated idl * add generic stripper to makefile, use ts IDL for binding loader
1 parent 56e4e70 commit 36e1da8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+3575
-15171
lines changed

.github/workflows/solana.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ jobs:
144144
shell: bash
145145
- name: Check idl
146146
run: |
147-
git diff --exit-code idl
147+
git diff --exit-code ts/idl
148148
- name: Run tests
149149
run: anchor test
150150
shell: bash

solana/Makefile

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
.PHONY: build
2-
build: _anchor-build target/idl/example_native_token_transfers.json idl
2+
build: _anchor-build target/idl/example_native_token_transfers.json
33

44
# remove the generics from the idl file. This is necessary as of anchor 0.29.0, because
55
# the javascript library does not support generics yet, and just panics
@@ -12,15 +12,20 @@ target/idl/example_native_token_transfers.json: _anchor-build
1212
_anchor-build:
1313
@anchor build --arch sbf
1414

15-
anchor-test: build target/idl/example_native_token_transfers.json sdk
15+
anchor-test: build idl sdk
1616
anchor test --skip-build
1717

1818
.PHONY: idl
1919
idl: target/idl/example_native_token_transfers.json
20-
@ mkdir -p $@/json
21-
@ mkdir -p $@/ts
22-
@ cp -r target/idl/* $@/json/
23-
@ cp -r target/types/* $@/ts/
20+
$(eval VERSION=$(shell grep "const VERSION" programs/example-native-token-transfers/src/lib.rs | cut -d'"' -f2 | sed 's/\./_/g'))
21+
@echo "IDL Version: $(VERSION)"
22+
@ mkdir -p ts/idl/$(VERSION)/json
23+
@ mkdir -p ts/idl/$(VERSION)/ts
24+
@ cp -r target/idl/* ts/idl/$(VERSION)/json/
25+
@for jsonfile in ts/idl/$(VERSION)/json/*.json; do \
26+
tsfile=$$(echo $$jsonfile | sed 's/json\/\(.*\)\.json/ts\/\1.ts/'); \
27+
tsx scripts/regenerateIdl.ts $$jsonfile > $$tsfile; \
28+
done
2429

2530
sdk: build
2631
@echo "Building SDK"

solana/idl/ts/wormhole_governance.ts

-153
This file was deleted.

solana/package.json

-3
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@
3939
"rebuild": "npm run clean && npm run build",
4040
"clean": "rm -rf ./dist",
4141
"test:ci": "jest --config ./jest.config.ts",
42-
"generate": "export IDL_VERSION=`tsx scripts/readVersion.ts` && mkdir -p ./ts/sdk/anchor-idl/$IDL_VERSION && npm run copy:idl && npm run copy:types",
43-
"copy:idl": "cp ./target/idl/*.json ./ts/sdk/anchor-idl/$IDL_VERSION/",
44-
"copy:types": "cp ./target/types/*.ts ./ts/sdk/anchor-idl/$IDL_VERSION/",
4542
"build:contracts": "make build"
4643
},
4744
"devDependencies": {

solana/scripts/regenerateIdl.ts

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { type Idl } from "@coral-xyz/anchor";
2+
import fs from "fs";
3+
4+
const jsonPath = process.argv[2];
5+
if (jsonPath === undefined) {
6+
console.error(`Usage:\
7+
${process.argv[0]} ${process.argv[1]} <idl.json>`);
8+
process.exit(1);
9+
}
10+
11+
// snake to title case
12+
const titleCase = (str: string) =>
13+
str
14+
.split("_")
15+
.map((word) => word[0].toUpperCase() + word.slice(1).toLowerCase())
16+
.join("");
17+
18+
const idl: Idl = JSON.parse(fs.readFileSync(jsonPath, "utf8"));
19+
20+
const name = titleCase(idl["name"]);
21+
22+
idl.accounts?.forEach((account) => {
23+
account.name = account.name.replace(/^[A-Z]+/, (match) =>
24+
match.toLowerCase()
25+
);
26+
});
27+
28+
// heredoc
29+
const ts = `\
30+
export type ${name} = ${JSON.stringify(idl, null, 2)}\
31+
32+
export const IDL: ${name} = ${JSON.stringify(idl, null, 2)}
33+
`;
34+
35+
console.log(ts);

solana/tests/anchor.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import {
1212
serialize,
1313
serializePayload,
1414
signSendWait as ssw,
15-
testing,
1615
} from "@wormhole-foundation/sdk-connect";
16+
import * as testing from "@wormhole-foundation/sdk-definitions/testing";
1717
import {
1818
SolanaAddress,
1919
SolanaPlatform,
@@ -24,7 +24,7 @@ import * as fs from "fs";
2424

2525
import { PublicKey, SystemProgram, Transaction } from "@solana/web3.js";
2626
import { AccountAddress } from "@wormhole-foundation/sdk";
27-
import { DummyTransferHook } from "../ts/sdk/anchor-idl/1_0_0/dummy_transfer_hook.js";
27+
import { DummyTransferHook } from "../ts/idl/1_0_0/ts/dummy_transfer_hook.js";
2828
import { SolanaNtt } from "../ts/sdk/index.js";
2929

3030
const solanaRootDir = `${__dirname}/../`;
File renamed without changes.

solana/idl/ts/dummy_transfer_hook.ts solana/ts/idl/1_0_0/ts/dummy_transfer_hook.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,7 @@ export type DummyTransferHook = {
107107
}
108108
}
109109
]
110-
};
111-
110+
}
112111
export const IDL: DummyTransferHook = {
113112
"version": "2.0.0",
114113
"name": "dummy_transfer_hook",
@@ -218,4 +217,5 @@ export const IDL: DummyTransferHook = {
218217
}
219218
}
220219
]
221-
};
220+
}
221+

0 commit comments

Comments
 (0)