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

Add support for Solidity using the Solang compiler #2421

Merged
merged 1 commit into from
Apr 4, 2023

Conversation

seanyoung
Copy link
Contributor

anchor init has a new option -s (or --solidity) which creates an example using Solidity in the soldity directory of the anchor workspace.

anchor deploy/build/test work accordingly.

solang is required to be in the path, this can be downloaded from the release at https://github.com/hyperledger/solang

anchor build/deploy finds all the solidity contracts by using the solidity parser to find all contract declarations.

Next steps:

  • Seek feedback
  • Add solang to solana SDK
  • Improve examples

@vercel
Copy link

vercel bot commented Mar 2, 2023

@seanyoung is attempting to deploy a commit to the coral-xyz Team on Vercel.

A member of the Team first needs to authorize it.

@Henry-E
Copy link

Henry-E commented Mar 3, 2023

Not to be rude but I am struggling to understand how this fits into anchor?

Can you help me to understand what the connection is between creating a solidity file as an option in anchor init and the overall anchor framework. Anchor mainly exists to help with the serialization of account data and instruction data, as well as adding lots of macro on the rust side to make it safer to write smart contracts by using account constraints and other checks.

anchor deploy/build/test work accordingly. maybe if you could expand on this part it might easier to get what the intended connection / interaction is.

@seanyoung
Copy link
Contributor Author

Can you help me to understand what the connection is between creating a solidity file as an option in anchor init and the overall anchor framework. Anchor mainly exists to help with the serialization of account data and instruction data, as well as adding lots of macro on the rust side to make it safer to write smart contracts by using account constraints and other checks.

You're right, this could do with some more explanation.

For some time now we have been working on a new solidity compiler which compiles Solidity directly to BPF and also produces an IDL metadata file. This is huge amount of work, and this is being worked on by Solana Labs.

anchor does provide a bunch of rust macros, that is true, but it also provides much more. There is an anchor npm library for client side code. The anchor cli tool allows you to create an anchor workspace, build the smart contracts, test, deploy, and work with the idl files. As you point out, anchor deals with account constraints which solana sealevel imposes. This problem also exists for Solidity as it does for any on-chain solana program.

So the aim is make Solidity also a supported language so that solidity programs can live side by side with rust programs. That way, all the tooling provided by anchor for managing builds, tests, publishing idl files, writing client code/tests, deal with account constraints can be reused. I would imagine that once the move compiler starts to mature, it would be great to also add support for move to anchor.

I hope this helps, happy to provide further explanation of course.

FWIW here is what you can do with this PR.

$ anchor init --solidity foo
yarn install v1.22.19
warning package.json: No license field
info No lockfile found.
warning No license field
[1/4] Resolving packages...
[2/4] Fetching packages...
[3/4] Linking dependencies...
[4/4] Building fresh packages...
success Saved lockfile.
Done in 29.16s.
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint: 
hint: 	git config --global init.defaultBranch <name>
hint: 
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint: 
hint: 	git branch -m <name>
Initialized empty Git repository in /home/sean/anchor/foo/.git/
foo initialized
$ cd foo
$ anchor build
info: Solang version v0.2.2-14-g28da9113
help: found contract 'foo'
   ┌─ /home/sean/anchor/foo/solidity/foo.sol:3:1
   │  
 3 │ ╭ contract foo {
 4 │ │     bool private value = true;
 5 │ │ 
 6 │ │     @payer(payer)
   · │
21 │ │     }
22 │ │ }
   │ ╰─^

info: contract foo uses at least 17 bytes account data
info: Generating LLVM IR for contract foo with target solana
info: Saving binary /home/sean/anchor/foo/target/deploy/foo.so for contract foo
info: Generating Anchor metadata for contract foo
info: Saving metadata /home/sean/anchor/foo/target/idl/foo.json for contract foo
$ anchor test
info: Solang version v0.2.2-14-g28da9113
help: found contract 'foo'
   ┌─ /home/sean/anchor/foo/solidity/foo.sol:3:1
   │  
 3 │ ╭ contract foo {
 4 │ │     bool private value = true;
 5 │ │ 
 6 │ │     @payer(payer)
   · │
21 │ │     }
22 │ │ }
   │ ╰─^

info: contract foo uses at least 17 bytes account data
info: Generating LLVM IR for contract foo with target solana
info: Saving binary /home/sean/anchor/foo/target/deploy/foo.so for contract foo
info: Generating Anchor metadata for contract foo
info: Saving metadata /home/sean/anchor/foo/target/idl/foo.json for contract foo

Found a 'test' script in the Anchor.toml. Running it as a test suite!

Running test suite: "/home/sean/anchor/foo/Anchor.toml"

yarn run v1.22.19
warning package.json: No license field
$ /home/sean/anchor/foo/node_modules/.bin/ts-mocha -p ./tsconfig.json -t 1000000 'tests/**/*.ts'


  foo
Your transaction signature 2DoHfoXDvLzqQUyccN5pBAsogPXKwsE3TyZqxoMKSTZMsguW7WrJh5JHSas8t6z5mxSCY8Aor4U1wGSFLWKm3zXi
state true
state false
    ✔ Is initialized! (687ms)


  1 passing (692ms)

Done in 2.29s.
[sean@xywoleh foo]$ ~/git/anchor/target/debug/anchor deploy
Deploying workspace: http://localhost:8899
Upgrade authority: /home/sean/.config/solana/id.json
Deploying program "foo.sol"...
Program path: /home/sean/anchor/foo/target/deploy/foo.so...
Program Id: 6PuQ1EmxFemjWSNUNKMMgqa1w64VjRtAWD2ZQ8pLrJKM

Deploy success

@Henry-E
Copy link

Henry-E commented Mar 3, 2023

Thanks for the detailed reply, I definitely understand a bit more what your goal is here!

The main issue I see is that the anchor framework itself isn't built into solang. So while it looks like an IDL gets added to the target directory, there's no guarantee that the IDL format is still up-to-date with the current anchor IDL format, particularly if there have been breaking changes or bug fixes. Also presumably none of the anchor idl functions would work either?

Wouldn't it be possible to make your own solang CLI? Sure, the anchor CLI is an important part of how anchor functions in practice but I don't know if it's important enough to start making it into its own thing. Namely, you seem to be framing the anchor CLI as one of the main focuses of anchor, rather than a supporting tool (it's really just init, test and maybe verify). The framing is that the anchor CLI should become a "one stop shop" for all supported smart contract languages or smart contract frameworks (like maybe adding native anchor build/test for shank programs) in solana. It's definitely a possible direction but it's certainly not one that's been considered up until this point. Though I feel like if that was the intended direction it would make more sense to split out the anchor CLI into its own repo and make it a solana-wide thing and less coupled with anchor.

My basic reaction is that given the anchor framework as it is today, it just doesn't seem that fitting to say to users: "hey, instead of writing a smart contract using anchor would you like to use something completely different instead".

As for using the anchor npm library, it should be easy to write client side code assuming you have a compatible IDL and you should totally encourage people to do that if it makes their life easier. That part doesn't require any integration or changes to anchor.

Anyway, let me know what you think. Does this reply make sense to you? Hopefully this isn't a totally unreasonable position.

@Henry-E Henry-E added the cli label Mar 3, 2023
@armaniferrante
Copy link
Member

Discussed in Discord. Going to go forward with this PR!

@seanyoung
Copy link
Contributor Author

Wonderful.

Tomorrow I'll go over the PR again, and see if there is anything that needs improving.

@armaniferrante
Copy link
Member

@seanyoung bump on this. Anything you'd like to update before a review pass?

@seanyoung
Copy link
Contributor Author

@armaniferrante so I've been working on getting verified builds working with Solidity. This is not finished yet.

What's here is ready to for review if you're happy to wait for verified builds to work. Thank you!

@seanyoung
Copy link
Contributor Author

There was an issue where solana-program 1.15.2 not building, so I've updated the dependencies. The tests should be better now.

(Unrelated by my changes)

@seanyoung seanyoung force-pushed the solidity branch 2 times, most recently from 0ae1b52 to 00b57ba Compare March 23, 2023 12:40
@armaniferrante
Copy link
Member

Reran the failing tests.

@seanyoung seanyoung force-pushed the solidity branch 2 times, most recently from 02950d9 to 17bb2f1 Compare March 24, 2023 19:55
@seanyoung
Copy link
Contributor Author

The only failing test is the vercel, which says Authorization required to deploy.. Is this something you can do @armaniferrante ? Thanks

Copy link
Collaborator

@acheroncrypto acheroncrypto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have any strong opinions but I have to agree with @Henry-E here that this feels out of place.

solang is required to be in the path, this can be downloaded from the release at https://github.com/hyperledger/solang

It seems like we are including a template to anchor cli that users would have to download separately to run anyway.

And as @Henry-E pointed out, any changes to the idl like in #2011 can/will cause solang to produce outdated/incompatible idl.

If these issues are not a big concern, I think this is OK to merge once the review comments are addressed and CHANGELOG is updated.

.github/workflows/tests.yaml Outdated Show resolved Hide resolved
cli/src/lib.rs Outdated Show resolved Hide resolved
cli/src/lib.rs Outdated Show resolved Hide resolved
cli/src/lib.rs Outdated Show resolved Hide resolved
anchor init and anchor new have a new option `-s` (or `--solidity`) which
creates an example using Solidity in the soldity directory of the
anchor workspace.

anchor deploy/build/test work accordingly.

solang is required to be in the path, this can be downloaded from the
release at https://github.com/hyperledger/solang

anchor build/deploy finds all the solidity contracts by using the
solidity parser to find all contract declarations.
@seanyoung
Copy link
Contributor Author

seanyoung commented Apr 4, 2023

solang is required to be in the path, this can be downloaded from the release at https://github.com/hyperledger/solang

It seems like we are including a template to anchor cli that users would have to download separately to run anyway.

The plan is to include solang in the Solana SDK.

If these issues are not a big concern, I think this is OK to merge once the review comments are addressed and CHANGELOG is updated.

I've added an entry to the changelog and addressed your sensible review comments. Please let me know what you think.

Copy link
Collaborator

@acheroncrypto acheroncrypto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thank you for updating!

@acheroncrypto acheroncrypto merged commit 9e7c33e into coral-xyz:master Apr 4, 2023
@seanyoung seanyoung deleted the solidity branch April 4, 2023 17:41
@seanyoung
Copy link
Contributor Author

@acheroncrypto thank you very much!

@sharadjaiswal1411
Copy link

anchor verify -p
Getting following error
Using image "backpackapp/build:v0.29.0"
Run docker image
ee1aa8a9d59f35f724baed5326b51248681cf52965108e1daabbeebfbcb97839
Using solana version: 1.18.19
curl: (22) The requested URL returned error: 403
Cleaning up the docker target directory
Removing the docker container
anchor-program
Error during Docker build: Failed to run command: ["curl", "-sSfL", "https://release.solana.com/v1.18.19/install", "-o", "solana_installer.sh"]
Error: Failed to run command: ["curl", "-sSfL", "https://release.solana.com/v1.18.19/install", "-o", "solana_installer.sh"]

@seanyoung
Copy link
Contributor Author

Solang is no longer maintained, unfortunately. Funding was cut and I was moved to other things: anza-xyz/agave#2010

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants