Skip to content

Commit 016eecc

Browse files
author
jeff1010-web
committed
fix chainlink issue
1 parent 1348b0e commit 016eecc

File tree

6 files changed

+88
-78
lines changed

6 files changed

+88
-78
lines changed

app/src/game/states/Game.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ class GameState extends Phaser.State {
3939
game: this.game,
4040
x: this.world.centerX,
4141
y: this.world.centerY - 50,
42-
text: 'The Crypto Purge 💸 \nfor '
42+
text: 'The Crypto Purge 💸 \n '
4343
})
4444
this.game.add.existing(this.banner)
4545
// this.logo = this.add.image(this.banner.x - 8, this.banner.y - 10, 'sonar')
4646
// this.logo.scale.setTo(0.28)
47-
this.logo = this.add.image(this.banner.x - 80, this.banner.y + 0, 'sonar-logo')
48-
this.logo.scale.setTo(0.055)
47+
// this.logo = this.add.image(this.banner.x - 80, this.banner.y + 0, 'sonar-logo')
48+
// this.logo.scale.setTo(0.055)
4949

5050
// this.ring = this.add.image(this.world.centerX - 3, this.world.centerY + 170, 'sonar-logo-ring')
5151
// this.ring.scale.setTo(0.8)

smartcontracts/contracts/RandomNumberConsumer.sol

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,11 @@ contract RandomNumberConsumer is VRFConsumerBase, Ownable {
1010
uint256 internal fee;
1111
uint256 public randomResult;
1212
event RequestedRandomness(bytes32 requestId);
13+
event EmitRandom(uint256 randomResult);
1314

1415
/**
1516
* Constructor inherits VRFConsumerBase
1617
*
17-
* Network: Kovan
18-
* Chainlink VRF Coordinator address: 0xdD3782915140c8f3b190B5D67eAc6dc5760C46E9
19-
* LINK token address: 0xa36085F69e2889c224210F603D836748e7dC0088
20-
* Key Hash: 0x6c3699283bda56ad74f6b855546325b68d482e983852a7a82979cc4807b641f4
2118
*/
2219
constructor(
2320
address _vrfCoordinator,
@@ -31,15 +28,18 @@ contract RandomNumberConsumer is VRFConsumerBase, Ownable {
3128
)
3229
{
3330
keyHash = _keyHash;
34-
fee = _fee;
31+
fee = _fee; // 0.1 * 10 ** 18 === 0.1 LINK (Varies by network)
3532
}
3633

3734
/**
3835
* Requests randomness
3936
*/
4037
function getRandomNumber() public returns (bytes32 requestId) {
38+
require(LINK.balanceOf(address(this)) >= fee, "Not enough LINK - fill contract first");
39+
4140
requestId = requestRandomness(keyHash, fee);
4241
emit RequestedRandomness(requestId);
42+
return requestId;
4343
}
4444

4545
/**
@@ -51,6 +51,9 @@ contract RandomNumberConsumer is VRFConsumerBase, Ownable {
5151
{
5252
console.log("Created random %s for request %s", randomness, string(abi.encodePacked(requestId)));
5353
randomResult = randomness;
54+
// get a random number in a range from 1 to 50
55+
// randomResult = (randomness % 50) + 1;
56+
emit EmitRandom(randomResult);
5457
}
5558

5659
/**
Lines changed: 2 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,4 @@
11
// SPDX-License-Identifier: MIT
2-
pragma solidity ^0.8.4;
2+
pragma solidity ^0.4.24;
33

4-
abstract contract LinkTokenReceiver {
5-
6-
bytes4 constant private ORACLE_REQUEST_SELECTOR = 0x40429946;
7-
uint256 constant private SELECTOR_LENGTH = 4;
8-
uint256 constant private EXPECTED_REQUEST_WORDS = 2;
9-
uint256 constant private MINIMUM_REQUEST_LENGTH = SELECTOR_LENGTH + (32 * EXPECTED_REQUEST_WORDS);
10-
/**
11-
* @notice Called when LINK is sent to the contract via `transferAndCall`
12-
* @dev The data payload's first 2 words will be overwritten by the `_sender` and `_amount`
13-
* values to ensure correctness. Calls oracleRequest.
14-
* @param _sender Address of the sender
15-
* @param _amount Amount of LINK sent (specified in wei)
16-
* @param _data Payload of the transaction
17-
*/
18-
function onTokenTransfer(
19-
address _sender,
20-
uint256 _amount,
21-
bytes memory _data
22-
)
23-
public
24-
onlyLINK
25-
validRequestLength(_data)
26-
permittedFunctionsForLINK(_data)
27-
{
28-
assembly {
29-
// solhint-disable-next-line avoid-low-level-calls
30-
mstore(add(_data, 36), _sender) // ensure correct sender is passed
31-
// solhint-disable-next-line avoid-low-level-calls
32-
mstore(add(_data, 68), _amount) // ensure correct amount is passed
33-
}
34-
// solhint-disable-next-line avoid-low-level-calls
35-
(bool success, ) = address(this).delegatecall(_data); // calls oracleRequest
36-
require(success, "Unable to create request");
37-
}
38-
39-
function getChainlinkToken() public view virtual returns (address);
40-
41-
/**
42-
* @dev Reverts if not sent from the LINK token
43-
*/
44-
modifier onlyLINK() {
45-
require(msg.sender == getChainlinkToken(), "Must use LINK token");
46-
_;
47-
}
48-
49-
/**
50-
* @dev Reverts if the given data does not begin with the `oracleRequest` function selector
51-
* @param _data The data payload of the request
52-
*/
53-
modifier permittedFunctionsForLINK(bytes memory _data) {
54-
bytes4 funcSelector;
55-
assembly {
56-
// solhint-disable-next-line avoid-low-level-calls
57-
funcSelector := mload(add(_data, 32))
58-
}
59-
require(funcSelector == ORACLE_REQUEST_SELECTOR, "Must use whitelisted functions");
60-
_;
61-
}
62-
63-
/**
64-
* @dev Reverts if the given payload is less than needed to create a request
65-
* @param _data The request payload
66-
*/
67-
modifier validRequestLength(bytes memory _data) {
68-
require(_data.length >= MINIMUM_REQUEST_LENGTH, "Invalid request length");
69-
_;
70-
}
71-
}
4+
import "@chainlink/token/contracts/v0.4/LinkToken.sol";
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.4;
3+
4+
abstract contract LinkTokenReceiver {
5+
6+
bytes4 constant private ORACLE_REQUEST_SELECTOR = 0x40429946;
7+
uint256 constant private SELECTOR_LENGTH = 4;
8+
uint256 constant private EXPECTED_REQUEST_WORDS = 2;
9+
uint256 constant private MINIMUM_REQUEST_LENGTH = SELECTOR_LENGTH + (32 * EXPECTED_REQUEST_WORDS);
10+
/**
11+
* @notice Called when LINK is sent to the contract via `transferAndCall`
12+
* @dev The data payload's first 2 words will be overwritten by the `_sender` and `_amount`
13+
* values to ensure correctness. Calls oracleRequest.
14+
* @param _sender Address of the sender
15+
* @param _amount Amount of LINK sent (specified in wei)
16+
* @param _data Payload of the transaction
17+
*/
18+
function onTokenTransfer(
19+
address _sender,
20+
uint256 _amount,
21+
bytes memory _data
22+
)
23+
public
24+
onlyLINK
25+
validRequestLength(_data)
26+
permittedFunctionsForLINK(_data)
27+
{
28+
assembly {
29+
// solhint-disable-next-line avoid-low-level-calls
30+
mstore(add(_data, 36), _sender) // ensure correct sender is passed
31+
// solhint-disable-next-line avoid-low-level-calls
32+
mstore(add(_data, 68), _amount) // ensure correct amount is passed
33+
}
34+
// solhint-disable-next-line avoid-low-level-calls
35+
(bool success, ) = address(this).delegatecall(_data); // calls oracleRequest
36+
require(success, "Unable to create request");
37+
}
38+
39+
function getChainlinkToken() public view virtual returns (address);
40+
41+
/**
42+
* @dev Reverts if not sent from the LINK token
43+
*/
44+
modifier onlyLINK() {
45+
require(msg.sender == getChainlinkToken(), "Must use LINK token");
46+
_;
47+
}
48+
49+
/**
50+
* @dev Reverts if the given data does not begin with the `oracleRequest` function selector
51+
* @param _data The data payload of the request
52+
*/
53+
modifier permittedFunctionsForLINK(bytes memory _data) {
54+
bytes4 funcSelector;
55+
assembly {
56+
// solhint-disable-next-line avoid-low-level-calls
57+
funcSelector := mload(add(_data, 32))
58+
}
59+
require(funcSelector == ORACLE_REQUEST_SELECTOR, "Must use whitelisted functions");
60+
_;
61+
}
62+
63+
/**
64+
* @dev Reverts if the given payload is less than needed to create a request
65+
* @param _data The request payload
66+
*/
67+
modifier validRequestLength(bytes memory _data) {
68+
require(_data.length >= MINIMUM_REQUEST_LENGTH, "Invalid request length");
69+
_;
70+
}
71+
}

smartcontracts/contracts/test/MockOracle.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ pragma solidity ^0.8.4;
33

44
import "@chainlink/contracts/src/v0.8/interfaces/ChainlinkRequestInterface.sol";
55
import "@chainlink/contracts/src/v0.8/interfaces/LinkTokenInterface.sol";
6-
import "./LinkToken.sol";
6+
import "./LinkTokenReceiver.sol";
77
import "./SafeMathChainlink.sol";
88

99
/**

smartcontracts/hardhat.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ const PRIVATE_KEY = process.env.PRIVATE_KEY || 'your private key'
3131
module.exports = {
3232
solidity: {
3333
compilers: [
34+
{
35+
version: "0.4.24"
36+
},
3437
{
3538
version: "0.6.6"
3639
},

0 commit comments

Comments
 (0)