Deploy & Interact with a KYC Smart Contract
You will set up Truffle + Ganache, deploy KYC.sol
, and interact through the Truffle console. Keep terminal screenshots as proof.
Reference files
1. Set Up Your Development Environment
1.1 Install Node.js and npm
Download and install Node.js from nodejs.org
. npm comes with Node.
1.2 Install Truffle and Ganache
npm install -g truffle
# Option A: Ganache GUI from trufflesuite.com/ganache
# Option B (CLI):
npm install -g ganache
2. Initialize a Truffle Project
mkdir my-project
cd my-project
truffle init
This creates contracts/
, migrations/
, test/
, and config files.
3. Write Your KYC Smart Contract
3.1 Create contracts/KYC.sol
(fixed/compilable)
The prompt version had minor typos (uinti
, spacing). Use this corrected version to compile successfully.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract KYC {
// Define Bank and Customer structs
struct Bank {
string name;
address bankAddress;
}
struct Customer {
string name;
address customerAddress;
bool isVerified;
}
// Mappings to store bank and customer data
mapping(address => Bank) public banks;
mapping(address => Customer) public customers;
// Arrays to store the list of bank and customer addresses
address[] public bankList;
address[] public customerList;
// Events
event BankAdded(string name, address bankAddress);
event CustomerAdded(string name, address customerAddress);
event CustomerVerified(address customerAddress);
// Function to add a new bank
function addNewBank(string memory name, address bankAddress) public {
require(bankAddress != address(0), "Invalid address");
require(bytes(name).length > 0, "Bank name cannot be empty");
banks[bankAddress] = Bank(name, bankAddress);
bankList.push(bankAddress);
emit BankAdded(name, bankAddress);
}
// Function to add a new customer
function addNewCustomer(string memory name, address customerAddress) public {
require(customerAddress != address(0), "Invalid address");
require(bytes(name).length > 0, "Customer name cannot be empty");
customers[customerAddress] = Customer(name, customerAddress, false);
customerList.push(customerAddress);
emit CustomerAdded(name, customerAddress);
}
// Function to verify a customer
function verifyCustomer(address customerAddress) public {
require(customerAddress != address(0), "Invalid address");
require(customers[customerAddress].customerAddress != address(0), "Customer does not exist");
customers[customerAddress].isVerified = true;
emit CustomerVerified(customerAddress);
}
// Function to retrieve all banks
function getBanks() public view returns (Bank[] memory) {
Bank[] memory allBanks = new Bank[](bankList.length);
for (uint i = 0; i < bankList.length; i++) {
allBanks[i] = banks[bankList[i]];
}
return allBanks;
}
// Function to retrieve all customers
function getCustomers() public view returns (Customer[] memory) {
Customer[] memory allCustomers = new Customer[](customerList.length);
for (uint i = 0; i < customerList.length; i++) {
allCustomers[i] = customers[customerList[i]];
}
return allCustomers;
}
}
3.2 Compile Your Contract
truffle compile
4. Migrate Your Smart Contract
4.1 Create Migration Script
// migrations/2_deploy_contracts.js
const KYC = artifacts.require("KYC");
module.exports = function (deployer) {
deployer.deploy(KYC);
};
4.2 Run Migrations
Start Ganache (GUI quickstart or npx ganache
). In another terminal:
truffle migrate --network development
5. Interact Using the Truffle Console
5.1 Start Ganache
Open Ganache and start a workspace (RPC usually http://127.0.0.1:7545
).
5.2 Open Truffle Console
truffle console --network development
5.3 Run the Following in the Console
const KYC = artifacts.require("KYC");
const kyc = await KYC.deployed();
const accounts = await web3.eth.getAccounts();
// Add new banks
await kyc.addNewBank("Bank1", accounts[1]);
await kyc.addNewBank("Bank2", accounts[2]);
// Add new customers
await kyc.addNewCustomer("Customer1", accounts[3]);
await kyc.addNewCustomer("Customer2", accounts[4]);
// Verify a customer
await kyc.verifyCustomer(accounts[3]);
// Retrieve and log banks and customers
const banks = await kyc.getBanks();
console.log("Banks:", banks);
const customers = await kyc.getCustomers();
console.log("Customers:", customers);
Appendix 1 – Contract Explanation (from prompt)
This KYC (Know Your Customer) smart contract is designed to manage KYC processes on Ethereum. Highlights:
- Admin role: An admin controls which banks can participate and their permissions.
- Banks: Added by admin; may add customers and perform KYC based on permissions.
- Customers: Added by banks; have data, a validating bank, and a KYC status.
Key Components
- Structs – Bank (name, kyc count, address, permissions) and Customer (name, data, validating bank, kyc status).
- Mappings –
banks
maps address→Bank;customersInfo
maps username→Customer.
Functions
- Admin:
addNewBank
,blockBankFromKYC
,allowBankFromKYC
,blockBankFromAddingNewCustomers
,allowBankFromAddingNewCustomers
. - Bank:
addNewCustomerToBank
,viewCustomerData
,addNewCustomerRequestForKYC
. - Utility:
getCustomerKycStatus
, areBothStringsSame (string equality helper).
Note: The minimal KYC.sol used in this HW implements a simpler subset so you can practice deployment and function calls quickly.
Troubleshooting
- Compilation errors: Ensure you used the corrected
KYC.sol
above and Solidity compiler ≥ 0.8.0. - "Could not connect to your Ethereum client": Start Ganache first; check RPC and port (e.g., 7545).
- Wrong network: If Truffle console can’t find the deployment, run
truffle migrate --reset
or verifynetworks
intruffle-config.js
. - Console reverts: Check addresses exist before verifying; follow the order: add bank → add customer → verify customer.
Submission Checklist
- Screenshot: successful
truffle compile
. - Screenshot:
truffle migrate --network development
with deployed contract address. - Screenshot(s): Truffle console showing calls and logs for banks & customers (including one verified customer).
- Optional: short note describing one improvement you’d add (e.g., admin controls, permissions).
Name your file:
HW3_FirstnameLastname.pdf
with screenshots in order.