基于 Solidity 的"自行车租赁店"在以太坊网络上运行。
This repository contains the source code and dependencies required to deploy a Solidity based "BIKE RENTAL SHOP" on Ethereum network.
The following diagram shows a high level overview of the interaction between the main components and actors in the bike rental service:
This is a summary with the main features implemented in the BikeRental contract:
etherCollateralThreshold or tokenCollateralThreshold ). 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 onlyOwnerstartRental()stopRental()The following two smart contracts are required to implement the Bike Rental Shop service:
Is the main contract, that implements the business logic for the BikeRental Shop.
… //@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 {function burn(address _to, uint256 _amount) onlyOwner external
function mint(address _to, uint256 _amount) onlyOwner externalAll 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:
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.
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.
The following is a short list of some considerations taken into account in order to minimize cost of gas
uint8 type storage allocation where possiblebyte type instead of string typeexternal instead of publicThe following are the use cases currently implemented in the truffle test suite
BikeRental.test1.js)BikeRental.test2.js)BikeRental.test3.js)BikeRental.test4.js)git clone https://github.com/alejoacosta74/BikeRentalShop bikeRentalShop
cd bikeRentalShop
npm install
truffle inittruffle compileOn a separate terminal start Ganache-CLI:
ganache-cli -m "" -h 0.0.0.0truffle migrate --network developmenttruffle test --network development //run test suite against Ganache…bc1q89aalael3kr5vhjn6ss4g4fscxnesc9gw8sfyryhzq7nhdphlm9semr93f
@alejoacosta74
暂无开放 Issues,或尚未同步最近议题。