Skip to content

Commit d14f719

Browse files
authored
feat: vrf interaction (#16)
1 parent 8277897 commit d14f719

4 files changed

Lines changed: 60 additions & 6 deletions

File tree

basics/ethereum/vrf/.env.example

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
PRIVATE_KEY=
2-
BSC_TESTNET_RPC_URL=https://data-seed-prebsc-1-s2.bnbchain.org:8545
2+
BSC_TESTNET_RPC_URL=https://bsc-testnet-rpc.publicnode.com
33
VRF_WRAPPER=0x471506e6ADED0b9811D05B8cAc8Db25eE839Ac94
4-
LOTTERY_ADDRESS=
5-
VALUE=
4+
LOTTERY_ADDRESS=0x27e5De33cB6d31894a875891D3D86aA5F5b8aB2a
5+
VALUE=8000000000000000

basics/ethereum/vrf/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ cache/
33
out/
44

55
# Ignores development broadcast logs
6-
!/broadcast
6+
/broadcast
77
/broadcast/*/31337/
88
/broadcast/**/dry-run/
99

basics/ethereum/vrf/README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,57 @@ forge install
194194
# Run tests
195195
forge test
196196
```
197+
198+
## 6. Hands-on Practice
199+
200+
The Lottery contract is deployed and verified on BSC Testnet at [0x27e5De33cB6d31894a875891D3D86aA5F5b8aB2a](https://testnet.bscscan.com/address/0x27e5De33cB6d31894a875891D3D86aA5F5b8aB2a#readContract).
201+
202+
### 6.1 Interacting with the Contract
203+
204+
There are two ways to interact with the deployed contract:
205+
206+
1. **Using Script**
207+
- Use the provided `JoinLottery.s.sol` script
208+
- Set required environment variables (refer to `.env.example`):
209+
```bash
210+
export PRIVATE_KEY="your_private_key"
211+
export BSC_TESTNET_RPC_URL="https://bsc-testnet-rpc.publicnode.com"
212+
export VRF_WRAPPER="0x471506e6ADED0b9811D05B8cAc8Db25eE839Ac94"
213+
export LOTTERY_ADDRESS="0x27e5De33cB6d31894a875891D3D86aA5F5b8aB2a"
214+
export VALUE="8000000000000000" # 0.008 BNB for VRF fee
215+
```
216+
- Run the script:
217+
```bash
218+
forge script script/JoinLottery.s.sol:JoinLottery --rpc-url $BSC_TESTNET_RPC_URL --broadcast -vvvv
219+
```
220+
221+
2. **Using BSCScan**
222+
- Connect your wallet (e.g., MetaMask) to BSC Testnet
223+
- Visit the contract page on BSCScan
224+
- Use the "Write Contract" section to interact with the contract
225+
- Make sure your wallet has enough BNB for gas fees (at least 0.008 BNB for VRF fee)
226+
227+
### 6.2 Getting Test BNB
228+
229+
To get test BNB for the BSC Testnet:
230+
1. Join the [BSC Discord](https://discord.com/invite/bnbchain)
231+
2. Follow the guide in the #faucet-guide channel
232+
3. Request test BNB from the faucet
233+
234+
### 6.3 Transaction Flow
235+
236+
When you interact with the contract, you can observe the following transaction flow:
237+
238+
1. **Join Transaction**
239+
- When you call the `join` function, a transaction is sent to the BSC Testnet
240+
- This transaction includes the VRF request fee and initiates the random number generation process
241+
- Example transaction: [0x94a9e62a5a35af01a671b14a3bb9092d97abaa22494989dbc2c4faa27a3ec5ad](https://testnet.bscscan.com/tx/0x94a9e62a5a35af01a671b14a3bb9092d97abaa22494989dbc2c4faa27a3ec5ad)
242+
243+
244+
2. **VRF Callback Transaction**
245+
- After approximately 3 blocks, Chainlink VRF generates the random number
246+
- The `fulfillRandomWords` function is automatically called with the random number
247+
- This determines if you win and handles prize distribution
248+
- Example transaction: [0xba1612cc888d270ab380cc2776e5a4156e26fa7c371872f2a3d288d001800d90](https://testnet.bscscan.com/tx/0xba1612cc888d270ab380cc2776e5a4156e26fa7c371872f2a3d288d001800d90)
249+
250+
The entire process demonstrates how Chainlink VRF provides provably fair random numbers in a decentralized manner, with each step verifiable on the blockchain.

basics/ethereum/vrf/script/DeployLottery.s.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ contract DeployLottery is Script {
1111
function run() external returns (Lottery) {
1212
// Get deployment parameters from environment variables
1313
uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
14-
address owner = vm.envOr("OWNER", msg.sender);
14+
address deployer = vm.addr(deployerPrivateKey);
1515
uint256 winningRate = vm.envOr("WINNING_RATE", DEFAULT_WINNING_RATE);
1616
address vrfWrapper = vm.envAddress("VRF_WRAPPER");
1717

@@ -21,7 +21,7 @@ contract DeployLottery is Script {
2121
// Deploy Lottery contract
2222
Lottery lottery = new Lottery(
2323
vrfWrapper,
24-
owner,
24+
deployer,
2525
winningRate
2626
);
2727

0 commit comments

Comments
 (0)