This example demonstrates a script that automatically adjusts the rental price of your servers on the Clore marketplace based on current demand. The goal is to set a higher price when demand is high and reduce it when demand drops, helping maximize revenue while keeping the server competitive on the market.
Objective
To dynamically adjust rental prices according to market demand, ensuring optimal pricing that attracts renters while maximizing revenue.
Prerequisites
Authorization token for Clore API.
At least one server listed on the Clore marketplace.
Implementation
import requests
import time
# Clore API credentials
AUTH_TOKEN = 'your_auth_token_here'
SERVERS_URL = 'https://api.clore.ai/v1/my_servers'
SET_PRICE_URL = 'https://api.clore.ai/v1/set_server_settings'
# Configuration for price adjustment
BASE_PRICE = 0.0001 # Base price in BTC or equivalent for reference
HIGH_DEMAND_MULTIPLIER = 1.5 # Multiplier to apply when demand is high
LOW_DEMAND_MULTIPLIER = 0.8 # Multiplier to apply when demand is low
# Demand threshold settings (example values)
HIGH_DEMAND_THRESHOLD = 80 # If above 80% servers are rented, consider it high demand
LOW_DEMAND_THRESHOLD = 20 # If below 20% servers are rented, consider it low demand
def get_servers():
headers = {'auth': AUTH_TOKEN}
response = requests.get(SERVERS_URL, headers=headers)
if response.status_code == 200:
data = response.json()
if data.get("code") == 0:
return data['servers']
else:
print("Error fetching server data:", response.status_code, response.text)
return []
def calculate_demand(servers):
total_servers = len(servers)
rented_servers = sum(1 for server in servers if server['rental_status'] > 0)
demand_percentage = (rented_servers / total_servers) * 100 if total_servers > 0 else 0
return demand_percentage
def adjust_price(server_id, new_price):
headers = {
'auth': AUTH_TOKEN,
'Content-type': 'application/json'
}
data = {
"name": f"server_{server_id}",
"availability": True,
"mrl": 72, # maximum rental length in hours (can be customized)
"on_demand": new_price,
"spot": new_price * 0.8 # Example of spot price being 80% of on-demand price
}
response = requests.post(SET_PRICE_URL, headers=headers, json=data)
if response.status_code == 200:
print(f"Price adjusted for server {server_id}: New Price = {new_price}")
else:
print(f"Failed to adjust price for server {server_id}: ", response.text)
def adjust_prices_based_on_demand():
servers = get_servers()
demand_percentage = calculate_demand(servers)
print(f"Current demand: {demand_percentage}%")
for server in servers:
if demand_percentage >= HIGH_DEMAND_THRESHOLD:
new_price = BASE_PRICE * HIGH_DEMAND_MULTIPLIER
elif demand_percentage <= LOW_DEMAND_THRESHOLD:
new_price = BASE_PRICE * LOW_DEMAND_MULTIPLIER
else:
new_price = BASE_PRICE # Keep the base price during normal demand
adjust_price(server['id'], new_price)
def main():
while True:
adjust_prices_based_on_demand()
time.sleep(1800) # Adjust prices every 30 minutes
if __name__ == "__main__":
main()
Explanation
get_servers: Retrieves the list of servers and their rental statuses.
calculate_demand: Calculates the demand percentage based on how many servers are currently rented.
adjust_price: Sets a new price for a specific server based on the calculated demand.
adjust_prices_based_on_demand: Checks the current demand and adjusts prices for each server accordingly.
main loop: Runs the demand-based price adjustment every 30 minutes.
Summary
This code monitors market demand and dynamically adjusts server prices to ensure optimal pricing, maximizing revenue during high demand and staying competitive during low demand. This automation helps server owners optimize their rental strategy without manual intervention.