| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
parent directory.. | ||||
These instructions will help you create a TokenScript interface for a smart contract. User interface code and business logic is organised in XML declarations for a TokenScript engine to render in a mobile wallet. E.g. AlphaWallet.
Although not conforming to the ERC20 interface, a few changes to the example code will have it demonstrable in AlphaWallet
contract Coin {
//...
string public name = "Coin";
uint256 public decimals = 18;
string public symbol = "COIN";
// Returns balance of an address
function balanceOf(address addr) public view returns (uint) {
return balances[addr];
}
lastly rename the function send( to function transfer(
(optional) you're welcome to modify this Coin example, or use an erc20 contract of your choosing.
Once you're happy with your contract compiling and running here, we will next deploy to the Ropsten Ethereum test network.
At the time of writing, studio.ethereum.org defaults contract deployment to the JavaScriptVM. Remix is a convenient way to deploy to Ropsten:
To see the default rendering of the contract as a fungible token:
<?xml version="1.0" encoding="UTF-8"?>
<ts:token xmlns:ts="http://tokenscript.org/2019/10/tokenscript"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:xml="http://www.w3.org/XML/1998/namespace"
xsi:schemaLocation="http://tokenscript.org/2019/10/tokenscript http://tokenscript.org/2019/10/tokenscript/tokenscript.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
custodian="false"
>
<ts:name>
<ts:string xml:lang="en">Coin</ts:string>
</ts:name>
<ts:contract name="Coin" interface="erc20">
<ts:address network="3">YOUR_CONTRACT_ADDRESS</ts:address>
</ts:contract>
<ts:origins>
<ts:ethereum contract="Coin"></ts:ethereum>
</ts:origins>
<ts:cards>
<!-- action cards will go here -->
</ts:cards>
<ts:attribute-types>
<!-- attributes will go here -->
<ts:attribute-types>
</ts:token>
In the contract address, where the network is 3 for Ropsten, paste your contract address in place of YOUR_CONTRACT_ADDRESS.
The UI elements rendering in studio (app.html) will be captured differently for TokenScript. Rather than a single view, individual actions are isolated in action cards.
<script type="text/javascript"><![CDATA[
class Token {
constructor(tokenInstance) {
this.props = tokenInstance
}
render() {
return `
<div class="ui container">
<div class="ui segment">
</div>
</div>`;
}
}
web3.tokens.dataChanged = (oldTokens, updatedTokens) => {
const currentTokenInstance = web3.tokens.data.currentInstance;
document.getElementById('root').innerHTML = new Token(currentTokenInstance).render();
};
]]></script>
<div id="root"></div>
mint.shtml: Inside the "ui segment" div, copy/paste contents of the "create" div from app.html
Coin.xml: At the top of the xml (after the version tag), include references to front-end files app.css and mint.shtml:
<!DOCTYPE token [
<!ENTITY style SYSTEM "app.css">
<!ENTITY mint.en SYSTEM "mint.shtml">
]>
<ts:action>
<ts:name>
<ts:string xml:lang="en">Mint</ts:string>
</ts:name>
...
<!-- attributetype tags -->
...
<!-- transaction tag -->
...
<style type="text/css">&style;</style>
<ts:view>&mint.en;</ts:view>
</ts:action>
...
<ts:attribute-type id="create-address" syntax="1.3.6.1.4.1.1466.115.121.1.15">
<ts:name>
<ts:string xml:lang="en">Address to receive created tokens</ts:string>
</ts:name>
<ts:origins>
<ts:user-entry as="address"/>
</ts:origins>
</ts:attribute-type>
<ts:attribute-type id="create-amount" syntax="1.3.6.1.4.1.1466.115.121.1.36">
<ts:name>
<ts:string xml:lang="en">Amount to create</ts:string>
</ts:name>
<ts:origins>
<!-- e18 is a hard coded multiplier.
rationale for hardcoding: avoiding over-design -->
<ts:user-entry as="e18"/>
</ts:origins>
</ts:attribute-type>
...
<ts:transaction>
<ts:ethereum function="mint" contract="Coin">
<ts:data>
<ts:address ref="create-address"/>
<ts:uint256 ref="create-amount"/>
</ts:data>
</ts:ethereum>
</ts:transaction>
A really good starting point to generating your own TokenScript (for the upcoming 2020/03 schema) is available with this ABI-to-TokenScript tool:
Inside you will find:
In the following section we'll combine these files in the xml.
Disclaimer: iOS users can simply airdrop the set of js/css/xml files to their device.
To combine the files you will need at least xmllint to combine the files, this will give you an unsigned (.canonicalized.xml) file for testing. When you want to have the file associated with your domain's SSL key, you will then need xmlsectool create a signed file (.tsml).
Congratulations, you can now add Coin.canonicalized.xml TokenScript for use in AlphaWallet!
ABI to TokenScript
You can use the TokenScript Viewer to see the major parts in a TokenScript file.
To get started you will need xmllint to import you resource files (eg html/css/js...) into a single canonicalized xml file.
Linux: in libxml2-utils - eg sudo apt install libxml2-utils OSX: Pre-installed Windows: See below
xmllint and xmlsec are available from xmlsoft.org. Precompiled binaries (tools and all dependencies) can be downloaded with curl commands below. Alternative: For convenience this zip has all binary files in a single folder that you can then move to a folder on your PATH.
curl -O http://xmlsoft.org/sources/win32/iconv-1.9.2.win32.zip -o iconv-1.9.2.win32.zip curl -O http://xmlsoft.org/sources/win32/libxml2-2.7.8.win32.zip -o libxml2-2.7.8.win32.zip curl -O http://xmlsoft.org/sources/win32/libxmlsec-1.2.18.win32.zip -o libxmlsec-1.2.18.win32.zip curl -O http://xmlsoft.org/sources/win32/libxslt-1.1.26.win32.zip -o libxslt-1.1.26.win32.zip curl -O http://xmlsoft.org/sources/win32/openssl-0.9.8a.win32.zip -o openssl-0.9.8a.win32.zip curl -O http://xmlsoft.org/sources/win32/xsldbg-3.1.7.win32.zip -o xsldbg-3.1.7.win32.zip curl -O http://xmlsoft.org/sources/win32/zlib-1.2.5.win32.zip -o zlib-1.2.5.win32.zip
(md5sum here for verification)
Ensure all binaries accessible in PATH
In lieu of adding each of the corresponding bin folders to your PATH variable, you can copy the contents of each bin folder (.dll/.exe files) to one folder and put that folder in your PATH.
Each OS: installation instructions here (java).
Linux: sudo apt install xmlstarlet OSX: brew install xmlstarlet Windows: https://sourceforge.net/projects/xmlstar/files/
Linux: sudo apt install xmlsec1 OSX: brew install libxmlsec1 Windows: See section Windows users above.
| Back | FazBrowse Home | New Git URL |