-
Notifications
You must be signed in to change notification settings - Fork 215
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
Add function annotations 🚀 #1557
Conversation
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.
How the annotations work with constructors and virtual functions?
.context | ||
.struct_type( | ||
&[ | ||
binary | ||
.module | ||
.get_struct_type("struct.SolPubkey") | ||
.unwrap() | ||
.ptr_type(AddressSpace::default()) | ||
.as_basic_type_enum(), | ||
binary | ||
.llvm_type(&Type::Struct(StructType::AccountMeta), ns) | ||
.ptr_type(AddressSpace::default()) | ||
.as_basic_type_enum(), | ||
binary.context.i64_type().as_basic_type_enum(), | ||
binary | ||
.context | ||
.i8_type() | ||
.ptr_type(AddressSpace::default()) | ||
.as_basic_type_enum(), | ||
binary.context.i64_type().as_basic_type_enum(), | ||
], | ||
false, | ||
) | ||
.as_basic_type_enum(); |
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.
declare_externals?
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 don't think there is a way to add struct definitions to the module.
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.
There must be a way to types to a module, how else would you build a complete IR for a file.
Having said that, I don't know how it is done. I can't find it either
As constructors are just like any other function on Solana, the annotations work as in an external function. A test case for constructors is missing, though. I missed the case about virtual functions. We should either disallow annotations or require that the overriding function repeat the account declaration (I'd extend these rules to interfaces). Do you have any opinion on the subject? |
63d035c
to
fd6b43c
Compare
@seanyoung I am requiring now that overriding functions declared the same accounts as their respective virtual ones. I may not have conceived the most ideal error message, though. Can you please check if you like it? |
|
||
res = await callee.program.methods.getX() | ||
.accounts({ dataAccount: callee.storage.publicKey }) | ||
.view(); | ||
.view({commitment: "confirmed"}); |
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.
Rather than providing this for each call, I think this can be set once for the connection as a second argument to AnchorProvider.local()
in setup.ts
.
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.
Adding {commitment: "confirmed"}
to AnchorProvider.local
causes all tests to fail.
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.
LGTM
Signed-off-by: Lucas Steuernagel <lucas.tnagel@gmail.com>
Signed-off-by: Lucas Steuernagel <lucas.tnagel@gmail.com>
Signed-off-by: Lucas Steuernagel <lucas.tnagel@gmail.com>
Codecov Report
@@ Coverage Diff @@
## main #1557 +/- ##
=======================================
Coverage ? 87.33%
=======================================
Files ? 133
Lines ? 64143
Branches ? 0
=======================================
Hits ? 56021
Misses ? 8122
Partials ? 0 |
This PR introduces account annotations for developers to declare accounts in a Solidity function. This is one of the task items in #1251.
The new syntax tremendously facilitates writing contracts and is less error prone as we do not need to both pass the account address as an argument and in the accounts field.
I wrote some tests where I noticed they were necessary and I attempted to refactor all existing Solana Solidity to use the annotations, so that my implementation would be stress tested in all those cases.
There are a few items this PR did not accomplish, but are going to be implemented in future changes:
address.balance
function is useless on Solana, but has not been eliminated yet. We can now just dotx.accounts.myAccount.lamports
.address.send
andaddress.transfer
are also not necessary, but I did not purge the compiler of them.