forked from foundry-rs/foundry
-
Notifications
You must be signed in to change notification settings - Fork 8
anvil: allow sending transactions from contract accounts #423
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c31f5b9
allow calling transactions from smart contract accounts
alindima 5e37096
add test
alindima 30b8217
make the storage overlay lru capacity configurable and increase the d…
alindima fadd9e9
undo
alindima 95c362e
use new PR version
alindima 6376e90
Merge remote-tracking branch 'origin/master' into alindima/fix-circle
alindima 2ca336c
use master
alindima File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -281,6 +281,130 @@ async fn test_set_balance() { | |
| assert_eq!(node.get_balance(random_addr, None).await, new_balance); | ||
| } | ||
|
|
||
| #[tokio::test(flavor = "multi_thread")] | ||
| async fn test_set_and_spend_balance_for_contract() { | ||
| let anvil_node_config = AnvilNodeConfig::test_config(); | ||
| let substrate_node_config = SubstrateNodeConfig::new(&anvil_node_config); | ||
| let mut node = TestNode::new(anvil_node_config, substrate_node_config).await.unwrap(); | ||
|
|
||
| let alith = | ||
| Address::from(ReviveAddress::new(Account::from(subxt_signer::eth::dev::alith()).address())); | ||
|
|
||
| let ContractCode { init: bytecode, .. } = get_contract_code("SimpleStorage"); | ||
|
|
||
| let tx_hash = node | ||
| .deploy_contract(&bytecode, Account::from(subxt_signer::eth::dev::alith()).address()) | ||
| .await; | ||
|
|
||
| unwrap_response::<()>(node.eth_rpc(EthRequest::Mine(None, None)).await.unwrap()).unwrap(); | ||
|
|
||
| let receipt = node.get_transaction_receipt(tx_hash).await; | ||
| let contract_address = Address::from(ReviveAddress::new(receipt.contract_address.unwrap())); | ||
|
|
||
| let set_value_data = SimpleStorage::setValueCall::new((U256::from(5),)).abi_encode(); | ||
| let tx = TransactionRequest::default() | ||
| .from(alith) | ||
| .to(contract_address) | ||
| .input(TransactionInput::both(set_value_data.into())); | ||
|
|
||
| let tx_hash = node.send_transaction(tx).await.unwrap(); | ||
|
|
||
| unwrap_response::<()>(node.eth_rpc(EthRequest::Mine(None, None)).await.unwrap()).unwrap(); | ||
|
|
||
| let _receipt = node.get_transaction_receipt(tx_hash).await; | ||
|
|
||
| // assert new value | ||
| let tx = TransactionRequest::default() | ||
| .from(alith) | ||
| .to(contract_address) | ||
| .input(TransactionInput::both(SimpleStorage::getValueCall.abi_encode().into())); | ||
|
|
||
| let value = unwrap_response::<Bytes>( | ||
| node.eth_rpc(EthRequest::EthCall(tx.into(), None, None, None)).await.unwrap(), | ||
| ) | ||
| .unwrap(); | ||
|
|
||
| let value = SimpleStorage::getValueCall::abi_decode_returns(&value.0).unwrap(); | ||
|
|
||
| assert_eq!(value, U256::from(5)); | ||
|
|
||
| // Get balance | ||
| assert_eq!( | ||
| node.get_balance(ReviveAddress::from(contract_address).inner(), None).await, | ||
| U256::from(0) | ||
| ); | ||
|
|
||
| // Set a new balance | ||
| let contract_balance = U256::from_str_radix("200000000000000000000", 10).unwrap(); | ||
| unwrap_response::<()>( | ||
| node.eth_rpc(EthRequest::SetBalance(contract_address, contract_balance)).await.unwrap(), | ||
| ) | ||
| .unwrap(); | ||
|
|
||
| // Check balance | ||
| assert_eq!( | ||
| node.get_balance(ReviveAddress::from(contract_address).inner(), None).await, | ||
| contract_balance | ||
| ); | ||
|
|
||
| // Try spending the balance, need to impersonate first. | ||
| unwrap_response::<()>( | ||
| node.eth_rpc(EthRequest::ImpersonateAccount(contract_address)).await.unwrap(), | ||
| ) | ||
| .unwrap(); | ||
|
|
||
| let charleth = Account::from(subxt_signer::eth::dev::charleth()); | ||
| let tx = TransactionRequest::default() | ||
| .value(U256::from(2e18)) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: capture 2e18 in a variable, and use it when asserting on the expected balance |
||
| .from(contract_address) | ||
| .to(Address::from(ReviveAddress::new(charleth.address()))); | ||
|
|
||
| let tx_hash = node.send_transaction(tx).await.unwrap(); | ||
|
|
||
| unwrap_response::<()>(node.eth_rpc(EthRequest::Mine(None, None)).await.unwrap()).unwrap(); | ||
|
|
||
| let transaction_receipt = node.get_transaction_receipt(tx_hash).await; | ||
| assert_eq!(transaction_receipt.transaction_hash, tx_hash); | ||
|
|
||
| let new_balance = contract_balance | ||
| - AlloyU256::from(transaction_receipt.effective_gas_price * transaction_receipt.gas_used) | ||
| .inner() | ||
| - U256::from(2e18); | ||
| assert_eq!( | ||
| node.get_balance(ReviveAddress::from(contract_address).inner(), None).await, | ||
| new_balance | ||
| ); | ||
| assert_eq!(node.get_balance(charleth.address(), None).await, U256::from(2e18)); | ||
|
|
||
| // Now try interacting with the contract again to check that it still works. | ||
| let set_value_data = SimpleStorage::setValueCall::new((U256::from(10),)).abi_encode(); | ||
| let tx = TransactionRequest::default() | ||
| .from(alith) | ||
| .to(contract_address) | ||
| .input(TransactionInput::both(set_value_data.into())); | ||
|
|
||
| let tx_hash = node.send_transaction(tx).await.unwrap(); | ||
|
|
||
| unwrap_response::<()>(node.eth_rpc(EthRequest::Mine(None, None)).await.unwrap()).unwrap(); | ||
|
|
||
| let _receipt = node.get_transaction_receipt(tx_hash).await; | ||
|
|
||
| // assert new value. | ||
| let tx = TransactionRequest::default() | ||
| .from(alith) | ||
| .to(contract_address) | ||
| .input(TransactionInput::both(SimpleStorage::getValueCall.abi_encode().into())); | ||
|
|
||
| let value = unwrap_response::<Bytes>( | ||
| node.eth_rpc(EthRequest::EthCall(tx.into(), None, None, None)).await.unwrap(), | ||
| ) | ||
| .unwrap(); | ||
|
|
||
| let value = SimpleStorage::getValueCall::abi_decode_returns(&value.0).unwrap(); | ||
|
|
||
| assert_eq!(value, U256::from(10)); | ||
| } | ||
|
|
||
| #[tokio::test(flavor = "multi_thread")] | ||
| // Test setting the code of an existing contract. | ||
| async fn test_set_code_existing_contract() { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.