Skip to content

Commit 6c51f74

Browse files
authored
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 3a83f72 commit 6c51f74

Some content is hidden

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

42 files changed

+130
-11639
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/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+

solana/ts/sdk/anchor-idl/1_0_0/example_native_token_transfers.ts solana/ts/idl/1_0_0/ts/example_native_token_transfers.ts

+3-225
Original file line numberDiff line numberDiff line change
@@ -1099,38 +1099,6 @@ export type ExampleNativeTokenTransfers = {
10991099
]
11001100
}
11011101
},
1102-
{
1103-
"name": "validatedTransceiverMessage",
1104-
"generics": [
1105-
"A"
1106-
],
1107-
"type": {
1108-
"kind": "struct",
1109-
"fields": [
1110-
{
1111-
"name": "fromChain",
1112-
"type": {
1113-
"defined": "ChainId"
1114-
}
1115-
},
1116-
{
1117-
"name": "message",
1118-
"type": {
1119-
"definedWithTypeArgs": {
1120-
"name": "TransceiverMessageData",
1121-
"args": [
1122-
{
1123-
"type": {
1124-
"generic": "A"
1125-
}
1126-
}
1127-
]
1128-
}
1129-
}
1130-
}
1131-
]
1132-
}
1133-
},
11341102
{
11351103
"name": "nttManagerPeer",
11361104
"docs": [
@@ -1641,85 +1609,6 @@ export type ExampleNativeTokenTransfers = {
16411609
]
16421610
}
16431611
},
1644-
{
1645-
"name": "NttManagerMessage",
1646-
"generics": [
1647-
"A"
1648-
],
1649-
"type": {
1650-
"kind": "struct",
1651-
"fields": [
1652-
{
1653-
"name": "id",
1654-
"type": {
1655-
"array": [
1656-
"u8",
1657-
32
1658-
]
1659-
}
1660-
},
1661-
{
1662-
"name": "sender",
1663-
"type": {
1664-
"array": [
1665-
"u8",
1666-
32
1667-
]
1668-
}
1669-
},
1670-
{
1671-
"name": "payload",
1672-
"type": {
1673-
"generic": "A"
1674-
}
1675-
}
1676-
]
1677-
}
1678-
},
1679-
{
1680-
"name": "TransceiverMessageData",
1681-
"generics": [
1682-
"A"
1683-
],
1684-
"type": {
1685-
"kind": "struct",
1686-
"fields": [
1687-
{
1688-
"name": "sourceNttManager",
1689-
"type": {
1690-
"array": [
1691-
"u8",
1692-
32
1693-
]
1694-
}
1695-
},
1696-
{
1697-
"name": "recipientNttManager",
1698-
"type": {
1699-
"array": [
1700-
"u8",
1701-
32
1702-
]
1703-
}
1704-
},
1705-
{
1706-
"name": "nttManagerPayload",
1707-
"type": {
1708-
"definedWithTypeArgs": {
1709-
"name": "NttManagerMessage",
1710-
"args": [
1711-
{
1712-
"type": {
1713-
"generic": "A"
1714-
}
1715-
}
1716-
]
1717-
}
1718-
}
1719-
}
1720-
]
1721-
}
1722-
},
17231612
{
17241613
"name": "TrimmedAmount",
17251614
"type": {
@@ -1878,8 +1767,7 @@ export type ExampleNativeTokenTransfers = {
18781767
"msg": "BitmapIndexOutOfBounds"
18791768
}
18801769
]
1881-
};
1882-
1770+
}
18831771
export const IDL: ExampleNativeTokenTransfers = {
18841772
"version": "1.0.0",
18851773
"name": "example_native_token_transfers",
@@ -2981,38 +2869,6 @@ export const IDL: ExampleNativeTokenTransfers = {
29812869
]
29822870
}
29832871
},
2984-
{
2985-
"name": "validatedTransceiverMessage",
2986-
"generics": [
2987-
"A"
2988-
],
2989-
"type": {
2990-
"kind": "struct",
2991-
"fields": [
2992-
{
2993-
"name": "fromChain",
2994-
"type": {
2995-
"defined": "ChainId"
2996-
}
2997-
},
2998-
{
2999-
"name": "message",
3000-
"type": {
3001-
"definedWithTypeArgs": {
3002-
"name": "TransceiverMessageData",
3003-
"args": [
3004-
{
3005-
"type": {
3006-
"generic": "A"
3007-
}
3008-
}
3009-
]
3010-
}
3011-
}
3012-
}
3013-
]
3014-
}
3015-
},
30162872
{
30172873
"name": "nttManagerPeer",
30182874
"docs": [
@@ -3523,85 +3379,6 @@ export const IDL: ExampleNativeTokenTransfers = {
35233379
]
35243380
}
35253381
},
3526-
{
3527-
"name": "NttManagerMessage",
3528-
"generics": [
3529-
"A"
3530-
],
3531-
"type": {
3532-
"kind": "struct",
3533-
"fields": [
3534-
{
3535-
"name": "id",
3536-
"type": {
3537-
"array": [
3538-
"u8",
3539-
32
3540-
]
3541-
}
3542-
},
3543-
{
3544-
"name": "sender",
3545-
"type": {
3546-
"array": [
3547-
"u8",
3548-
32
3549-
]
3550-
}
3551-
},
3552-
{
3553-
"name": "payload",
3554-
"type": {
3555-
"generic": "A"
3556-
}
3557-
}
3558-
]
3559-
}
3560-
},
3561-
{
3562-
"name": "TransceiverMessageData",
3563-
"generics": [
3564-
"A"
3565-
],
3566-
"type": {
3567-
"kind": "struct",
3568-
"fields": [
3569-
{
3570-
"name": "sourceNttManager",
3571-
"type": {
3572-
"array": [
3573-
"u8",
3574-
32
3575-
]
3576-
}
3577-
},
3578-
{
3579-
"name": "recipientNttManager",
3580-
"type": {
3581-
"array": [
3582-
"u8",
3583-
32
3584-
]
3585-
}
3586-
},
3587-
{
3588-
"name": "nttManagerPayload",
3589-
"type": {
3590-
"definedWithTypeArgs": {
3591-
"name": "NttManagerMessage",
3592-
"args": [
3593-
{
3594-
"type": {
3595-
"generic": "A"
3596-
}
3597-
}
3598-
]
3599-
}
3600-
}
3601-
}
3602-
]
3603-
}
3604-
},
36053382
{
36063383
"name": "TrimmedAmount",
36073384
"type": {
@@ -3760,4 +3537,5 @@ export const IDL: ExampleNativeTokenTransfers = {
37603537
"msg": "BitmapIndexOutOfBounds"
37613538
}
37623539
]
3763-
};
3540+
}
3541+

0 commit comments

Comments
 (0)