-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
[WIP] Adds types to Anchor #537
Closed
Closed
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
9cdb365
Update accountsArray types
macalinao c2fc4d8
Adds types for InstructionFn
macalinao 6488b11
Adds more types
macalinao 5980b9c
More granular types
macalinao 65285cf
remove event parser export
macalinao e73f638
Account and state data types
macalinao aabe73c
Merge branch 'master' into igm/types
macalinao 8b9464d
Merge remote-tracking branch 'origin/master' into igm/types
macalinao 18d9989
Merge remote-tracking branch 'upstream/master' into igm/types
macalinao dfb9bd2
T
macalinao ea35d62
simulate.ts: remove duplicate event import
crispheaney c935f75
examples/escrow: refector tests to typescript
crispheaney 1f949f1
example/escrow: bash script to generate escrow type for test
crispheaney 26b6a04
examples/escrow: add idl typehint to Program
crispheaney e80c7db
.travis.yml: fix command for escrow test
crispheaney c377fbd
ts/account: use account names from IDL for keys in AccountNamespace
crispheaney 0a47d0d
ts/namespace/index: fix undefined error from undefined idl.accounts
crispheaney File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
{ | ||
"dependencies": { | ||
"@project-serum/anchor": "^0.9.0", | ||
"@project-serum/serum": "0.13.38", | ||
"@solana/web3.js": "^1.18.0", | ||
"@solana/spl-token": "^0.1.6" | ||
}, | ||
"devDependencies": { | ||
"ts-mocha": "^8.0.0" | ||
"@project-serum/anchor": "../../ts", | ||
"@project-serum/serum": "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" | ||
} | ||
} |
24 changes: 13 additions & 11 deletions
24
examples/escrow/tests/escrow.js → examples/escrow/tests/escrow.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"}}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"compilerOptions": { | ||
"types": ["mocha", "chai", "node"], | ||
"typeRoots": ["./node_modules/@types"], | ||
"lib": ["es2015"], | ||
"module": "commonjs", | ||
"target": "es6", | ||
"esModuleInterop": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm the account namespace doesn't type check. I can do
program.account.notAValidType
and it compiles.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this expected @macalinao or did I miss something.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I may have forgotten to add this in
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I fixed this by tweaking the
AccountNamespace
. But for this to work, I also needed to customize the script that produces the idl type such that the Account names are camel case.I think we'd want to think through the dev flow for producing the IDL type definition if we are to ship this. Maybe we add a script to
anchor-cli
that produces the IDL types for you?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Putting this on your radar @armaniferrante
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this ready for another review/testing?