This repository showcases aims to foster adoption and enhance microfinance solutions.The core element of this repository is a Hardhat-based smart contract suite that implements registration and delegation functionalities for creditors and debtors. These contracts collectively manage data-sharing requests, purchase of packages, and the essential on-chain registration of users.
- Project Structure
- Smart Contracts
- Prerequisites
- Installation
- Scripts and Commands
- Usage and Development
- Testing
├─ contracts/
│ ├─ core/
│ │ ├─ Delegation.sol
│ │ └─ Registration.sol
│ ├─ DataSharing.sol
│ └─ ...
├─ scripts/
│ ├─ 1_deploy.ts
│ ├─ 2_deploy_localhost.ts
├─ test/
│ ├─ index.ts
├─ package.json
├─ hardhat.config.ts
├─ README.md (this file)
└─ ...
-
contracts/:
- core/: Contains base contracts like
Delegation.solandRegistration.sol. - DataSharing.sol: Main contract extending
Delegationand providing higher-level functionality for data sharing and package purchases.
- core/: Contains base contracts like
-
scripts/: Deployment and setup scripts for various environments.
-
test/: Contains unit/integration tests for each contract.
-
Inherits from:
Delegation.sol→Registration.solOwnable(from OpenZeppelin)
-
Key Responsibilities:
- Platform Management: Authorizes a
_platformaddress for sensitive operations. - Event Emissions:
- Tracks creditor and debtor registrations with metadata.
- Logs delegation requests and approvals.
- Logs purchase package events.
- High-Level Registration: Adds/removes creditors and debtors, ensuring only the platform can call these methods.
- Delegation Flow: Requests delegation (consumer → provider), and provider approves or rejects.
- Purchase Packages: Records on-chain events for package acquisitions without storing large data structures.
- Platform Management: Authorizes a
-
Inherits from:
Registration.sol -
Key Responsibilities:
- Delegation Requests:
_requestDelegationhandles the creation of a delegation request from one creditor (consumer) to another (provider)._delegateallows the provider to approve or reject a request.
- Status Management: Keeps track of PENDING, APPROVED, REJECTED states.
- Storage:
- Maintains a
_debtorInfomapping for each debtor, storing creditors and their statuses. - Maintains a
_requestmapping for delegation requests between consumer-provider pairs.
- Maintains a
- Delegation Requests:
- Base Contract providing:
- Mappings:
_debtorsand_creditors, storing(hash → address)relationships. - Core Functions:
_addDebtor,_addCreditor→ Register new participants._removeDebtor,_removeCreditor→ Deregister participants.
- Validation: Checks for zero-address, zero-hash, or duplicate entries.
- Mappings:
- Node.js (>= 16) and npm or Yarn
- Hardhat globally or run locally with
npx. - A valid environment for test or deployment (e.g., local Hardhat node, Ganache, or public test network like Goerli/Polygon Mumbai).
Clone the repository and install dependencies:
git clone https://github.com/baliola/microfinance-smartcontract.git
cd microfinance-smartcontract
# Install dependencies
yarn installBelow is a list of useful scripts defined in package.json. Use either npm run <command> or yarn <command>.
Forces a clean compilation of the Solidity contracts:
yarn compileExecutes the test suite:
yarn testSpins up a local Hardhat development blockchain:
yarn local-nodeDeploy contracts to your local node or a specified network:
# or
yarn deploy --netowrk <your-network>
# Deploy to a local Hardhat node
yarn deploy-localhostIf you're deploying to a public network (e.g., Etherscan-supported networks), you can verify the contracts:
yarn verify <your-contract-address> --netowrk <your-network>Environment Configuration Create a .env file in the root directory (if not already present) with relevant settings:
NETWORK_TESTNET_URL=https://abcde
NETWORK_TESTNET_PRIVATE_KEY=0xasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasda
NETWORK_MAINNET_URL=https://abcde
NETWORK_MAINNET_PRIVATE_KEY=0xnasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasda
BLOCK_EXPLORER_API_KEY=
GAS_REPORTER_COIN_MARKET_CAP_API_KEY=In your hardhat.config.ts, reference these values to configure networks, e.g.:
networks: {
hardhat: {
chainId: 1337,
},
localhost: {
url: "http://127.0.0.1:8545",
chainId: 1337,
},
truffle: {
url: 'http://localhost:24012/rpc',
timeout: 60000,
gasMultiplier: DEFAULT_GAS_MULTIPLIER,
},
niskala: {
url: 'https://mlg1.mandalachain.io',
chainId: 6025,
accounts: process.env.NETWORK_TESTNET_PRIVATE_KEY ? [process.env.NETWORK_TESTNET_PRIVATE_KEY] : [],
},
devnet: {
url: 'https://nbs.mandalachain.io',
chainId: 895670,
accounts: process.env.NETWORK_TESTNET_PRIVATE_KEY ? [process.env.NETWORK_TESTNET_PRIVATE_KEY] : [],
},
},- Local Deployment:
- Run
yarn local-nodein one terminal to start a local Hardhat node. - In a new terminal, run
yarn deploy-localhostto deploy the contracts.
- Run
- Public Testnet/Mainnet:
- Configure your
.envwith the correct keys and network details. - Update network settings in
hardhat.config.ts. - Run npm run deploy with the
--network <networkName>option, for example:yarn deploy --netowrk <your-network>
- Configure your
- Hardhat console:
npx hardhat console --network localhost
- Scripts:
Additional setup scripts (e.g.,
scripts/1_deploy.ts) demonstrate common tasks like deploying contract.
All tests reside in the test/ folder. Each contract has a corresponding test file index.ts. Tests cover:
- Registration: Adding/removing creditors and debtors, validation checks.
- Delegation: Requesting, approving, and rejecting delegation.
- DataSharing: Purchasing packages, setting the platform address, event emissions. Running:
yarn testUse test-gas or test-extended for different reporting modes.