Smart Contracts For Dummies - PlutusX - Medium
Smart Contracts For Dummies - PlutusX - Medium
PlutusX Follow
Feb 24, 2018 · 10 min read
You may have heard this term floating around the crypto-community
for a while, and even if you’re new the the HODL life you possibly heard
the term smart contracts used before.
Example:
HODLer: “Man, I just bought this lambo with a smart contract once my
BTC hit 20k.”
This sounds great, like the real future of contracts but what exactly are
smart contracts, how are they different than traditional contracts, and
why are they more superior? Lets dive into some history real quick.
These machines run a basic program that states “if money enters & click
button = drink”. If you put in $1.00 into the machine and click coca-
cola the system program acts as some type of agreement and as a result
the coke is dispensed.
This proposes a serious problem with trust as you scale up. The purpose
of the middleman (escrow for example) is to enact trust between both
parties to reassure that either party won’t defraud the other. The
solution lies in the blockchain tech. The complete transparency places
the code publicly to be verified. This removes the need for the middle-
man and takes advantage of the trust-less ecosystem native to the
blockchain.
Security Issues
Because of the visibility in the trestles environment the program is
susceptible to malicious actors. Potential bugs including security holes
are exposed. The biggest example was the DAO attack in June of ’16.
These problems are not easily fixable, and the solution resulted in a
hard fork.
You could possibly send the wrong code, or, as lawyer Bill Marino
points out, I send the right code, but an apartment is condemned (i.e.,
taken for public use without my consent) before the rental date arrives?
If this were the traditional contract, I could rescind it in court, but the
blockchain is a different situation. The contract performs, no matter
what.
Use Case
The use cases for smart contracts are endless across almost every major
industry imaginable.
* Real Estate
* Healthcare
* Automobile
* Government
* And much more
contract owned {
contract TokenERC20 {
// Public variables of the token
string public name;
string public symbol;
uint8 public decimals = 18;
// 18 decimals is the strongly suggested default, avoid
changing it
uint256 public totalSupply;
// This creates an array with all balances
mapping (address => uint256) public balanceOf;
mapping (address => mapping (address => uint256))
public allowance;
// This generates a public event on the blockchain
that will notify clients
event Transfer(address indexed from, address indexed
to, uint256 value);
// This notifies clients about the amount burnt
event Burn(address indexed from, uint256 value);
/**
* Constrctor function
*
* Initializes contract with initial supply tokens to
the creator of the contract
*/
function TokenERC20(
uint256 initialSupply,
string tokenName,
string tokenSymbol
) public {
totalSupply = initialSupply * 10 **
uint256(decimals); // Update total supply with the decimal
amount
balanceOf[msg.sender] = totalSupply;
// Give the creator all initial tokens
name = tokenName;
// Set the name for display purposes
symbol = tokenSymbol;
// Set the symbol for display purposes
}
/**
* Internal transfer, only can be called by this
contract
*/
function _transfer(address _from, address _to, uint
_value) internal {
// Prevent transfer to 0x0 address. Use burn()
instead
require(_to != 0x0);
// Check if the sender has enough
require(balanceOf[_from] >= _value);
// Check for overflows
require(balanceOf[_to] + _value > balanceOf[_to]);
// Save this for an assertion in the future
uint previousBalances = balanceOf[_from] +
balanceOf[_to];
// Subtract from the sender
balanceOf[_from] -= _value;
// Add the same to the recipient
balanceOf[_to] += _value;
Transfer(_from, _to, _value);
// Asserts are used to use static analysis to find
bugs in your code. They should never fail
assert(balanceOf[_from] + balanceOf[_to] ==
previousBalances);
}
/**
* Transfer tokens
*
* Send `_value` tokens to `_to` from your account
*
* @param _to The address of the recipient
* @param _value the amount to send
*/
function transfer(address _to, uint256 _value) public {
_transfer(msg.sender, _to, _value);
}
/**
* Transfer tokens from other address
*
* Send `_value` tokens to `_to` in behalf of `_from`
*
* @param _from The address of the sender
* @param _to The address of the recipient
* @param _value the amount to send
*/
function transferFrom(address _from, address _to,
uint256 _value) public returns (bool success) {
require(_value <= allowance[_from][msg.sender]);
// Check allowance
allowance[_from][msg.sender] -= _value;
_transfer(_from, _to, _value);
return true;
}
/**
* Set allowance for other address
*
* Allows `_spender` to spend no more than `_value`
tokens in your behalf
*
* @param _spender The address authorized to spend
* @param _value the max amount they can spend
*/
function approve(address _spender, uint256 _value)
public returns (bool success) {
allowance[msg.sender][_spender] = _value;
return true;
}
/**
* Set allowance for other address and notify
*
* Allows `_spender` to spend no more than `_value`
tokens in your behalf, and then ping the contract about it
*
* @param _spender The address authorized to spend
* @param _value the max amount they can spend
* @param _extraData some extra information to send to
the approved contract
*/
function approveAndCall(address _spender, uint256
_value, bytes _extraData)
public
returns (bool success) {
tokenRecipient spender = tokenRecipient(_spender);
if (approve(_spender, _value)) {
spender.receiveApproval(msg.sender, _value,
this, _extraData);
return true;
}
}
/**
* Destroy tokens
*
* Remove `_value` tokens from the system irreversibly
*
* @param _value the amount of money to burn
*/
/******************************************/
/* ADVANCED TOKEN STARTS HERE */
/******************************************/
. . .
Conclusion
Smart contracts are simply a program that are used as a tool to more
efficiently execute agreements without a middleman charging extra on
top. The trust-less environment removed the need for a middle man but
also acts as a catch-22 because the code is public, therefore vulnerable
to malicious actors. As we continue to perfect these contracts we will
soon see a massive adoption which will result in changes within major
industries.
. . .
Whitepaper | Community
We are releasing a teaser for our whitepaper in addition to our first
months results for our fund. We are releasing it on our telegram. Find
the channel HERE.
. . .