visit
The foundation of our decentralized DSP is a set of smart contracts that manage ad campaigns, user rewards, and NFT minting.
pragma solidity ^0.8.0;
contract AdCampaign {
struct Campaign {
uint256 id;
address advertiser;
string ipfsHash;
uint256 budget;
uint256 reward;
bool isActive;
}
uint256 private campaignCounter;
mapping(uint256 => Campaign) public campaigns;
function createCampaign(string memory ipfsHash, uint256 budget, uint256 reward) public {
campaignCounter++;
campaigns[campaignCounter] = Campaign(campaignCounter, msg.sender, ipfsHash, budget, reward, true);
}
function toggleCampaignStatus(uint256 campaignId) public {
require(campaigns[campaignId].advertiser == msg.sender, "Not the owner of the campaign");
campaigns[campaignId].isActive = !campaigns[campaignId].isActive;
}
}
from web3 import Web3
# Connect to the Ethereum node
w3 = Web3(Web3.HTTPProvider('<YOUR_ETHEREUM_NODE_URL>'))
# Load the contract ABI and address
contract_abi = [...] # Contract ABI goes here
contract_address = '0x123...'
# Initialize the contract object
contract = w3.eth.contract(address=contract_address, abi=contract_abi)
# Define the advertiser's account
advertiser_account = w3.eth.account.privateKeyToAccount('<YOUR_PRIVATE_KEY>')
# Create a new ad campaign
ipfs_hash = 'QmWATWQ7fVPP2EFGu71UkfnqhYXDYH566qy47CnJDgvs8u'
budget = 1000
reward = 10
# Estimate gas
gas_estimate = contract.functions.createCampaign(ipfs_hash, budget, reward).estimateGas({'from': advertiser_account.address})
# Send the transaction
transaction = contract.functions.createCampaign(ipfs_hash, budget, reward).buildTransaction({
'from': advertiser_account.address,
'gas': gas_estimate,
'gasPrice': w3.eth.gasPrice,
'nonce': w3.eth.getTransactionCount(advertiser_account.address)
})
signed_transaction = advertiser_account.signTransaction(transaction)
transaction_hash = w3.eth.sendRawTransaction(signed_transaction.rawTransaction)
In Python, we can use the ipfshttpclient
library to add and retrieve content from IPFS:
import ipfshttpclient
client = ipfshttpclient.connect('/ip4/127.0.0.1/tcp/5001/http')
# Add a file to IPFS
with open('ad_content.json', 'rb') as f:
result = client.add(f)
ipfs_hash = result['Hash']
# Retrieve content from IPFS
content = client.cat(ipfs_hash)
pragma solidity ^0.8.0;
import "@chainlink/contracts/src/v0.8/ChainlinkClient.sol";
contract AdDataOracle is ChainlinkClient {
uint256 public adPerformance;
address private oracle;
bytes32 private jobId;
uint256 private fee;
event AdPerformanceUpdated(uint256 performance);
constructor(address _oracle, bytes32 _jobId, uint256 _fee) {
setPublicChainlinkToken();
oracle = _oracle;
jobId = _jobId;
fee = _fee;
}
function requestAdPerformanceData() public {
Chainlink.Request memory req = buildChainlinkRequest(jobId, address(this), this.fulfill.selector);
req.add("get", "//example.com/api/ad_performance");
req.add("path", "performance");
sendChainlinkRequestTo(oracle, req, fee);
}
function fulfill(bytes32 _requestId, uint256 _performance) public recordChainlinkFulfillment(_requestId) {
adPerformance = _performance;
emit AdPerformanceUpdated(_performance);
}
}
// Load Web3
const Web3 = require('web3');
// Connect to Ethereum node
const web3 = new Web3('<YOUR_ETHEREUM_NODE_URL>');
// Load the contract ABI and address
const contractABI = [...] // Contract ABI goes here
const contractAddress = '0x123...';
// Initialize the contract object
const contract = new web3.eth.Contract(contractABI, contractAddress);
// Fetch ad campaign details
const campaignId = 1; // Example campaign ID
const campaign = await contract.methods.campaigns(campaignId).call();
// Load ad content from IPFS
const ipfs = require('ipfs-http-client');
const ipfsClient = ipfs.create();
const adContent = JSON.parse(await ipfsClient.cat(campaign.ipfsHash));
// Render ad content on the publisher's website or app
// (actual rendering logic will depend on the specific platform and ad format)
Lead image by on Unsplash