This repository contains a Solidity smart contract designed to store users’ permissions (and related metadata) on Binance Smart Chain (BSC). Each user’s data is stored as a JSON string, allowing you to include various fields (e.g., timestamps, status, etc.). The contract is immutable and transparent, letting you permanently keep a record of who has enabled or revoked permissions.
- Stores JSON strings in a mapping of
address => string. - Emits an event (
UserJSONSet) whenever a user’s permissions data is updated. - Easily integrates with web3 libraries like
ethers.jsfor reading/writing data. - Verified on BscScan for public audit and convenient “Read/Write Contract” interaction.
- Node.js (v14+ recommended)
- Hardhat or another Ethereum development framework
- BNB for gas fees (Testnet or Mainnet)
- A wallet with private key you can use for deployment
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;
contract StorePermissions {
mapping(address => string) private userJsonData;
event UserJSONSet(address indexed user, string jsonData);
function setUserData(address _user, string calldata _jsonString) external {
userJsonData[_user] = _jsonString;
emit UserJSONSet(_user, _jsonString);
}
function getUserData(address _user) external view returns (string memory) {
return userJsonData[_user];
}
}setUserData(address _user, string calldata _jsonString)
Stores any valid JSON string on-chain (for example,{"permissions":"enabled","timestamp":"...","otherInfo":"..."}) keyed by the user’s address.getUserData(address _user)
Returns the raw JSON string for that user’s address.
├─ contracts/
│ └─ StorePermissions.sol # The primary Solidity contract
├─ scripts/
│ └─ deploy.js # Hardhat script to deploy the contract
├─ test/
│ └─ StorePermissions.test.js # (Optional) Example tests
├─ hardhat.config.js
├─ package.json
├─ README.md
└─ .env # (Ignored file containing private keys & config)
- Install Dependencies
npm install
- Update
.envwith your private key and any other secrets:PRIVATE_KEY=0xyourPrivateKey
- Configure
hardhat.config.jswith your BSC network settings:require("dotenv").config(); require("@nomicfoundation/hardhat-toolbox"); module.exports = { solidity: "0.8.18", networks: { bsctest: { url: "https://data-seed-prebsc-1-s1.binance.org:8545/", chainId: 97, accounts: [process.env.PRIVATE_KEY] }, bscmain: { url: "https://bsc-dataseed.binance.org/", chainId: 56, accounts: [process.env.PRIVATE_KEY] } } };
- Compile & Deploy to BSC Testnet (or Mainnet):
npx hardhat compile npx hardhat run scripts/deploy.js --network bsctest
- Verify the deployed contract on BscScan (optional but recommended):
npx hardhat verify --network bsctest <DEPLOYED_CONTRACT_ADDRESS>
import { ethers } from "ethers";
async function updatePermissions() {
const provider = new ethers.JsonRpcProvider("https://data-seed-prebsc-1-s1.binance.org:8545/");
const wallet = new ethers.Wallet(process.env.PRIVATE_KEY, provider);
const abi = [
"function setUserData(address _user, string _jsonString) external",
"function getUserData(address _user) external view returns (string)",
"event UserJSONSet(address indexed user, string jsonData)"
];
const contractAddress = "0xYourDeployedContract";
const contract = new ethers.Contract(contractAddress, abi, wallet);
// Example JSON
const jsonString = JSON.stringify({ permissions: "enabled", timestamp: new Date().toISOString() });
// Post to chain
const tx = await contract.setUserData("0xUserAddress", jsonString);
console.log("Transaction:", tx.hash);
await tx.wait();
console.log("Permissions updated on-chain!");
}
// Then call updatePermissions() or use your own script
updatePermissions();This project is licensed under the MIT License.