| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595 |
- // SPDX-License-Identifier: Apache-2.0
- use parity_scale_codec::{Decode, Encode};
- use crate::build_solidity;
- #[test]
- fn external_call_value() {
- let mut runtime = build_solidity(
- r##"
- contract b {
- a f;
- constructor() payable {}
- function step1() public {
- f = new a();
- }
- function step2() public {
- f.test{value: 1023}(501);
- }
- }
- contract a {
- function test(int32 l) public payable {
- }
- }"##,
- );
- runtime.set_transferred_value(20000);
- runtime.constructor(0, Vec::new());
- runtime.set_transferred_value(0);
- runtime.function("step1", Vec::new());
- runtime.function("step2", Vec::new());
- // Transferred value = 1023
- assert_eq!(runtime.balance(2), 1023);
- let mut runtime = build_solidity(
- r##"
- contract b {
- constructor() payable {}
- function step1() public {
- a f = new a();
- try f.test{value: 1023}(501) {
- //
- }
- catch (bytes) {
- //
- }
- }
- }
- contract a {
- function test(int32 l) public payable {
- }
- }"##,
- );
- runtime.set_transferred_value(20000);
- runtime.constructor(0, Vec::new());
- runtime.set_transferred_value(0);
- runtime.function("step1", Vec::new());
- // Transferred value = 1023
- assert_eq!(runtime.balance(2), 1023);
- }
- #[test]
- fn constructor_value() {
- let mut runtime = build_solidity(
- r##"
- contract b {
- constructor() payable {}
- function step1() public {
- a f = new a{value: 500}();
- }
- }
- contract a {
- constructor() payable {}
- function test(int32 l) public payable {
- }
- }"##,
- );
- runtime.set_transferred_value(20000);
- runtime.constructor(0, Vec::new());
- runtime.set_transferred_value(0);
- runtime.function("step1", Vec::new());
- assert_eq!(runtime.balance(2), 500);
- let mut runtime = build_solidity(
- r##"
- contract b {
- constructor() payable {}
- function step1() public {
- a f = (new a){value: 0}();
- }
- }
- contract a {
- function test(int32 l) public payable {
- }
- }"##,
- );
- runtime.set_transferred_value(20000);
- runtime.constructor(0, Vec::new());
- runtime.set_transferred_value(0);
- runtime.function("step1", Vec::new());
- assert_eq!(runtime.balance(2), 0);
- let mut runtime = build_solidity(
- r##"
- contract b {
- constructor() payable {}
- function step1() public {
- a f = new a{value: 499}();
- }
- }
- contract a {
- constructor() payable {}
- function test(int32 l) public payable {
- }
- }"##,
- );
- runtime.set_transferred_value(20000);
- runtime.constructor(0, Vec::new());
- runtime.set_transferred_value(0);
- runtime.function("step1", Vec::new());
- assert_eq!(runtime.balance(2), 499);
- let mut runtime = build_solidity(
- r##"
- contract b {
- constructor() payable {}
- function step1() public {
- try (new a{value: 511})() {
- //
- }
- catch (bytes) {
- //
- }
- }
- }
- contract a {
- constructor() payable {}
- function test(int32 l) public payable {
- }
- }"##,
- );
- runtime.set_transferred_value(20000);
- runtime.constructor(0, Vec::new());
- runtime.set_transferred_value(0);
- runtime.function("step1", Vec::new());
- assert_eq!(runtime.balance(2), 511);
- let mut runtime = build_solidity(
- r##"
- contract b {
- constructor() payable {}
- function step1() public {
- try (new a){value: 511}() returns (a) {
- //
- }
- catch (bytes) {
- //
- }
- }
- }
- contract a {
- constructor() payable {}
- function test(int32 l) public payable {
- }
- }"##,
- );
- runtime.set_transferred_value(20000);
- runtime.constructor(0, Vec::new());
- runtime.set_transferred_value(0);
- runtime.function("step1", Vec::new());
- assert_eq!(runtime.balance(2), 511)
- }
- #[test]
- fn constructor_salt() {
- let mut runtime = build_solidity(
- r##"
- contract b {
- function step1() public {
- a f = new a{salt: 0}();
- }
- }
- contract a {
- function test(int32 l) public payable {
- }
- }"##,
- );
- runtime.constructor(0, Vec::new());
- runtime.function("step1", Vec::new());
- let mut runtime = build_solidity(
- r#"
- contract b {
- function step1() public {
- a f = new a{salt: hex"01"}();
- }
- }
- contract a {
- function test(int32 l) public payable {
- }
- }"#,
- );
- runtime.constructor(0, Vec::new());
- runtime.function("step1", Vec::new());
- // we can instantiate the same contract if we provide a different contract
- let mut runtime = build_solidity(
- r#"
- contract b {
- function step1() public {
- a f = new a{salt: hex"01"}();
- f = new a{salt: hex"02"}();
- }
- }
- contract a {
- function test(int32 l) public payable {
- }
- }"#,
- );
- runtime.constructor(0, Vec::new());
- runtime.function("step1", Vec::new());
- }
- #[test]
- fn this_address() {
- let mut runtime = build_solidity(
- r##"
- contract b {
- function step1() public returns (address) {
- return address(this);
- }
- }"##,
- );
- runtime.constructor(0, Vec::new());
- runtime.function("step1", Vec::new());
- assert_eq!(runtime.output(), runtime.caller());
- let mut runtime = build_solidity(
- r##"
- contract b {
- function step1() public returns (b) {
- return this;
- }
- }"##,
- );
- runtime.constructor(0, Vec::new());
- runtime.function("step1", Vec::new());
- assert_eq!(runtime.output(), runtime.caller());
- #[derive(Debug, PartialEq, Eq, Encode, Decode)]
- struct Ret(u32);
- let mut runtime = build_solidity(
- r##"
- contract b {
- int32 s;
- function step1() public returns (int32) {
- this.other{flags: 8}(102);
- return s;
- }
- function other(int32 n) public {
- s = n;
- }
- }"##,
- );
- runtime.constructor(0, Vec::new());
- runtime.function("step1", Vec::new());
- assert_eq!(runtime.output(), Ret(102).encode());
- let mut runtime = build_solidity(
- r##"
- contract b {
- function step1() public returns (b) {
- return this;
- }
- }"##,
- );
- runtime.constructor(0, Vec::new());
- runtime.function("step1", Vec::new());
- assert_eq!(runtime.output(), runtime.caller());
- }
- #[test]
- fn balance() {
- let mut runtime = build_solidity(
- r##"
- contract b {
- other o;
- constructor() public payable {
- o = new other();
- }
- function step1() public returns (uint128) {
- return address(this).balance;
- }
- function step2() public returns (uint128) {
- return o.balance();
- }
- }
- contract other {
- function balance() public returns (uint128) {
- return address(this).balance;
- }
- }"##,
- );
- runtime.set_transferred_value(20000);
- runtime.constructor(0, Vec::new());
- runtime.set_transferred_value(0);
- runtime.function("step1", Vec::new());
- // Constructor received 20000, 0 were sent to "o"
- assert_eq!(runtime.output(), 20000u128.to_le_bytes());
- runtime.function("step2", Vec::new());
- assert_eq!(runtime.output(), 0u128.to_le_bytes());
- }
- #[test]
- fn selfdestruct() {
- let mut runtime = build_solidity(
- r##"
- contract c {
- other o;
- constructor() payable {}
- function step1() public {
- o = new other{value: 511}();
- }
- function step2() public {
- o.goaway(payable(address(this)));
- }
- }
- contract other {
- constructor() payable {}
- function goaway(address payable recipient) public returns (bool) {
- selfdestruct(recipient);
- }
- }"##,
- );
- runtime.set_transferred_value(20000);
- runtime.constructor(0, Vec::new());
- runtime.set_transferred_value(0);
- runtime.function("step1", Vec::new());
- assert_eq!(runtime.balance(0), 20000 - 511);
- runtime.function("step2", Vec::new());
- assert_eq!(runtime.balance(0), 20000);
- }
- #[test]
- fn send_and_transfer() {
- let mut runtime = build_solidity(
- r##"
- contract c {
- other o;
- constructor() public payable {
- o = new other();
- }
- function step1() public returns (bool) {
- return payable(o).send(511);
- }
- }
- contract other {
- function giveme() public payable {
- }
- }"##,
- );
- runtime.set_transferred_value(20000);
- runtime.constructor(0, Vec::new());
- runtime.function("step1", Vec::new());
- // no receive() required for send/transfer
- assert_eq!(runtime.output(), true.encode());
- assert_eq!(runtime.balance(2), 511);
- let mut runtime = build_solidity(
- r##"
- contract c {
- other o;
- constructor() public payable {
- o = new other();
- }
- function step1() public returns (bool) {
- return payable(o).send(511);
- }
- }
- contract other {
- receive() external payable {
- }
- }"##,
- );
- runtime.set_transferred_value(20000);
- runtime.constructor(0, Vec::new());
- runtime.set_transferred_value(0);
- runtime.function("step1", Vec::new());
- assert_eq!(runtime.output(), true.encode());
- assert_eq!(runtime.balance(2), 511);
- let mut runtime = build_solidity(
- r##"
- contract c {
- other o;
- constructor() public payable {
- o = new other();
- }
- function step1() public {
- payable(o).transfer(511);
- }
- }
- contract other {
- function giveme() public {
- }
- }"##,
- );
- runtime.set_transferred_value(20000);
- runtime.constructor(0, Vec::new());
- runtime.set_transferred_value(0);
- runtime.function("step1", Vec::new());
- assert_eq!(runtime.balance(2), 511);
- let mut runtime = build_solidity(
- r##"
- contract c {
- other o;
- constructor() public payable {
- o = new other();
- }
- function step1() public {
- payable(o).transfer(511);
- }
- }
- contract other {
- receive() external payable {
- }
- }"##,
- );
- runtime.set_transferred_value(20000);
- runtime.constructor(0, Vec::new());
- runtime.set_transferred_value(0);
- runtime.function("step1", Vec::new());
- assert_eq!(runtime.balance(2), 511);
- }
- #[test]
- fn nonpayable_constructor_reverts() {
- let mut runtime = build_solidity(
- r#"contract C {
- uint8 public c;
- constructor (uint8 val) {
- c = val;
- }
- }"#,
- );
- let mut input = runtime.blobs()[0].constructors[0].to_vec();
- let storage_value = 123;
- input.push(storage_value);
- // Expect the deploy to fail with value
- runtime.set_transferred_value(1);
- runtime.raw_constructor_failure(input.clone());
- // The same input should work without value
- runtime.set_transferred_value(0);
- runtime.raw_constructor(input.clone());
- runtime.function("c", Vec::new());
- assert_eq!(runtime.output(), storage_value.encode());
- }
- #[test]
- fn transfer_bubble_up() {
- let mut runtime = build_solidity(
- r##"
- contract C {
- function c(uint128 amount) public payable {
- Other o = new Other{value: 1000}();
- o.payback(amount);
- }
- }
- contract Other {
- constructor() payable {}
- function payback(uint128 amount) public payable {
- payable(msg.sender).transfer(amount);
- }
- }"##,
- );
- runtime.set_transferred_value(2000);
- runtime.function("c", 100u128.encode());
- runtime.function_expect_failure("c", 1000000u128.encode());
- assert!(runtime.output().is_empty());
- }
- #[test]
- fn send_does_not_bubble_up() {
- let mut runtime = build_solidity(
- r##"
- contract C {
- function c(uint128 amount) public payable returns(bool) {
- Other o = new Other{value: 1000}();
- return o.payback(amount);
- }
- }
- contract Other {
- constructor() payable {}
- function payback(uint128 amount) public payable returns(bool) {
- return payable(msg.sender).send(amount);
- }
- }"##,
- );
- runtime.set_transferred_value(2000);
- runtime.function("c", 100u128.encode());
- assert_eq!(runtime.output(), true.encode());
- runtime.function("c", 1000000u128.encode());
- assert_eq!(runtime.output(), false.encode());
- }
|