Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Main #39

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Main #39

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions portfolio_management/app/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from flask import Flask, jsonify, render_template, request
from web3_interface import get_portfolio, add_stock, rebalance_portfolio
from flask_cors import CORS

app = Flask(__name__)
CORS(app)

# Route to render the homepage
@app.route('/')
def index():
return render_template('index.html')

# API endpoint to get a portfolio
@app.route('/api/portfolio', methods=['GET'])
def portfolio():
user_address = request.args.get('address')
portfolio_data = get_portfolio(user_address)
return jsonify(portfolio_data)

# API endpoint to add a stock
@app.route('/api/add_stock', methods=['POST'])
def add_stock_route():
data = request.json
add_stock(data['address'], data['symbol'], data['quantity'], data['average_price'])
return jsonify({"message": "Stock added successfully"})

# API endpoint to rebalance the portfolio
@app.route('/api/rebalance', methods=['POST'])
def rebalance():
user_address = request.json.get('address')
rebalance_portfolio(user_address)
return jsonify({"message": "Portfolio rebalanced successfully"})

if __name__ == '__main__':
app.run(debug=True)
44 changes: 44 additions & 0 deletions portfolio_management/app/static/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
async function getPortfolio() {
const address = document.getElementById('address').value;
const response = await fetch(`/api/portfolio?address=${address}`);
const portfolioData = await response.json();
document.getElementById('portfolio-data').innerHTML = JSON.stringify(portfolioData);
}

async function addStock() {
const address = document.getElementById('address').value;
const symbol = document.getElementById('symbol').value;
const quantity = document.getElementById('quantity').value;
const averagePrice = document.getElementById('averagePrice').value;

const response = await fetch('/api/add_stock', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
address,
symbol,
quantity,
average_price: averagePrice
})
});

const result = await response.json();
alert(result.message);
}

async function rebalancePortfolio() {
const address = document.getElementById('address').value;

const response = await fetch('/api/rebalance', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ address })
});

const result = await response.json();
alert(result.message);
}
32 changes: 32 additions & 0 deletions portfolio_management/app/static/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
padding: 20px;
}

.container {
background-color: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
max-width: 600px;
margin: auto;
}

input {
display: block;
width: 100%;
margin: 10px 0;
padding: 10px;
border-radius: 5px;
border: 1px solid #ccc;
}

button {
padding: 10px 20px;
background-color: #28a745;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
39 changes: 39 additions & 0 deletions portfolio_management/app/templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>NexTrade Portfolio Manager</title>
<link rel="stylesheet" href="/static/style.css">
<script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script>
</head>
<body>
<div class="container">
<h1>Automated Portfolio Management</h1>

<!-- Portfolio Section -->
<div id="portfolio">
<input type="text" id="address" placeholder="Enter your Ethereum address">
<button onclick="getPortfolio()">View Portfolio</button>
<div id="portfolio-data"></div>
</div>

<!-- Add Stock Section -->
<div id="add-stock">
<h2>Add Stock to Portfolio</h2>
<input type="text" id="symbol" placeholder="Stock Symbol">
<input type="number" id="quantity" placeholder="Quantity">
<input type="number" id="averagePrice" placeholder="Average Price">
<button onclick="addStock()">Add Stock</button>
</div>

<!-- Rebalance Section -->
<div id="rebalance">
<h2>Rebalance Portfolio</h2>
<button onclick="rebalancePortfolio()">Rebalance</button>
</div>
</div>

<script src="/static/script.js"></script>
</body>
</html>
34 changes: 34 additions & 0 deletions portfolio_management/app/web3_interface.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from web3 import Web3

# Connect to local Ethereum node or testnet
w3 = Web3(Web3.HTTPProvider("http://127.0.0.1:8545")) # Or use Infura for testnets

# ABI for the PortfolioManager contract
contract_abi = [
# Add your contract's ABI here
]

contract_address = Web3.toChecksumAddress('0xYourContractAddress')
portfolio_contract = w3.eth.contract(address=contract_address, abi=contract_abi)

def get_portfolio(user_address):
user_address = Web3.toChecksumAddress(user_address)
return portfolio_contract.functions.getPortfolio(user_address).call()

def add_stock(user_address, symbol, quantity, average_price):
user_address = Web3.toChecksumAddress(user_address)
tx = portfolio_contract.functions.addStock(symbol, quantity, average_price).buildTransaction({
'from': user_address,
'nonce': w3.eth.getTransactionCount(user_address)
})
signed_tx = w3.eth.account.signTransaction(tx, private_key="YourPrivateKey")
w3.eth.sendRawTransaction(signed_tx.rawTransaction)

def rebalance_portfolio(user_address):
user_address = Web3.toChecksumAddress(user_address)
tx = portfolio_contract.functions.rebalancePortfolio().buildTransaction({
'from': user_address,
'nonce': w3.eth.getTransactionCount(user_address)
})
signed_tx = w3.eth.account.signTransaction(tx, private_key="YourPrivateKey")
w3.eth.sendRawTransaction(signed_tx.rawTransaction)
32 changes: 32 additions & 0 deletions portfolio_management/contracts/PortfolioManager.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract PortfolioManager {
struct Stock {
string symbol;
uint256 quantity;
uint256 averagePrice;
}

mapping(address => Stock[]) public portfolios;

// Function to add a stock to the portfolio
function addStock(string memory symbol, uint256 quantity, uint256 averagePrice) public {
portfolios[msg.sender].push(Stock(symbol, quantity, averagePrice));
}

// Function to get the portfolio for a user
function getPortfolio(address user) public view returns (Stock[] memory) {
return portfolios[user];
}

// Function to rebalance the portfolio (simple logic for demonstration)
function rebalancePortfolio() public {
Stock[] storage stocks = portfolios[msg.sender];

// Example: Rebalance by updating average prices by some logic (customize further)
for (uint256 i = 0; i < stocks.length; i++) {
stocks[i].averagePrice = (stocks[i].averagePrice * 105) / 100; // Adding 5% to average price
}
}
}
13 changes: 13 additions & 0 deletions portfolio_management/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
The NexTrade Portfolio Management Tool is an automated portfolio management feature integrated into the NexTrade platform. It allows users to:
Track their portfolio holdings and performance.
Automatically rebalance their portfolio based on predefined criteria.
Receive market insights, alerts, and risk assessment.
Learn about portfolio management strategies through educational resources.
The tool interacts with the Ethereum blockchain using a Solidity smart contract and the Web3.py library, providing a decentralized and secure solution for managing investments.

Features
Portfolio Tracking: Users can view their current stock holdings and portfolio performance.
Automated Rebalancing: Rebalance portfolios based on risk tolerance and target allocations.
Market Insights & Alerts: Get real-time insights and notifications on market trends.
Risk Assessment: Analyze portfolio risk and receive suggestions to mitigate risks.
Educational Resources: Learn how to manage your portfolio and investments effectively.