flow-playground
flow-playground copied to clipboard
Feature: Add support for specifying arguments to smart contract initializers
Is your feature request related to a problem? Please describe. Smart contracts can have arguments to their initializers, but we cannot submit arguments as part of contract deployment in the playground UI.
Describe the solution you'd like When deploying a contract, a box with argument boxes could pop up and a user could type in the arguments for the init function.
Oh nice. This is obviously a common pattern?
I wouldn't say it is super common, but it is something that is useful to be able to do and will be encountered somewhat often. Definitely worth discussing how we want to include it
It would be useful when for example- token contracts have a fixed total supply and they want to initialize it by passing an arument to the contract's init function. Same for any contracts that want to define variable values dynamically at contract initialization.
This could be implemented in a similar fashion to #29. Arguments could be specified before clicking the "Deploy" button, similar to the "Send" button for transactions.
@MaxStalker is blocked until he can get some help from @turbolent and @psiemens to get arguments accepted on server side
@psiemens @turbolent Is the API ready for this?
@bthaile I just discovered a bug related to this. When trying to deploy a contract with an init argument, the contract doesn't report an error and also doesn't deploy the contract. it just returns to the same state from before the deployment without giving any information.
You can reproduce by creating a new project, adding any argument to HelloWorld
's init method, then clicking Deploy.
I assume the answer is Yes, but does emulator support passing in arguments when deploying contracts?
@bthaile yap,yap :) You can check example (and the whole series as well ๐) here: https://dev.to/onflow/build-on-flow-js-testing-2-setup-emulator-and-deploy-contracts-38l6
More I look into it, maybe the Language Server isn't returning the "executionArguments" when process the contract.
@bthaile oh, wait - you mean on Playground? ๐
Yes, looking at playground. Which uses cadence language server to parse code. Looks like cadence tools does support finding contract init arguments. At least found this test
https://github.com/onflow/cadence-tools/blob/2dd0a849291ab0277765798d039982f50e102ec8/languageserver/test/index.test.ts#L175
const args = await languageClient.sendRequest(
ExecuteCommandRequest.type,
{
command: 'cadence.server.getEntryPointParameters',
arguments: [editor.getModel().uri.toString()],
},
);
Adding init parameter
...
init(message: String) {
self.greeting = message
}
...
LS returns args is empty for contracts
Side Note: playground api needs to add arguments to CreateContractDeployment parameters.
I found the issue, there is a separate command for getting contract initialization parameters. cadence.server.getContractInitializerParameters
playground wasn't using it.
Downside there isn't a cadence.server.parseEntryPointArguments
for contract initialization parameters, this seems like lacking functionality.
https://github.com/onflow/cadence-tools/blob/2dd0a849291ab0277765798d039982f50e102ec8/languageserver/server/server.go#L282
Changes have been made to Cadence Language server. Next will be updating the API to take in arguments.