From 95d0bc46968f956851539c22ead1908404c389eb Mon Sep 17 00:00:00 2001 From: filip Date: Fri, 5 Aug 2022 14:33:54 +0200 Subject: [PATCH 01/29] init tikcet token --- contracts/ticket_token/Cargo.lock | 1007 +++++++++++++++++++++++++++++ contracts/ticket_token/Cargo.toml | 50 ++ contracts/ticket_token/lib.rs | 201 ++++++ 3 files changed, 1258 insertions(+) create mode 100644 contracts/ticket_token/Cargo.lock create mode 100644 contracts/ticket_token/Cargo.toml create mode 100644 contracts/ticket_token/lib.rs diff --git a/contracts/ticket_token/Cargo.lock b/contracts/ticket_token/Cargo.lock new file mode 100644 index 0000000000..a41adcd8fe --- /dev/null +++ b/contracts/ticket_token/Cargo.lock @@ -0,0 +1,1007 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "access_control" +version = "0.1.0" +dependencies = [ + "ink_env", + "ink_lang", + "ink_lang_codegen", + "ink_primitives", + "ink_storage", + "parity-scale-codec", +] + +[[package]] +name = "array-init" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfb6d71005dc22a708c7496eee5c8dc0300ee47355de6256c3b35b12b5fef596" + +[[package]] +name = "arrayref" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4c527152e37cf757a3f78aae5a06fbeefdb07ccc535c980a3208ee3060dd544" + +[[package]] +name = "arrayvec" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8da52d66c7071e2e3fa2a1e5c6d088fec47b593032b254f5e980de8ea54454d6" + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "bitvec" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bc2832c24239b0141d5674bb9174f9d68a8b5b3f2753311927c172ca46f7e9c" +dependencies = [ + "funty", + "radium", + "tap", + "wyz", +] + +[[package]] +name = "blake2" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0a4e37d16930f5459780f5621038b6382b9bb37c19016f39fb6b5808d831f174" +dependencies = [ + "crypto-mac", + "digest 0.9.0", + "opaque-debug", +] + +[[package]] +name = "blake2" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9cf849ee05b2ee5fba5e36f97ff8ec2533916700fc0758d40d92136a42f3388" +dependencies = [ + "digest 0.10.3", +] + +[[package]] +name = "block-buffer" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bf7fe51849ea569fd452f37822f606a5cabb684dc918707a0193fd4664ff324" +dependencies = [ + "generic-array", +] + +[[package]] +name = "byte-slice-cast" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87c5fdd0166095e1d463fc6cc01aa8ce547ad77a4e84d42eb6762b084e28067e" + +[[package]] +name = "camino" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "869119e97797867fd90f5e22af7d0bd274bd4635ebb9eb68c04f3f513ae6c412" +dependencies = [ + "serde", +] + +[[package]] +name = "cargo-platform" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cbdb825da8a5df079a43676dbe042702f1707b1109f713a01420fbb4cc71fa27" +dependencies = [ + "serde", +] + +[[package]] +name = "cargo_metadata" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "081e3f0755c1f380c2d010481b6fa2e02973586d5f2b24eebb7a2a1d98b143d8" +dependencies = [ + "camino", + "cargo-platform", + "semver", + "semver-parser", + "serde", + "serde_json", +] + +[[package]] +name = "cc" +version = "1.0.73" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2fff2a6927b3bb87f9595d67196a70493f627687a71d87a0d692242c33f58c11" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "const_format" +version = "0.2.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "939dc9e2eb9077e0679d2ce32de1ded8531779360b003b4a972a7a39ec263495" +dependencies = [ + "const_format_proc_macros", +] + +[[package]] +name = "const_format_proc_macros" +version = "0.2.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef196d5d972878a48da7decb7686eded338b4858fbabeed513d63a7c98b2b82d" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + +[[package]] +name = "cpufeatures" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59a6001667ab124aebae2a495118e11d30984c3a653e99d86d58971708cf5e4b" +dependencies = [ + "libc", +] + +[[package]] +name = "crypto-common" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" +dependencies = [ + "generic-array", + "typenum", +] + +[[package]] +name = "crypto-mac" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b584a330336237c1eecd3e94266efb216c56ed91225d634cb2991c5f3fd1aeab" +dependencies = [ + "generic-array", + "subtle", +] + +[[package]] +name = "derive_more" +version = "0.99.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fb810d30a7c1953f91334de7244731fc3f3c10d7fe163338a35b9f640960321" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "digest" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3dd60d1080a57a05ab032377049e0591415d2b31afd7028356dbf3cc6dcb066" +dependencies = [ + "generic-array", +] + +[[package]] +name = "digest" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2fb860ca6fafa5552fb6d0e816a69c8e49f0908bf524e30a90d97c85892d506" +dependencies = [ + "block-buffer", + "crypto-common", + "subtle", +] + +[[package]] +name = "either" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f107b87b6afc2a64fd13cac55fe06d6c8859f12d4b14cbcdd2c67d0976781be" + +[[package]] +name = "fs2" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9564fc758e15025b46aa6643b1b77d047d1a56a1aea6e01002ac0c7026876213" +dependencies = [ + "libc", + "winapi", +] + +[[package]] +name = "funty" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c" + +[[package]] +name = "generic-array" +version = "0.14.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bff49e947297f3312447abdca79f45f4738097cc82b06e72054d2223f601f1b9" +dependencies = [ + "typenum", + "version_check", +] + +[[package]] +name = "getrandom" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4eb1a864a501629691edf6c15a593b7a51eebaa1e8468e9ddc623de7c9b58ec6" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "heck" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c" +dependencies = [ + "unicode-segmentation", +] + +[[package]] +name = "heck" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9" + +[[package]] +name = "impl-serde" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4551f042f3438e64dbd6226b20527fc84a6e1fe65688b58746a2f53623f25f5c" +dependencies = [ + "serde", +] + +[[package]] +name = "impl-trait-for-tuples" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11d7a9f6330b71fea57921c9b61c47ee6e84f72d394754eff6163ae67e7395eb" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "ink_allocator" +version = "3.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed249de74298ed051ebcf6d3082b8d3dbd19cbc448d9ed3235d8a7b92713049" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "ink_engine" +version = "3.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acb9d32ec27d71fefb3f2b6a26bae82a2c6509d7ad61e8a5107b6291a1b03ecb" +dependencies = [ + "blake2 0.10.4", + "derive_more", + "parity-scale-codec", + "rand", + "secp256k1", + "sha2", + "sha3", +] + +[[package]] +name = "ink_env" +version = "3.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1549f5966167387c89fb3dfcdc59973bfb396cc3a7110d7a31ad5fdea56db0cf" +dependencies = [ + "arrayref", + "blake2 0.10.4", + "cfg-if", + "derive_more", + "ink_allocator", + "ink_engine", + "ink_metadata", + "ink_prelude", + "ink_primitives", + "num-traits", + "parity-scale-codec", + "paste", + "rand", + "rlibc", + "scale-info", + "secp256k1", + "sha2", + "sha3", + "static_assertions", +] + +[[package]] +name = "ink_lang" +version = "3.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e5282f2722ac6dca469e7f223a7b38b2a6d20fbca6b974497e630d5dc8934e9" +dependencies = [ + "derive_more", + "ink_env", + "ink_lang_macro", + "ink_metadata", + "ink_prelude", + "ink_primitives", + "ink_storage", + "parity-scale-codec", +] + +[[package]] +name = "ink_lang_codegen" +version = "3.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb3a5de33b59450adc3f61c5eb05b768067c7ab8af9d00f33e284310598168dc" +dependencies = [ + "blake2 0.10.4", + "derive_more", + "either", + "heck 0.4.0", + "impl-serde", + "ink_lang_ir", + "itertools", + "parity-scale-codec", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "ink_lang_ir" +version = "3.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9d4d614462280fa06e15b9ca5725d7c8440dde93c8dae1c6f15422f7756cacb" +dependencies = [ + "blake2 0.10.4", + "either", + "itertools", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "ink_lang_macro" +version = "3.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72f85f64141957c5db7cbabbb97a9c16c489e5e9d363e9f147d132a43c71cd29" +dependencies = [ + "ink_lang_codegen", + "ink_lang_ir", + "ink_primitives", + "parity-scale-codec", + "proc-macro2", + "syn", +] + +[[package]] +name = "ink_metadata" +version = "3.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dca6c159a2774f07437c6fd9ea710eb73a6b5e9a031a932bddf08742bf2c081a" +dependencies = [ + "derive_more", + "impl-serde", + "ink_prelude", + "ink_primitives", + "scale-info", + "serde", +] + +[[package]] +name = "ink_prelude" +version = "3.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1f7f4dec15e573496c9d2af353e78bde84add391251608f25b5adcf175dc777" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "ink_primitives" +version = "3.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b3296dd1c4f4fe12ede7c92d60e6fcb94d46a959ec19c701e4ac588b09e0b4a6" +dependencies = [ + "cfg-if", + "ink_prelude", + "parity-scale-codec", + "scale-info", +] + +[[package]] +name = "ink_storage" +version = "3.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ff9b503995a7b41fe201a7a2643ce22f5a11e0b67db7b685424b6d5fe0ecf0b" +dependencies = [ + "array-init", + "cfg-if", + "derive_more", + "ink_env", + "ink_metadata", + "ink_prelude", + "ink_primitives", + "ink_storage_derive", + "parity-scale-codec", + "scale-info", +] + +[[package]] +name = "ink_storage_derive" +version = "3.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "afb68e24e93e8327dda1924868d7ee4dbe01e1ed2b392f28583caa96809b585c" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "synstructure", +] + +[[package]] +name = "itertools" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9a9d19fa1e79b6215ff29b9d6880b706147f16e9b1dbb1e4e5947b5b02bc5e3" +dependencies = [ + "either", +] + +[[package]] +name = "itoa" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c8af84674fe1f223a982c933a0ee1086ac4d4052aa0fb8060c12c6ad838e754" + +[[package]] +name = "keccak" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9b7d56ba4a8344d6be9729995e6b06f928af29998cdf79fe390cbf6b1fee838" + +[[package]] +name = "libc" +version = "0.2.127" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "505e71a4706fa491e9b1b55f51b95d4037d0821ee40131190475f692b35b009b" + +[[package]] +name = "num-traits" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd" +dependencies = [ + "autocfg", +] + +[[package]] +name = "once_cell" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "18a6dbe30758c9f83eb00cbea4ac95966305f5a7772f3f42ebfc7fc7eddbd8e1" + +[[package]] +name = "opaque-debug" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" + +[[package]] +name = "openbrush" +version = "2.1.0" +source = "git+https://github.com/Supercolony-net/openbrush-contracts.git?rev=8a20f95#8a20f951feaf8c6ea72c4bf113bf115bad9f158f" +dependencies = [ + "ink_env", + "ink_lang", + "ink_metadata", + "ink_prelude", + "ink_primitives", + "ink_storage", + "openbrush_contracts", + "openbrush_lang", + "parity-scale-codec", + "scale-info", +] + +[[package]] +name = "openbrush_contracts" +version = "2.1.0" +source = "git+https://github.com/Supercolony-net/openbrush-contracts.git?rev=8a20f95#8a20f951feaf8c6ea72c4bf113bf115bad9f158f" +dependencies = [ + "ink_env", + "ink_lang", + "ink_metadata", + "ink_prelude", + "ink_primitives", + "ink_storage", + "openbrush_lang", + "parity-scale-codec", + "scale-info", +] + +[[package]] +name = "openbrush_lang" +version = "2.1.0" +source = "git+https://github.com/Supercolony-net/openbrush-contracts.git?rev=8a20f95#8a20f951feaf8c6ea72c4bf113bf115bad9f158f" +dependencies = [ + "const_format", + "ink_env", + "ink_lang", + "ink_metadata", + "ink_primitives", + "ink_storage", + "openbrush_lang_macro", + "parity-scale-codec", + "scale-info", + "sha2-const", +] + +[[package]] +name = "openbrush_lang_codegen" +version = "2.1.0" +source = "git+https://github.com/Supercolony-net/openbrush-contracts.git?rev=8a20f95#8a20f951feaf8c6ea72c4bf113bf115bad9f158f" +dependencies = [ + "blake2 0.9.2", + "cargo_metadata", + "fs2", + "heck 0.3.3", + "ink_lang_ir", + "proc-macro2", + "quote", + "serde", + "serde_json", + "syn", + "synstructure", + "unwrap", +] + +[[package]] +name = "openbrush_lang_macro" +version = "2.1.0" +source = "git+https://github.com/Supercolony-net/openbrush-contracts.git?rev=8a20f95#8a20f951feaf8c6ea72c4bf113bf115bad9f158f" +dependencies = [ + "openbrush_lang_codegen", + "proc-macro2", + "syn", + "synstructure", +] + +[[package]] +name = "parity-scale-codec" +version = "3.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9182e4a71cae089267ab03e67c99368db7cd877baf50f931e5d6d4b71e195ac0" +dependencies = [ + "arrayvec", + "bitvec", + "byte-slice-cast", + "impl-trait-for-tuples", + "parity-scale-codec-derive", + "serde", +] + +[[package]] +name = "parity-scale-codec-derive" +version = "3.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9299338969a3d2f491d65f140b00ddec470858402f888af98e8642fb5e8965cd" +dependencies = [ + "proc-macro-crate", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "paste" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9423e2b32f7a043629287a536f21951e8c6a82482d0acb1eeebfc90bc2225b22" + +[[package]] +name = "pest" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69486e2b8c2d2aeb9762db7b4e00b0331156393555cff467f4163ff06821eef8" +dependencies = [ + "thiserror", + "ucd-trie", +] + +[[package]] +name = "ppv-lite86" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872" + +[[package]] +name = "proc-macro-crate" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26d50bfb8c23f23915855a00d98b5a35ef2e0b871bb52937bacadb798fbb66c8" +dependencies = [ + "once_cell", + "thiserror", + "toml", +] + +[[package]] +name = "proc-macro2" +version = "1.0.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0a2ca2c61bc9f3d74d2886294ab7b9853abd9c1ad903a3ac7815c58989bb7bab" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "radium" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09" + +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7" +dependencies = [ + "getrandom", +] + +[[package]] +name = "rlibc" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc874b127765f014d792f16763a81245ab80500e2ad921ed4ee9e82481ee08fe" + +[[package]] +name = "ryu" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09" + +[[package]] +name = "scale-info" +version = "2.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c46be926081c9f4dd5dd9b6f1d3e3229f2360bc6502dd8836f84a93b7c75e99a" +dependencies = [ + "bitvec", + "cfg-if", + "derive_more", + "parity-scale-codec", + "scale-info-derive", + "serde", +] + +[[package]] +name = "scale-info-derive" +version = "2.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50e334bb10a245e28e5fd755cabcafd96cfcd167c99ae63a46924ca8d8703a3c" +dependencies = [ + "proc-macro-crate", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "secp256k1" +version = "0.22.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26947345339603ae8395f68e2f3d85a6b0a8ddfe6315818e80b8504415099db0" +dependencies = [ + "secp256k1-sys", +] + +[[package]] +name = "secp256k1-sys" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "152e20a0fd0519390fc43ab404663af8a0b794273d2a91d60ad4a39f13ffe110" +dependencies = [ + "cc", +] + +[[package]] +name = "semver" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f301af10236f6df4160f7c3f04eec6dbc70ace82d23326abad5edee88801c6b6" +dependencies = [ + "semver-parser", + "serde", +] + +[[package]] +name = "semver-parser" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00b0bef5b7f9e0df16536d3961cfb6e84331c065b4066afb39768d0e319411f7" +dependencies = [ + "pest", +] + +[[package]] +name = "serde" +version = "1.0.142" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e590c437916fb6b221e1d00df6e3294f3fccd70ca7e92541c475d6ed6ef5fee2" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.142" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34b5b8d809babe02f538c2cfec6f2c1ed10804c0e5a6a041a049a4f5588ccc2e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.83" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38dd04e3c8279e75b31ef29dbdceebfe5ad89f4d0937213c53f7d49d01b3d5a7" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "sha2" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55deaec60f81eefe3cce0dc50bda92d6d8e88f2a27df7c5033b42afeb1ed2676" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest 0.10.3", +] + +[[package]] +name = "sha2-const" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5edcd790916d95ff81bdc1505b09c74d30d47a755929cc8c71c59cbbfa99f91b" + +[[package]] +name = "sha3" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0a31480366ec990f395a61b7c08122d99bd40544fdb5abcfc1b06bb29994312c" +dependencies = [ + "digest 0.10.3", + "keccak", +] + +[[package]] +name = "static_assertions" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" + +[[package]] +name = "subtle" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6bdef32e8150c2a081110b42772ffe7d7c9032b606bc226c8260fd97e0976601" + +[[package]] +name = "syn" +version = "1.0.99" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "58dbef6ec655055e20b86b15a8cc6d439cca19b667537ac6a1369572d151ab13" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "synstructure" +version = "0.12.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f36bdaa60a83aca3921b5259d5400cbf5e90fc51931376a9bd4a0eb79aa7210f" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "unicode-xid", +] + +[[package]] +name = "tap" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" + +[[package]] +name = "thiserror" +version = "1.0.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f5f6586b7f764adc0231f4c79be7b920e766bb2f3e51b3661cdb263828f19994" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12bafc5b54507e0149cdf1b145a5d80ab80a90bcd9275df43d4fff68460f6c21" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "ticket_token" +version = "2.1.0" +dependencies = [ + "access_control", + "ink_engine", + "ink_env", + "ink_lang", + "ink_metadata", + "ink_prelude", + "ink_primitives", + "ink_storage", + "openbrush", + "parity-scale-codec", + "scale-info", +] + +[[package]] +name = "toml" +version = "0.5.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d82e1a7758622a465f8cee077614c73484dac5b836c02ff6a40d5d1010324d7" +dependencies = [ + "serde", +] + +[[package]] +name = "typenum" +version = "1.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dcf81ac59edc17cc8697ff311e8f5ef2d99fcbd9817b34cec66f90b6c3dfd987" + +[[package]] +name = "ucd-trie" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89570599c4fe5585de2b388aab47e99f7fa4e9238a1399f707a02e356058141c" + +[[package]] +name = "unicode-ident" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4f5b37a154999a8f3f98cc23a628d850e154479cd94decf3414696e12e31aaf" + +[[package]] +name = "unicode-segmentation" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e8820f5d777f6224dc4be3632222971ac30164d4a258d595640799554ebfd99" + +[[package]] +name = "unicode-xid" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "957e51f3646910546462e67d5f7599b9e4fb8acdd304b087a6494730f9eebf04" + +[[package]] +name = "unwrap" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e33648dd74328e622c7be51f3b40a303c63f93e6fa5f08778b6203a4c25c20f" + +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "wyz" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30b31594f29d27036c383b53b59ed3476874d518f0efb151b27a4c275141390e" +dependencies = [ + "tap", +] diff --git a/contracts/ticket_token/Cargo.toml b/contracts/ticket_token/Cargo.toml new file mode 100644 index 0000000000..69bb61d1d8 --- /dev/null +++ b/contracts/ticket_token/Cargo.toml @@ -0,0 +1,50 @@ +[package] +name = "ticket_token" +version = "2.1.0" +authors = ["Cardinal Cryptography"] +edition = "2021" +license = "Apache 2.0" + +[dependencies] +ink_primitives = { version = "~3.3.0", default-features = false } +ink_metadata = { version = "~3.3.0", default-features = false, features = ["derive"], optional = true } +ink_env = { version = "~3.3.0", default-features = false } +ink_storage = { version = "~3.3.0", default-features = false } +ink_lang = { version = "~3.3.0", default-features = false } +ink_prelude = { version = "~3.3.0", default-features = false } +ink_engine = { version = "~3.3.0", default-features = false, optional = true } + +scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] } +scale-info = { version = "2", default-features = false, features = ["derive"], optional = true } + +openbrush = { git = "https://github.com/Supercolony-net/openbrush-contracts.git", rev = "8a20f95", default-features = false, features = ["psp22"] } +access_control = { path = "../access_control", default-features = false, features = ["ink-as-dependency"] } + +[lib] +name = "ticket_token" +path = "lib.rs" +crate-type = [ + # Used for normal contract Wasm blobs. + "cdylib", + # Used for ABI generation. + "rlib", +] + +[features] +default = ["std"] +std = [ + "ink_env/std", + "ink_lang/std", + "ink_metadata", + "ink_metadata/std", + "ink_primitives/std", + "ink_storage/std", + "openbrush/std", + "scale-info", + "scale-info/std", + "scale/std", +] +ink-as-dependency = [] + +[profile.dev] +codegen-units = 16 diff --git a/contracts/ticket_token/lib.rs b/contracts/ticket_token/lib.rs new file mode 100644 index 0000000000..fe55ae76c9 --- /dev/null +++ b/contracts/ticket_token/lib.rs @@ -0,0 +1,201 @@ +#![cfg_attr(not(feature = "std"), no_std)] +#![feature(min_specialization)] + +use ink_env::{DefaultEnvironment, Environment}; + +pub use crate::ticket_token::{BALANCE_OF_SELECTOR, TRANSFER_SELECTOR}; + +pub enum TicketEnvironment {} + +impl Environment for TicketEnvironment { + const MAX_EVENT_TOPICS: usize = ::MAX_EVENT_TOPICS; + + type AccountId = ::AccountId; + type Balance = u64; + type Hash = ::Hash; + type BlockNumber = u64; + type Timestamp = ::Timestamp; + type ChainExtension = ::ChainExtension; +} + +#[openbrush::contract] +pub mod ticket_token { + use access_control::{traits::AccessControlled, Role, ACCESS_CONTROL_PUBKEY}; + use ink_env::Error as InkEnvError; + use ink_lang::{ + codegen::{EmitEvent, Env}, + reflect::ContractEventBase, + }; + use ink_prelude::format; + use ink_storage::traits::SpreadAllocate; + use openbrush::{contracts::psp22::*, traits::Storage}; + + pub const BALANCE_OF_SELECTOR: [u8; 4] = [0x65, 0x68, 0x38, 0x2f]; + pub const TRANSFER_SELECTOR: [u8; 4] = [0xdb, 0x20, 0xf9, 0xf5]; + + #[ink(storage)] + #[derive(Default, SpreadAllocate, Storage)] + pub struct TicketToken { + #[storage_field] + psp22: psp22::Data, + /// access control contract + access_control: AccountId, + } + + impl PSP22 for TicketToken {} + + // emit events + // https://github.com/w3f/PSPs/blob/master/PSPs/psp-22.md + impl Internal for TicketToken { + fn _emit_transfer_event( + &self, + _from: Option, + _to: Option, + _amount: Balance, + ) { + TicketToken::emit_event( + self.env(), + Event::Transfer(Transfer { + from: _from, + to: _to, + value: _amount, + }), + ); + } + + fn _emit_approval_event(&self, _owner: AccountId, _spender: AccountId, _amount: Balance) { + TicketToken::emit_event( + self.env(), + Event::Approval(Approval { + owner: _owner, + spender: _spender, + value: _amount, + }), + ); + } + } + + impl AccessControlled for TicketToken { + type ContractError = PSP22Error; + } + + /// Result type + pub type Result = core::result::Result; + /// Event type + pub type Event = ::Type; + + /// Event emitted when a token transfer occurs. + #[ink(event)] + #[derive(Debug)] + pub struct Transfer { + #[ink(topic)] + pub from: Option, + #[ink(topic)] + pub to: Option, + pub value: Balance, + } + + /// Event emitted when an approval occurs that `spender` is allowed to withdraw + /// up to the amount of `value` tokens from `owner`. + #[ink(event)] + #[derive(Debug)] + pub struct Approval { + #[ink(topic)] + owner: AccountId, + #[ink(topic)] + spender: AccountId, + value: Balance, + } + + impl TicketToken { + /// Creates a new contract with the specified initial supply. + /// + /// Will revert if called from an account without a proper role + #[ink(constructor)] + pub fn new(total_supply: Balance) -> Self { + let caller = Self::env().caller(); + let code_hash = Self::env() + .own_code_hash() + .expect("Called new on a contract with no code hash"); + let required_role = Role::Initializer(code_hash); + + let role_check = ::check_role( + AccountId::from(ACCESS_CONTROL_PUBKEY), + caller, + required_role, + |why: InkEnvError| { + PSP22Error::Custom(format!("Calling access control has failed: {:?}", why)) + }, + |role: Role| PSP22Error::Custom(format!("MissingRole:{:?}", role)), + ); + + match role_check { + Ok(_) => ink_lang::codegen::initialize_contract(|instance: &mut TicketToken| { + instance + ._mint(instance.env().caller(), total_supply) + .expect("Should mint"); + + instance.access_control = AccountId::from(ACCESS_CONTROL_PUBKEY); + }), + Err(why) => panic!("Could not initialize the contract {:?}", why), + } + } + + pub fn emit_event>(emitter: EE, event: Event) { + emitter.emit_event(event); + } + + /// Terminates the contract. + /// + /// can only be called by the contract's Owner + #[ink(message, selector = 7)] + pub fn terminate(&mut self) -> Result<()> { + let caller = self.env().caller(); + let this = self.env().account_id(); + let required_role = Role::Owner(this); + + self.check_role(caller, required_role)?; + self.env().terminate_contract(caller) + } + + /// Returns the contract's access control contract address + #[ink(message, selector = 8)] + pub fn access_control(&self) -> AccountId { + self.access_control + } + + fn check_role(&self, account: AccountId, role: Role) -> Result<()> { + ::check_role( + self.access_control, + account, + role, + |why: InkEnvError| { + PSP22Error::Custom(format!("Calling access control has failed: {:?}", why)) + }, + |role: Role| PSP22Error::Custom(format!("MissingRole:{:?}", role)), + ) + } + + /// Sets new access control contract address + /// + /// Can only be called by the contract's Owner + #[ink(message, selector = 9)] + pub fn set_access_control(&mut self, access_control: AccountId) -> Result<()> { + let caller = self.env().caller(); + let this = self.env().account_id(); + let required_role = Role::Owner(this); + + self.check_role(caller, required_role)?; + self.access_control = access_control; + Ok(()) + } + + /// Returns own code hash + #[ink(message, selector = 10)] + pub fn code_hash(&self) -> Result { + Self::env() + .own_code_hash() + .map_err(|why| PSP22Error::Custom(format!("Calling control has failed: {:?}", why))) + } + } +} From a09d3729ee0638eab54f4e554c9f873c3c012792 Mon Sep 17 00:00:00 2001 From: filip Date: Tue, 9 Aug 2022 13:48:32 +0200 Subject: [PATCH 02/29] throw i fnot One --- contracts/ticket_token/lib.rs | 59 +++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 24 deletions(-) diff --git a/contracts/ticket_token/lib.rs b/contracts/ticket_token/lib.rs index fe55ae76c9..d4f93f2efe 100644 --- a/contracts/ticket_token/lib.rs +++ b/contracts/ticket_token/lib.rs @@ -1,23 +1,8 @@ #![cfg_attr(not(feature = "std"), no_std)] #![feature(min_specialization)] -use ink_env::{DefaultEnvironment, Environment}; - pub use crate::ticket_token::{BALANCE_OF_SELECTOR, TRANSFER_SELECTOR}; -pub enum TicketEnvironment {} - -impl Environment for TicketEnvironment { - const MAX_EVENT_TOPICS: usize = ::MAX_EVENT_TOPICS; - - type AccountId = ::AccountId; - type Balance = u64; - type Hash = ::Hash; - type BlockNumber = u64; - type Timestamp = ::Timestamp; - type ChainExtension = ::ChainExtension; -} - #[openbrush::contract] pub mod ticket_token { use access_control::{traits::AccessControlled, Role, ACCESS_CONTROL_PUBKEY}; @@ -26,9 +11,13 @@ pub mod ticket_token { codegen::{EmitEvent, Env}, reflect::ContractEventBase, }; - use ink_prelude::format; + use ink_prelude::{format, string::String}; use ink_storage::traits::SpreadAllocate; - use openbrush::{contracts::psp22::*, traits::Storage}; + // use num_traits::identities::One; + use openbrush::{ + contracts::psp22::{extensions::metadata::*, Internal, Transfer}, + traits::Storage, + }; pub const BALANCE_OF_SELECTOR: [u8; 4] = [0x65, 0x68, 0x38, 0x2f]; pub const TRANSFER_SELECTOR: [u8; 4] = [0xdb, 0x20, 0xf9, 0xf5]; @@ -38,14 +27,33 @@ pub mod ticket_token { pub struct TicketToken { #[storage_field] psp22: psp22::Data, + #[storage_field] + metadata: metadata::Data, /// access control contract access_control: AccountId, } impl PSP22 for TicketToken {} - // emit events - // https://github.com/w3f/PSPs/blob/master/PSPs/psp-22.md + impl PSP22Metadata for TicketToken {} + + impl Transfer for TicketToken { + fn _before_token_transfer( + &mut self, + _from: Option<&AccountId>, + _to: Option<&AccountId>, + amount: &Balance, + ) -> core::result::Result<(), PSP22Error> { + if !amount.eq(&1u128) { + return Err(PSP22Error::Custom(String::from( + "Only single ticket can be transfered at once", + ))); + } + + Ok(()) + } + } + impl Internal for TicketToken { fn _emit_transfer_event( &self, @@ -55,7 +63,7 @@ pub mod ticket_token { ) { TicketToken::emit_event( self.env(), - Event::Transfer(Transfer { + Event::TransferEvent(TransferEvent { from: _from, to: _to, value: _amount, @@ -87,7 +95,7 @@ pub mod ticket_token { /// Event emitted when a token transfer occurs. #[ink(event)] #[derive(Debug)] - pub struct Transfer { + pub struct TransferEvent { #[ink(topic)] pub from: Option, #[ink(topic)] @@ -112,7 +120,7 @@ pub mod ticket_token { /// /// Will revert if called from an account without a proper role #[ink(constructor)] - pub fn new(total_supply: Balance) -> Self { + pub fn new(name: String, symbol: String, total_supply: Balance) -> Self { let caller = Self::env().caller(); let code_hash = Self::env() .own_code_hash() @@ -131,11 +139,14 @@ pub mod ticket_token { match role_check { Ok(_) => ink_lang::codegen::initialize_contract(|instance: &mut TicketToken| { + instance.access_control = AccountId::from(ACCESS_CONTROL_PUBKEY); + instance.metadata.name = Some(name); + instance.metadata.symbol = Some(symbol); + instance.metadata.decimals = 0; + instance ._mint(instance.env().caller(), total_supply) .expect("Should mint"); - - instance.access_control = AccountId::from(ACCESS_CONTROL_PUBKEY); }), Err(why) => panic!("Could not initialize the contract {:?}", why), } From d9d9f6622f81ea924549e50c799210ab6abeaa58 Mon Sep 17 00:00:00 2001 From: filip Date: Tue, 9 Aug 2022 15:27:50 +0200 Subject: [PATCH 03/29] wip : reworking base lib --- contracts/button/lib.rs | 290 ++++++++++++++-------------------- contracts/ticket_token/lib.rs | 1 - 2 files changed, 117 insertions(+), 174 deletions(-) diff --git a/contracts/button/lib.rs b/contracts/button/lib.rs index 2fa75c207b..22e870bb0d 100644 --- a/contracts/button/lib.rs +++ b/contracts/button/lib.rs @@ -8,16 +8,11 @@ use ink_env::{ AccountId, DefaultEnvironment, Environment, Error as InkEnvError, }; use ink_lang as ink; -use ink_prelude::{format, string::String, vec, vec::Vec}; -use ink_storage::{ - traits::{SpreadAllocate, SpreadLayout}, - Mapping, -}; +use ink_prelude::{format, string::String, vec}; +use ink_storage::traits::{SpreadAllocate, SpreadLayout}; use openbrush::contracts::psp22::PSP22Error; pub type BlockNumber = ::BlockNumber; -// scores are denominated in block numbers -pub type Score = BlockNumber; pub type Balance = ::Balance; pub type ButtonResult = core::result::Result; @@ -38,20 +33,12 @@ impl Environment for ButtonGameEnvironment { #[derive(Debug, PartialEq, Eq, scale::Encode, scale::Decode)] #[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] pub enum GameError { - /// Returned if given account already pressed The Button - AlreadyParticipated, - /// Returned if death is called before the deadline + /// Returned if reset is called before the deadline BeforeDeadline, /// Returned if button is pressed after the deadline AfterDeadline, - /// Returned if given accunt has already had its reward paid out - AlreadyCLaimed, - /// Account not whitelisted to play - NotWhitelisted, /// Returned if a call is made from an account with missing access control priviledges MissingRole(String), - /// Returned whenever there was already a press tx recorded in this block - BetterLuckNextTime, /// Returned if a call to another contract has failed ContractCall(String), } @@ -135,20 +122,16 @@ impl From for GameError { pub struct ButtonData { /// How long does TheButton live for? pub button_lifetime: BlockNumber, - /// Stores a mapping between user accounts and the number of blocks they extended The Buttons life for - pub presses: Mapping, - /// stores total sum of user scores - pub total_scores: Score, /// stores the last account that pressed The Button pub last_presser: Option, /// block number of the last press pub last_press: Option, - /// AccountId of the ERC20 ButtonToken instance on-chain + /// counter for the number of presses + pub presses: u128, + /// AccountId of the PSP22 ButtonToken instance on-chain pub game_token: AccountId, - /// accounts whitelisted to play the game - pub can_play: Mapping, - /// stores a set of acounts that already collected their rewards - pub reward_claimed: Mapping, + /// Account ID of the ticket token + pub ticket_token: AccountId, /// access control contract pub access_control: AccountId, } @@ -168,7 +151,17 @@ pub trait ButtonGame { fn get_mut(&mut self) -> &mut ButtonData; /// Logic for calculating user score given the particular games rules - fn score(&self, now: BlockNumber) -> Score; + fn score(&self, now: BlockNumber) -> Balance; + + /// Logic for calculating pressiah score + /// + /// By defaul the pressiah score is defined as k * sqrt(k) + /// where k is the number of players that participated until the button has died + /// Can be overriden to some other custom calculation + fn pressiah_score(&self) -> Balance { + let presses = self.get().presses as f64; + (presses * f64::sqrt(presses)) as Balance + } fn is_dead(&self, now: BlockNumber) -> bool { now > self.deadline(now) @@ -183,18 +176,14 @@ pub trait ButtonGame { last_press.unwrap_or(now) + button_lifetime } - fn score_of(&self, user: AccountId) -> Score { - self.get().presses.get(&user).unwrap_or(0) - } - - fn can_play(&self, user: AccountId) -> bool { - self.get().can_play.get(&user).is_some() - } - fn access_control(&self) -> AccountId { self.get().access_control } + fn ticket_token(&self) -> AccountId { + self.get().ticket_token + } + fn set_access_control( &mut self, new_access_control: AccountId, @@ -218,18 +207,18 @@ pub trait ButtonGame { self.get().game_token } - fn balance(&self, balance_of_selector: [u8; 4], this: AccountId) -> ButtonResult - where - E: Environment, - { - let game_token = self.get().game_token; - let balance = build_call::() - .call_type(Call::new().callee(game_token)) - .exec_input(ExecutionInput::new(Selector::new(balance_of_selector)).push_arg(this)) - .returns::() - .fire()?; - Ok(balance) - } + // fn balance(&self, balance_of_selector: [u8; 4], this: AccountId) -> ButtonResult + // where + // E: Environment, + // { + // let game_token = self.get().game_token; + // let balance = build_call::() + // .call_type(Call::new().callee(game_token)) + // .exec_input(ExecutionInput::new(Selector::new(balance_of_selector)).push_arg(this)) + // .returns::() + // .fire()?; + // Ok(balance) + // } fn transfer_tx( &self, @@ -252,6 +241,26 @@ pub trait ButtonGame { .fire() } + fn mint_tx( + &self, + mint_to_selector: [u8; 4], + to: AccountId, + amount: Balance, + ) -> Result, InkEnvError> + where + E: Environment, + { + build_call::() + .call_type(Call::new().callee(self.get().game_token)) + .exec_input( + ExecutionInput::new(Selector::new(mint_to_selector)) + .push_arg(to) + .push_arg(amount), + ) + .returns::>() + .fire() + } + fn check_role(&self, account: AccountId, role: Role) -> ButtonResult<()> where Self: AccessControlled, @@ -267,114 +276,65 @@ pub trait ButtonGame { ) } - fn allow(&mut self, player: AccountId, caller: AccountId, this: AccountId) -> ButtonResult<()> - where - Self: AccessControlled, - { - let required_role = Role::Admin(this); - self.check_role(caller, required_role)?; - self.get_mut().can_play.insert(player, &()); - Ok(()) - } - - fn bulk_allow( - &mut self, - players: Vec, - caller: AccountId, - this: AccountId, - ) -> ButtonResult<()> - where - Self: AccessControlled, - { - let required_role = Role::Admin(this); - self.check_role(caller, required_role)?; - - for player in players { - self.get_mut().can_play.insert(player, &()); - } - Ok(()) - } - - fn disallow( + // TODO : transfer ticket token + fn press( &mut self, - player: AccountId, + transfer_selector: [u8; 4], + mint_to_selector: [u8; 4], + now: BlockNumber, caller: AccountId, this: AccountId, ) -> ButtonResult<()> where - Self: AccessControlled, + E: Environment, { - let required_role = Role::Admin(this); - self.check_role(caller, required_role)?; - self.get_mut().can_play.remove(&player); - Ok(()) - } - - // TODO : add nonce? - fn press(&mut self, now: BlockNumber, caller: AccountId) -> ButtonResult<()> { - let ButtonData { - can_play, - presses, - last_press, - .. - } = self.get(); - if self.is_dead(now) { return Err(GameError::AfterDeadline); } - if presses.get(&caller).is_some() { - return Err(GameError::AlreadyParticipated); - } + let ButtonData { presses, .. } = self.get(); - if can_play.get(&caller).is_none() { - return Err(GameError::NotWhitelisted); - } - - // TODO : instead of this? - // this is to handle a situation when multiple accounts press at the same time (in the same block) - // as there can be only one succesfull press recorded per block - // the users are effectively competing for this one tx - if let Some(last_press) = last_press { - if last_press.eq(&now) { - return Err(GameError::BetterLuckNextTime); - } - } + // transfers 1 ticket token to self + // tx will fail if user did not give allowance to the game contract + // or does not have enough balance + self.transfer_tx::(transfer_selector, this, 1)??; let root_key = ::ink_primitives::Key::from([0x00; 32]); let mut state = ::ink_storage::traits::pull_spread_root::(&root_key); let score = self.score(now); - state.presses.insert(&caller, &score); + // mints reward tokens to pay out the reward + // contract needs to have a Minter role on the reward token contract + self.mint_tx::(mint_to_selector, caller, score)??; + + state.presses = presses + 1; state.last_presser = Some(caller); state.last_press = Some(now); - state.total_scores += score; swap(self.get_mut(), &mut state); Ok(()) } - /// Pays award to a participant + /// Reset the game /// + /// Erases the storage and pays award to the Pressiah /// Can be called by any account on behalf of a player /// Can only be called after button's deadline - fn claim_reward( + fn reset( &mut self, now: BlockNumber, for_player: AccountId, - balance_of_selector: [u8; 4], + // balance_of_selector: [u8; 4], transfer_selector: [u8; 4], this: AccountId, - ) -> ButtonResult + ) -> ButtonResult<()> where E: Environment, { let ButtonData { - reward_claimed, - last_presser, presses, - total_scores, + last_presser, .. } = self.get(); @@ -382,38 +342,39 @@ pub trait ButtonGame { return Err(GameError::BeforeDeadline); } - if reward_claimed.get(&for_player).is_some() { - return Err(GameError::AlreadyCLaimed); - } + // if reward_claimed.get(&for_player).is_some() { + // return Err(GameError::AlreadyCLaimed); + // } - let mut total_rewards = 0; + // let mut total_rewards = 0; + // TODO match last_presser { - None => Ok(0), // there weren't any players + None => Ok(()), // there weren't any players Some(pressiah) => { - let total_balance = self.balance::(balance_of_selector, this)?; - let pressiah_reward = total_balance / 2; - let remaining_balance = total_balance - pressiah_reward; + // let total_balance = self.balance::(balance_of_selector, this)?; + // let pressiah_reward = total_balance / 2; + // let remaining_balance = total_balance - pressiah_reward; - if &for_player == pressiah { - // Pressiah gets 50% of supply - self.transfer_tx::(transfer_selector, *pressiah, pressiah_reward)??; - total_rewards += pressiah_reward; - } + // if &for_player == pressiah { + // // Pressiah gets 50% of supply + // self.transfer_tx::(transfer_selector, *pressiah, pressiah_reward)??; + // total_rewards += pressiah_reward; + // } - // NOTE: in this design the Pressiah gets *both* his/her reward *and* a reward for playing + // // NOTE: in this design the Pressiah gets *both* his/her reward *and* a reward for playing - if let Some(score) = presses.get(&for_player) { - // transfer reward proportional to the score - let reward = (score as u128 * remaining_balance) / *total_scores as u128; - self.transfer_tx::(transfer_selector, for_player, reward)??; + // if let Some(score) = presses.get(&for_player) { + // // transfer reward proportional to the score + // let reward = (score as u128 * remaining_balance) / *total_scores as u128; + // self.transfer_tx::(transfer_selector, for_player, reward)??; - // pressiah is also marked as having made the claim, because his/her score was recorder - self.get_mut().reward_claimed.insert(&for_player, &()); - total_rewards += reward; - } + // // pressiah is also marked as having made the claim, because his/her score was recorder + // self.get_mut().reward_claimed.insert(&for_player, &()); + // total_rewards += reward; + // } - Ok(total_rewards) + Ok(()) } } } @@ -429,9 +390,9 @@ pub trait IButtonGame { #[ink(message)] fn press(&mut self) -> ButtonResult<()>; - /// Pays out the award - #[ink(message)] - fn claim_reward(&mut self, for_player: AccountId) -> ButtonResult<()>; + // /// Pays out the award + // #[ink(message)] + // fn claim_reward(&mut self, for_player: AccountId) -> ButtonResult<()>; /// Returns the buttons status #[ink(message)] @@ -443,29 +404,30 @@ pub trait IButtonGame { #[ink(message)] fn deadline(&self) -> BlockNumber; - /// Returns the user score - #[ink(message)] - fn score_of(&self, user: AccountId) -> Score; - - /// Returns whether given account can play + /// Returns the current Pressiah #[ink(message)] - fn can_play(&self, user: AccountId) -> bool; + fn last_presser(&self) -> Option; /// Returns the current access control contract address #[ink(message)] fn access_control(&self) -> AccountId; - /// Returns the current Pressiah + /// Returns address of the game's reward token #[ink(message)] - fn last_presser(&self) -> Option; + fn game_token(&self) -> AccountId; - /// Returns address of the game's ERC20 token + /// Returns address of the game's ticket token #[ink(message)] - fn game_token(&self) -> AccountId; + fn ticket_token(&self) -> AccountId; + + // TODO : balance of ticket token + // /// Returns then game token balance of the game contract + // #[ink(message)] + // fn balance(&self) -> ButtonResult; - /// Returns then game token balance of the game contract + /// Resets the game #[ink(message)] - fn balance(&self) -> ButtonResult; + fn reset(&mut self) -> ButtonResult<()>; /// Sets new access control contract address /// @@ -474,24 +436,6 @@ pub trait IButtonGame { #[ink(message)] fn set_access_control(&mut self, access_control: AccountId) -> ButtonResult<()>; - /// Whitelists given AccountId to participate in the game - /// - /// Should only be called by the contracts Admin - #[ink(message)] - fn allow(&mut self, player: AccountId) -> ButtonResult<()>; - - /// Whitelists an array of accounts to participate in the game - /// - /// Should return an error if called by someone else but the Admin - #[ink(message)] - fn bulk_allow(&mut self, players: Vec) -> ButtonResult<()>; - - /// Blacklists given AccountId from participating in the game - /// - /// Should return an error if called by someone else but the Admin - #[ink(message)] - fn disallow(&mut self, player: AccountId) -> ButtonResult<()>; - /// Terminates the contract /// /// Should only be called by the contract Owner diff --git a/contracts/ticket_token/lib.rs b/contracts/ticket_token/lib.rs index d4f93f2efe..4caa888f25 100644 --- a/contracts/ticket_token/lib.rs +++ b/contracts/ticket_token/lib.rs @@ -13,7 +13,6 @@ pub mod ticket_token { }; use ink_prelude::{format, string::String}; use ink_storage::traits::SpreadAllocate; - // use num_traits::identities::One; use openbrush::{ contracts::psp22::{extensions::metadata::*, Internal, Transfer}, traits::Storage, From 72d77b56cc7b6cfe94d33f6f167217653f9e4d56 Mon Sep 17 00:00:00 2001 From: filip Date: Tue, 9 Aug 2022 17:19:31 +0200 Subject: [PATCH 04/29] wip --- contracts/back_to_the_future/lib.rs | 9 +- contracts/button/Cargo.lock | 68 ++++++++++++ contracts/button/Cargo.toml | 2 + contracts/button/lib.rs | 112 +++++++------------ contracts/early_bird_special/Cargo.lock | 68 ++++++++++++ contracts/early_bird_special/lib.rs | 140 ++++++++---------------- contracts/game_token/lib.rs | 4 +- 7 files changed, 227 insertions(+), 176 deletions(-) diff --git a/contracts/back_to_the_future/lib.rs b/contracts/back_to_the_future/lib.rs index 1fa4f45e43..2032e48f6a 100755 --- a/contracts/back_to_the_future/lib.rs +++ b/contracts/back_to_the_future/lib.rs @@ -13,9 +13,9 @@ mod back_to_the_future { use access_control::{traits::AccessControlled, Role, ACCESS_CONTROL_PUBKEY}; use button::{ - ButtonData, ButtonGame, ButtonGameEnvironment, ButtonResult, GameError, IButtonGame, Score, + ButtonData, ButtonGame, ButtonGameEnvironment, ButtonResult, GameError, IButtonGame, }; - use game_token::{BALANCE_OF_SELECTOR, TRANSFER_SELECTOR}; + use game_token::{BALANCE_OF_SELECTOR, MINT_TO_SELECTOR, TRANSFER_SELECTOR}; use ink_env::Error as InkEnvError; use ink_lang::{ codegen::{initialize_contract, EmitEvent}, @@ -88,7 +88,7 @@ mod back_to_the_future { &mut self.data } - fn score(&self, now: BlockNumber) -> Score { + fn score(&self, now: BlockNumber) -> Balance { if let Some(last_press) = self.get().last_press { return now - last_press; } @@ -108,7 +108,7 @@ mod back_to_the_future { fn press(&mut self) -> ButtonResult<()> { let caller = self.env().caller(); let now = Self::env().block_number(); - ButtonGame::press(self, now, caller)?; + ButtonGame::press(self, TRANSFER_SELECTOR, MINT_TO_SELECTOR, now, caller, this)?; Self::emit_event( self.env(), Event::ButtonPressed(ButtonPressed { @@ -270,6 +270,7 @@ mod back_to_the_future { self.data.access_control = AccountId::from(ACCESS_CONTROL_PUBKEY); self.data.button_lifetime = button_lifetime; self.data.game_token = game_token; + self.data.last_press = Some(now); Self::emit_event( Self::env(), diff --git a/contracts/button/Cargo.lock b/contracts/button/Cargo.lock index 54af630597..de2c0f797a 100644 --- a/contracts/button/Cargo.lock +++ b/contracts/button/Cargo.lock @@ -90,6 +90,7 @@ dependencies = [ "ink_prelude", "ink_primitives", "ink_storage", + "num", "openbrush", "parity-scale-codec", "scale-info", @@ -509,6 +510,73 @@ version = "0.2.126" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836" +[[package]] +name = "num" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43db66d1170d347f9a065114077f7dccb00c1b9478c89384490a3425279a4606" +dependencies = [ + "num-bigint", + "num-complex", + "num-integer", + "num-iter", + "num-rational", + "num-traits", +] + +[[package]] +name = "num-bigint" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f93ab6289c7b344a8a9f60f88d80aa20032336fe78da341afc91c8a2341fc75f" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-complex" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ae39348c8bc5fbd7f40c727a9925f03517afd2ab27d46702108b6a7e5414c19" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-integer" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-iter" +version = "0.1.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d03e6c028c5dc5cac6e2dec0efda81fc887605bb3d884578bb6d6bf7514e252" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-rational" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0" +dependencies = [ + "autocfg", + "num-bigint", + "num-integer", + "num-traits", +] + [[package]] name = "num-traits" version = "0.2.15" diff --git a/contracts/button/Cargo.toml b/contracts/button/Cargo.toml index 2d8484c2ad..e29c57b626 100644 --- a/contracts/button/Cargo.toml +++ b/contracts/button/Cargo.toml @@ -17,6 +17,7 @@ scale-info = { version = "2", default-features = false, features = ["derive"], o access_control = { path = "../access_control", default-features = false, features = ["ink-as-dependency"] } openbrush = { git = "https://github.com/Supercolony-net/openbrush-contracts.git", rev = "8a20f95", default-features = false, features = ["psp22"] } +num = {version = "0.4.0", default-features = false} [lib] name = "button" @@ -36,5 +37,6 @@ std = [ "ink_storage/std", "scale-info/std", "scale/std", + "num/std" ] ink-as-dependency = [] diff --git a/contracts/button/lib.rs b/contracts/button/lib.rs index 22e870bb0d..26e392be68 100644 --- a/contracts/button/lib.rs +++ b/contracts/button/lib.rs @@ -129,7 +129,7 @@ pub struct ButtonData { /// counter for the number of presses pub presses: u128, /// AccountId of the PSP22 ButtonToken instance on-chain - pub game_token: AccountId, + pub reward_token: AccountId, /// Account ID of the ticket token pub ticket_token: AccountId, /// access control contract @@ -159,8 +159,8 @@ pub trait ButtonGame { /// where k is the number of players that participated until the button has died /// Can be overriden to some other custom calculation fn pressiah_score(&self) -> Balance { - let presses = self.get().presses as f64; - (presses * f64::sqrt(presses)) as Balance + let presses = self.get().presses; + (presses * num::integer::sqrt(presses)) as Balance } fn is_dead(&self, now: BlockNumber) -> bool { @@ -203,22 +203,22 @@ pub trait ButtonGame { self.get().last_presser } - fn game_token(&self) -> AccountId { - self.get().game_token + fn reward_token(&self) -> AccountId { + self.get().reward_token } - // fn balance(&self, balance_of_selector: [u8; 4], this: AccountId) -> ButtonResult - // where - // E: Environment, - // { - // let game_token = self.get().game_token; - // let balance = build_call::() - // .call_type(Call::new().callee(game_token)) - // .exec_input(ExecutionInput::new(Selector::new(balance_of_selector)).push_arg(this)) - // .returns::() - // .fire()?; - // Ok(balance) - // } + fn balance(&self, balance_of_selector: [u8; 4], this: AccountId) -> ButtonResult + where + E: Environment, + { + let ticket_token = self.get().ticket_token; + let balance = build_call::() + .call_type(Call::new().callee(ticket_token)) + .exec_input(ExecutionInput::new(Selector::new(balance_of_selector)).push_arg(this)) + .returns::() + .fire()?; + Ok(balance) + } fn transfer_tx( &self, @@ -230,7 +230,7 @@ pub trait ButtonGame { E: Environment, { build_call::() - .call_type(Call::new().callee(self.get().game_token)) + .call_type(Call::new().callee(self.get().reward_token)) .exec_input( ExecutionInput::new(Selector::new(transfer_selector)) .push_arg(to) @@ -251,7 +251,7 @@ pub trait ButtonGame { E: Environment, { build_call::() - .call_type(Call::new().callee(self.get().game_token)) + .call_type(Call::new().callee(self.get().reward_token)) .exec_input( ExecutionInput::new(Selector::new(mint_to_selector)) .push_arg(to) @@ -276,7 +276,6 @@ pub trait ButtonGame { ) } - // TODO : transfer ticket token fn press( &mut self, transfer_selector: [u8; 4], @@ -311,6 +310,7 @@ pub trait ButtonGame { state.presses = presses + 1; state.last_presser = Some(caller); state.last_press = Some(now); + swap(self.get_mut(), &mut state); Ok(()) @@ -321,62 +321,31 @@ pub trait ButtonGame { /// Erases the storage and pays award to the Pressiah /// Can be called by any account on behalf of a player /// Can only be called after button's deadline - fn reset( - &mut self, - now: BlockNumber, - for_player: AccountId, - // balance_of_selector: [u8; 4], - transfer_selector: [u8; 4], - this: AccountId, - ) -> ButtonResult<()> + fn reset(&mut self, now: BlockNumber, mint_to_selector: [u8; 4]) -> ButtonResult<()> where E: Environment, { - let ButtonData { - presses, - last_presser, - .. - } = self.get(); + let ButtonData { last_presser, .. } = self.get(); if !self.is_dead(now) { return Err(GameError::BeforeDeadline); } - // if reward_claimed.get(&for_player).is_some() { - // return Err(GameError::AlreadyCLaimed); - // } + if let Some(pressiah) = last_presser { + let reward = self.pressiah_score(); + self.mint_tx::(mint_to_selector, *pressiah, reward)??; + }; - // let mut total_rewards = 0; - - // TODO - match last_presser { - None => Ok(()), // there weren't any players - Some(pressiah) => { - // let total_balance = self.balance::(balance_of_selector, this)?; - // let pressiah_reward = total_balance / 2; - // let remaining_balance = total_balance - pressiah_reward; - - // if &for_player == pressiah { - // // Pressiah gets 50% of supply - // self.transfer_tx::(transfer_selector, *pressiah, pressiah_reward)??; - // total_rewards += pressiah_reward; - // } - - // // NOTE: in this design the Pressiah gets *both* his/her reward *and* a reward for playing - - // if let Some(score) = presses.get(&for_player) { - // // transfer reward proportional to the score - // let reward = (score as u128 * remaining_balance) / *total_scores as u128; - // self.transfer_tx::(transfer_selector, for_player, reward)??; + // zero the counters in storage + let root_key = ::ink_primitives::Key::from([0x00; 32]); + let mut state = ::ink_storage::traits::pull_spread_root::(&root_key); - // // pressiah is also marked as having made the claim, because his/her score was recorder - // self.get_mut().reward_claimed.insert(&for_player, &()); - // total_rewards += reward; - // } + state.presses = 0; + state.last_presser = None; + state.last_press = None; + swap(self.get_mut(), &mut state); - Ok(()) - } - } + Ok(()) } } @@ -390,10 +359,6 @@ pub trait IButtonGame { #[ink(message)] fn press(&mut self) -> ButtonResult<()>; - // /// Pays out the award - // #[ink(message)] - // fn claim_reward(&mut self, for_player: AccountId) -> ButtonResult<()>; - /// Returns the buttons status #[ink(message)] fn is_dead(&self) -> bool; @@ -414,16 +379,15 @@ pub trait IButtonGame { /// Returns address of the game's reward token #[ink(message)] - fn game_token(&self) -> AccountId; + fn reward_token(&self) -> AccountId; /// Returns address of the game's ticket token #[ink(message)] fn ticket_token(&self) -> AccountId; - // TODO : balance of ticket token - // /// Returns then game token balance of the game contract - // #[ink(message)] - // fn balance(&self) -> ButtonResult; + /// Returns then number of ticket tokens in the game contract + #[ink(message)] + fn balance(&self) -> ButtonResult; /// Resets the game #[ink(message)] diff --git a/contracts/early_bird_special/Cargo.lock b/contracts/early_bird_special/Cargo.lock index bab1895693..4b6d5dcc84 100644 --- a/contracts/early_bird_special/Cargo.lock +++ b/contracts/early_bird_special/Cargo.lock @@ -90,6 +90,7 @@ dependencies = [ "ink_prelude", "ink_primitives", "ink_storage", + "num", "openbrush", "parity-scale-codec", "scale-info", @@ -543,6 +544,73 @@ version = "0.2.126" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836" +[[package]] +name = "num" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43db66d1170d347f9a065114077f7dccb00c1b9478c89384490a3425279a4606" +dependencies = [ + "num-bigint", + "num-complex", + "num-integer", + "num-iter", + "num-rational", + "num-traits", +] + +[[package]] +name = "num-bigint" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f93ab6289c7b344a8a9f60f88d80aa20032336fe78da341afc91c8a2341fc75f" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-complex" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ae39348c8bc5fbd7f40c727a9925f03517afd2ab27d46702108b6a7e5414c19" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-integer" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-iter" +version = "0.1.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d03e6c028c5dc5cac6e2dec0efda81fc887605bb3d884578bb6d6bf7514e252" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-rational" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0" +dependencies = [ + "autocfg", + "num-bigint", + "num-integer", + "num-traits", +] + [[package]] name = "num-traits" version = "0.2.15" diff --git a/contracts/early_bird_special/lib.rs b/contracts/early_bird_special/lib.rs index 3a90ad9411..492ba22fff 100644 --- a/contracts/early_bird_special/lib.rs +++ b/contracts/early_bird_special/lib.rs @@ -13,9 +13,9 @@ mod early_bird_special { use access_control::{traits::AccessControlled, Role, ACCESS_CONTROL_PUBKEY}; use button::{ - ButtonData, ButtonGame, ButtonGameEnvironment, ButtonResult, GameError, IButtonGame, Score, + ButtonData, ButtonGame, ButtonGameEnvironment, ButtonResult, GameError, IButtonGame, }; - use game_token::{BALANCE_OF_SELECTOR, TRANSFER_SELECTOR}; + use game_token::{BALANCE_OF_SELECTOR, MINT_TO_SELECTOR, TRANSFER_SELECTOR}; use ink_env::Error as InkEnvError; use ink_lang::{ codegen::{initialize_contract, EmitEvent}, @@ -32,27 +32,13 @@ mod early_bird_special { #[derive(Debug)] pub struct ButtonCreated { #[ink(topic)] - game_token: AccountId, + reward_token: AccountId, + #[ink(topic)] + ticket_token: AccountId, start: BlockNumber, deadline: BlockNumber, } - /// Event emitted when account is whitelisted to play the game - #[ink(event)] - #[derive(Debug)] - pub struct AccountWhitelisted { - #[ink(topic)] - player: AccountId, - } - - /// Event emitted when account is blacklisted from playing the game - #[ink(event)] - #[derive(Debug)] - pub struct AccountBlacklisted { - #[ink(topic)] - player: AccountId, - } - /// Event emitted when TheButton is pressed #[ink(event)] #[derive(Debug)] @@ -65,9 +51,8 @@ mod early_bird_special { /// Event emitted when a players reward is claimed #[ink(event)] #[derive(Debug)] - pub struct RewardClaimed { - for_player: AccountId, - rewards: u128, + pub struct GameReset { + when: u64, } #[ink(storage)] @@ -89,9 +74,9 @@ mod early_bird_special { &mut self.data } - fn score(&self, now: BlockNumber) -> Score { + fn score(&self, now: BlockNumber) -> Balance { let deadline = ButtonGame::deadline(self, now); - deadline - now + (deadline - now) as Balance } } @@ -107,7 +92,15 @@ mod early_bird_special { fn press(&mut self) -> ButtonResult<()> { let caller = self.env().caller(); let now = Self::env().block_number(); - ButtonGame::press(self, now, caller)?; + let this = self.env().account_id(); + ButtonGame::press::( + self, + TRANSFER_SELECTOR, + MINT_TO_SELECTOR, + now, + caller, + this, + )?; Self::emit_event( self.env(), Event::ButtonPressed(ButtonPressed { @@ -119,26 +112,12 @@ mod early_bird_special { } #[ink(message)] - fn claim_reward(&mut self, for_player: AccountId) -> ButtonResult<()> { - let this = self.env().account_id(); + fn reset(&mut self) -> ButtonResult<()> { let now = Self::env().block_number(); - let rewards = ButtonGame::claim_reward::( - self, - now, - for_player, - BALANCE_OF_SELECTOR, - TRANSFER_SELECTOR, - this, - )?; + ButtonGame::reset::(self, now, MINT_TO_SELECTOR)?; - Self::emit_event( - self.env(), - Event::RewardClaimed(RewardClaimed { - for_player, - rewards, - }), - ); + Self::emit_event(self.env(), Event::GameReset(GameReset { when: now })); Ok(()) } @@ -148,16 +127,6 @@ mod early_bird_special { ButtonGame::deadline(self, now) } - #[ink(message)] - fn score_of(&self, user: AccountId) -> Score { - ButtonGame::score_of(self, user) - } - - #[ink(message)] - fn can_play(&self, user: AccountId) -> bool { - ButtonGame::can_play(self, user) - } - #[ink(message)] fn access_control(&self) -> AccountId { ButtonGame::access_control(self) @@ -169,8 +138,13 @@ mod early_bird_special { } #[ink(message)] - fn game_token(&self) -> AccountId { - ButtonGame::game_token(self) + fn reward_token(&self) -> AccountId { + ButtonGame::reward_token(self) + } + + #[ink(message)] + fn ticket_token(&self) -> AccountId { + ButtonGame::ticket_token(self) } #[ink(message)] @@ -186,44 +160,6 @@ mod early_bird_special { ButtonGame::set_access_control(self, new_access_control, caller, this) } - #[ink(message)] - fn allow(&mut self, player: AccountId) -> ButtonResult<()> { - let caller = self.env().caller(); - let this = self.env().account_id(); - ButtonGame::allow(self, player, caller, this)?; - Self::emit_event( - self.env(), - Event::AccountWhitelisted(AccountWhitelisted { player }), - ); - Ok(()) - } - - #[ink(message)] - fn bulk_allow(&mut self, players: Vec) -> ButtonResult<()> { - let caller = self.env().caller(); - let this = self.env().account_id(); - ButtonGame::bulk_allow(self, players.clone(), caller, this)?; - for player in players { - Self::emit_event( - self.env(), - Event::AccountWhitelisted(AccountWhitelisted { player }), - ); - } - Ok(()) - } - - #[ink(message)] - fn disallow(&mut self, player: AccountId) -> ButtonResult<()> { - let caller = self.env().caller(); - let this = self.env().account_id(); - ButtonGame::disallow(self, player, caller, this)?; - Self::emit_event( - self.env(), - Event::AccountBlacklisted(AccountBlacklisted { player }), - ); - Ok(()) - } - #[ink(message)] fn terminate(&mut self) -> ButtonResult<()> { let caller = self.env().caller(); @@ -236,7 +172,11 @@ mod early_bird_special { impl EarlyBirdSpecial { #[ink(constructor)] - pub fn new(game_token: AccountId, button_lifetime: BlockNumber) -> Self { + pub fn new( + ticket_token: AccountId, + reward_token: AccountId, + button_lifetime: BlockNumber, + ) -> Self { let caller = Self::env().caller(); let code_hash = Self::env() .own_code_hash() @@ -256,26 +196,32 @@ mod early_bird_special { match role_check { Ok(_) => initialize_contract(|contract| { - Self::new_init(contract, game_token, button_lifetime) + Self::new_init(contract, ticket_token, reward_token, button_lifetime) }), Err(why) => panic!("Could not initialize the contract {:?}", why), } } - fn new_init(&mut self, game_token: AccountId, button_lifetime: BlockNumber) { + fn new_init( + &mut self, + ticket_token: AccountId, + reward_token: AccountId, + button_lifetime: BlockNumber, + ) { let now = Self::env().block_number(); let deadline = now + button_lifetime; self.data.access_control = AccountId::from(ACCESS_CONTROL_PUBKEY); self.data.button_lifetime = button_lifetime; - self.data.game_token = game_token; + self.data.reward_token = reward_token; Self::emit_event( Self::env(), Event::ButtonCreated(ButtonCreated { start: now, deadline, - game_token, + ticket_token, + reward_token, }), ) } diff --git a/contracts/game_token/lib.rs b/contracts/game_token/lib.rs index bce0e999bc..0f373bcb0c 100644 --- a/contracts/game_token/lib.rs +++ b/contracts/game_token/lib.rs @@ -1,7 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] #![feature(min_specialization)] -pub use crate::game_token::{BALANCE_OF_SELECTOR, TRANSFER_SELECTOR}; +pub use crate::game_token::{BALANCE_OF_SELECTOR, MINT_TO_SELECTOR, TRANSFER_SELECTOR}; #[openbrush::contract] pub mod game_token { @@ -20,6 +20,8 @@ pub mod game_token { pub const BALANCE_OF_SELECTOR: [u8; 4] = [0x65, 0x68, 0x38, 0x2f]; pub const TRANSFER_SELECTOR: [u8; 4] = [0xdb, 0x20, 0xf9, 0xf5]; + // TODO : use correct selector when mint/burn is implemented + pub const MINT_TO_SELECTOR: [u8; 4] = [0x0, 0x0, 0x0, 0x0]; #[ink(storage)] #[derive(Default, SpreadAllocate, Storage)] From 1429c38187362a2707e5c68c9bd0ffc06131b14e Mon Sep 17 00:00:00 2001 From: filip Date: Tue, 9 Aug 2022 17:55:34 +0200 Subject: [PATCH 05/29] wip : altering deploy script --- contracts/back_to_the_future/Cargo.lock | 85 ++++++++++++++++ contracts/back_to_the_future/Cargo.toml | 2 + contracts/back_to_the_future/lib.rs | 127 +++++++++--------------- contracts/button/lib.rs | 3 +- contracts/early_bird_special/Cargo.lock | 17 ++++ contracts/early_bird_special/Cargo.toml | 2 + contracts/early_bird_special/lib.rs | 8 +- contracts/scripts/deploy.sh | 30 +++++- 8 files changed, 186 insertions(+), 88 deletions(-) diff --git a/contracts/back_to_the_future/Cargo.lock b/contracts/back_to_the_future/Cargo.lock index 10f4cc0b3c..bedf0e55d3 100644 --- a/contracts/back_to_the_future/Cargo.lock +++ b/contracts/back_to_the_future/Cargo.lock @@ -54,6 +54,7 @@ dependencies = [ "ink_storage", "parity-scale-codec", "scale-info", + "ticket_token", ] [[package]] @@ -108,6 +109,7 @@ dependencies = [ "ink_prelude", "ink_primitives", "ink_storage", + "num", "openbrush", "parity-scale-codec", "scale-info", @@ -543,6 +545,73 @@ version = "0.2.126" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836" +[[package]] +name = "num" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43db66d1170d347f9a065114077f7dccb00c1b9478c89384490a3425279a4606" +dependencies = [ + "num-bigint", + "num-complex", + "num-integer", + "num-iter", + "num-rational", + "num-traits", +] + +[[package]] +name = "num-bigint" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f93ab6289c7b344a8a9f60f88d80aa20032336fe78da341afc91c8a2341fc75f" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-complex" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ae39348c8bc5fbd7f40c727a9925f03517afd2ab27d46702108b6a7e5414c19" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-integer" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-iter" +version = "0.1.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d03e6c028c5dc5cac6e2dec0efda81fc887605bb3d884578bb6d6bf7514e252" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-rational" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0" +dependencies = [ + "autocfg", + "num-bigint", + "num-integer", + "num-traits", +] + [[package]] name = "num-traits" version = "0.2.15" @@ -943,6 +1012,22 @@ dependencies = [ "syn", ] +[[package]] +name = "ticket_token" +version = "2.1.0" +dependencies = [ + "access_control", + "ink_env", + "ink_lang", + "ink_metadata", + "ink_prelude", + "ink_primitives", + "ink_storage", + "openbrush", + "parity-scale-codec", + "scale-info", +] + [[package]] name = "toml" version = "0.5.9" diff --git a/contracts/back_to_the_future/Cargo.toml b/contracts/back_to_the_future/Cargo.toml index 3c8383bd9f..4a5ab2c25a 100644 --- a/contracts/back_to_the_future/Cargo.toml +++ b/contracts/back_to_the_future/Cargo.toml @@ -19,6 +19,7 @@ scale-info = { version = "2", default-features = false, features = ["derive"], o button = { path = "../button", default-features = false } game_token = { path = "../game_token", default-features = false, features = ["ink-as-dependency"] } +ticket_token = { path = "../ticket_token", default-features = false, features = ["ink-as-dependency"] } access_control = { path = "../access_control", default-features = false, features = ["ink-as-dependency"] } [lib] @@ -34,6 +35,7 @@ default = ["std"] std = [ "button/std", "game_token/std", + "ticket_token/std", "ink_env/std", "ink_lang_codegen/std", "ink_metadata/std", diff --git a/contracts/back_to_the_future/lib.rs b/contracts/back_to_the_future/lib.rs index 2032e48f6a..b7e307f8f3 100755 --- a/contracts/back_to_the_future/lib.rs +++ b/contracts/back_to_the_future/lib.rs @@ -15,14 +15,15 @@ mod back_to_the_future { use button::{ ButtonData, ButtonGame, ButtonGameEnvironment, ButtonResult, GameError, IButtonGame, }; - use game_token::{BALANCE_OF_SELECTOR, MINT_TO_SELECTOR, TRANSFER_SELECTOR}; + use game_token::MINT_TO_SELECTOR; use ink_env::Error as InkEnvError; use ink_lang::{ codegen::{initialize_contract, EmitEvent}, reflect::ContractEventBase, }; - use ink_prelude::{format, vec::Vec}; + use ink_prelude::format; use ink_storage::traits::SpreadAllocate; + use ticket_token::{BALANCE_OF_SELECTOR, TRANSFER_SELECTOR}; type Event = ::Type; @@ -31,7 +32,8 @@ mod back_to_the_future { #[derive(Debug)] pub struct ButtonCreated { #[ink(topic)] - game_token: AccountId, + reward_token: AccountId, + ticket_token: AccountId, start: BlockNumber, deadline: BlockNumber, } @@ -64,9 +66,8 @@ mod back_to_the_future { /// Event emitted when a players reward is claimed #[ink(event)] #[derive(Debug)] - pub struct RewardClaimed { - for_player: AccountId, - rewards: u128, + pub struct GameReset { + when: u64, } #[ink(storage)] @@ -90,7 +91,7 @@ mod back_to_the_future { fn score(&self, now: BlockNumber) -> Balance { if let Some(last_press) = self.get().last_press { - return now - last_press; + return (now - last_press) as Balance; } 0 } @@ -108,7 +109,17 @@ mod back_to_the_future { fn press(&mut self) -> ButtonResult<()> { let caller = self.env().caller(); let now = Self::env().block_number(); - ButtonGame::press(self, TRANSFER_SELECTOR, MINT_TO_SELECTOR, now, caller, this)?; + let this = self.env().account_id(); + + ButtonGame::press::( + self, + TRANSFER_SELECTOR, + MINT_TO_SELECTOR, + now, + caller, + this, + )?; + Self::emit_event( self.env(), Event::ButtonPressed(ButtonPressed { @@ -116,30 +127,17 @@ mod back_to_the_future { when: now, }), ); + Ok(()) } #[ink(message)] - fn claim_reward(&mut self, for_player: AccountId) -> ButtonResult<()> { - let this = self.env().account_id(); - let now = self.env().block_number(); + fn reset(&mut self) -> ButtonResult<()> { + let now = Self::env().block_number(); - let rewards = ButtonGame::claim_reward::( - self, - now, - for_player, - BALANCE_OF_SELECTOR, - TRANSFER_SELECTOR, - this, - )?; + ButtonGame::reset::(self, now, MINT_TO_SELECTOR)?; - Self::emit_event( - self.env(), - Event::RewardClaimed(RewardClaimed { - for_player, - rewards, - }), - ); + Self::emit_event(self.env(), Event::GameReset(GameReset { when: now })); Ok(()) } @@ -149,16 +147,6 @@ mod back_to_the_future { ButtonGame::deadline(self, now) } - #[ink(message)] - fn score_of(&self, user: AccountId) -> Score { - ButtonGame::score_of(self, user) - } - - #[ink(message)] - fn can_play(&self, user: AccountId) -> bool { - ButtonGame::can_play(self, user) - } - #[ink(message)] fn access_control(&self) -> AccountId { ButtonGame::access_control(self) @@ -170,8 +158,13 @@ mod back_to_the_future { } #[ink(message)] - fn game_token(&self) -> AccountId { - ButtonGame::game_token(self) + fn reward_token(&self) -> AccountId { + ButtonGame::reward_token(self) + } + + #[ink(message)] + fn ticket_token(&self) -> AccountId { + ButtonGame::ticket_token(self) } #[ink(message)] @@ -187,44 +180,6 @@ mod back_to_the_future { ButtonGame::set_access_control(self, new_access_control, caller, this) } - #[ink(message)] - fn allow(&mut self, player: AccountId) -> ButtonResult<()> { - let caller = self.env().caller(); - let this = self.env().account_id(); - ButtonGame::allow(self, player, caller, this)?; - Self::emit_event( - self.env(), - Event::AccountWhitelisted(AccountWhitelisted { player }), - ); - Ok(()) - } - - #[ink(message)] - fn bulk_allow(&mut self, players: Vec) -> ButtonResult<()> { - let caller = self.env().caller(); - let this = self.env().account_id(); - ButtonGame::bulk_allow(self, players.clone(), caller, this)?; - for player in players { - Self::emit_event( - self.env(), - Event::AccountWhitelisted(AccountWhitelisted { player }), - ); - } - Ok(()) - } - - #[ink(message)] - fn disallow(&mut self, player: AccountId) -> ButtonResult<()> { - let caller = self.env().caller(); - let this = self.env().account_id(); - ButtonGame::disallow(self, player, caller, this)?; - Self::emit_event( - self.env(), - Event::AccountBlacklisted(AccountBlacklisted { player }), - ); - Ok(()) - } - #[ink(message)] fn terminate(&mut self) -> ButtonResult<()> { let caller = self.env().caller(); @@ -237,7 +192,11 @@ mod back_to_the_future { impl BackToTheFuture { #[ink(constructor)] - pub fn new(game_token: AccountId, button_lifetime: BlockNumber) -> Self { + pub fn new( + ticket_token: AccountId, + reward_token: AccountId, + button_lifetime: BlockNumber, + ) -> Self { let caller = Self::env().caller(); let code_hash = Self::env() .own_code_hash() @@ -257,19 +216,24 @@ mod back_to_the_future { match role_check { Ok(_) => initialize_contract(|contract| { - Self::new_init(contract, game_token, button_lifetime) + Self::new_init(contract, ticket_token, reward_token, button_lifetime) }), Err(why) => panic!("Could not initialize the contract {:?}", why), } } - fn new_init(&mut self, game_token: AccountId, button_lifetime: BlockNumber) { + fn new_init( + &mut self, + ticket_token: AccountId, + reward_token: AccountId, + button_lifetime: BlockNumber, + ) { let now = Self::env().block_number(); let deadline = now + button_lifetime; self.data.access_control = AccountId::from(ACCESS_CONTROL_PUBKEY); self.data.button_lifetime = button_lifetime; - self.data.game_token = game_token; + self.data.reward_token = reward_token; self.data.last_press = Some(now); Self::emit_event( @@ -277,7 +241,8 @@ mod back_to_the_future { Event::ButtonCreated(ButtonCreated { start: now, deadline, - game_token, + ticket_token, + reward_token, }), ) } diff --git a/contracts/button/lib.rs b/contracts/button/lib.rs index 26e392be68..0f990b6a23 100644 --- a/contracts/button/lib.rs +++ b/contracts/button/lib.rs @@ -230,7 +230,7 @@ pub trait ButtonGame { E: Environment, { build_call::() - .call_type(Call::new().callee(self.get().reward_token)) + .call_type(Call::new().callee(self.get().ticket_token)) .exec_input( ExecutionInput::new(Selector::new(transfer_selector)) .push_arg(to) @@ -331,6 +331,7 @@ pub trait ButtonGame { return Err(GameError::BeforeDeadline); } + // reward the Pressiah if let Some(pressiah) = last_presser { let reward = self.pressiah_score(); self.mint_tx::(mint_to_selector, *pressiah, reward)??; diff --git a/contracts/early_bird_special/Cargo.lock b/contracts/early_bird_special/Cargo.lock index 4b6d5dcc84..134c5eede8 100644 --- a/contracts/early_bird_special/Cargo.lock +++ b/contracts/early_bird_special/Cargo.lock @@ -242,6 +242,7 @@ dependencies = [ "ink_storage", "parity-scale-codec", "scale-info", + "ticket_token", ] [[package]] @@ -1011,6 +1012,22 @@ dependencies = [ "syn", ] +[[package]] +name = "ticket_token" +version = "2.1.0" +dependencies = [ + "access_control", + "ink_env", + "ink_lang", + "ink_metadata", + "ink_prelude", + "ink_primitives", + "ink_storage", + "openbrush", + "parity-scale-codec", + "scale-info", +] + [[package]] name = "toml" version = "0.5.9" diff --git a/contracts/early_bird_special/Cargo.toml b/contracts/early_bird_special/Cargo.toml index 30046b0527..ace9eca141 100644 --- a/contracts/early_bird_special/Cargo.toml +++ b/contracts/early_bird_special/Cargo.toml @@ -18,6 +18,7 @@ scale-info = { version = "2", default-features = false, features = ["derive"], o button = { path = "../button", default-features = false } game_token = { path = "../game_token", default-features = false, features = ["ink-as-dependency"] } +ticket_token = { path = "../ticket_token", default-features = false, features = ["ink-as-dependency"] } access_control = { path = "../access_control", default-features = false, features = ["ink-as-dependency"] } [lib] @@ -33,6 +34,7 @@ default = ["std"] std = [ "button/std", "game_token/std", + "ticket_token/std", "ink_env/std", "ink_lang_codegen/std", "ink_metadata/std", diff --git a/contracts/early_bird_special/lib.rs b/contracts/early_bird_special/lib.rs index 492ba22fff..a30148c2be 100644 --- a/contracts/early_bird_special/lib.rs +++ b/contracts/early_bird_special/lib.rs @@ -15,14 +15,15 @@ mod early_bird_special { use button::{ ButtonData, ButtonGame, ButtonGameEnvironment, ButtonResult, GameError, IButtonGame, }; - use game_token::{BALANCE_OF_SELECTOR, MINT_TO_SELECTOR, TRANSFER_SELECTOR}; + use game_token::MINT_TO_SELECTOR; use ink_env::Error as InkEnvError; use ink_lang::{ codegen::{initialize_contract, EmitEvent}, reflect::ContractEventBase, }; - use ink_prelude::{format, vec::Vec}; + use ink_prelude::format; use ink_storage::traits::SpreadAllocate; + use ticket_token::{BALANCE_OF_SELECTOR, TRANSFER_SELECTOR}; /// Event type type Event = ::Type; @@ -93,6 +94,7 @@ mod early_bird_special { let caller = self.env().caller(); let now = Self::env().block_number(); let this = self.env().account_id(); + ButtonGame::press::( self, TRANSFER_SELECTOR, @@ -101,6 +103,7 @@ mod early_bird_special { caller, this, )?; + Self::emit_event( self.env(), Event::ButtonPressed(ButtonPressed { @@ -108,6 +111,7 @@ mod early_bird_special { when: now, }), ); + Ok(()) } diff --git a/contracts/scripts/deploy.sh b/contracts/scripts/deploy.sh index 20ecdceb01..801898a0b8 100755 --- a/contracts/scripts/deploy.sh +++ b/contracts/scripts/deploy.sh @@ -100,9 +100,9 @@ function link_bytecode() { NODE_IMAGE=public.ecr.aws/p6e8q1z1/aleph-node:latest -# mint this many tokens, 20% go to the future LP on DEX +# mint this many ticket tokens TOTAL_BALANCE=1000 -GAME_BALANCE=$(echo "0.8 * $TOTAL_BALANCE" | bc) +# GAME_BALANCE=$(echo "0.8 * $TOTAL_BALANCE" | bc) CONTRACTS_PATH=$(pwd)/contracts @@ -111,6 +111,9 @@ CONTRACTS_PATH=$(pwd)/contracts cd "$CONTRACTS_PATH"/access_control cargo contract build --release +cd "$CONTRACTS_PATH"/ticket_token +cargo contract build --release + cd "$CONTRACTS_PATH"/game_token cargo contract build --release @@ -131,7 +134,23 @@ ACCESS_CONTROL_PUBKEY=$(docker run --rm --entrypoint "/bin/sh" "${NODE_IMAGE}" - echo "access control contract address: $ACCESS_CONTROL" echo "access control contract public key \(hex\): $ACCESS_CONTROL_PUBKEY" -# --- UPLOAD TOKEN CONTRACT CODE +# --- UPLOAD TICKET TOKEN CONTRACT CODE + +cd "$CONTRACTS_PATH"/ticket_token +# replace address placeholder with the on-chain address of the AccessControl contract +link_bytecode ticket_token 4465614444656144446561444465614444656144446561444465614444656144 $ACCESS_CONTROL_PUBKEY +# remove just in case +rm target/ink/ticket_token.wasm +# NOTE : here we go from hex to binary using a nodejs cli tool +# availiable from https://github.com/fbielejec/polkadot-cljs +node ../scripts/hex-to-wasm.js target/ink/ticket_token.contract target/ink/ticket_token.wasm + +CODE_HASH=$(cargo contract upload --url $NODE --suri "$AUTHORITY_SEED") +TICKET_TOKEN_CODE_HASH=$(echo "$CODE_HASH" | grep hash | tail -1 | cut -c 15-) + +echo "ticket token code hash" $TICKET_TOKEN_CODE_HASH + +# --- UPLOAD REWARD TOKEN CONTRACT CODE cd "$CONTRACTS_PATH"/game_token # replace address placeholder with the on-chain address of the AccessControl contract @@ -147,13 +166,16 @@ GAME_TOKEN_CODE_HASH=$(echo "$CODE_HASH" | grep hash | tail -1 | cut -c 15-) echo "button token code hash" "$GAME_TOKEN_CODE_HASH" -# --- GRANT INIT PRIVILEDGES ON THE TOKEN CONTRACT CODE +# --- GRANT INIT PRIVILEDGES ON THE TOKEN AND TICKET CONTRACT CODE cd "$CONTRACTS_PATH"/access_control # set the initializer of the token contract cargo contract call --url "$NODE" --contract "$ACCESS_CONTROL" --message grant_role --args "$AUTHORITY" 'Initializer('"$GAME_TOKEN_CODE_HASH"')' --suri "$AUTHORITY_SEED" +# set the initializer of the ticket contract +cargo contract call --url $NODE --contract $ACCESS_CONTROL --message grant_role --args $AUTHORITY 'Initializer('$TICKET_TOKEN_CODE_HASH')' --suri "$AUTHORITY_SEED" + # # --- EARLY_BIRD_SPECIAL GAME # From 400a627288b23ffd990fb74734991f077d4ba10b Mon Sep 17 00:00:00 2001 From: filip Date: Tue, 9 Aug 2022 18:21:57 +0200 Subject: [PATCH 06/29] wip : deployment script --- contracts/env/dev | 3 -- contracts/scripts/deploy.sh | 84 ++++++++++++++++++++++------------- contracts/ticket_token/lib.rs | 2 +- 3 files changed, 53 insertions(+), 36 deletions(-) diff --git a/contracts/env/dev b/contracts/env/dev index eefa45d663..70b5a76e56 100644 --- a/contracts/env/dev +++ b/contracts/env/dev @@ -6,6 +6,3 @@ export AUTHORITY_SEED=//Alice # how long do the games live for (in blocks) export LIFETIME=20 - -# initial whitelist of the accounts allowed to play the games -export WHITELIST="[5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY,5D34dL5prEUaGNQtPPZ3yN5Y6BnkfXunKXXz6fo7ZJbLwRRH]" diff --git a/contracts/scripts/deploy.sh b/contracts/scripts/deploy.sh index 801898a0b8..89ad790f82 100755 --- a/contracts/scripts/deploy.sh +++ b/contracts/scripts/deploy.sh @@ -4,6 +4,33 @@ set -euo pipefail # --- FUNCTIONS +function instrument_ticket_token { + + local __resultvar=$1 + local contract_name=$2 + local salt=$3 + local token_name=$4 + local token_symbol=$5 + + # --- CREATE AN INSTANCE OF THE TOKEN CONTRACT + + cd "$CONTRACTS_PATH"/$contract_name + + local contract_address=$(cargo contract instantiate --url $NODE --constructor new --args $token_name $token_symbol $TOTAL_BALANCE --suri "$AUTHORITY_SEED" --salt $salt) + local contract_address=$(echo "$contract_address" | grep Contract | tail -1 | cut -c 15-) + + echo $contract_name "ticket contract instance address: " $contract_address + + # --- GRANT PRIVILEDGES ON THE TOKEN CONTRACT + + cd "$CONTRACTS_PATH"/access_control + + # set the admin and the owner of the contract instance + cargo contract call --url $NODE --contract $ACCESS_CONTROL --message grant_role --args $AUTHORITY 'Owner('$contract_address')' --suri "$AUTHORITY_SEED" + + eval $__resultvar="'$contract_address'" +} + function instrument_game_token { local __resultvar=$1 @@ -16,9 +43,9 @@ function instrument_game_token { cd "$CONTRACTS_PATH"/"$contract_name" - local contract_address - contract_address=$(cargo contract instantiate --url "$NODE" --constructor new --args "$token_name" "$token_symbol" "$TOTAL_BALANCE" --suri "$AUTHORITY_SEED" --salt "$salt") - contract_address=$(echo "$contract_address" | grep Contract | tail -1 | cut -c 15-) + # TODO : remove balance + local contract_address=$(cargo contract instantiate --url $NODE --constructor new --args $TOTAL_BALANCE --suri "$AUTHORITY_SEED" --salt $salt) + local contract_address=$(echo "$contract_address" | grep Contract | tail -1 | cut -c 15-) echo "$contract_name token contract instance address: $contract_address" @@ -26,18 +53,20 @@ function instrument_game_token { cd "$CONTRACTS_PATH"/access_control - # set the admin and the owner of the contract instance - cargo contract call --url "$NODE" --contract "$ACCESS_CONTROL" --message grant_role --args "$AUTHORITY" 'Admin('"$contract_address"')' --suri "$AUTHORITY_SEED" - cargo contract call --url "$NODE" --contract "$ACCESS_CONTROL" --message grant_role --args "$AUTHORITY" 'Owner('"$contract_address"')' --suri "$AUTHORITY_SEED" + # set the owner of the contract instance + cargo contract call --url $NODE --contract $ACCESS_CONTROL --message grant_role --args $AUTHORITY 'Owner('$contract_address')' --suri "$AUTHORITY_SEED" - eval "$__resultvar='$contract_address'" + # TODO : MINTER / BURNER + + eval $__resultvar="'$contract_address'" } function deploy_and_instrument_game { local __resultvar=$1 local contract_name=$2 - local game_token=$3 + local ticket_token=$3 + local game_token=$4 # --- UPLOAD CONTRACT CODE @@ -60,9 +89,8 @@ function deploy_and_instrument_game { cd "$CONTRACTS_PATH/$contract_name" - local contract_address - contract_address=$(cargo contract instantiate --url "$NODE" --constructor new --args "$game_token" "$LIFETIME" --suri "$AUTHORITY_SEED") - contract_address=$(echo "$contract_address" | grep Contract | tail -1 | cut -c 15-) + local contract_address=$(cargo contract instantiate --url $NODE --constructor new --args $ticket_token $game_token $LIFETIME --suri "$AUTHORITY_SEED") + local contract_address=$(echo "$contract_address" | grep Contract | tail -1 | cut -c 15-) echo "$contract_name contract instance address: $contract_address" @@ -70,20 +98,7 @@ function deploy_and_instrument_game { cd "$CONTRACTS_PATH"/access_control - cargo contract call --url "$NODE" --contract "$ACCESS_CONTROL" --message grant_role --args "$AUTHORITY" 'Owner('"$contract_address"')' --suri "$AUTHORITY_SEED" - cargo contract call --url "$NODE" --contract "$ACCESS_CONTROL" --message grant_role --args "$AUTHORITY" 'Admin('"$contract_address"')' --suri "$AUTHORITY_SEED" - - # --- TRANSFER TOKENS TO THE CONTRACT - - cd "$CONTRACTS_PATH"/game_token - - cargo contract call --url "$NODE" --contract "$game_token" --message PSP22::transfer --args "$contract_address" "$GAME_BALANCE" "[0]" --suri "$AUTHORITY_SEED" - - # --- WHITELIST ACCOUNTS FOR PLAYING - - cd "$CONTRACTS_PATH/$contract_name" - - cargo contract call --url "$NODE" --contract "$contract_address" --message IButtonGame::bulk_allow --args "$WHITELIST" --suri "$AUTHORITY_SEED" + cargo contract call --url $NODE --contract $ACCESS_CONTROL --message grant_role --args $AUTHORITY 'Owner('$contract_address')' --suri "$AUTHORITY_SEED" eval "$__resultvar='$contract_address'" } @@ -102,7 +117,6 @@ NODE_IMAGE=public.ecr.aws/p6e8q1z1/aleph-node:latest # mint this many ticket tokens TOTAL_BALANCE=1000 -# GAME_BALANCE=$(echo "0.8 * $TOTAL_BALANCE" | bc) CONTRACTS_PATH=$(pwd)/contracts @@ -176,31 +190,37 @@ cargo contract call --url "$NODE" --contract "$ACCESS_CONTROL" --message grant_r # set the initializer of the ticket contract cargo contract call --url $NODE --contract $ACCESS_CONTROL --message grant_role --args $AUTHORITY 'Initializer('$TICKET_TOKEN_CODE_HASH')' --suri "$AUTHORITY_SEED" +start=`date +%s.%N` + # # --- EARLY_BIRD_SPECIAL GAME # -# --- CREATE AN INSTANCE OF THE TOKEN CONTRACT FOR THE EARLY_BIRD_SPECIAL GAME +# --- CREATE AN INSTANCE OF THE TICKET CONTRACT FOR THE EARLY_BIRD_SPECIAL GAME -start=$( date +%s.%N ) +instrument_ticket_token EARLY_BIRD_SPECIAL_TICKET game_ticket 0x4561726C79426972645370656369616C early_bird_special ebs instrument_game_token EARLY_BIRD_SPECIAL_TOKEN game_token Ubik UBI 0x4561726C79426972645370656369616C -# --- UPLOAD CODE AND CREATE AN INSTANCE OF THE EARLY_BIRD_SPECIAL GAME CONTRACT +# --- UPLOAD CODE AND CREATE AN INSTANCE OF THE EARLY_BIRD_SPECIAL GAME -deploy_and_instrument_game EARLY_BIRD_SPECIAL early_bird_special "$EARLY_BIRD_SPECIAL_TOKEN" +deploy_and_instrument_game EARLY_BIRD_SPECIAL early_bird_special $EARLY_BIRD_SPECIAL_TICKET $EARLY_BIRD_SPECIAL_TOKEN # # --- BACK_TO_THE_FUTURE GAME # +# --- CREATE AN INSTANCE OF THE TICKET CONTRACT FOR THE BACK_TO_THE_FUTURE GAME + +instrument_ticket_token BACK_TO_THE_FUTURE_TICKET game_ticket 0x4261636B546F546865467574757265 back_to_the_future + # --- CREATE AN INSTANCE OF THE TOKEN CONTRACT FOR THE BACK_TO_THE_FUTURE GAME instrument_game_token BACK_TO_THE_FUTURE_TOKEN game_token Cyberiad CYB 0x4261636B546F546865467574757265 -# --- UPLOAD CODE AND CREATE AN INSTANCE OF THE EARLY_BIRD_SPECIAL GAME CONTRACT +# --- UPLOAD CODE AND CREATE AN INSTANCE OF THE EARLY_BIRD_SPECIAL GAME -deploy_and_instrument_game BACK_TO_THE_FUTURE back_to_the_future "$BACK_TO_THE_FUTURE_TOKEN" +deploy_and_instrument_game BACK_TO_THE_FUTURE back_to_the_future $BACK_TO_THE_FUTURE_TICKET $BACK_TO_THE_FUTURE_TOKEN # spit adresses to a JSON file cd "$CONTRACTS_PATH" diff --git a/contracts/ticket_token/lib.rs b/contracts/ticket_token/lib.rs index 4caa888f25..63e56d9365 100644 --- a/contracts/ticket_token/lib.rs +++ b/contracts/ticket_token/lib.rs @@ -45,7 +45,7 @@ pub mod ticket_token { ) -> core::result::Result<(), PSP22Error> { if !amount.eq(&1u128) { return Err(PSP22Error::Custom(String::from( - "Only single ticket can be transfered at once", + "Only single ticket can be transferred at once", ))); } From 2dee371389eb150f69f2c1fe2849d0223354a8ba Mon Sep 17 00:00:00 2001 From: filip Date: Tue, 9 Aug 2022 18:32:50 +0200 Subject: [PATCH 07/29] fix --- contracts/scripts/deploy.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contracts/scripts/deploy.sh b/contracts/scripts/deploy.sh index 89ad790f82..aef4aaa7b9 100755 --- a/contracts/scripts/deploy.sh +++ b/contracts/scripts/deploy.sh @@ -198,7 +198,7 @@ start=`date +%s.%N` # --- CREATE AN INSTANCE OF THE TICKET CONTRACT FOR THE EARLY_BIRD_SPECIAL GAME -instrument_ticket_token EARLY_BIRD_SPECIAL_TICKET game_ticket 0x4561726C79426972645370656369616C early_bird_special ebs +instrument_ticket_token EARLY_BIRD_SPECIAL_TICKET ticket_token 0x4561726C79426972645370656369616C early_bird_special ebs instrument_game_token EARLY_BIRD_SPECIAL_TOKEN game_token Ubik UBI 0x4561726C79426972645370656369616C @@ -212,7 +212,7 @@ deploy_and_instrument_game EARLY_BIRD_SPECIAL early_bird_special $EARLY_BIRD_SPE # --- CREATE AN INSTANCE OF THE TICKET CONTRACT FOR THE BACK_TO_THE_FUTURE GAME -instrument_ticket_token BACK_TO_THE_FUTURE_TICKET game_ticket 0x4261636B546F546865467574757265 back_to_the_future +instrument_ticket_token BACK_TO_THE_FUTURE_TICKET ticket_token 0x4261636B546F546865467574757265 back_to_the_future # --- CREATE AN INSTANCE OF THE TOKEN CONTRACT FOR THE BACK_TO_THE_FUTURE GAME From 351674ace35d8bdb692abad1a2ed980174fe1545 Mon Sep 17 00:00:00 2001 From: filip Date: Tue, 9 Aug 2022 18:43:18 +0200 Subject: [PATCH 08/29] wip fixit --- contracts/scripts/deploy.sh | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/contracts/scripts/deploy.sh b/contracts/scripts/deploy.sh index aef4aaa7b9..dd25117ea8 100755 --- a/contracts/scripts/deploy.sh +++ b/contracts/scripts/deploy.sh @@ -12,11 +12,17 @@ function instrument_ticket_token { local token_name=$4 local token_symbol=$5 + echo "token: " $token_name "symbol: " $token_symbol "balance: " $TOTAL_BALANCE + # --- CREATE AN INSTANCE OF THE TOKEN CONTRACT cd "$CONTRACTS_PATH"/$contract_name - local contract_address=$(cargo contract instantiate --url $NODE --constructor new --args $token_name $token_symbol $TOTAL_BALANCE --suri "$AUTHORITY_SEED" --salt $salt) + # local contract_address=$(cargo contract instantiate --url $NODE --constructor new --args $token_name $token_symbol $TOTAL_BALANCE --suri "$AUTHORITY_SEED" --salt $salt) + + + local contract_address=$(cargo contract instantiate --url $NODE --constructor new --args "fubar" "fu" $TOTAL_BALANCE --suri "$AUTHORITY_SEED" --salt $salt) + local contract_address=$(echo "$contract_address" | grep Contract | tail -1 | cut -c 15-) echo $contract_name "ticket contract instance address: " $contract_address @@ -198,7 +204,7 @@ start=`date +%s.%N` # --- CREATE AN INSTANCE OF THE TICKET CONTRACT FOR THE EARLY_BIRD_SPECIAL GAME -instrument_ticket_token EARLY_BIRD_SPECIAL_TICKET ticket_token 0x4561726C79426972645370656369616C early_bird_special ebs +instrument_ticket_token EARLY_BIRD_SPECIAL_TICKET ticket_token 0x4561726C79426972645370656369616C 'early_bird_special' 'ebs' instrument_game_token EARLY_BIRD_SPECIAL_TOKEN game_token Ubik UBI 0x4561726C79426972645370656369616C @@ -212,18 +218,18 @@ deploy_and_instrument_game EARLY_BIRD_SPECIAL early_bird_special $EARLY_BIRD_SPE # --- CREATE AN INSTANCE OF THE TICKET CONTRACT FOR THE BACK_TO_THE_FUTURE GAME -instrument_ticket_token BACK_TO_THE_FUTURE_TICKET ticket_token 0x4261636B546F546865467574757265 back_to_the_future +# instrument_ticket_token BACK_TO_THE_FUTURE_TICKET ticket_token 0x4261636B546F546865467574757265 back_to_the_future -# --- CREATE AN INSTANCE OF THE TOKEN CONTRACT FOR THE BACK_TO_THE_FUTURE GAME +# # --- CREATE AN INSTANCE OF THE TOKEN CONTRACT FOR THE BACK_TO_THE_FUTURE GAME instrument_game_token BACK_TO_THE_FUTURE_TOKEN game_token Cyberiad CYB 0x4261636B546F546865467574757265 -# --- UPLOAD CODE AND CREATE AN INSTANCE OF THE EARLY_BIRD_SPECIAL GAME +# # --- UPLOAD CODE AND CREATE AN INSTANCE OF THE EARLY_BIRD_SPECIAL GAME -deploy_and_instrument_game BACK_TO_THE_FUTURE back_to_the_future $BACK_TO_THE_FUTURE_TICKET $BACK_TO_THE_FUTURE_TOKEN +# deploy_and_instrument_game BACK_TO_THE_FUTURE back_to_the_future $BACK_TO_THE_FUTURE_TICKET $BACK_TO_THE_FUTURE_TOKEN -# spit adresses to a JSON file -cd "$CONTRACTS_PATH" +# # spit adresses to a JSON file +# cd "$CONTRACTS_PATH" jq -n --arg early_bird_special "$EARLY_BIRD_SPECIAL" \ --arg back_to_the_future "$BACK_TO_THE_FUTURE" \ From bd05c684719fd3b2d524bbbe29d1d2a2efcf8146 Mon Sep 17 00:00:00 2001 From: filip Date: Wed, 10 Aug 2022 10:41:00 +0200 Subject: [PATCH 09/29] fixed deployment script --- contracts/scripts/deploy.sh | 44 +++++++++++++++++++++-------------- contracts/ticket_token/lib.rs | 4 +++- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/contracts/scripts/deploy.sh b/contracts/scripts/deploy.sh index dd25117ea8..09935c06f0 100755 --- a/contracts/scripts/deploy.sh +++ b/contracts/scripts/deploy.sh @@ -12,17 +12,16 @@ function instrument_ticket_token { local token_name=$4 local token_symbol=$5 - echo "token: " $token_name "symbol: " $token_symbol "balance: " $TOTAL_BALANCE - # --- CREATE AN INSTANCE OF THE TOKEN CONTRACT cd "$CONTRACTS_PATH"/$contract_name # local contract_address=$(cargo contract instantiate --url $NODE --constructor new --args $token_name $token_symbol $TOTAL_BALANCE --suri "$AUTHORITY_SEED" --salt $salt) + # local contract_address=$(cargo contract instantiate --url $NODE --constructor new --args \"fubar\" \"fu\" $TOTAL_BALANCE --suri "$AUTHORITY_SEED" --salt $salt) + + local contract_address=$(cargo contract instantiate --url $NODE --constructor new --args \"$token_name\" \"$token_symbol\" $TOTAL_BALANCE --suri "$AUTHORITY_SEED" --salt $salt) - local contract_address=$(cargo contract instantiate --url $NODE --constructor new --args "fubar" "fu" $TOTAL_BALANCE --suri "$AUTHORITY_SEED" --salt $salt) - local contract_address=$(echo "$contract_address" | grep Contract | tail -1 | cut -c 15-) echo $contract_name "ticket contract instance address: " $contract_address @@ -49,7 +48,7 @@ function instrument_game_token { cd "$CONTRACTS_PATH"/"$contract_name" - # TODO : remove balance + # TODO : remove balance when token is mintable local contract_address=$(cargo contract instantiate --url $NODE --constructor new --args $TOTAL_BALANCE --suri "$AUTHORITY_SEED" --salt $salt) local contract_address=$(echo "$contract_address" | grep Contract | tail -1 | cut -c 15-) @@ -62,7 +61,7 @@ function instrument_game_token { # set the owner of the contract instance cargo contract call --url $NODE --contract $ACCESS_CONTROL --message grant_role --args $AUTHORITY 'Owner('$contract_address')' --suri "$AUTHORITY_SEED" - # TODO : MINTER / BURNER + # TODO : MINTER / BURNER roles eval $__resultvar="'$contract_address'" } @@ -204,7 +203,7 @@ start=`date +%s.%N` # --- CREATE AN INSTANCE OF THE TICKET CONTRACT FOR THE EARLY_BIRD_SPECIAL GAME -instrument_ticket_token EARLY_BIRD_SPECIAL_TICKET ticket_token 0x4561726C79426972645370656369616C 'early_bird_special' 'ebs' +instrument_ticket_token EARLY_BIRD_SPECIAL_TICKET ticket_token 0x4561726C79426972645370656369616C early_bird_special EBS instrument_game_token EARLY_BIRD_SPECIAL_TOKEN game_token Ubik UBI 0x4561726C79426972645370656369616C @@ -218,22 +217,31 @@ deploy_and_instrument_game EARLY_BIRD_SPECIAL early_bird_special $EARLY_BIRD_SPE # --- CREATE AN INSTANCE OF THE TICKET CONTRACT FOR THE BACK_TO_THE_FUTURE GAME -# instrument_ticket_token BACK_TO_THE_FUTURE_TICKET ticket_token 0x4261636B546F546865467574757265 back_to_the_future - -# # --- CREATE AN INSTANCE OF THE TOKEN CONTRACT FOR THE BACK_TO_THE_FUTURE GAME +instrument_ticket_token BACK_TO_THE_FUTURE_TICKET ticket_token 0x4261636B546F546865467574757265 back_to_the_future BTF instrument_game_token BACK_TO_THE_FUTURE_TOKEN game_token Cyberiad CYB 0x4261636B546F546865467574757265 -# # --- UPLOAD CODE AND CREATE AN INSTANCE OF THE EARLY_BIRD_SPECIAL GAME +instrument_game_token BACK_TO_THE_FUTURE_TOKEN game_token 0x4261636B546F546865467574757265 -# deploy_and_instrument_game BACK_TO_THE_FUTURE back_to_the_future $BACK_TO_THE_FUTURE_TICKET $BACK_TO_THE_FUTURE_TOKEN - -# # spit adresses to a JSON file -# cd "$CONTRACTS_PATH" +# --- UPLOAD CODE AND CREATE AN INSTANCE OF THE EARLY_BIRD_SPECIAL GAME -jq -n --arg early_bird_special "$EARLY_BIRD_SPECIAL" \ - --arg back_to_the_future "$BACK_TO_THE_FUTURE" \ - '{early_bird_special: $early_bird_special, back_to_the_future: $back_to_the_future}' > addresses.json +deploy_and_instrument_game BACK_TO_THE_FUTURE back_to_the_future $BACK_TO_THE_FUTURE_TICKET $BACK_TO_THE_FUTURE_TOKEN + +# spit adresses to a JSON file +cd "$CONTRACTS_PATH" + +jq -n --arg early_bird_special $EARLY_BIRD_SPECIAL \ + --arg early_bird_special_ticket $EARLY_BIRD_SPECIAL_TICKET \ + --arg early_bird_special_token $EARLY_BIRD_SPECIAL_TOKEN \ + --arg back_to_the_future $BACK_TO_THE_FUTURE \ + --arg back_to_the_future_ticket $BACK_TO_THE_FUTURE_TICKET \ + --arg back_to_the_future_token $BACK_TO_THE_FUTURE_TOKEN \ + '{early_bird_special: $early_bird_special, + early_bird_special_ticket: $early_bird_special_ticket, + early_bird_special_token: $early_bird_special_token, + back_to_the_future: $back_to_the_future, + back_to_the_future_ticket: $back_to_the_future_ticket, + back_to_the_future_token: $back_to_the_future_token}' > addresses.json end=$( date +%s.%N ) echo "Time elapsed: $( echo "$end - $start" | bc -l )" diff --git a/contracts/ticket_token/lib.rs b/contracts/ticket_token/lib.rs index 63e56d9365..c5326a21b9 100644 --- a/contracts/ticket_token/lib.rs +++ b/contracts/ticket_token/lib.rs @@ -43,7 +43,9 @@ pub mod ticket_token { _to: Option<&AccountId>, amount: &Balance, ) -> core::result::Result<(), PSP22Error> { - if !amount.eq(&1u128) { + // if from is None this is an initial mint in the constructor + // and we don't want to enforce it there + if _from.is_some() && _to.is_none() && !amount.eq(&1u128) { return Err(PSP22Error::Custom(String::from( "Only single ticket can be transferred at once", ))); From 46bc61290b4c9332abbfa2d389c81fd9c804222e Mon Sep 17 00:00:00 2001 From: filip Date: Wed, 10 Aug 2022 11:36:00 +0200 Subject: [PATCH 10/29] wip : test --- contracts/scripts/test.sh | 12 ++++++++---- contracts/ticket_token/lib.rs | 2 +- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/contracts/scripts/test.sh b/contracts/scripts/test.sh index 8ef62ce212..c959d0a72f 100755 --- a/contracts/scripts/test.sh +++ b/contracts/scripts/test.sh @@ -4,6 +4,8 @@ set -euo pipefail # --- FUNCTIONS +# TODO : give allowance + function play { local contract_name=$1 @@ -21,13 +23,15 @@ function play { cargo contract call --url $NODE --contract $contract_address --message IButtonGame::press --suri $PLAYER2_SEED - # --- TRIGGER DEATH AND REWARDS DISTRIBUTION + # --- WAIT FOR BUTTON DEATH sleep $(($LIFETIME + 1)) - echo "claiming rewards for" $contract_name - cargo contract call --url $NODE --contract $contract_address --message IButtonGame::claim_reward --args $PLAYER1 --suri $AUTHORITY_SEED - cargo contract call --url $NODE --contract $contract_address --message IButtonGame::claim_reward --args $PLAYER2 --suri $AUTHORITY_SEED + # --- TRIGGER RESTART (PAYS OUT PRESSIAH REWARD) + + # echo "claiming rewards for" $contract_name + # cargo contract call --url $NODE --contract $contract_address --message IButtonGame::claim_reward --args $PLAYER1 --suri $AUTHORITY_SEED + # cargo contract call --url $NODE --contract $contract_address --message IButtonGame::claim_reward --args $PLAYER2 --suri $AUTHORITY_SEED echo "Done playing" $contract_name } diff --git a/contracts/ticket_token/lib.rs b/contracts/ticket_token/lib.rs index c5326a21b9..6768f9b910 100644 --- a/contracts/ticket_token/lib.rs +++ b/contracts/ticket_token/lib.rs @@ -45,7 +45,7 @@ pub mod ticket_token { ) -> core::result::Result<(), PSP22Error> { // if from is None this is an initial mint in the constructor // and we don't want to enforce it there - if _from.is_some() && _to.is_none() && !amount.eq(&1u128) { + if _from.is_some() && _to.is_some() && !amount.eq(&1u128) { return Err(PSP22Error::Custom(String::from( "Only single ticket can be transferred at once", ))); From 78723ad2629a0fb6d358ebc3b6994bf2e041c57a Mon Sep 17 00:00:00 2001 From: filip Date: Wed, 10 Aug 2022 14:15:56 +0200 Subject: [PATCH 11/29] cleanup --- contracts/access_control/lib.rs | 2 +- contracts/back_to_the_future/Cargo.lock | 13 +---- contracts/back_to_the_future/Cargo.toml | 4 -- contracts/back_to_the_future/lib.rs | 15 ++---- contracts/button/Cargo.lock | 30 +++++++++++ contracts/button/Cargo.toml | 2 + contracts/button/lib.rs | 70 +++++++++++++++++-------- contracts/early_bird_special/Cargo.lock | 13 +---- contracts/early_bird_special/Cargo.toml | 5 +- contracts/early_bird_special/lib.rs | 15 ++---- contracts/env/dev | 3 ++ contracts/scripts/deploy.sh | 3 -- contracts/scripts/test.sh | 56 ++++++++++++++------ contracts/ticket_token/lib.rs | 3 +- 14 files changed, 137 insertions(+), 97 deletions(-) diff --git a/contracts/access_control/lib.rs b/contracts/access_control/lib.rs index 452b0789f7..5e6f3f7961 100644 --- a/contracts/access_control/lib.rs +++ b/contracts/access_control/lib.rs @@ -16,7 +16,7 @@ mod access_control { }; use scale::{Decode, Encode}; - // address placeholder, set in the bytecode + // address placeholder, to be set in the bytecode // 4465614444656144446561444465614444656144446561444465614444656144 => 5DcPEG9AQ4Y9Lo9C5WXuKJDDawens77jWxZ6zGChnm8y8FUX pub const ACCESS_CONTROL_PUBKEY: [u8; 32] = *b"DeaDDeaDDeaDDeaDDeaDDeaDDeaDDeaD"; pub const HAS_ROLE_SELECTOR: [u8; 4] = [0, 0, 0, 3]; diff --git a/contracts/back_to_the_future/Cargo.lock b/contracts/back_to_the_future/Cargo.lock index bedf0e55d3..b99187be66 100644 --- a/contracts/back_to_the_future/Cargo.lock +++ b/contracts/back_to_the_future/Cargo.lock @@ -44,7 +44,6 @@ version = "0.1.0" dependencies = [ "access_control", "button", - "game_token", "ink_env", "ink_lang", "ink_lang_codegen", @@ -54,7 +53,6 @@ dependencies = [ "ink_storage", "parity-scale-codec", "scale-info", - "ticket_token", ] [[package]] @@ -103,6 +101,7 @@ name = "button" version = "0.1.0" dependencies = [ "access_control", + "game_token", "ink_env", "ink_lang", "ink_metadata", @@ -113,6 +112,7 @@ dependencies = [ "openbrush", "parity-scale-codec", "scale-info", + "ticket_token", ] [[package]] @@ -274,13 +274,11 @@ dependencies = [ "access_control", "ink_env", "ink_lang", - "ink_metadata", "ink_prelude", "ink_primitives", "ink_storage", "openbrush", "parity-scale-codec", - "scale-info", ] [[package]] @@ -634,14 +632,12 @@ source = "git+https://github.com/Supercolony-net/openbrush-contracts.git?rev=8a2 dependencies = [ "ink_env", "ink_lang", - "ink_metadata", "ink_prelude", "ink_primitives", "ink_storage", "openbrush_contracts", "openbrush_lang", "parity-scale-codec", - "scale-info", ] [[package]] @@ -651,13 +647,11 @@ source = "git+https://github.com/Supercolony-net/openbrush-contracts.git?rev=8a2 dependencies = [ "ink_env", "ink_lang", - "ink_metadata", "ink_prelude", "ink_primitives", "ink_storage", "openbrush_lang", "parity-scale-codec", - "scale-info", ] [[package]] @@ -668,7 +662,6 @@ dependencies = [ "const_format", "ink_env", "ink_lang", - "ink_metadata", "ink_primitives", "ink_storage", "openbrush_lang_macro", @@ -1019,13 +1012,11 @@ dependencies = [ "access_control", "ink_env", "ink_lang", - "ink_metadata", "ink_prelude", "ink_primitives", "ink_storage", "openbrush", "parity-scale-codec", - "scale-info", ] [[package]] diff --git a/contracts/back_to_the_future/Cargo.toml b/contracts/back_to_the_future/Cargo.toml index 4a5ab2c25a..7c31b6daf0 100644 --- a/contracts/back_to_the_future/Cargo.toml +++ b/contracts/back_to_the_future/Cargo.toml @@ -18,8 +18,6 @@ scale = { package = "parity-scale-codec", version = "3", default-features = fals scale-info = { version = "2", default-features = false, features = ["derive"], optional = true } button = { path = "../button", default-features = false } -game_token = { path = "../game_token", default-features = false, features = ["ink-as-dependency"] } -ticket_token = { path = "../ticket_token", default-features = false, features = ["ink-as-dependency"] } access_control = { path = "../access_control", default-features = false, features = ["ink-as-dependency"] } [lib] @@ -34,8 +32,6 @@ crate-type = [ default = ["std"] std = [ "button/std", - "game_token/std", - "ticket_token/std", "ink_env/std", "ink_lang_codegen/std", "ink_metadata/std", diff --git a/contracts/back_to_the_future/lib.rs b/contracts/back_to_the_future/lib.rs index b7e307f8f3..068da23965 100755 --- a/contracts/back_to_the_future/lib.rs +++ b/contracts/back_to_the_future/lib.rs @@ -15,7 +15,6 @@ mod back_to_the_future { use button::{ ButtonData, ButtonGame, ButtonGameEnvironment, ButtonResult, GameError, IButtonGame, }; - use game_token::MINT_TO_SELECTOR; use ink_env::Error as InkEnvError; use ink_lang::{ codegen::{initialize_contract, EmitEvent}, @@ -23,7 +22,6 @@ mod back_to_the_future { }; use ink_prelude::format; use ink_storage::traits::SpreadAllocate; - use ticket_token::{BALANCE_OF_SELECTOR, TRANSFER_SELECTOR}; type Event = ::Type; @@ -111,14 +109,7 @@ mod back_to_the_future { let now = Self::env().block_number(); let this = self.env().account_id(); - ButtonGame::press::( - self, - TRANSFER_SELECTOR, - MINT_TO_SELECTOR, - now, - caller, - this, - )?; + ButtonGame::press::(self, now, caller, this)?; Self::emit_event( self.env(), @@ -135,7 +126,7 @@ mod back_to_the_future { fn reset(&mut self) -> ButtonResult<()> { let now = Self::env().block_number(); - ButtonGame::reset::(self, now, MINT_TO_SELECTOR)?; + ButtonGame::reset::(self, now)?; Self::emit_event(self.env(), Event::GameReset(GameReset { when: now })); Ok(()) @@ -170,7 +161,7 @@ mod back_to_the_future { #[ink(message)] fn balance(&self) -> ButtonResult { let this = self.env().account_id(); - ButtonGame::balance::(self, BALANCE_OF_SELECTOR, this) + ButtonGame::balance::(self, this) } #[ink(message)] diff --git a/contracts/button/Cargo.lock b/contracts/button/Cargo.lock index de2c0f797a..e67c537d50 100644 --- a/contracts/button/Cargo.lock +++ b/contracts/button/Cargo.lock @@ -84,6 +84,7 @@ name = "button" version = "0.1.0" dependencies = [ "access_control", + "game_token", "ink_env", "ink_lang", "ink_metadata", @@ -94,6 +95,7 @@ dependencies = [ "openbrush", "parity-scale-codec", "scale-info", + "ticket_token", ] [[package]] @@ -248,6 +250,20 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c" +[[package]] +name = "game_token" +version = "2.1.0" +dependencies = [ + "access_control", + "ink_env", + "ink_lang", + "ink_prelude", + "ink_primitives", + "ink_storage", + "openbrush", + "parity-scale-codec", +] + [[package]] name = "generic-array" version = "0.14.5" @@ -973,6 +989,20 @@ dependencies = [ "syn", ] +[[package]] +name = "ticket_token" +version = "2.1.0" +dependencies = [ + "access_control", + "ink_env", + "ink_lang", + "ink_prelude", + "ink_primitives", + "ink_storage", + "openbrush", + "parity-scale-codec", +] + [[package]] name = "toml" version = "0.5.9" diff --git a/contracts/button/Cargo.toml b/contracts/button/Cargo.toml index e29c57b626..2ac9b1838a 100644 --- a/contracts/button/Cargo.toml +++ b/contracts/button/Cargo.toml @@ -16,6 +16,8 @@ scale = { package = "parity-scale-codec", version = "3", default-features = fals scale-info = { version = "2", default-features = false, features = ["derive"], optional = true } access_control = { path = "../access_control", default-features = false, features = ["ink-as-dependency"] } +game_token = { path = "../game_token", default-features = false, features = ["ink-as-dependency"] } +ticket_token = { path = "../ticket_token", default-features = false, features = ["ink-as-dependency"] } openbrush = { git = "https://github.com/Supercolony-net/openbrush-contracts.git", rev = "8a20f95", default-features = false, features = ["psp22"] } num = {version = "0.4.0", default-features = false} diff --git a/contracts/button/lib.rs b/contracts/button/lib.rs index 0f990b6a23..3a8fbedbb5 100644 --- a/contracts/button/lib.rs +++ b/contracts/button/lib.rs @@ -3,6 +3,7 @@ use core::mem::swap; use access_control::{traits::AccessControlled, Role}; +use game_token::MINT_TO_SELECTOR; use ink_env::{ call::{build_call, Call, ExecutionInput, Selector}, AccountId, DefaultEnvironment, Environment, Error as InkEnvError, @@ -11,6 +12,7 @@ use ink_lang as ink; use ink_prelude::{format, string::String, vec}; use ink_storage::traits::{SpreadAllocate, SpreadLayout}; use openbrush::contracts::psp22::PSP22Error; +use ticket_token::{BALANCE_OF_SELECTOR, TRANSFER_FROM_SELECTOR}; pub type BlockNumber = ::BlockNumber; pub type Balance = ::Balance; @@ -207,22 +209,22 @@ pub trait ButtonGame { self.get().reward_token } - fn balance(&self, balance_of_selector: [u8; 4], this: AccountId) -> ButtonResult + fn balance(&self, this: AccountId) -> ButtonResult where E: Environment, { let ticket_token = self.get().ticket_token; let balance = build_call::() .call_type(Call::new().callee(ticket_token)) - .exec_input(ExecutionInput::new(Selector::new(balance_of_selector)).push_arg(this)) + .exec_input(ExecutionInput::new(Selector::new(BALANCE_OF_SELECTOR)).push_arg(this)) .returns::() .fire()?; Ok(balance) } - fn transfer_tx( + fn transfer_from_tx( &self, - transfer_selector: [u8; 4], + from: AccountId, to: AccountId, value: Balance, ) -> Result, InkEnvError> @@ -232,7 +234,8 @@ pub trait ButtonGame { build_call::() .call_type(Call::new().callee(self.get().ticket_token)) .exec_input( - ExecutionInput::new(Selector::new(transfer_selector)) + ExecutionInput::new(Selector::new(TRANSFER_FROM_SELECTOR)) + .push_arg(from) .push_arg(to) .push_arg(value) .push_arg(vec![0x0]), @@ -241,9 +244,29 @@ pub trait ButtonGame { .fire() } + // fn transfer_tx( + // &self, + // transfer_selector: [u8; 4], + // to: AccountId, + // value: Balance, + // ) -> Result, InkEnvError> + // where + // E: Environment, + // { + // build_call::() + // .call_type(Call::new().callee(self.get().ticket_token)) + // .exec_input( + // ExecutionInput::new(Selector::new(transfer_selector)) + // .push_arg(to) + // .push_arg(value) + // .push_arg(vec![0x0]), + // ) + // .returns::>() + // .fire() + // } + fn mint_tx( &self, - mint_to_selector: [u8; 4], to: AccountId, amount: Balance, ) -> Result, InkEnvError> @@ -253,7 +276,7 @@ pub trait ButtonGame { build_call::() .call_type(Call::new().callee(self.get().reward_token)) .exec_input( - ExecutionInput::new(Selector::new(mint_to_selector)) + ExecutionInput::new(Selector::new(MINT_TO_SELECTOR)) .push_arg(to) .push_arg(amount), ) @@ -276,14 +299,7 @@ pub trait ButtonGame { ) } - fn press( - &mut self, - transfer_selector: [u8; 4], - mint_to_selector: [u8; 4], - now: BlockNumber, - caller: AccountId, - this: AccountId, - ) -> ButtonResult<()> + fn press(&mut self, now: BlockNumber, caller: AccountId, this: AccountId) -> ButtonResult<()> where E: Environment, { @@ -293,19 +309,20 @@ pub trait ButtonGame { let ButtonData { presses, .. } = self.get(); - // transfers 1 ticket token to self + // transfers 1 ticket token from the caller to self // tx will fail if user did not give allowance to the game contract // or does not have enough balance - self.transfer_tx::(transfer_selector, this, 1)??; + self.transfer_from_tx::(caller, this, 1u128)??; let root_key = ::ink_primitives::Key::from([0x00; 32]); let mut state = ::ink_storage::traits::pull_spread_root::(&root_key); - let score = self.score(now); + let _score = self.score(now); // mints reward tokens to pay out the reward // contract needs to have a Minter role on the reward token contract - self.mint_tx::(mint_to_selector, caller, score)??; + // TODO : UNCOMMENT WHEN MINT/BURN IS IMPLEMENTED FOR THE GAME TOKEN + // self.mint_tx::(caller, score)??; state.presses = presses + 1; state.last_presser = Some(caller); @@ -321,7 +338,7 @@ pub trait ButtonGame { /// Erases the storage and pays award to the Pressiah /// Can be called by any account on behalf of a player /// Can only be called after button's deadline - fn reset(&mut self, now: BlockNumber, mint_to_selector: [u8; 4]) -> ButtonResult<()> + fn reset(&mut self, now: BlockNumber) -> ButtonResult<()> where E: Environment, { @@ -332,9 +349,10 @@ pub trait ButtonGame { } // reward the Pressiah - if let Some(pressiah) = last_presser { - let reward = self.pressiah_score(); - self.mint_tx::(mint_to_selector, *pressiah, reward)??; + if let Some(_pressiah) = last_presser { + let _reward = self.pressiah_score(); + // TODO : UNCOMMENT WHEN MINT/BURN IS IMPLEMENTED FOR THE GAME TOKEN + // self.mint_tx::(mint_to_selector, *pressiah, reward)??; }; // zero the counters in storage @@ -357,6 +375,8 @@ pub trait ButtonGame { #[ink::trait_definition] pub trait IButtonGame { /// Button press logic + /// + /// Will instantenously mint reward tokens to the caller #[ink(message)] fn press(&mut self) -> ButtonResult<()>; @@ -371,6 +391,8 @@ pub trait IButtonGame { fn deadline(&self) -> BlockNumber; /// Returns the current Pressiah + /// + /// When button is DEAD this is ThePressiah and the winner of the current iteration #[ink(message)] fn last_presser(&self) -> Option; @@ -391,6 +413,8 @@ pub trait IButtonGame { fn balance(&self) -> ButtonResult; /// Resets the game + /// + /// rewards the Pressiah and resets the counters as well as all other neccessary storage fields #[ink(message)] fn reset(&mut self) -> ButtonResult<()>; diff --git a/contracts/early_bird_special/Cargo.lock b/contracts/early_bird_special/Cargo.lock index 134c5eede8..f100cd7965 100644 --- a/contracts/early_bird_special/Cargo.lock +++ b/contracts/early_bird_special/Cargo.lock @@ -84,6 +84,7 @@ name = "button" version = "0.1.0" dependencies = [ "access_control", + "game_token", "ink_env", "ink_lang", "ink_metadata", @@ -94,6 +95,7 @@ dependencies = [ "openbrush", "parity-scale-codec", "scale-info", + "ticket_token", ] [[package]] @@ -232,7 +234,6 @@ version = "0.1.0" dependencies = [ "access_control", "button", - "game_token", "ink_env", "ink_lang", "ink_lang_codegen", @@ -242,7 +243,6 @@ dependencies = [ "ink_storage", "parity-scale-codec", "scale-info", - "ticket_token", ] [[package]] @@ -274,13 +274,11 @@ dependencies = [ "access_control", "ink_env", "ink_lang", - "ink_metadata", "ink_prelude", "ink_primitives", "ink_storage", "openbrush", "parity-scale-codec", - "scale-info", ] [[package]] @@ -634,14 +632,12 @@ source = "git+https://github.com/Supercolony-net/openbrush-contracts.git?rev=8a2 dependencies = [ "ink_env", "ink_lang", - "ink_metadata", "ink_prelude", "ink_primitives", "ink_storage", "openbrush_contracts", "openbrush_lang", "parity-scale-codec", - "scale-info", ] [[package]] @@ -651,13 +647,11 @@ source = "git+https://github.com/Supercolony-net/openbrush-contracts.git?rev=8a2 dependencies = [ "ink_env", "ink_lang", - "ink_metadata", "ink_prelude", "ink_primitives", "ink_storage", "openbrush_lang", "parity-scale-codec", - "scale-info", ] [[package]] @@ -668,7 +662,6 @@ dependencies = [ "const_format", "ink_env", "ink_lang", - "ink_metadata", "ink_primitives", "ink_storage", "openbrush_lang_macro", @@ -1019,13 +1012,11 @@ dependencies = [ "access_control", "ink_env", "ink_lang", - "ink_metadata", "ink_prelude", "ink_primitives", "ink_storage", "openbrush", "parity-scale-codec", - "scale-info", ] [[package]] diff --git a/contracts/early_bird_special/Cargo.toml b/contracts/early_bird_special/Cargo.toml index ace9eca141..dcc5b1e615 100644 --- a/contracts/early_bird_special/Cargo.toml +++ b/contracts/early_bird_special/Cargo.toml @@ -3,6 +3,7 @@ name = "early_bird_special" version = "0.1.0" authors = ["Cardinal Cryptography"] edition = "2021" +license = "Apache 2.0" [dependencies] ink_env = { version = "3.2.0", default-features = false } @@ -17,8 +18,6 @@ scale = { package = "parity-scale-codec", version = "3", default-features = fals scale-info = { version = "2", default-features = false, features = ["derive"], optional = true } button = { path = "../button", default-features = false } -game_token = { path = "../game_token", default-features = false, features = ["ink-as-dependency"] } -ticket_token = { path = "../ticket_token", default-features = false, features = ["ink-as-dependency"] } access_control = { path = "../access_control", default-features = false, features = ["ink-as-dependency"] } [lib] @@ -33,8 +32,6 @@ crate-type = [ default = ["std"] std = [ "button/std", - "game_token/std", - "ticket_token/std", "ink_env/std", "ink_lang_codegen/std", "ink_metadata/std", diff --git a/contracts/early_bird_special/lib.rs b/contracts/early_bird_special/lib.rs index a30148c2be..13bb915825 100644 --- a/contracts/early_bird_special/lib.rs +++ b/contracts/early_bird_special/lib.rs @@ -15,7 +15,6 @@ mod early_bird_special { use button::{ ButtonData, ButtonGame, ButtonGameEnvironment, ButtonResult, GameError, IButtonGame, }; - use game_token::MINT_TO_SELECTOR; use ink_env::Error as InkEnvError; use ink_lang::{ codegen::{initialize_contract, EmitEvent}, @@ -23,7 +22,6 @@ mod early_bird_special { }; use ink_prelude::format; use ink_storage::traits::SpreadAllocate; - use ticket_token::{BALANCE_OF_SELECTOR, TRANSFER_SELECTOR}; /// Event type type Event = ::Type; @@ -95,14 +93,7 @@ mod early_bird_special { let now = Self::env().block_number(); let this = self.env().account_id(); - ButtonGame::press::( - self, - TRANSFER_SELECTOR, - MINT_TO_SELECTOR, - now, - caller, - this, - )?; + ButtonGame::press::(self, now, caller, this)?; Self::emit_event( self.env(), @@ -119,7 +110,7 @@ mod early_bird_special { fn reset(&mut self) -> ButtonResult<()> { let now = Self::env().block_number(); - ButtonGame::reset::(self, now, MINT_TO_SELECTOR)?; + ButtonGame::reset::(self, now)?; Self::emit_event(self.env(), Event::GameReset(GameReset { when: now })); Ok(()) @@ -154,7 +145,7 @@ mod early_bird_special { #[ink(message)] fn balance(&self) -> ButtonResult { let this = self.env().account_id(); - ButtonGame::balance::(self, BALANCE_OF_SELECTOR, this) + ButtonGame::balance::(self, this) } #[ink(message)] diff --git a/contracts/env/dev b/contracts/env/dev index 70b5a76e56..b1fe8364fd 100644 --- a/contracts/env/dev +++ b/contracts/env/dev @@ -6,3 +6,6 @@ export AUTHORITY_SEED=//Alice # how long do the games live for (in blocks) export LIFETIME=20 + +# mint this many ticket tokens +export TOTAL_BALANCE=1000 diff --git a/contracts/scripts/deploy.sh b/contracts/scripts/deploy.sh index 09935c06f0..68a8948b28 100755 --- a/contracts/scripts/deploy.sh +++ b/contracts/scripts/deploy.sh @@ -120,9 +120,6 @@ function link_bytecode() { NODE_IMAGE=public.ecr.aws/p6e8q1z1/aleph-node:latest -# mint this many ticket tokens -TOTAL_BALANCE=1000 - CONTRACTS_PATH=$(pwd)/contracts # --- COMPILE CONTRACTS diff --git a/contracts/scripts/test.sh b/contracts/scripts/test.sh index c959d0a72f..ba1d88c340 100755 --- a/contracts/scripts/test.sh +++ b/contracts/scripts/test.sh @@ -4,34 +4,59 @@ set -euo pipefail # --- FUNCTIONS -# TODO : give allowance - function play { local contract_name=$1 local contract_address=$(cat "$CONTRACTS_PATH"/addresses.json | jq --raw-output ".$contract_name") + local ticket_address=$(cat "$CONTRACTS_PATH"/addresses.json | jq --raw-output ".${contract_name}_ticket") - cd "$CONTRACTS_PATH"/$contract_name + # airdrop initial tickets - echo "calling press for" $contract_name "["$contract_address"]" "by" $PLAYER1_SEED + cd "$CONTRACTS_PATH"/ticket_token - cargo contract call --url $NODE --contract $contract_address --message IButtonGame::press --suri $PLAYER1_SEED + echo "sending ticket token" ${contract_name}_ticket "["$ticket_address"]" "to " $PLAYER1 - sleep 1 + cargo contract call --url $NODE --contract $ticket_address --message PSP22::transfer --args $PLAYER1 1 "[0]" --suri $AUTHORITY_SEED - echo "calling press for" $contract_name "["$contract_address "]" "by" $PLAYER2_SEED + echo "sending ticket token" ${contract_name}_ticket "["$ticket_address"]" "to " $PLAYER2 - cargo contract call --url $NODE --contract $contract_address --message IButtonGame::press --suri $PLAYER2_SEED + cargo contract call --url $NODE --contract $ticket_address --message PSP22::transfer --args $PLAYER2 1 "[0]" --suri $AUTHORITY_SEED - # --- WAIT FOR BUTTON DEATH + # give allowance for spending tickets to the game contract - sleep $(($LIFETIME + 1)) + cd "$CONTRACTS_PATH"/ticket_token + + echo "allowing" $contract_name "["$contract_address"]" "to spend up to" $TOTAL_BALANCE "of" ${contract_name}_ticket "["$ticket_address"]" "on behalf of" $PLAYER1 + + cargo contract call --url $NODE --contract $ticket_address --message PSP22::approve --args $contract_address $TOTAL_BALANCE --suri $PLAYER1_SEED + + echo "allowing" $contract_name "["$contract_address"]" "to spend up to" $TOTAL_BALANCE "of" ${contract_name}_ticket "["$ticket_address"]" "on behalf of" $PLAYER2 + + cargo contract call --url $NODE --contract $ticket_address --message PSP22::approve --args $contract_address $TOTAL_BALANCE --suri $PLAYER2_SEED + + # play the game + + # cd "$CONTRACTS_PATH"/$contract_name - # --- TRIGGER RESTART (PAYS OUT PRESSIAH REWARD) + # echo "calling press for" $contract_name "["$contract_address"]" "by" $PLAYER1_SEED - # echo "claiming rewards for" $contract_name - # cargo contract call --url $NODE --contract $contract_address --message IButtonGame::claim_reward --args $PLAYER1 --suri $AUTHORITY_SEED - # cargo contract call --url $NODE --contract $contract_address --message IButtonGame::claim_reward --args $PLAYER2 --suri $AUTHORITY_SEED + # cargo contract call --url $NODE --contract $contract_address --message IButtonGame::press --suri $PLAYER1_SEED + + # sleep 1 + + # echo "calling press for" $contract_name "["$contract_address "]" "by" $PLAYER2_SEED + + # cargo contract call --url $NODE --contract $contract_address --message IButtonGame::press --suri $PLAYER2_SEED + + # --- WAIT FOR THE BUTTON DEATH + + sleep $(($LIFETIME + 1)) + + # --- TRIGGER GAME RESET + + cd "$CONTRACTS_PATH"/$contract_name + + cargo contract call --url $NODE --contract $contract_address --message IButtonGame::reset --suri $AUTHORITY_SEED echo "Done playing" $contract_name } @@ -45,7 +70,8 @@ PLAYER1_SEED=//0 PLAYER2=5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY PLAYER2_SEED=//Alice -GAMES=(early_bird_special back_to_the_future) +# GAMES=(early_bird_special back_to_the_future) +GAMES=(early_bird_special) for GAME in "${GAMES[@]}"; do ( play $GAME diff --git a/contracts/ticket_token/lib.rs b/contracts/ticket_token/lib.rs index 6768f9b910..c9f27154d7 100644 --- a/contracts/ticket_token/lib.rs +++ b/contracts/ticket_token/lib.rs @@ -1,7 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] #![feature(min_specialization)] -pub use crate::ticket_token::{BALANCE_OF_SELECTOR, TRANSFER_SELECTOR}; +pub use crate::ticket_token::{BALANCE_OF_SELECTOR, TRANSFER_FROM_SELECTOR}; #[openbrush::contract] pub mod ticket_token { @@ -20,6 +20,7 @@ pub mod ticket_token { pub const BALANCE_OF_SELECTOR: [u8; 4] = [0x65, 0x68, 0x38, 0x2f]; pub const TRANSFER_SELECTOR: [u8; 4] = [0xdb, 0x20, 0xf9, 0xf5]; + pub const TRANSFER_FROM_SELECTOR: [u8; 4] = [0x54, 0xb3, 0xc7, 0x6e]; #[ink(storage)] #[derive(Default, SpreadAllocate, Storage)] From 89da634a6a3482ca81863c5961bc22d05584e216 Mon Sep 17 00:00:00 2001 From: filip Date: Wed, 10 Aug 2022 14:19:10 +0200 Subject: [PATCH 12/29] clean --- contracts/button/lib.rs | 10 ++++------ contracts/scripts/test.sh | 22 ++++++++++++---------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/contracts/button/lib.rs b/contracts/button/lib.rs index 3a8fbedbb5..dbd01fb07b 100644 --- a/contracts/button/lib.rs +++ b/contracts/button/lib.rs @@ -321,8 +321,7 @@ pub trait ButtonGame { // mints reward tokens to pay out the reward // contract needs to have a Minter role on the reward token contract - // TODO : UNCOMMENT WHEN MINT/BURN IS IMPLEMENTED FOR THE GAME TOKEN - // self.mint_tx::(caller, score)??; + self.mint_tx::(caller, score)??; state.presses = presses + 1; state.last_presser = Some(caller); @@ -349,10 +348,9 @@ pub trait ButtonGame { } // reward the Pressiah - if let Some(_pressiah) = last_presser { - let _reward = self.pressiah_score(); - // TODO : UNCOMMENT WHEN MINT/BURN IS IMPLEMENTED FOR THE GAME TOKEN - // self.mint_tx::(mint_to_selector, *pressiah, reward)??; + if let Some(pressiah) = last_presser { + let reward = self.pressiah_score(); + self.mint_tx::(*pressiah, reward)??; }; // zero the counters in storage diff --git a/contracts/scripts/test.sh b/contracts/scripts/test.sh index ba1d88c340..022835edce 100755 --- a/contracts/scripts/test.sh +++ b/contracts/scripts/test.sh @@ -34,8 +34,10 @@ function play { cargo contract call --url $NODE --contract $ticket_address --message PSP22::approve --args $contract_address $TOTAL_BALANCE --suri $PLAYER2_SEED - # play the game - + # TODO : can't test before mint / burn for game tokens is implemented + + # # play the game + # cd "$CONTRACTS_PATH"/$contract_name # echo "calling press for" $contract_name "["$contract_address"]" "by" $PLAYER1_SEED @@ -48,17 +50,17 @@ function play { # cargo contract call --url $NODE --contract $contract_address --message IButtonGame::press --suri $PLAYER2_SEED - # --- WAIT FOR THE BUTTON DEATH + # # --- WAIT FOR THE BUTTON DEATH + + # sleep $(($LIFETIME + 1)) - sleep $(($LIFETIME + 1)) + # # --- TRIGGER GAME RESET + + # cd "$CONTRACTS_PATH"/$contract_name - # --- TRIGGER GAME RESET - - cd "$CONTRACTS_PATH"/$contract_name - - cargo contract call --url $NODE --contract $contract_address --message IButtonGame::reset --suri $AUTHORITY_SEED + # cargo contract call --url $NODE --contract $contract_address --message IButtonGame::reset --suri $AUTHORITY_SEED - echo "Done playing" $contract_name + # echo "Done playing" $contract_name } # --- ARGUMENTS From dfcfe0ec52bb4c1dd12103f5fb018d907864e8f3 Mon Sep 17 00:00:00 2001 From: filip Date: Wed, 10 Aug 2022 14:19:49 +0200 Subject: [PATCH 13/29] ignore addresses file --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index f9e426644d..5f48695f9a 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,6 @@ p2p_secret # Python __pycache__/ *.py[cod] + +# ink! +contracts/addresses.json \ No newline at end of file From f7220d9733d4b48979f49ba64097ef3f941ce7af Mon Sep 17 00:00:00 2001 From: filip Date: Wed, 10 Aug 2022 14:21:04 +0200 Subject: [PATCH 14/29] comment --- contracts/scripts/test.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/scripts/test.sh b/contracts/scripts/test.sh index 022835edce..b4d72f6ecf 100755 --- a/contracts/scripts/test.sh +++ b/contracts/scripts/test.sh @@ -34,7 +34,7 @@ function play { cargo contract call --url $NODE --contract $ticket_address --message PSP22::approve --args $contract_address $TOTAL_BALANCE --suri $PLAYER2_SEED - # TODO : can't test before mint / burn for game tokens is implemented + # TODO : can't test before mint / burn for game tokens is implemented; uncomment when A0-1236 is done # # play the game From a66d7b1dd1585eebc5cb3c5079d95d100a2345a7 Mon Sep 17 00:00:00 2001 From: filip Date: Wed, 10 Aug 2022 14:35:18 +0200 Subject: [PATCH 15/29] simplified storage --- contracts/back_to_the_future/lib.rs | 13 +++++----- contracts/button/lib.rs | 37 +++++++---------------------- contracts/early_bird_special/lib.rs | 7 +++--- 3 files changed, 18 insertions(+), 39 deletions(-) diff --git a/contracts/back_to_the_future/lib.rs b/contracts/back_to_the_future/lib.rs index 068da23965..fc568b862d 100755 --- a/contracts/back_to_the_future/lib.rs +++ b/contracts/back_to_the_future/lib.rs @@ -88,10 +88,7 @@ mod back_to_the_future { } fn score(&self, now: BlockNumber) -> Balance { - if let Some(last_press) = self.get().last_press { - return (now - last_press) as Balance; - } - 0 + (now - self.get().last_press) as Balance } } @@ -134,8 +131,7 @@ mod back_to_the_future { #[ink(message)] fn deadline(&self) -> BlockNumber { - let now = self.env().block_number(); - ButtonGame::deadline(self, now) + ButtonGame::deadline(self) } #[ink(message)] @@ -225,7 +221,10 @@ mod back_to_the_future { self.data.access_control = AccountId::from(ACCESS_CONTROL_PUBKEY); self.data.button_lifetime = button_lifetime; self.data.reward_token = reward_token; - self.data.last_press = Some(now); + self.data.ticket_token = ticket_token; + + // NOTE: so that score never returns a 0 + self.data.last_press = now; Self::emit_event( Self::env(), diff --git a/contracts/button/lib.rs b/contracts/button/lib.rs index dbd01fb07b..023d952e06 100644 --- a/contracts/button/lib.rs +++ b/contracts/button/lib.rs @@ -126,8 +126,8 @@ pub struct ButtonData { pub button_lifetime: BlockNumber, /// stores the last account that pressed The Button pub last_presser: Option, - /// block number of the last press - pub last_press: Option, + /// block number of the last press, set to current block number at button start/reset + pub last_press: BlockNumber, /// counter for the number of presses pub presses: u128, /// AccountId of the PSP22 ButtonToken instance on-chain @@ -166,16 +166,16 @@ pub trait ButtonGame { } fn is_dead(&self, now: BlockNumber) -> bool { - now > self.deadline(now) + now > self.deadline() } - fn deadline(&self, now: BlockNumber) -> BlockNumber { + fn deadline(&self) -> BlockNumber { let ButtonData { last_press, button_lifetime, .. } = self.get(); - last_press.unwrap_or(now) + button_lifetime + last_press + button_lifetime } fn access_control(&self) -> AccountId { @@ -244,27 +244,6 @@ pub trait ButtonGame { .fire() } - // fn transfer_tx( - // &self, - // transfer_selector: [u8; 4], - // to: AccountId, - // value: Balance, - // ) -> Result, InkEnvError> - // where - // E: Environment, - // { - // build_call::() - // .call_type(Call::new().callee(self.get().ticket_token)) - // .exec_input( - // ExecutionInput::new(Selector::new(transfer_selector)) - // .push_arg(to) - // .push_arg(value) - // .push_arg(vec![0x0]), - // ) - // .returns::>() - // .fire() - // } - fn mint_tx( &self, to: AccountId, @@ -317,7 +296,7 @@ pub trait ButtonGame { let root_key = ::ink_primitives::Key::from([0x00; 32]); let mut state = ::ink_storage::traits::pull_spread_root::(&root_key); - let _score = self.score(now); + let score = self.score(now); // mints reward tokens to pay out the reward // contract needs to have a Minter role on the reward token contract @@ -325,7 +304,7 @@ pub trait ButtonGame { state.presses = presses + 1; state.last_presser = Some(caller); - state.last_press = Some(now); + state.last_press = now; swap(self.get_mut(), &mut state); @@ -359,7 +338,7 @@ pub trait ButtonGame { state.presses = 0; state.last_presser = None; - state.last_press = None; + state.last_press = now; swap(self.get_mut(), &mut state); Ok(()) diff --git a/contracts/early_bird_special/lib.rs b/contracts/early_bird_special/lib.rs index 13bb915825..da452c2d11 100644 --- a/contracts/early_bird_special/lib.rs +++ b/contracts/early_bird_special/lib.rs @@ -74,7 +74,7 @@ mod early_bird_special { } fn score(&self, now: BlockNumber) -> Balance { - let deadline = ButtonGame::deadline(self, now); + let deadline = ButtonGame::deadline(self); (deadline - now) as Balance } } @@ -118,8 +118,7 @@ mod early_bird_special { #[ink(message)] fn deadline(&self) -> BlockNumber { - let now = self.env().block_number(); - ButtonGame::deadline(self, now) + ButtonGame::deadline(self) } #[ink(message)] @@ -209,6 +208,8 @@ mod early_bird_special { self.data.access_control = AccountId::from(ACCESS_CONTROL_PUBKEY); self.data.button_lifetime = button_lifetime; self.data.reward_token = reward_token; + self.data.ticket_token = ticket_token; + self.data.last_press = now; Self::emit_event( Self::env(), From 5504983ac627bd51b198bf4cb77c1166ee367908 Mon Sep 17 00:00:00 2001 From: filip Date: Wed, 10 Aug 2022 14:38:11 +0200 Subject: [PATCH 16/29] clean --- contracts/back_to_the_future/lib.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/contracts/back_to_the_future/lib.rs b/contracts/back_to_the_future/lib.rs index fc568b862d..20ba082892 100755 --- a/contracts/back_to_the_future/lib.rs +++ b/contracts/back_to_the_future/lib.rs @@ -222,8 +222,6 @@ mod back_to_the_future { self.data.button_lifetime = button_lifetime; self.data.reward_token = reward_token; self.data.ticket_token = ticket_token; - - // NOTE: so that score never returns a 0 self.data.last_press = now; Self::emit_event( From 9b31d69b118bf1df14b030fb4350936c657cb185 Mon Sep 17 00:00:00 2001 From: filip Date: Thu, 11 Aug 2022 15:41:19 +0200 Subject: [PATCH 17/29] update err message --- contracts/game_token/lib.rs | 6 +++--- contracts/ticket_token/lib.rs | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/contracts/game_token/lib.rs b/contracts/game_token/lib.rs index 0f373bcb0c..eac59c2770 100644 --- a/contracts/game_token/lib.rs +++ b/contracts/game_token/lib.rs @@ -193,9 +193,9 @@ pub mod game_token { /// Returns own code hash #[ink(message, selector = 10)] pub fn code_hash(&self) -> Result { - Self::env() - .own_code_hash() - .map_err(|why| PSP22Error::Custom(format!("Calling control has failed: {:?}", why))) + Self::env().own_code_hash().map_err(|why| { + PSP22Error::Custom(format!("Can't retrieve own code hash: {:?}", why)) + }) } } } diff --git a/contracts/ticket_token/lib.rs b/contracts/ticket_token/lib.rs index c9f27154d7..4aecc68789 100644 --- a/contracts/ticket_token/lib.rs +++ b/contracts/ticket_token/lib.rs @@ -206,9 +206,9 @@ pub mod ticket_token { /// Returns own code hash #[ink(message, selector = 10)] pub fn code_hash(&self) -> Result { - Self::env() - .own_code_hash() - .map_err(|why| PSP22Error::Custom(format!("Calling control has failed: {:?}", why))) + Self::env().own_code_hash().map_err(|why| { + PSP22Error::Custom(format!("Can't retrieve own code hash: {:?}", why)) + }) } } } From 90abc65d6e3d4a9238a0fe15da898ada2cc3750c Mon Sep 17 00:00:00 2001 From: Filip Bielejec Date: Thu, 11 Aug 2022 09:58:28 +0200 Subject: [PATCH 18/29] Update contracts/button/Cargo.toml Co-authored-by: kostekIV <27210860+kostekIV@users.noreply.github.com> --- contracts/button/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/button/Cargo.toml b/contracts/button/Cargo.toml index 2ac9b1838a..437b6609e8 100644 --- a/contracts/button/Cargo.toml +++ b/contracts/button/Cargo.toml @@ -19,7 +19,7 @@ access_control = { path = "../access_control", default-features = false, feature game_token = { path = "../game_token", default-features = false, features = ["ink-as-dependency"] } ticket_token = { path = "../ticket_token", default-features = false, features = ["ink-as-dependency"] } openbrush = { git = "https://github.com/Supercolony-net/openbrush-contracts.git", rev = "8a20f95", default-features = false, features = ["psp22"] } -num = {version = "0.4.0", default-features = false} +num = {version = "0.4.0", default-features = false } [lib] name = "button" From 4aa078d12b32711e60bfd0d117e9272584945e9d Mon Sep 17 00:00:00 2001 From: filip Date: Thu, 11 Aug 2022 10:01:26 +0200 Subject: [PATCH 19/29] BN --- contracts/back_to_the_future/lib.rs | 2 +- contracts/early_bird_special/lib.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/contracts/back_to_the_future/lib.rs b/contracts/back_to_the_future/lib.rs index 20ba082892..e47012c938 100755 --- a/contracts/back_to_the_future/lib.rs +++ b/contracts/back_to_the_future/lib.rs @@ -65,7 +65,7 @@ mod back_to_the_future { #[ink(event)] #[derive(Debug)] pub struct GameReset { - when: u64, + when: BlockNumber, } #[ink(storage)] diff --git a/contracts/early_bird_special/lib.rs b/contracts/early_bird_special/lib.rs index da452c2d11..e151547c84 100644 --- a/contracts/early_bird_special/lib.rs +++ b/contracts/early_bird_special/lib.rs @@ -51,7 +51,7 @@ mod early_bird_special { #[ink(event)] #[derive(Debug)] pub struct GameReset { - when: u64, + when: BlockNumber, } #[ink(storage)] From efb5ec588ede3b14dc8ed6acad2172f6c686dae3 Mon Sep 17 00:00:00 2001 From: filip Date: Thu, 11 Aug 2022 10:06:26 +0200 Subject: [PATCH 20/29] comments addressed --- contracts/early_bird_special/lib.rs | 2 +- contracts/scripts/deploy.sh | 4 ---- contracts/scripts/test.sh | 3 +-- contracts/ticket_token/lib.rs | 18 +++++++++--------- 4 files changed, 11 insertions(+), 16 deletions(-) diff --git a/contracts/early_bird_special/lib.rs b/contracts/early_bird_special/lib.rs index e151547c84..24613dad40 100644 --- a/contracts/early_bird_special/lib.rs +++ b/contracts/early_bird_special/lib.rs @@ -75,7 +75,7 @@ mod early_bird_special { fn score(&self, now: BlockNumber) -> Balance { let deadline = ButtonGame::deadline(self); - (deadline - now) as Balance + deadline.saturating_sub(now) as Balance } } diff --git a/contracts/scripts/deploy.sh b/contracts/scripts/deploy.sh index 68a8948b28..1027b73b96 100755 --- a/contracts/scripts/deploy.sh +++ b/contracts/scripts/deploy.sh @@ -16,10 +16,6 @@ function instrument_ticket_token { cd "$CONTRACTS_PATH"/$contract_name - # local contract_address=$(cargo contract instantiate --url $NODE --constructor new --args $token_name $token_symbol $TOTAL_BALANCE --suri "$AUTHORITY_SEED" --salt $salt) - - # local contract_address=$(cargo contract instantiate --url $NODE --constructor new --args \"fubar\" \"fu\" $TOTAL_BALANCE --suri "$AUTHORITY_SEED" --salt $salt) - local contract_address=$(cargo contract instantiate --url $NODE --constructor new --args \"$token_name\" \"$token_symbol\" $TOTAL_BALANCE --suri "$AUTHORITY_SEED" --salt $salt) local contract_address=$(echo "$contract_address" | grep Contract | tail -1 | cut -c 15-) diff --git a/contracts/scripts/test.sh b/contracts/scripts/test.sh index b4d72f6ecf..a1f51107b8 100755 --- a/contracts/scripts/test.sh +++ b/contracts/scripts/test.sh @@ -72,8 +72,7 @@ PLAYER1_SEED=//0 PLAYER2=5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY PLAYER2_SEED=//Alice -# GAMES=(early_bird_special back_to_the_future) -GAMES=(early_bird_special) +GAMES=(early_bird_special back_to_the_future) for GAME in "${GAMES[@]}"; do ( play $GAME diff --git a/contracts/ticket_token/lib.rs b/contracts/ticket_token/lib.rs index 4aecc68789..abfed6d137 100644 --- a/contracts/ticket_token/lib.rs +++ b/contracts/ticket_token/lib.rs @@ -40,13 +40,13 @@ pub mod ticket_token { impl Transfer for TicketToken { fn _before_token_transfer( &mut self, - _from: Option<&AccountId>, - _to: Option<&AccountId>, + from: Option<&AccountId>, + to: Option<&AccountId>, amount: &Balance, ) -> core::result::Result<(), PSP22Error> { // if from is None this is an initial mint in the constructor // and we don't want to enforce it there - if _from.is_some() && _to.is_some() && !amount.eq(&1u128) { + if from.is_some() && to.is_some() && !amount.eq(&1u128) { return Err(PSP22Error::Custom(String::from( "Only single ticket can be transferred at once", ))); @@ -59,16 +59,16 @@ pub mod ticket_token { impl Internal for TicketToken { fn _emit_transfer_event( &self, - _from: Option, - _to: Option, - _amount: Balance, + from: Option, + to: Option, + amount: Balance, ) { TicketToken::emit_event( self.env(), Event::TransferEvent(TransferEvent { - from: _from, - to: _to, - value: _amount, + from, + to, + value: amount, }), ); } From 3343816a2da04ba7fe100e3ee5b7cc00cd21cf5e Mon Sep 17 00:00:00 2001 From: filip Date: Thu, 11 Aug 2022 10:10:50 +0200 Subject: [PATCH 21/29] comments addressed --- contracts/ticket_token/lib.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/contracts/ticket_token/lib.rs b/contracts/ticket_token/lib.rs index abfed6d137..7c45806867 100644 --- a/contracts/ticket_token/lib.rs +++ b/contracts/ticket_token/lib.rs @@ -73,13 +73,13 @@ pub mod ticket_token { ); } - fn _emit_approval_event(&self, _owner: AccountId, _spender: AccountId, _amount: Balance) { + fn _emit_approval_event(&self, owner: AccountId, spender: AccountId, amount: Balance) { TicketToken::emit_event( self.env(), Event::Approval(Approval { - owner: _owner, - spender: _spender, - value: _amount, + owner, + spender, + value: amount, }), ); } From 88f213074eb677ba722146fbbbc20441fef48ea7 Mon Sep 17 00:00:00 2001 From: filip Date: Thu, 11 Aug 2022 13:13:35 +0200 Subject: [PATCH 22/29] newline char --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 5f48695f9a..e720b4cca6 100644 --- a/.gitignore +++ b/.gitignore @@ -25,4 +25,4 @@ __pycache__/ *.py[cod] # ink! -contracts/addresses.json \ No newline at end of file +contracts/addresses.json From a395c6bf28414002b3b82606d7cba5ced57c9bbc Mon Sep 17 00:00:00 2001 From: filip Date: Thu, 11 Aug 2022 17:15:45 +0200 Subject: [PATCH 23/29] not needed --- contracts/back_to_the_future/lib.rs | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/contracts/back_to_the_future/lib.rs b/contracts/back_to_the_future/lib.rs index e47012c938..f7ce518729 100755 --- a/contracts/back_to_the_future/lib.rs +++ b/contracts/back_to_the_future/lib.rs @@ -36,22 +36,6 @@ mod back_to_the_future { deadline: BlockNumber, } - /// Event emitted when account is whitelisted to play the game - #[ink(event)] - #[derive(Debug)] - pub struct AccountWhitelisted { - #[ink(topic)] - player: AccountId, - } - - /// Event emitted when account is blacklisted from playing the game - #[ink(event)] - #[derive(Debug)] - pub struct AccountBlacklisted { - #[ink(topic)] - player: AccountId, - } - /// Event emitted when TheButton is pressed #[ink(event)] #[derive(Debug)] From 19c976588848d76990e3f49ddc7d8f00d46b9989 Mon Sep 17 00:00:00 2001 From: filip Date: Fri, 12 Aug 2022 13:43:45 +0200 Subject: [PATCH 24/29] updated README --- contracts/README.md | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/contracts/README.md b/contracts/README.md index d654183b59..9e9390e9f4 100644 --- a/contracts/README.md +++ b/contracts/README.md @@ -72,11 +72,9 @@ source contracts/env/dev && ./contracts/scripts/deploy.sh Specifically it will: -- Deploy the token contracts. -- Deploy the game contracts. -- Set access control. -- Transfer token balances to the game contracts. -- Whitelist accounts for playing the games. +- Deploy the contracts. +- Set access control on them. +- Make neccessary token transfers. Third `test.sh` script plays the game from two well-known dev addresses. @@ -86,5 +84,5 @@ Third `test.sh` script plays the game from two well-known dev addresses. It will: -- Interact with the games from the whitelisted accounts. +- Interact with the games from known accounts. - Wait past the game deadline, trigger the game end and reward distribution. From c3d76b8bd0a0cf06668a49913ec87d20b484459d Mon Sep 17 00:00:00 2001 From: filip Date: Fri, 12 Aug 2022 14:08:24 +0200 Subject: [PATCH 25/29] fix typo --- contracts/access_control/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/access_control/lib.rs b/contracts/access_control/lib.rs index 5e6f3f7961..44c2f3f706 100644 --- a/contracts/access_control/lib.rs +++ b/contracts/access_control/lib.rs @@ -87,7 +87,7 @@ mod access_control { /// Initializes the contract. /// - /// caller is granted admin and owner piviledges + /// caller is granted admin and owner priviledges fn new_init(&mut self) { let caller = Self::env().caller(); let this = Self::env().account_id(); From d83561ec895189fb564bcb98ce7ccae5a314e40c Mon Sep 17 00:00:00 2001 From: filip Date: Tue, 16 Aug 2022 11:04:28 +0200 Subject: [PATCH 26/29] typo --- contracts/access_control/lib.rs | 16 ++++++++-------- contracts/back_to_the_future/Cargo.lock | 2 ++ contracts/back_to_the_future/Cargo.toml | 2 ++ contracts/button/lib.rs | 2 +- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/contracts/access_control/lib.rs b/contracts/access_control/lib.rs index 44c2f3f706..7dc9e848fd 100644 --- a/contracts/access_control/lib.rs +++ b/contracts/access_control/lib.rs @@ -40,7 +40,7 @@ mod access_control { #[derive(SpreadAllocate)] pub struct AccessControl { /// Stores a de-facto hashset of user accounts and their roles - pub priviledges: Mapping<(AccountId, Role), ()>, + pub privileges: Mapping<(AccountId, Role), ()>, } #[ink(event)] @@ -87,12 +87,12 @@ mod access_control { /// Initializes the contract. /// - /// caller is granted admin and owner priviledges + /// caller is granted admin and owner privileges fn new_init(&mut self) { let caller = Self::env().caller(); let this = Self::env().account_id(); - self.priviledges.insert((caller, Role::Admin(this)), &()); - self.priviledges.insert((caller, Role::Owner(this)), &()); + self.privileges.insert((caller, Role::Admin(this)), &()); + self.privileges.insert((caller, Role::Owner(this)), &()); } /// Gives a role to an account @@ -101,11 +101,11 @@ mod access_control { #[ink(message, selector = 1)] pub fn grant_role(&mut self, account: AccountId, role: Role) -> Result<()> { let key = (account, role); - if !self.priviledges.contains(key) { + if !self.privileges.contains(key) { let caller = self.env().caller(); let this = self.env().account_id(); self.check_role(caller, Role::Admin(this))?; - self.priviledges.insert(key, &()); + self.privileges.insert(key, &()); let event = Event::RoleGranted(RoleGranted { by: caller, @@ -126,7 +126,7 @@ mod access_control { let caller = self.env().caller(); let this = self.env().account_id(); self.check_role(caller, Role::Admin(this))?; - self.priviledges.remove((account, role)); + self.privileges.remove((account, role)); let event = Event::RoleRevoked(RoleRevoked { by: caller, @@ -141,7 +141,7 @@ mod access_control { /// Returns true if account has a role #[ink(message, selector = 3)] pub fn has_role(&self, account: AccountId, role: Role) -> bool { - self.priviledges.contains((account, role)) + self.privileges.contains((account, role)) } /// Terminates the contract. diff --git a/contracts/back_to_the_future/Cargo.lock b/contracts/back_to_the_future/Cargo.lock index b99187be66..a6738b34f4 100644 --- a/contracts/back_to_the_future/Cargo.lock +++ b/contracts/back_to_the_future/Cargo.lock @@ -9,9 +9,11 @@ dependencies = [ "ink_env", "ink_lang", "ink_lang_codegen", + "ink_metadata", "ink_primitives", "ink_storage", "parity-scale-codec", + "scale-info", ] [[package]] diff --git a/contracts/back_to_the_future/Cargo.toml b/contracts/back_to_the_future/Cargo.toml index 7c31b6daf0..20159f06e9 100644 --- a/contracts/back_to_the_future/Cargo.toml +++ b/contracts/back_to_the_future/Cargo.toml @@ -31,8 +31,10 @@ crate-type = [ [features] default = ["std"] std = [ + "access_control/std", "button/std", "ink_env/std", + "ink_lang/std", "ink_lang_codegen/std", "ink_metadata/std", "ink_prelude/std", diff --git a/contracts/button/lib.rs b/contracts/button/lib.rs index 023d952e06..efe8bce489 100644 --- a/contracts/button/lib.rs +++ b/contracts/button/lib.rs @@ -39,7 +39,7 @@ pub enum GameError { BeforeDeadline, /// Returned if button is pressed after the deadline AfterDeadline, - /// Returned if a call is made from an account with missing access control priviledges + /// Returned if a call is made from an account with missing access control privileges MissingRole(String), /// Returned if a call to another contract has failed ContractCall(String), From c4cd044a8411e12743a1afb685b1ecb0a089b95f Mon Sep 17 00:00:00 2001 From: filip Date: Tue, 16 Aug 2022 14:28:18 +0200 Subject: [PATCH 27/29] cleanup --- contracts/scripts/deploy.sh | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/contracts/scripts/deploy.sh b/contracts/scripts/deploy.sh index 1027b73b96..47b734b023 100755 --- a/contracts/scripts/deploy.sh +++ b/contracts/scripts/deploy.sh @@ -44,9 +44,10 @@ function instrument_game_token { cd "$CONTRACTS_PATH"/"$contract_name" + local contract_address # TODO : remove balance when token is mintable - local contract_address=$(cargo contract instantiate --url $NODE --constructor new --args $TOTAL_BALANCE --suri "$AUTHORITY_SEED" --salt $salt) - local contract_address=$(echo "$contract_address" | grep Contract | tail -1 | cut -c 15-) + contract_address=$(cargo contract instantiate --url "$NODE" --constructor new --args "$token_name" "$token_symbol" "$TOTAL_BALANCE" --suri "$AUTHORITY_SEED" --salt "$salt") + contract_address=$(echo "$contract_address" | grep Contract | tail -1 | cut -c 15-) echo "$contract_name token contract instance address: $contract_address" @@ -54,12 +55,13 @@ function instrument_game_token { cd "$CONTRACTS_PATH"/access_control - # set the owner of the contract instance - cargo contract call --url $NODE --contract $ACCESS_CONTROL --message grant_role --args $AUTHORITY 'Owner('$contract_address')' --suri "$AUTHORITY_SEED" + # set the admin and the owner of the contract instance + # cargo contract call --url "$NODE" --contract "$ACCESS_CONTROL" --message grant_role --args "$AUTHORITY" 'Admin('"$contract_address"')' --suri "$AUTHORITY_SEED" + cargo contract call --url "$NODE" --contract "$ACCESS_CONTROL" --message grant_role --args "$AUTHORITY" 'Owner('"$contract_address"')' --suri "$AUTHORITY_SEED" # TODO : MINTER / BURNER roles - eval $__resultvar="'$contract_address'" + eval "$__resultvar='$contract_address'" } function deploy_and_instrument_game { @@ -214,8 +216,6 @@ instrument_ticket_token BACK_TO_THE_FUTURE_TICKET ticket_token 0x4261636B546F546 instrument_game_token BACK_TO_THE_FUTURE_TOKEN game_token Cyberiad CYB 0x4261636B546F546865467574757265 -instrument_game_token BACK_TO_THE_FUTURE_TOKEN game_token 0x4261636B546F546865467574757265 - # --- UPLOAD CODE AND CREATE AN INSTANCE OF THE EARLY_BIRD_SPECIAL GAME deploy_and_instrument_game BACK_TO_THE_FUTURE back_to_the_future $BACK_TO_THE_FUTURE_TICKET $BACK_TO_THE_FUTURE_TOKEN From 054ce75cb0e33730213c37eb35cb90cf80ec701a Mon Sep 17 00:00:00 2001 From: filip Date: Tue, 16 Aug 2022 14:29:45 +0200 Subject: [PATCH 28/29] cleanup --- contracts/scripts/deploy.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/contracts/scripts/deploy.sh b/contracts/scripts/deploy.sh index 47b734b023..b527cd070d 100755 --- a/contracts/scripts/deploy.sh +++ b/contracts/scripts/deploy.sh @@ -22,7 +22,7 @@ function instrument_ticket_token { echo $contract_name "ticket contract instance address: " $contract_address - # --- GRANT PRIVILEDGES ON THE TOKEN CONTRACT + # --- GRANT PRIVILEGES ON THE TOKEN CONTRACT cd "$CONTRACTS_PATH"/access_control @@ -51,7 +51,7 @@ function instrument_game_token { echo "$contract_name token contract instance address: $contract_address" - # --- GRANT PRIVILEDGES ON THE TOKEN CONTRACT + # --- GRANT PRIVILEGES ON THE TOKEN CONTRACT cd "$CONTRACTS_PATH"/access_control @@ -97,7 +97,7 @@ function deploy_and_instrument_game { echo "$contract_name contract instance address: $contract_address" - # --- GRANT PRIVILEDGES ON THE CONTRACT + # --- GRANT PRIVILEGES ON THE CONTRACT cd "$CONTRACTS_PATH"/access_control @@ -180,7 +180,7 @@ GAME_TOKEN_CODE_HASH=$(echo "$CODE_HASH" | grep hash | tail -1 | cut -c 15-) echo "button token code hash" "$GAME_TOKEN_CODE_HASH" -# --- GRANT INIT PRIVILEDGES ON THE TOKEN AND TICKET CONTRACT CODE +# --- GRANT INIT PRIVILEGES ON THE TOKEN AND TICKET CONTRACT CODE cd "$CONTRACTS_PATH"/access_control From 17e81cc576af6a52376080321ea6a829296aa6fc Mon Sep 17 00:00:00 2001 From: filip Date: Tue, 16 Aug 2022 14:31:37 +0200 Subject: [PATCH 29/29] remove comment --- contracts/scripts/deploy.sh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/contracts/scripts/deploy.sh b/contracts/scripts/deploy.sh index b527cd070d..a27a59261c 100755 --- a/contracts/scripts/deploy.sh +++ b/contracts/scripts/deploy.sh @@ -55,8 +55,7 @@ function instrument_game_token { cd "$CONTRACTS_PATH"/access_control - # set the admin and the owner of the contract instance - # cargo contract call --url "$NODE" --contract "$ACCESS_CONTROL" --message grant_role --args "$AUTHORITY" 'Admin('"$contract_address"')' --suri "$AUTHORITY_SEED" + # set the owner of the contract instance cargo contract call --url "$NODE" --contract "$ACCESS_CONTROL" --message grant_role --args "$AUTHORITY" 'Owner('"$contract_address"')' --suri "$AUTHORITY_SEED" # TODO : MINTER / BURNER roles