Example usage¶
Now we install our new dapp using dappman; contractvmd will automatically restart (if running):
dappman -i /path/of/your/dapp/myfirstdapp
We now exploit the created dapp for writing a client application. We first create a ConsensusManager, and we initialize it with a static set of nodes (at the moment we can use the our local contractvmd instance).
Then we create a Wallet object, by using an external blockchain explorer. The first run of the example will create a new file test.wallet containing a key-pair. If you want to send messages using this address, remember to send some testnet coins to the address in test.wallet; you can do this by using a faucet like http://tpfaucet.appspot.com/.
Next we create a MyFirstManager, by using the ConsensusManager and Wallet objects created before. The script asks the user for a key-value pair, and it publishes it to the framework. Then, the script asks the user for a key, and then queries and returns the associated value (if any).
from libcontractvm import Wallet, WalletExplorer, ConsensusManager
from myfirstdapp import MyFirstManager
import sys
import config
consMan = ConsensusManager.ConsensusManager ()
consMan.bootstrap ("http://127.0.0.1:8181")
wallet = WalletExplorer.WalletExplorer (wallet_file='test.wallet')
bsMan = MyFirstManager.MyFirstManager (consMan, wallet=wallet)
def set_key ():
ykey = input ('Insert a key to set: ')
yvalue = input ('Insert a value to set: ')
bsMan.set (ykey, yvalue)
def get_key ():
ykey = input ('Insert a key to get: ')
value = bsMan.get (ykey)
print (ykey,'=',value)
if __name__ == "__main__":
if len (sys.argv) > 1:
if sys.argv[1] == 'set':
set_key ()
sys.exit (0)
elif sys.argv[1] == 'get':
get_key ()
sys.exit (0)
print ('usage:', sys.argv[0], 'get|set')