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

Unable to transfer contract balance to another account #538

Closed
LaurentTrk opened this issue Oct 23, 2020 · 9 comments · Fixed by #540
Closed

Unable to transfer contract balance to another account #538

LaurentTrk opened this issue Oct 23, 2020 · 9 comments · Fixed by #540
Labels
C-bug Something isn't working P-high High priority issue.

Comments

@LaurentTrk
Copy link

Hi,

The env.transfer() seems not doing its job in my environment. The balance is not changed, neither in the contract nor in the destination account. No exception/panic is raised.

I use it in a Ink contract, with something like :

let value = self.env().balance();
let _ = self.env().transfer(self.charity_pot, value);

Note : In the same contract, using the same charity_pot account, the env.terminate_contract() is correctly transferring the funds to the given account.

Please let me know if you need additionnal informations. Thanks for your help !

Environment

Using nightly-2020-08-23 and cargo-contract 0.7.0 on Ubuntu 20.04.

[dependencies]
ink_primitives = { version = "3.0.0-rc1", default-features = false }
ink_metadata = { version = "3.0.0-rc1", default-features = false, features = ["derive"], optional = true }
ink_env = { version = "3.0.0-rc1", default-features = false }
ink_storage = { version = "3.0.0-rc1", default-features = false }
ink_lang = { version = "3.0.0-rc1", default-features = false }

scale = { package = "parity-scale-codec", version = "1.3", default-features = false, features = ["derive"] }
scale-info = { version = "0.4", default-features = false, features = ["derive"], optional = true }
@Robbepop
Copy link
Collaborator

Robbepop commented Oct 23, 2020

Thanks for the issue report!
I will look on the ink! side and @athei might take a look on the contracts-pallet side.
More information is always welcome. Can you maybe post your smart contract fully so that we can reproduce?
Did you try this on the Canvas test chain or locally?

@Robbepop Robbepop added the C-bug Something isn't working label Oct 23, 2020
@LaurentTrk
Copy link
Author

Hi @Robbepop !
I run a local node.
You can find the whole contract here : https://github.com/LaurentTrk/hello-world-by-polkadot/blob/CharityRaffle/frame-development/charity-raffle
(that's my work in progress for the Hello World! hackathon ;) )

@Robbepop
Copy link
Collaborator

You should definitely update your ink! version to 3.0.0-rc2. But this will probably not fix this particular bug for you.

@LaurentTrk
Copy link
Author

Done. And you are right, the bug is not fixed ;)

@cmichi cmichi added the P-high High priority issue. label Oct 24, 2020
@athei
Copy link
Contributor

athei commented Oct 24, 2020

I can't see any transfer call in your contract. Can you link the line which is not working? Also transfer won't panic but returns a recoverable error. The ink! doc is wrong about the panic. So make sure to not drop the Result on the floor (the compiler should give a warning if you do).

@LaurentTrk
Copy link
Author

LaurentTrk commented Oct 24, 2020

Argh, my bad, I sent you the working version with env.terminate_contract()... Sorry for that ;)

The result.Error is BelowSubsistenceThreshold which make sense if I transferred all the balance.
Before opening the issue, I tried keeping the env.minimum_balance() into the contract balance, without success...
I guess I misunderstanding the minimum balance meaning 🙄

Well, I changed the code logic to only transfer the amount transferred (leaving the endowment in the contract balance) and it works, the funds are correctly transferred 😃

So no problem. But maybe we can add some documentation about the transfer and the minimum balance ?

Btw, is there any way to return the Result<Ok or Err> to the caller ? I return some custom Errors in my contract for logic errors, but the extrinsic always succeed... Thanks for your help.

@cmichi
Copy link
Collaborator

cmichi commented Oct 26, 2020

Btw, is there any way to return the Result to the caller ? I return some custom Errors in my contract for logic errors, but the extrinsic always succeed...

@LaurentTrk Unfortunately we don't have error forwarding yet, it's on the roadmap though!

@LaurentTrk
Copy link
Author

Thanks @cmichi for your answer and the docs improvement !

By any chance, do you have any tip on how to mock transferred_balance() when unit testing payable transactions ? Maybe we should add it in the samples as well...

@cmichi
Copy link
Collaborator

cmichi commented Oct 26, 2020

@LaurentTrk Here's an example: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=871521ec0b23f6fb0c4facd1cc5ca420. Make sure to read the comment at the bottom, mocking is the only possibility at the moment, we hope to improve this in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Something isn't working P-high High priority issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants