ThisisaSmartContractthatrunsonEthereum
ItiswritteninSolidityandrepresentsaretailstore.Itsupportscustomerandproductregistrations.Everyregisteredcustomerownsashoppingcarttocollectproductsbeforecheckingout.
DappInthisearlyversionthere'snoproperwebinterfaceavailableandyou'llhavetousetruffleconsoletoaccessthecontract.InfutureI'llprovideaweb-appwritteninAngular4.x.Theultimategoalistonotonlyproduceaweb-sitebutacompleteweb-platformbehindit.Embeddingareal-worldbusinessmodelintosomethinglikeaDAppimpliescertainfunctionalities:
database[youcertainlydon'twanttostoreyourcustomerspersonaldataontheblockchain]
errorhandling[there'snoerror-handlinginEthereumbutyourbusinessisn'tEthereum]
transactions[Ethereumtransactionsaren'tyourbusinesstransactions]
unavoidableupdates[nocodeiseternal]
automaticbackups[I'mrepeatingmyself...seedatabasesabove]
backendAPIs[forexample:detailedproductinfos,currencyconversions,geo-locationsetc.]
...andmanyotherthings.
Givingcustomersaninterfacewheretheycanaddorremoveproductsto/fromtheirshoppingcartsisimportantbutnottheultimategoal.TheshoppingexperienceontheUIandasophisticatedbusinesslogicinthebackendmustbothexisttosupporteachother.Aslongaswecan'tputanon-public&fastdatabaseonEthereumwe'llhavetomaintainitsomewhereelse.AndtoachievethisgoalourDappwillrelyonbackendAPIs.
Currently,asimpledemotoplayaroundwithweb3-APIisavailable.Togettheabovedemoworkingpleasefollowthesesteps:
CompilethecontractswithtrufflecompileThenmovethenewlycreatedbuildfoldertosrcNowyoucanboottheappvianpmrunstart:hmrTokensStoreTokenswillsoonbesupported.Onecouldusethemtopurchasegoodsinthestoreorforinitialcoinofferings.Forexample:you'replanningtoopenastorethatdealswithcertainpopulargoodsbutyou'reunsurehowmanypotentialcustomersareoutthere.Nowyoucouldsimplybuysomeethersorothercoinstofinanceyourstore(topaygoodsinadvance,hireadevtocodeaproperDappforyourcustomersetc.).Noweverythingdependsonhowsuccessfulyourbusinesswillbe.Youmayormaynotbeabletosustainit.
Asweallknowtherearealwayscertainriskstotakecareofandthat'swhypeopletrytoconvinceotherpeopletosupporttheirbusinessideas.So,youdecidetosellsharesofyournascentbusinesstointerestedparties.Youcreateaproperbusinessinfomaterial,forexampleaweb-sitethatdescribesyourbusiness,howitshouldlooklike,whatarepotentialrisksetc.Yougenerateacertainamountoftokensbasedonapricethatcouldbefixedornot.Let'ssayyousell1MyStoreTokenfor0.001ETH.AdditionallyyoucandeterminecertainlimitsandhowlongyourICOwilllast.Ofcoursethere'snoobligationtocreateallofyourtokensinadvance.YoucouldeasilydefineadynamictokensupplythatdependsonincomingETHs.
Untilthefirstworkingversiongetsoutyoucantestthecurrentdevelopmentintruffleconsole:
Declareareferencevariablefordeployedtokencontract.
vartoken;
GetitsreferenceviaJSPromise.
BaseStoreToken.deployed().then(d=>token=d);
Displayinitialtokensupply.
token.initialSupply();
Transfer10tokensfromdefaultaddresstoweb3.eth.accounts[1]
token.transfer(web3.eth.accounts[1],10);
APINameGroupSignatureUsageReturnstransferOwnershipowneraddressstore.transferOwnership(new_owner_address)registerProductowneruint256,bytes32,bytes32,uint,uintstore.registerProduct(id,name,description,price,default_amount)boolderegisterProductowneruint256store.deregisterProduct(id)boolgetProductcustomeruint256store.getProduct(id)(bytes32name,bytes32description,uint256price,uint256default_amount)registerCustomerowneraddress,bytes32,uint256store.registerCustomer(cust_address,cust_name,cust_balance)boolderegisterCustomerowneraddressstore.deregisterCustomer(cust_address)boolinsertProductIntoCartcustomeruint256store.insertProductIntoCart(prod_id)(boolsuccess,uint256position_in_prod_mapping)removeProductFromCartcustomeruintstore.removeProductFromCart(prod_position_in_mapping)firesaneventonsuccessfulremovalgetCartcustomerstore.getCart()(uint256[]memoryproduct_ids,uint256completeSum)checkoutCartcustomerstore.checkoutCart()boolemptyCartcustomerstore.emptyCart()boolgetBalancecustomerstore.getBalance()uint256renameStoreToownerbytes32store.renameStoreTo(new_store_name)boolUsageFirst,activatealocaltest-blockchainwithtestrpc.Ifyoudon'thaveitjusttypenpminstall-gethereumjs-testrpcandletNPMinstallitforyou.
Second,gointotherootofthisprojectandexecutetrufflecompileandtrufflemigrate(whenchangingthecodeduringlive-testingusetrufflemigrate--resetinstead).
Third,jumpintotruffle'sconsolewithtruffleconsole.NowyoucanusethelocalBlockchaintoplaywiththeStore
评论