For me, examples are always very helpful. I feel like I haven't seen that many contract examples... Lets try to make some basic examples.
I'll start.
A nameserver, where only the sender can edit the data created by that sender:
// input: tx.data[0] = name, tx.data[1] = name data
// stores: key: name, value: [owner address, name data]
// if the fee isn't large enough to run the contract, stop
if tx.value < 100 * block.basefee:
stop
// if the name can overwrite the contract code, stop
if tx.data[0] < 100:
stop
// if name already exists and the sender isn't the name owner, stop
if contract.storage[tx.data[0]]:
if contract.storage[tx.data[0]][0] != tx.sender:
stop
// set name owner and associated data
a = array()
a[0] = tx.sender
a[1] = tx.data[1]
contract.storage[tx.data[0]] = a
Ok, lets make that contract a bit better. Lets add the ability to swap the 'owner' of the name to another address.
// input: tx.data[0] = name, tx.data[1] = name data, tx.data[2] = new owner (optional)
// stores: key: name, value: [owner address (or new owner), name data]
// if the fee isn't large enough to run the contract, stop
if tx.value < 100 * block.basefee:
stop
// if the name can overwrite the contract code, stop
if tx.data[0] < 100:
stop
// if name already exists and the sender isn't the name owner, stop
if contract.storage[tx.data[0]]:
if contract.storage[tx.data[0]][0] != tx.sender:
stop
// set name owner and associated data
a = array()
// if the new owner has been specified, store as owner
if tx.data[2] != 0:
a[0] = tx.data[2]
else:
a[0] = tx.sender
a[1] = tx.data[1]
contract.storage[tx.data[0]] = a
Comments
// input: tx.data[0] = name, tx.data[1] = name data, tx.data[2] = new owner (optional) // stores: key: name, value: [owner address (or new owner), name data] // if the fee isn't large enough to run the contract, stop if tx.value < 100 * block.basefee: stop // if the name can overwrite the contract code, stop if tx.data[0] < 100: stop // if name already exists and the sender isn't the name owner, stop if contract.storage[tx.data[0]]: if contract.storage[tx.data[0]][0] != tx.sender: stop // set name owner and associated data a = array() // if the new owner has been specified, store as owner if tx.data[2] != 0: a[0] = tx.data[2] else: a[0] = tx.sender a[1] = tx.data[1] contract.storage[tx.data[0]] = a