コード例 #1
0
    def test_create(self, mock_shard_mask):
        # create contract
        owner = Entity()
        contract = Contract(CONTRACT_TEXT, owner)

        # Mock api for providing number of lanes and receiving create call
        api = mock.Mock(spec=LedgerApi)
        api.server = mock.Mock()
        lane_number = 2
        api.server.num_lanes.side_effect = [lane_number]
        api.contracts = mock.Mock(spec=ContractsApi)

        # Mock shard mask static method
        dummy_shard_mask = mock.Mock()
        mock_shard_mask.side_effect = [dummy_shard_mask]

        contract.create(api, owner, 1000)

        # Check shard mask gen called with contract digest address
        mock_shard_mask.assert_called_once_with(
            ['fetch.contract.state.{}'.format(contract.digest.to_hex())],
            lane_number)
        # Check api create method called
        api.contracts.create.assert_called_once_with(
            owner, contract, 1000, shard_mask=dummy_shard_mask)
コード例 #2
0
    def test_init(self):
        # Test rejection of contract with multiple init statements
        owner = Entity()
        with self.assertRaises(RuntimeError):
            contract = Contract(MULTIPLE_INITS, owner)

        # Test successful creation without init (to support local etch testing)
        try:
            contract = Contract(NO_INIT, owner)
        except Exception:
            self.fail("Contract initialisation with @init failed")

        # Test creation failure without init
        api = mock.Mock()
        with self.assertRaises(RuntimeError):
            contract.create(api, owner, 100)
コード例 #3
0
def main():
    # create our first private key pair
    entity1 = Entity()
    address1 = Address(entity1)

    # create a second private key pair
    entity2 = Entity()
    address2 = Address(entity2)

    # build the ledger API
    api = LedgerApi('127.0.0.1', 8000)

    # create wealth so that we have the funds to be able to create contracts on the network
    api.sync(api.tokens.wealth(entity1, 10000))

    # create the smart contract
    contract = Contract(CONTRACT_TEXT, entity1)

    with track_cost(api.tokens, entity1, "Cost of creation: "):
        api.sync(contract.create(api, entity1, 4000))

    # print the current status of all the tokens
    print('-- BEFORE --')
    print_address_balances(api, contract, [address1, address2])

    # transfer from one to the other using our newly deployed contract
    tok_transfer_amount = 200
    fet_tx_fee = 160
    with track_cost(api.tokens, entity1, "Cost of transfer: "):
        api.sync(contract.action(api, 'transfer', fet_tx_fee, [entity1], address1, address2, tok_transfer_amount))

    print('-- AFTER --')
    print_address_balances(api, contract, [address1, address2])
コード例 #4
0
def main():
    # In examples we use addresses which already have funds
    entity1 = Entity.from_hex(
        '6e8339a0c6d51fc58b4365bf2ce18ff2698d2b8c40bb13fcef7e1ba05df18e4b')
    entity2 = Entity.from_hex(
        'e833c747ee0aeae29e6823e7c825d3001638bc30ffe50363f8adf2693c3286f8')

    address1 = Address(entity1)

    # create a second private key pair
    address2 = Address(entity2)

    # build the ledger API
    api = LedgerApi('127.0.0.1', 8000)

    # create the smart contract
    contract = Contract(CONTRACT_TEXT, entity1)

    with track_cost(api.tokens, entity1, "Cost of creation: "):
        api.sync(contract.create(api, entity1, 4000))

    # print the current status of all the tokens
    print('-- BEFORE --')
    print_address_balances(api, contract, [address1, address2])

    # transfer from one to the other using our newly deployed contract
    tok_transfer_amount = 200
    fet_tx_fee = 160
    with track_cost(api.tokens, entity1, "Cost of transfer: "):
        api.sync(
            contract.action(api, 'transfer', fet_tx_fee, entity1, address1,
                            address2, tok_transfer_amount))

    print('-- AFTER --')
    print_address_balances(api, contract, [address1, address2])
コード例 #5
0
def contract_setup(source, benefactor, options):
    # Create keypair for the contract owner
    entity1 = Entity()
    Address(entity1)
    entity2 = Entity()
    Address(entity2)

    host = options['host']
    port = options['port']
    # create the APIs
    api = LedgerApi(host, port)

    # Transfer tokens from benefactor
    api.sync(api.tokens.transfer(benefactor, entity1, int(1e7), 1000))
    api.sync(api.tokens.transfer(benefactor, entity2, int(1e7), 1000))

    # Create contract
    contract = Contract(source, entity1)

    # Deploy contract
    api.sync(contract.create(api, entity1, int(1e7)))

    return api, contract, entity1, entity2
コード例 #6
0
    sys.exit('File not found')

if 0 == len(contract_text):
    print("Contract is zero length.")
    sys.exit("Invalid contract")

# Private key of the address to deploy from
# WARNING: Unencrypted private keys should not be present in production code
entity = Entity(b'... your private key here ...')
address = Address(entity)

print("Deploying contract:", contract_name, '\n')
print("  Owner:", address)
print(" Length:", len(contract_text), "byte(s)")

# Perform the deployment now
try:
    contract = Contract(contract_text, entity)
    gas_fee = 600000
    api.sync(contract.create(api, entity, gas_fee), None, 0)
except Exception as e:
    sys.exit(e)

# Deployed, so we can now announce address and owner
print("\nContract deployed:\n")
print("Address:", contract.address)
print("  Owner:", contract.owner)

# Confirm by querying the contract
print(" Output:", contract.query(api, 'sayHello'))