def main(source): # Create keypair for the contract owner entity = Entity() address = Address(entity) # Setting API up api = LedgerApi('127.0.0.1', 8100) # Need funds to deploy contract api.sync(api.tokens.wealth(entity, 5000000)) # Create contract contract = SmartContract(source) # Deploy contract api.sync(api.contracts.create(entity, contract, 2456766)) # Printing balance of the creating address print(contract.query(api, 'balanceOf', owner=address)) # Getting the 9'th token id. token_id = contract.query(api, 'getTokenId', number=9) # Testing contract.query(api, 'isEqual', number=9, expected=token_id) # Locating the owner of a token print("Finding the owner of ", token_id) print(contract.query(api, 'ownerOf', token_id=token_id))
def main(source, name): # Create keypair for the contract owner charge_provider1 = Entity() charge_address1 = Address(charge_provider1) charge_provider2 = Entity() charge_address2 = Address(charge_provider2) parking_provider1 = Entity() parking_address1 = Address(parking_provider1) parking_provider2 = Entity() parking_address2 = Address(parking_provider2) charge1 = Entity() charge_station_address1 = Address(charge1) parking1 = Entity() parking_place_address1 = Address(parking1) # Setting API up api = LedgerApi('127.0.0.1', 8100) # Need funds to deploy contract api.sync(api.tokens.wealth(charge_provider1, 59000000)) # Create contract contract = SmartContract(source) # Deploy contract api.sync(api.contracts.create(charge_provider1, contract, 2456766)) if name.endswith("contract.etch"): contract.action(api, 'addChargeProvider', 2456766, [charge_provider2, charge_provider1], charge_address2, charge_address1) contract.action(api, 'addCharge', 2456766, [charge_provider2, charge_provider1], charge_address2, charge_address1, 22, 1) print("Wait for charge txs to be mined ...") time.sleep(5) # Printing balance of the creating address1 print(contract.query(api, 'getChargeFleetSize'), " charge in fleet") contract.action(api, 'addParkingProvider', 2456766, [parking_provider2, parking_provider1], parking_address2, parking_address1) contract.action(api, 'addParking', 2456766, [parking_provider2, parking_provider1], parking_address2, parking_address1, 22, 1) print("Wait for parking txs to be mined ...") time.sleep(5) # Printing balance of the creating address1 print(contract.query(api, 'getParkingFleetSize'), " parking in fleet")
def print_address_balances(api: LedgerApi, contract: SmartContract, addresses: List[Address]): for idx, address in enumerate(addresses): print('Address{}: {:<6d} bFET {:<10d} TOK'.format( idx, api.tokens.balance(address), contract.query(api, 'balance', address=address))) print()
def main(source, name): # Create keypair for the contract owner provider1 = Entity() address1 = Address(provider1) provider2 = Entity() address2 = Address(provider2) scooter1 = Entity() scooter_address1 = Address(scooter1) # Setting API up api = LedgerApi('127.0.0.1', 8100) # Need funds to deploy contract api.sync(api.tokens.wealth(provider1, 59000000)) # Create contract contract = SmartContract(source) # Deploy contract api.sync(api.contracts.create(provider1, contract, 2456766)) if name.endswith("contract.etch"): contract.action(api, 'addProvider', 2456766, [provider2, provider1], address2, address1) contract.action(api, 'addScooter', 2456766, [provider2, provider1], address2, address1, 22, 1) print("Wait for txs to be mined ...") time.sleep(5) # Printing balance of the creating address1 print(contract.query(api, 'getFleetSize'), " scooter in fleet")
def call(name: str) -> SmartContract: # Create keypair for the contract owner provider1 = Entity() address1 = Address(provider1) # Setting API up api = LedgerApi('185.91.52.11', 10002) # Need funds to deploy contract api.sync(api.tokens.wealth(provider1, 59000000)) with open(name, "r") as fb: source = fb.read() # Create contract contract = SmartContract(source) # Deploy contract api.sync(api.contracts.create(provider1, contract, 2456766)) print("Wait for txs to be mined ...") time.sleep(5) # Printing balance of the creating address1 print(contract.query(api, 'getAccountRides', acc_id="1"))
def main(): # create our first private key pair entity1 = Entity() # build the ledger API api = LedgerApi('127.0.0.1', 8100) # create wealth so that we have the funds to be able to create contracts on the network api.sync(api.tokens.wealth(entity1, 1000000000000000)) # create the smart contract contract = SmartContract(CONTRACT_TEXT) # deploy the contract to the network api.sync(api.contracts.create(entity1, contract, 1000000000)) # update the graph with a new model fet_tx_fee = 100000000 with open(GRAPH_FILE_NAME, mode='rb') as file: print("reading in graph file...") rfile = file.read() print("encoding to base64 string...") b64obj = base64.b64encode(rfile) obj = b64obj.decode() print("updating smart contract graph...") api.sync( contract.action(api, 'updateGraph', fet_tx_fee, [entity1], obj)) print("finished updating smart contract graph") # set one real example input data set fet_tx_fee = 100000000 api.sync( contract.action(api, 'setHistorics', fet_tx_fee, [entity1], EXAMPLE_INPUT_HISTORICS)) current_historics = contract.query(api, 'getHistorics') print("current historics: " + current_historics) # make a prediction current_prediction = contract.query(api, 'makePrediction') print("current prediction: " + current_prediction)
def run(options): entity1 = Entity() # build the ledger API api = LedgerApi(options['host'], options['port']) # create wealth so that we have the funds to be able to create contracts on the network api.sync(api.tokens.wealth(entity1, 100000)) # create the smart contract contract = SmartContract(CONTRACT_TEXT) # deploy the contract to the network api.sync(api.contracts.create(entity1, contract, 2000)) api.sync(contract.action(api, 'set_block_number_state', 400, [entity1])) assert contract.query(api, 'query_block_number_state') > 0
def main(source): # Create keypair for the contract owner entity = Entity() address = Address(entity) # Setting API up api = LedgerApi('127.0.0.1', 8100) # Need funds to deploy contract api.sync(api.tokens.wealth(entity, 1000000)) # Create contract contract = SmartContract(source) # Deploy contract api.sync(api.contracts.create(entity, contract, 20000)) # Printing message print(contract.query(api, 'totalSupply'))
def main(source): # Create keypair for the contract owner entity = Entity() address = Address(entity) # Setting API up api = LedgerApi('localhost', 8000) # Need funds to deploy contract api.sync(api.tokens.wealth(entity, 100000)) # Create contract contract = SmartContract(source) # Deploy contract api.sync(api.contracts.create(entity, contract, 100000)) # Printing message contract.action(api, 'add', 100, [entity], "first") time.sleep(5) print(contract.query(api, 'getSize'))
def main(source): # Create keypair for the contract owner entity = Entity() address = Address(entity) # Setting API up api = LedgerApi('10.168.172.59', 8000) # Need funds to deploy contract api.sync(api.tokens.wealth(entity, 100000)) # Create contract contract = SmartContract(source) # Deploy contract api.sync(api.contracts.create(entity, contract, 10000)) # Printing message print( contract.query(api, 'register', owner='artyom', scooter='bike1', price='100'))
from fetchai.ledger.api import LedgerApi from fetchai.ledger.contract import SmartContract from fetchai.ledger.crypto import Entity, Address import time contract_owner = Entity.from_hex( 'c25ace8a7a485b396f30e4a0332d0d18fd2e462b3f1404f85a1b7bcac4b4b19d') contract_owner_address = Address( contract_owner) # atsREugsanXS828FnTvmLM9vCkBsWnDgushDH9YjEgsdBuRGv with open("./cbns_token.etch", "r") as fb: contract_source = fb.read() api = LedgerApi('127.0.0.1', 8000) api.sync(api.tokens.wealth(contract_owner, 5000000)) contract = SmartContract(contract_source) api.sync(api.contracts.create(contract_owner, contract, 2456766)) time.sleep(10) print('Deployed contract address:', Address(contract.digest)) print('my balance:', contract.query(api, 'balanceOf', owner=contract_owner_address))
from fetchai.ledger.api import LedgerApi from fetchai.ledger.contract import SmartContract from fetchai.ledger.crypto import Entity, Address import time contract_owner = Entity.from_hex('c25ace8a7a485b396f30e4a0332d0d18fd2e462b3f1404f85a1b7bcac4b4b19d') contract_owner_address = Address(contract_owner) # atsREugsanXS828FnTvmLM9vCkBsWnDgushDH9YjEgsdBuRGv with open("./chrg_token.etch", "r") as fb: contract_source = fb.read() api = LedgerApi('127.0.0.1', 8000) api.sync(api.tokens.wealth(contract_owner, 5000000)) contract = SmartContract(contract_source) api.sync(api.contracts.create(contract_owner, contract, 2456766)) time.sleep(10) print('Deployed contract address:', Address(contract.digest)) riders = ['WfQDBasLx396CCxWvUoa4BmEsStPNRUi9iA45zu6k3eeaUMGs', 'pUkSDemAkhTob8UiqmRoRbjBW2rwTdnDR68thfToZwYrYHdGr', '2GNnBTmnkUwDeJfuig3hxN77gteXixw45mhita58MzZyMoqQ9u', 'fzoGmzeHN3EkvtbgTNYxuP1Zokpn7AZy5eiSSdP9Rw7KwitPW', '2R6SJK7hoiVTQbMVQDMpB86NaHX9CAXBb5hmH5kyTHCndsNSfe'] for rider in riders: api.sync(contract.action(api, 'transfer', 100, [contract_owner], contract_owner_address, Address(rider), 100000)) print('my balance:', contract.query(api, 'balanceOf', owner=contract_owner_address))
class ChargerAgent(OEFAgent): """Class that implements the behaviour of the charger agent.""" charger_description = Description({ "price_per_energy_percent": True, }, JOURNEY_MODEL) def __init__(self, *args, **kwargs): super(ChargerAgent, self).__init__(*args, **kwargs) self._entity = Entity() self._address = Address(self._entity) with open("../03_full_contract.etch", "r") as fb: self._source = fb.read() self.prepare_contract(22) def prepare_contract(self, price): # Setting API up self._api = LedgerApi('127.0.0.1', 8100) self.longitude = 10 self.latitude = 10 self.scheduler = {} self.price = price self.rate = 22 self.max_count = 10 # Need funds to deploy contract self._api.sync(self._api.tokens.wealth(self._entity, 5000000)) # Create contract self._contract = SmartContract(self._source) # Deploy contract self._api.sync( self._api.contracts.create(self._entity, self._contract, 2456766)) self.chargers = [ Entity(), ] #self._contract.action(self._api, 'test', 2456755, [self._entity], Address(self.chargers[0]), Address(self.chargers[0])) self._contract.action(self._api, 'addCharger', 2456766, [self._entity], Address(self.chargers[0]), self.longitude, self.latitude, self.price, self.rate, self.max_count) scooter = Entity() scooter_start_time = 150000 scooter_end_time = 151000 tx_digest = self._contract.action(self._api, 'book', 2456766, [self._entity], Address(self.chargers[0]), Address(scooter), scooter_start_time, scooter_end_time) time.sleep(3) tx_status = self._api.tx.status(tx_digest) if tx_status == "Executed": self.scheduler[str( Address(scooter))] = [scooter_start_time, scooter_end_time] query = self._contract.query(api=self._api, name='getSlotInfo', charger=Address(self.chargers[0])) print(query) def on_cfp(self, msg_id: int, dialogue_id: int, origin: str, target: int, query: CFP_TYPES): """Send a simple Propose to the sender of the CFP.""" print("[{0}]: Received CFP from {1}".format(self.public_key, origin)) price = self.price # prepare the proposal with a given price. proposal = Description({ "price_per_energy_percent": price, "digest": str(self._contract.digest), "longitude": self.longitude, "latitude": self.latitude, "scheduler": json.dumps(self.scheduler), "rate": self.rate, "max_count": self.max_count }) print("[{}]: Sending propose at price: {}".format( self.public_key, price)) self.send_propose(msg_id + 1, dialogue_id, origin, target + 1, [proposal]) def on_accept(self, msg_id: int, dialogue_id: int, origin: str, target: int): """Once we received an Accept, send the requested data.""" print("[{0}]: Received accept from {1}.".format( self.public_key, origin)) # Preparing contract # PLACE HOLDER TO PREPARE AND SIGN TRANSACTION print(self._contract) contract = { "digest": str(self._contract.digest), "owner": str(self._contract.owner), "chargers": [str(Address(s)) for s in self.chargers] } # Sending contract encoded_data = json.dumps(contract).encode("utf-8") print("[{0}]: Sending contract to {1}".format(self.public_key, origin)) self.send_message(0, dialogue_id, origin, encoded_data)