A learning note of deploying/testing contract by using hardhat and using vrf service.
I'm starting learning solidity today and create a simple contract
using hardhat and chainlink VRF. I will continue to upgrade it in the
days to come as I learn more deeper.
Completed the following functions today:
Deploy contract to Goerli TestNet by hardhat framework.
Interact with the contract by hardhat framework.
Implement a simple gambling function using VRF in the contract.
learning notes:
1.Basic Contract
Note:
The address and address payable types both
store a 160-bit Ethereum address. The concept of payable and non-payable
addresses only exists in the Solidity type system at compile-time. The
difference between payable and non-payable addresses is gone in the
compiled contract code. You can use payable(address) to
convert address to payable address.
Use new CpntractFactory(cantractAddress) to create a
new contract in this contract.
The A.transfer(value) function is used to transfer
ether to address A from this contract. The keyword payable
allowed transferring ether to someone or receiving ether.
function Owner()public view returns (address) { return owner; }
function Balance()public view returns (uint256) { return address(this).balance; }
function VRFAddress()public view returns (address) { return address(VRF); }
function buy_lottery() payable external returns (uint256) { uint256 value = msg.value; uint random = 5 + VRF.requestRandomWords()%10; uint256 lottery_value = (value * random) /10; payable(msg.sender).transfer(lottery_value); return lottery_value; }
function withdraw_lottery() external { owner.transfer(address(this).balance); }
receive() external payable{ emit Received(msg.sender, msg.value,"receive was called"); }
}
2.VRFConsumercontract
Note:
VRF is a random number supplier in chainlink. We can deploy a
VRFConsumer contract and register it in your Subscription
Manager(require LINK token). The owner of VRFConsumer can get random
numbers so we should deploy it by basic contract instead of the EOF
account.