Skip to content

Commit a52f221

Browse files
committed
GetDrandByEpoch
1 parent 49fc67a commit a52f221

File tree

4 files changed

+53
-8
lines changed

4 files changed

+53
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ Additional FVM support can be found in the [filecoin-solidity library](https://g
7171
|| CallActorByAddress | `0xfe00000000000000000000000000000000000003` |
7272
|| GetActorType | `0xfe00000000000000000000000000000000000004` |
7373
|| CallActorById | `0xfe00000000000000000000000000000000000005` |
74-
| | GetDrandByEpoch | `0xfe00000000000000000000000000000000000006` |
74+
| | GetDrandByEpoch | `0xfe00000000000000000000000000000000000006` |
7575

7676
### Methods
7777

src/Demo.sol

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
pragma solidity ^0.8.30;
33

44
import {FVMPay} from "./FVMPay.sol";
5+
import {FVMRandom} from "./FVMRandom.sol";
56

67
contract Demo {
78
// baseline solidity methods using CALL opcode
@@ -34,4 +35,19 @@ contract Demo {
3435
function burn() external payable {
3536
require(msg.value.burn());
3637
}
38+
39+
// randomness
40+
using FVMRandom for uint256;
41+
42+
function prev() external view returns (uint256 randomness) {
43+
randomness = (block.number - 1).drand();
44+
}
45+
46+
function curr() external view returns (uint256 randomness) {
47+
randomness = block.number.drand();
48+
}
49+
50+
function next() external view returns (uint256 randomness) {
51+
randomness = (block.number + 1).drand();
52+
}
3753
}

src/FVMRandom.sol

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// SPDX-License-Identifier: Apache-2.0 OR MIT
2+
pragma solidity ^0.8.30;
3+
4+
import {GET_DRAND_BY_EPOCH} from "./FVMPrecompiles.sol";
5+
6+
library FVMRandom {
7+
// the precompile will assert and consume all gas if epoch > block.number
8+
function drand(uint256 epoch) internal view returns (uint256 randomness) {
9+
assembly ("memory-safe") {
10+
let fmp := mload(0x40)
11+
mstore(fmp, epoch)
12+
pop(staticcall(gas(), GET_DRAND_BY_EPOCH, fmp, 32, fmp, 32))
13+
randomness := mload(fmp)
14+
}
15+
}
16+
}

tools/gas-profile.sh

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,36 @@
33
RPC_URL=https://api.calibration.node.glif.io/rpc/v1
44

55
# Demo.sol
6-
ADDRESS=0x91A9f1b2Aa333936A15B1F9D399C3c3b8647De6e
6+
ADDRESS=0x1Ff1ceFcf1739d1b6aD4B2Cd27FB970A3214d174
77

88
estimateGas() {
9-
curl -H "Content-Type: application/json" -X POST --data "{\"id\": 1, \"method\": \"eth_estimateGas\", \"params\": [{\"to\": \"$ADDRESS\", \"data\": \"$1\", \"value\": \"0x1\"}, \"latest\"]}" $RPC_URL
9+
curl -H "Content-Type: application/json" -X POST --data "{\"id\": 1, \"method\": \"eth_estimateGas\", \"params\": [{\"to\": \"$ADDRESS\", \"data\": \"$1\", \"value\": \"$2\"}, \"latest\"]}" $RPC_URL
10+
}
11+
12+
call() {
13+
curl -H "Content-Type: application/json" -X POST --data "{\"id\": 1, \"method\": \"eth_call\", \"params\": [{\"to\": \"$ADDRESS\", \"data\": \"$1\", \"value\": \"$2\"}, \"latest\"]}" $RPC_URL
1014
}
1115

1216
echo "send(address)"
13-
estimateGas 0x3e58c58c0000000000000000000000004a6f6b9ff1fc974096f9063a45fd12bd5b928ad1
17+
estimateGas 0x3e58c58c0000000000000000000000004a6f6b9ff1fc974096f9063a45fd12bd5b928ad1 0x1
1418

1519
echo "transfer(address)"
16-
estimateGas 0x1a6952300000000000000000000000004a6f6b9ff1fc974096f9063a45fd12bd5b928ad1
20+
estimateGas 0x1a6952300000000000000000000000004a6f6b9ff1fc974096f9063a45fd12bd5b928ad1 0x1
1721

1822
echo "pay(address)"
19-
estimateGas 0x0c11dedd0000000000000000000000004a6f6b9ff1fc974096f9063a45fd12bd5b928ad1
23+
estimateGas 0x0c11dedd0000000000000000000000004a6f6b9ff1fc974096f9063a45fd12bd5b928ad1 0x1
2024

2125
echo "pay(uint64)"
22-
estimateGas 0x68f165f0000000000000000000000000000000000000000000000000000000000002a9a2
26+
estimateGas 0x68f165f0000000000000000000000000000000000000000000000000000000000002a9a2 0x1
2327

2428
echo "burn()"
25-
estimateGas 0x44df8e70
29+
estimateGas 0x44df8e70 0x1
30+
31+
echo "prev()"
32+
estimateGas 0x479c9254 0x0
33+
34+
echo "curr()"
35+
estimateGas 0x8103aa45 0x0
36+
37+
echo "next() (reverts)"
38+
estimateGas 0x4c8fe526 0x0

0 commit comments

Comments
 (0)