A Wolfram Language platform for smart contracts and connecting blockchains

Johan Veerman, Wolfram Research

Join the Conversation #WolframTechConf

Blockchain?

Outline

An architecture for transactions and smart contracts

Transactions and smart contracts in 3 easy steps

Create a transaction

Sign a transaction

Submit a transaction

Cross-chaining

Token analysis

Integration with Wolfram|Alpha

Architecture

Transactions

Creating and sending a transaction

To send a transaction, an address with some ether balance and a private key are required

$BlockchainBase = {"Ethereum", "Testnet"}

ethereumTestAddress="74a256286da13b98d64e29eed9915dcb51cc5a90";

privateKey="caab89c53a5c8344e7ad79c4ee99b764dda09f7b1083779470f6c14ca919757d";

We also need to know the transaction count for the address. We can use BlockchainAddressData for this.

addressInfo=BlockchainAddressData[ethereumTestAddress]//Dataset

txCount = addressInfo["TransactionCount"]

Let’s check the current blockchain data information. It shows gas price values that the address can use as a reference for a good fee.

blockchainInfo=BlockchainData[]//Dataset

Let’s pick the highest gas price (for faster mining). For reference, 100 Gwei is equivalent to $0.00002

gasPrice=blockchainInfo["HighestGasPrice"]

We’re ready for doing a blockchain transaction.
1. BlockchainTransaction to create the transaction object.
2. BlockchainTransactionSign to sign it with your private key.
3. BlockchainTransactionSubmit to send it to our local node so that it can be mined.

t1=BlockchainTransaction[<|"TransactionCount"->txCount,"DestinationAddress"->"3535353535353535353535353535353535353535","Amount"->1000000000000000,"GasPrice"->gasPrice|>]

t1sign=BlockchainTransactionSign[t1, privateKey]

t1submit=BlockchainTransactionSubmit[t1sign]

Let’s check if our transaction has been mined and added to the blockchain.

t1ID=t1submit["TransactionID"]

BlockchainTransactionData[t1ID]

BlockchainTransactionData[t1ID]//Dataset

addressInfo

BlockchainAddressData[ethereumTestAddress]//Dataset

Smart Contracts

Uploading a WL contract to an external storage

Let’s upload a very simple contract that checks for the weather of a beautiful city.

temperature = WeatherData[GeoPosition[Entity["City", {"Lima", "Lima", "Peru"}]], "Temperature"];

result = Developer`WriteRawJSONString[<|"Description" -> "Temperature",
     "Location" -> "Lima, Peru",
     "Magnitude" -> QuantityMagnitude[temperature],
     "Unit" -> QuantityUnit[temperature],
     "DateTime" -> DateString["ISODateTime"]|>, "Compact" -> True]

ipfs=ExternalStorage`IPFSUpload["ExampleData/simple.m","FileHash"->True]

Creating and deploying a smart contract

All we need to create the smart contract is to know where it is stored. the BlockchainContract function will add the Solidity template needed to include our Wolfram Language script as a payload.

sco=BlockchainContract[ipfs, BlockchainBase->{"Ethereum","Testnet"}]

txCount=BlockchainAddressData[ethereumTestAddress]["TransactionCount"]

We know follow the exact same steps we used for a regular transaction.

t2=BlockchainTransaction[<|"BlockchainContract"->sco,"TransactionCount"->txCount,"GasPrice"->gasPrice|>]

t2sign=BlockchainTransactionSign[t2, privateKey]

t2submit=BlockchainTransactionSubmit[t2sign]

Checking if our transaction has been mined.

t2submit["TransactionID"]

txdata=BlockchainTransactionData[t2submit["TransactionID"]]//Dataset

Checking for smart contract update

Let’s get the blockchain address assigned to our smart contract.

contractAddress=First[#["Addresses"]&@@txdata["Outputs"]]

Checking if the script has been harvested and executed using BlockchainContractValue.

BlockchainContractValue[contractAddress,<|"Function"->"getStatus()","Inputs"->{},"Outputs"->"Boolean"|>]

BlockchainContractValue[contractAddress,<|"Function"->"getStatus()","Inputs"->{},"Outputs"->"Boolean"|>]

Download result from external storage

Getting the address where the result has been stored.

resultAddress=BlockchainContractValue[contractAddress,<|"Function"->"getResultAddress()","Inputs"->{},"Outputs"->"String"|>]

Let’s retrieve the result of executing the script.

esodsc =ExternalStorage`IPFSObject[<|"ExternalStorage" -> "IPFS", 
  "Address" -> resultAddress|>]

GetResult[esodsc]

Cross-Chaining

The focus today is mostly on cryptocurrency exchange

Two approaches exist:

Create a blockchain of blockchains

Include a piece of code to make it compatible with others

The Wolfram Blockchain Platform will allow for natural integration once a blockchain is included in our ecosystem

Tokens

$BlockchainBase = "Ethereum";

Getting information about a certain token.

BlockchainTokenData["Cryptokitties"]//Dataset

Let’s retrieve the top holders of that token.

BlockchainTokenData["CK","Holders",MaxItems->10]//Dataset

Let’s get the latest transfers.

BlockchainTokenData["CK","Transfers",MaxItems->10]//Dataset

Getting a list of all the tokens (above a certain market cap) of a specific token standard.

SortBy[BlockchainTokenData[<|"TokenStandard"->"ERC721"|>],#["Name"]&]//Dataset

We can list all the transfers related to a specific token between some blocks.

First[BlockchainTokenData[<|"Symbol"->"CK","Blocks"->{4900000,4901000}|>,"Transfers"]]//Dataset

Or get a list of all the transfers in a specific time interval.

First[BlockchainTokenData[<|"Symbol"->"CK","TimeInterval"->(DateObject["Hour: ""Fri 10 Aug 2018 10 pm""GMT"-5.`, DateObject[{2018,8,10,22},"Hour","Gregorian",-5.`]])|>,"Transfers"]]//Dataset

Maybe we want to know all the tokens a holder has from a specific standard.

BlockchainTokenData[<|"Name"->All,"Standard"->"ERC20","HolderAddresses"->"4affd0270e6fff6f9b3c3cdb30be4b2d0f030f52"|>,{"Name","Symbol","HolderBalance"}]//Dataset

Wolfram|Alpha

All the blockchain reading functionality of the Wolfram Language will soon be integrated in Wolfram|Alpha.

Hyperlink["ethereum data","http://www.centos6-cur.wolframalpha.com/input/?i=devmode:+ethereum+blockchain&assumptionsversion=2"]

Hyperlink["bitcoin first block","http://www.centos6-cur.wolframalpha.com/input/?i=devmode:+bitcoin+block+1&assumptionsversion=2"]

Hyperlink["ethereum last block","http://www.centos6-cur.wolframalpha.com/input/?i=devmode:+ethereum+block+-1&assumptionsversion=2"]

Hyperlink["bitcoin blockchain latest hash","http://www.centos6-cur.wolframalpha.com/input/?i=devmode:+bitcoin+blockchain+latest+hash&assumptionsversion=2"]

Hyperlink["when was ethereum block 15 mined?","http://www.centos6-cur.wolframalpha.com/input/?i=devmode:+ethereum+block+15+time+mined"]

Hyperlink["transaction a47c052169d6acc5beb2935d76b27b2c0ecd1702d15912ee5f5d1144009da82c","http://www.centos6-cur.wolframalpha.com/input/?i=devmode:+ethereum+blockchain+transaction+a47c052169d6acc5beb2935d76b27b2c0ecd1702d15912ee5f5d1144009da82c+in+base+16"]

Query information about specific addresses.

And tokens are coming as well...

The Blockchain Team

Grid[{{,,,,},{"Akira Toma","Christian Pasquel","Daniel Suarez","Dariia Porechna","Johan Veerman"},{,,,,},{"Jon Woodard","Juan Leiva","Karla Santana","Piero Sanchez","Rodrigo Velarde"}},Spacings->{5,1}]