百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
B

blockchain-bike-rental

> 编程语言
开源

基于 Solidity 的"自行车租赁店"在以太坊网络上运行。

398 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

基于 Solidity 的"自行车租赁店"在以太坊网络上运行。

Bike Rental Shop

  • Bike Rental Shop
    • About
    • Overview
    • Main features
    • Rental service start / stop
    • Smart contracts
    • Payment model
    • Gas optimization
    • Further improvements
    • Use case tested
    • Usage
    • Expected test outcome
    • BTC address

About

This repository contains the source code and dependencies required to deploy a Solidity based "BIKE RENTAL SHOP" on Ethereum network.

Overview

The following diagram shows a high level overview of the interaction between the main components and actors in the bike rental service:

Main features

This is a summary with the main features implemented in the BikeRental contract:

  • Billing: the rental fee is calculated as a function of time (i.e. duration of rental in seconds)
  • Payment medium: customers can choose to pay with Ether or Tokens
  • Payment priority: BikeRental contract will try to fullfil pending payment with Token funds from customer's TokenAccount as a first priority. If payment is not cancelled 100%, contract will try to fulfill pending payment with Ether funds from customer's EtherAccount balance as a second priority.
  • Collateral rate: a premium rate (i.e. 20% cheaper than standard rate) is applicable to those customers that choose to fund their tokenAccount or etherAccount above a predefined collateral level (i.e. etherCollateralThreshold or tokenCollateralThreshold ).
  • Un-spent funds: Token or Ether amount that is not spent when rental finishes, is automatically returned/refunded to customers.
  • Customer debt: if customer rental fee is greater than available funds, a debt is generated and customer is banned from renting again until debt is cancelled.
  • Contract upgradeability: additional functions are implemented to enable the update of main business parameters (i.e. minimum balance required, rate, collateral threshold), that only BikeRental owner is allowed to execute:
javascript
    function setRate(uint _rate) external onlyOwner
    function setCollateralThreshold(uint _threshold) external onlyOwner
    function setEtherMinimumBalance(uint _etherMin) external onlyOwner
    function setTokenConversionRate(uint8 _conversion) external onlyOwner
    function setCollateralPremium(uint8 _premium) external onlyOwner

Rental service start / stop

  • The rental service starts when customer calls function startRental()
  • The rental service is finished when customer calls function stopRental()

Smart contracts

The following two smart contracts are required to implement the Bike Rental Shop service:

1. BikeRental.sol

Is the main contract, that implements the business logic for the BikeRental Shop.

  • Main state variables:
…
  • Main external functions:
javascript
    //@dev: `buyTokens` used by customer to purchase rental Tokens in exchange of Ether    
    function buyTokens() payable public

    //@dev: `startRental` function is called by customers to start the bike rental service
    //@params: `bikeId` is id of bike to rent     
    function startRental (uint8 _bikeId) payable public{

    //@dev: `stopRental` function is called by customers to stop the bike rental service     
     function stopRental () external {

2. Token.sol

  • ERC20 token compliance smart contract
  • Addon functions:
javascript
function burn(address _to, uint256 _amount) onlyOwner external 
function mint(address _to, uint256 _amount) onlyOwner external

Payment model

All customers have 2 accounts at BikeRentalShop: etherAccount and tokenAccount.

And they can choose to pay the rental service with ether, tokens or both.

Below is included a description of each payment model:


Paying with ETHER

  • If the customer chooses to pay bike rental with Ether, he/she calls startRental() (payable) and sends Ether to BikeRental.sol contract in order to provide funds to the customerEtherAccount.

  • When the customer returns the bike, he/she calls stopRental().

  • At this point the BikeRental.sol contract will debit the total fee from the customerEtherAccount, and credit it to the rental shop owner's account.

  • The remaining (un-spent) ether funds are transferred back to the customer.


Paying with TOKENS

  • If the customer wishes to pay bike rental with Tokens, first he/she should purchase Tokens with Ether by calling buyTokens() (payable). The BikeRental.sol contract sends the received Ether to the owner of the bike rental shop.

  • Then customer should approve() the BikeRental.sol contract to transfer the desired amount of tokens into the customerTokenAccount.

  • When customer wants to start the rental, he/she calls startRental() and the BikeRental.sol contract will execute the transfer of approved tokens to the customerTokenAccount.

  • When the customer returns the bike, he/she calls stopRental().

  • At this point the BikeRental.sol contract will debit the total fee from the customerTokenAccount, and transfer the amount of tokens back to the BikeRental pool of tokens.

  • The remaining (un-spent) Token funds are transferred back to the customer.


Gas optimization

The following is a short list of some considerations taken into account in order to minimize cost of gas

  • Enforcement of debt cancelling before rental to avoid revert after updating balances
  • uint8 type storage allocation where possible
  • byte type instead of string type
  • use of function external instead of public
  • use events to keep track of data off-chain (i.e. record of bike details)

Further improvements

  • Implement a minimum rental fee (applicable when rental elapsed time is below i.e. 15 minutes)
  • Deploy an off-chain Oracle that updates the rental rate according to real time traffic statistics
  • Set rate according to time of day (i.e., low, medium, high)
  • Develop additional use case tests
  • Usage of SafeMath library

Use case tested

The following are the use cases currently implemented in the truffle test suite

  • Use Case1: customer transfer Ether (above collateral threshold) and starts/stops bike rent (BikeRental.test1.js)
  • Use Case2: customer buys tokens, approves transfer of tokens, and starts/stops bike rent (BikeRental.test2.js)
  • Use Case3: customer buys Tokens, approves transfer of tokens, starts/stops bike rent, debt is created, and customer transfer additional Ether to cancel debt (BikeRental.test3.js)
  • Use Case4: customer buys Tokens, approves transfer of tokens, transfer Ether, and starts/stops bike rent (BikeRental.test4.js)

Usage

Dependencies

  • Truffle
  • Node.js
  • npm
  • Ganache Cli

Installation

bash
git clone https://github.com/alejoacosta74/BikeRentalShop bikeRentalShop
cd bikeRentalShop
npm install
truffle init

Compile

bash
truffle compile

On a separate terminal start Ganache-CLI:

bash
ganache-cli -m "" -h 0.0.0.0

Migrate and deploy to Ganache

bash
truffle migrate --network development

Run tests

javascript
truffle test --network development  //run test suite against Ganache

Expected test outcome

…

BTC address

bc1q89aalael3kr5vhjn6ss4g4fscxnesc9gw8sfyryhzq7nhdphlm9semr93f

@alejoacosta74

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

JavaScriptetherethereumsoliditytokenization

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言