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
│ ├─ 1_verify.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 (>= 18) and pnpm
- 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
pnpm installBelow is a list of useful scripts defined in package.json. Use pnpm run <command>.
Forces a clean compilation of the Solidity contracts:
pnpm run compileExecutes the test suite:
pnpm run testSpins up a local Hardhat development blockchain:
pnpm run local-nodeDeploy contracts to your local node or a specified network:
pnpm run deploy:contract
# Deploy to a local Hardhat node
pnpm run deploy:localhost
# Deploy to a configured remote network
pnpm run deploy:contract -- --network <your-network>If you're deploying to a public network (e.g., Etherscan-supported networks), you can verify the contracts:
pnpm run verify -- --network <your-network>
# Override the address if needed
VERIFY_ADDRESS=0xYourContractAddress pnpm run verify -- --network <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,
},
mandalaTestnet: {
url: 'https://rpc1-testnet.mandalachain.io',
chainId: 20011,
accounts: process.env.NETWORK_TESTNET_PRIVATE_KEY ? [process.env.NETWORK_TESTNET_PRIVATE_KEY] : [],
},
mandalaMainnet: {
url: 'https://rpc1-mainnet.mandalachain.io',
chainId: 20010,
accounts: process.env.NETWORK_MAINNET_PRIVATE_KEY ? [process.env.NETWORK_MAINNET_PRIVATE_KEY] : [],
},
},- Local Deployment:
- Run
pnpm run local-nodein one terminal to start a local Hardhat node. - In a new terminal, run
pnpm run 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 the deploy script with the
--network <networkName>option, for example:pnpm run deploy:contract -- --network <your-network>
- Configure your
- Hardhat console:
pnpm exec hardhat console --network localhost - Scripts:
Additional setup scripts such as
scripts/1_deploy.tsandscripts/1_verify.tsdemonstrate the deploy and verification flow for theDataSharingcontract.
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:
pnpm run testUse test-gas or test-extended for different reporting modes.