Skip to content

Instantly share code, notes, and snippets.

@developerfred
Created September 26, 2024 01:59
Show Gist options
  • Save developerfred/84512fb238fe24385b7657fc3165cdbf to your computer and use it in GitHub Desktop.
Save developerfred/84512fb238fe24385b7657fc3165cdbf to your computer and use it in GitHub Desktop.
Ad Manager Smart Contract Interaction Guide
// Ad Manager Smart Contract Interaction Guide
// Contract Address: 0x020243968704ccF8202Afd1F1134a90953385877
import { createPublicClient, http, parseAbi } from 'viem'
import { base } from 'viem/chains'
// Initialize the Viem client
const client = createPublicClient({
chain: base,
transport: http(),
})
// ABI (partial, focusing on main functions)
const abi = parseAbi([
'function createAdvertisement(string _link, string _imageUrl, address _referrer) payable',
'function recordEngagement(uint256 _adIndex)',
'function getCurrentAd() view returns (string, string, uint256, address, address, bool, uint256)',
'function getNextAdPrice() view returns (uint256)',
'function getAdTokenBalance(address _address) view returns (uint256)',
'function claimChiefOfAdvertising()',
'function getUserStats(address _user) view returns (uint256, uint256, uint256, uint256, uint256, bool, bool)',
])
/**
* Ad Manager Smart Contract Overview
*
* This contract implements a decentralized advertising platform with the following key features:
*
* 1. Advertisement Creation and Management:
* - Users can create and manage advertisements
* - Dynamic pricing based on the number of active ads
* - Referral system for discounts and rewards
*
* 2. Engagement and Rewards:
* - Users earn tokens for engaging with advertisements
* - Weekly bonuses for top engagers
* - Leveling system based on total engagements
*
* 3. Gamification Elements:
* - Achievements system with unlockable rewards
* - Reputation system affecting advertisement pricing
* - Community challenges with shared rewards
* - Special events with increased reward multipliers
*
* 4. Governance and Roles:
* - Chief of Advertising role with additional benefits
*
* 5. Token Economy:
* - Native ERC20 token (AdToken) for platform interactions
* - Token minting for rewards and incentives
*/
// 1. Create an Advertisement
/**
* Creates a new advertisement on the platform.
* @param {string} link - The URL of the advertisement
* @param {string} imageUrl - The URL of the ad image
* @param {string} referrer - The address of the referrer (use zero address if none)
*
* Note: This function is payable. The required payment is determined by getNextAdPrice().
* If a valid referrer is provided, a discount is applied to the ad price.
*/
async function createAd(link, imageUrl, referrer) {
const price = await client.readContract({
address: '0x020243968704ccF8202Afd1F1134a90953385877',
abi,
functionName: 'getNextAdPrice',
})
const { request } = await client.prepareTransactionRequest({
account: '0xYourAddress',
to: '0x020243968704ccF8202Afd1F1134a90953385877',
data: client.encodeFunctionData({
abi,
functionName: 'createAdvertisement',
args: [link, imageUrl, referrer],
}),
value: price,
})
const hash = await client.sendTransaction(request)
console.log('Ad created! Transaction hash:', hash)
}
// 2. Record Engagement
/**
* Records user engagement with an advertisement.
* @param {number} adIndex - The index of the advertisement
*
* Users earn tokens for engaging with ads. The reward amount depends on the user's level
* and any active special events. There's a cooldown period between engagements.
*/
async function engageWithAd(adIndex) {
const { request } = await client.prepareTransactionRequest({
account: '0xYourAddress',
to: '0x020243968704ccF8202Afd1F1134a90953385877',
data: client.encodeFunctionData({
abi,
functionName: 'recordEngagement',
args: [adIndex],
}),
})
const hash = await client.sendTransaction(request)
console.log('Engagement recorded! Transaction hash:', hash)
}
// 3. Get Current Ad
/**
* Retrieves the current active advertisement.
* @returns {Array} An array containing ad details: [link, imageUrl, price, advertiser, referrer, isActive, engagements]
*/
async function getCurrentAd() {
const ad = await client.readContract({
address: '0x020243968704ccF8202Afd1F1134a90953385877',
abi,
functionName: 'getCurrentAd',
})
console.log('Current Ad:', ad)
return ad
}
// 4. Get Ad Token Balance
/**
* Retrieves the Ad Token balance of a given address.
* @param {string} address - The address to check the balance for
* @returns {BigInt} The Ad Token balance
*/
async function getAdTokenBalance(address) {
const balance = await client.readContract({
address: '0x020243968704ccF8202Afd1F1134a90953385877',
abi,
functionName: 'getAdTokenBalance',
args: [address],
})
console.log('Ad Token Balance:', balance)
return balance
}
// 5. Claim Chief of Advertising
/**
* Attempts to claim the Chief of Advertising role.
*
* Requirements:
* - Must have created at least one advertisement
* - Must hold a minimum number of Ad Tokens
* - Must have reached a minimum referral level
*
* The Chief of Advertising receives bonuses from new ad creations and engagements.
*/
async function claimChiefOfAdvertising() {
const { request } = await client.prepareTransactionRequest({
account: '0xYourAddress',
to: '0x020243968704ccF8202Afd1F1134a90953385877',
data: client.encodeFunctionData({
abi,
functionName: 'claimChiefOfAdvertising',
}),
})
const hash = await client.sendTransaction(request)
console.log('Chief of Advertising claimed! Transaction hash:', hash)
}
// 6. Get User Stats
/**
* Retrieves comprehensive stats for a user.
* @param {string} address - The address of the user
* @returns {Array} An array containing user stats:
* [adsCreated, adsEngaged, timesChief, referralsCount, achievementsUnlocked, challengeParticipation, eventParticipation]
*/
async function getUserStats(address) {
const stats = await client.readContract({
address: '0x020243968704ccF8202Afd1F1134a90953385877',
abi,
functionName: 'getUserStats',
args: [address],
})
console.log('User Stats:', stats)
return stats
}
// Example usage
createAd('https://example.com', 'https://example.com/image.jpg', '0x0000000000000000000000000000000000000000')
engageWithAd(0)
getCurrentAd()
getAdTokenBalance('0xYourAddress')
claimChiefOfAdvertising()
getUserStats('0xYourAddress')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment