aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWei-Ning Huang <w@byzantine-lab.io>2019-06-26 10:56:22 +0800
committerWei-Ning Huang <w@byzantine-lab.io>2019-09-17 16:53:55 +0800
commit8948709eca6d688401b3d11e6abe1d5412e60d2a (patch)
tree1f11718794677efe0cd11ad1d885ea1477d357c6
downloadtangerine-random-lib-8948709eca6d688401b3d11e6abe1d5412e60d2a.tar.gz
tangerine-random-lib-8948709eca6d688401b3d11e6abe1d5412e60d2a.tar.zst
tangerine-random-lib-8948709eca6d688401b3d11e6abe1d5412e60d2a.zip
Initial commit
-rw-r--r--README.md37
-rw-r--r--RandomLib.sol13
2 files changed, 50 insertions, 0 deletions
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..1c56a9f
--- /dev/null
+++ b/README.md
@@ -0,0 +1,37 @@
+# Tangerine Network RNG Library
+
+## Introduction
+
+Tangerine Network provides on-chain randomness through a precompiled contract.
+Using a precompiled contract instead of introducing a new OP code ensures that
+we are compatible with EVM, and eliminates the need to maintain a separate
+toolchain.
+
+The precompiled contract is located at
+`0xc327ff1025c5b3d2deb5e3f0f161b3f7e557579a`, which is the first 20 bytes of
+`Keccak256Hash("Tangerine Network Random")`. The gas cost for each invocation is 64.
+
+To use the random library, simply include the library file then call `Random.rand()` to get the random number.
+
+## Example
+
+```
+import "github.com/tangerine-network/random-lib/RandomLib.sol";
+
+contract UseRandom {
+ event Win();
+ event Lose();
+
+ function readRand() public view returns (uint256) {
+ return Random.rand();
+ }
+
+ function bet() public {
+ if (Random.rand() % 100 >= 50) {
+ emit Win();
+ } else {
+ emit Lose();
+ }
+ }
+}
+```
diff --git a/RandomLib.sol b/RandomLib.sol
new file mode 100644
index 0000000..228c804
--- /dev/null
+++ b/RandomLib.sol
@@ -0,0 +1,13 @@
+pragma solidity >= 0.5.0;
+
+library Random {
+ function rand() internal view returns (uint256) {
+ uint256[1] memory m;
+ assembly {
+ if iszero(staticcall(not(0), 0xC327fF1025c5B3D2deb5e3F0f161B3f7E557579a, 0, 0x0, m, 0x20)) {
+ revert(0, 0)
+ }
+ }
+ return m[0];
+ }
+}