---
# System prepended metadata

title: week13

---

# Week 13: Gas Optimization, Oracles, and Chainlink VRF
# 

This week began with gas optimization techniques in Solidity, followed by an exploration of oracle systems and hands-on work with Chainlink’s Verifiable Random Function (VRF) and other tools.

### 1. Gas Optimization in Solidity
To reduce transaction costs, we learned several key strategies:

Using uint256/int256 over smaller types (EVM wordsize optimization).

Minimizing storage writes by caching variables in memory.

Packing variables into fewer storage slots (struct packing).

Using external visibility for functions when possible.

Leveraging events over storage for non-critical data.

Example: Struct Packing & Storage Savings

```
// Gas-inefficient: Wastes storage slots  
uint8 a;  // Slot 1 (32 bytes, but only uses 8)  
uint256 b; // Slot 2 (forces new slot)  

// Optimized: Packs variables into 1 slot  
struct Packed {  
    uint8 a;  // Shares Slot 1  
    uint248 b; // Fills remaining space  
}
```

Other optimizations:

calldata over memory for function parameters.
Loop bounds caching: Store array.length outside loops.
Early reverts: Check conditions before heavy operations.

## Understanding Oracles
## What Are Oracles?
Oracles are bridges between blockchains and external data (e.g., APIs, weather data). They enable smart contracts to interact with off-chain information securely.

we focused on chainlink as our oracle for the class,

### Why Chainlink?
Chainlink is the most widely used decentralized oracle network. It provides:

Tamper-proof data via multiple node operators.

Modular services like VRF, Automation, and Data Feeds.

### Chainlink VRF (Verifiable Random Function)
Problem: Blockchains are deterministic; randomness is hard.
Solution: Chainlink VRF provides provably fair randomness with:

On-chain verification: Uses cryptographic proofs.

User-paid requests: Requires LINK tokens for requests.

Example: Requesting Randomness

```
function requestRandomWinner() external {  
    uint256 requestId = COORDINATOR.requestRandomWords(  
        keyHash,  
        subscriptionId,  
        3, // Confirmations  
        100000, // Gas limit  
        1 // Number of random values  
    );  
}
```

Thats a bried summary for the week and im excited on the grounds covered so far and what the future holds.