Creating an instance of PiggyBank


I’m having problems creating an instance of the PiggyBank smart contract (deployed as sc_piggy_bank) on the testnet.

What am I doing wrong?

Below is the output to the console from the failing attempt.

concordium-client_1.1.1-0 contract init 68ca306a53ea6b206a169788b1bb0e98fcc486ad36d949b6dad287df1f2dc79f --sender SysConDev-01 --contract sc_piggy_bank --energy 1000 --grpc-ip --grpc-port 10001
Initialize contract 'sc_piggy_bank' from module '68ca306a53ea6b206a169788b1bb0e98fcc486ad36d949b6dad287df1f2dc79f' with no parameters. Sending 0.000000 GTU.
Allowing up to 1000 NRG to be spent as transaction fee.
Transaction expires on Fri, 15 Oct 2021 11:04:43 UTC.
Confirm [yN]: y
Enter password for credential with index 0 and signing key with index 0:
Transaction 'd7f1031e680e0ef712bbbea24950a3b7a1d0ed7ad9774aee85e7217a2857ea0f' sent to the baker.
Waiting for the transaction to be committed and finalized.
You may skip this step by interrupting the command using Ctrl-C (pass flag '--no-wait' to do this by default).
The transaction will still get processed and may be queried using
  'concordium-client transaction status d7f1031e680e0ef712bbbea24950a3b7a1d0ed7ad9774aee85e7217a2857ea0f'.
[12:54:48] Waiting for the transaction to be committed....
Transaction is committed into block 0c688ffe2677cdcc76f68d00463fc56c7914a3cb5e2faa646fdd6403d69fa9fa with status "rejected" and cost 0.041223 GTU (742 NRG).
Transaction rejected: invalid init method.
[12:54:50] Waiting for the transaction to be finalized....
Transaction is finalized into block 0c688ffe2677cdcc76f68d00463fc56c7914a3cb5e2faa646fdd6403d69fa9fa with status "rejected" and cost 0.041223 GTU (742 NRG).
Transaction rejected: invalid init method.

You need --contract PiggyBank instead of --contract sc_piggy_bank, because that’s the name of the contract defined in the module. (You can give the contract instance a name with the --name parameter, which can be a bit confusing.)

You can see the name of the smart contract that is defined in the module with:

concordium-client module inspect 68ca306a53ea6b206a169788b1bb0e98fcc486ad36d949b6dad287df1f2dc79f

Ok, that makes sense.

How should one understand the NRG concept? - Is this a value that can be translated into GTU or what is the meaning of this? - If so the value of 1000 seems rather high.

And how to know which number to use? - I tried with several different values lower than 1000 and many of them were too low, but how to know what value to use?

Hi petlan,

NRG is a unit that represents energy (and is also pronounced as energy :blush:). Its conversion to GTU is controlled by two parameters on-chain, which can be viewed with concordium-client raw GetBlockSummary:

  • microGTUPerEuro, which is 2777778 / 1 on testnet at the moment.
  • euroPerEnergy, which is 1 / 50000 on testnet at the moment.

The conversion from NRG to microGTU is thus:
n NRG * microGTUPerEuro * euroPerEnergy = m microGTU
n NRG * (2777778 / 1) * (1 / 50000) = m microGTU

This translates into roughly 55 microGTU per NRG.

For smart contracts the --energy flag is used to specify the maximum allowed energy a contract invocation can use.

You can get a good estimate of the energy consumption for a particular contract invocation by simulating it with cargo concordium run [init/update]. But please note that this is only an estimate and the accuracy depends on whether the simulated context matches the actual context on-chain for when the invocation is committed to a block. Also, note that some additional NRG will be required on-chain to pay for checking signatures.

I hope that clarifies the NRG concept, otherwise please let us know! :blush:

/ Kasper

Thank you for explaining.

I’m having some problems interacting with the contract.

concordium-client_1.1.1-0 contract show 79 --grpc-ip --grpc-port 10001
Contract:        PiggyBank
Owner:           '3PHLj6j6YyqHVF3hXjqg5zeKHzZVQgCuaAZSaGo4TN5EXpaFUJ' ('SysConDev-01')
ModuleReference: '68ca306a53ea6b206a169788b1bb0e98fcc486ad36d949b6dad287df1f2dc79f' ('sc_piggy_bank')
Balance:         0.000000 GTU
State size:      1 bytes
        "Intact": []
 - insert
 - smash

But all attempts to call insert fails. I have been trying with various commands like:

concordium-client_1.1.1-0 contract update 79 --sender SysConDev-01 --func insert --energy 1000 --parameter-json amount_3_gtu.json --grpc-ip --grpc-port 10001

But it fails with:

Error: The schema did not include the provided function.

I have also tried with piggy_insert, but that gives me the following error instead:

Error: The identifier 'default' is neither the address nor the name of an account.

Hi again,

I see that you are trying to send GTU via the --parameter-json flag, but that is not possible. To send GTU to a contract you need to use the --amount <GTU> flag.

You are seeing the error message related to the schema because there is no schema included for the given function. To include a schema for a function, you need to add a Parameter attribute to it. However, for insert in the piggy bank example, no schema is needed. Schemas for functions are only needed if you want to use the parameter from the context, i.e. with let parameter = ctx.parameter_cursor.get()?;.

To explain the error regarding addresses, you need a bit of context.

  • By default, if you do not provide a --sender flag, concordium-client will try to use the account named `default.
  • If you do not have an account named default, then you will see the error you posted.
  • To make use of this feature, you need to name an account default, which you can do with: concordium-client config account name <account-address> --name default (NB: each account can have multiple names).

I hope this clears things up :blush:

/ Kasper

Hmm I don’t think I’m trying to use the send GTU method, I believe that is a general function of the client, I’m trying to use the insert method of the PiggyBank contract, which happens to take amount as input as far as I know.

UPDATE: I tried with the --amount flag and it worked.