Smart contract represents a vending machine, where customer can buy products from catalog.
- Customer inputs the product id and attaches required deposit.
- Money is not returnable, so check if attachment is enough to buy it.
- If attached deposit exceeds the product price, it's kept as tips.
- Only the owner of the shop (shop itself) can change the number of available products in the stock.
- get slice of the list of products with their corresponding id.
Required Parameters:
from_index
- start index of slicelimit
- length of the slice
Request Sample:
near view shop.ezer.testnet view_catalog '{"from_index": 0, "limit": 10}'
Response Sample:
[
[ 0, 'SmallSnack' ],
[ 1, 'LargeSnack' ],
[ 2, 'Soda' ],
[ 3, 'IceCream' ]
]
- get slice of the list of products with their amount in stock.
Required Parameters:
from_index
- start index of slicelimit
- length of the slice
Request Sample:
near view shop.ezer.testnet view_stock '{"from_index": 0, "limit": 10}'
Response Sample:
[
[ 0, 100 ],
[ 1, 150 ],
[ 2, 200 ],
[ 3, 100 ]
]
- get price of the product in yocto Nears
Required Parameters:
product
- product id
Request Sample:
near view shop.ezer.testnet get_product_price '{"product": 0}'
Response Sample:
'1000000000000000000000000'
Raises
Product not found
- if product id does not exist
- buy the product by id
Required Parameters:
product
- product id
Request Sample with deposit of 2 NEAR
near call shop.ezer.testnet buy '{"product": 0}' --accountId *YOUR ACCOUNT* --deposit 2
Request Sample with deposit of 2 NEAR in yocto
near call shop.ezer.testnet buy '{"product": 0}' --accountId *YOUR ACCOUNT* --depositYocto 2000000000000000000000000
Response Sample:
'Thank you for purchase'
Raises
Product not found
Product out of stock
Product price not found
- if product is in catalog and in stock, but has no information about its price (if owner forgets to put it there)Attached deposit not enough
- set amount of available products for purchase. Only owner of the shop can do this operation.
Required Parameters:
product
- product idamount
- amount of products to set for sale
Request Sample
near call shop.ezer.testnet set_product_availability '{"product": 0, "amount": 100}' --accountId *OWNER_ACCOUNT*
Response Sample:
[0, 100]
Raises
Only owner can set products availability
- rust with cargo
- git clone
https://github.com/zhanymkanov/near-ncd-mini-shop
cd near-ncd-mini-shop
- Make some changes to code & add tests
- Run tests with
cargo test
- If everything is ok, build it with
RUSTFLAGS='-C link-arg=-s' cargo build --target wasm32-unknown-unknown --release
- Deploy the contract to your account with
near deploy --wasmFile target/wasm32-unknown-unknown/release/mini_shop.wasm --accountId *YOUR ACCOUNT*