Community

Seele Javascript SDK demo

Seele | 08.03| 146

Selena Sha

With the recent release of Seele Javascript SDK , we came up a simple example to demonstrate some of it’s capability. We aren’t going to get into too much technical details here, some understanding of EVM and smart contract will be very helpful. So please excuse me if you find this article is not that in detail.

The idea of the example is straight forward, store and retrieve a single game score in the Seele main net.

Here are the steps for the project.

A smart contract to store and retrieve the score, below is the code for the smart contract.

pragma solidity ^0.4.24;
contract scoreBoard {
mapping(address => uint256) public scores;
mapping(address => string) public names;
address[] public users;
function getUsers() public view returns(address[]){
return users;
}
// add a score, assume app to makesure
function update(address _user, uint256 _score, string _name) public{
scores[_user] = _score;
names[_user] = _name;
users.push(_user);
}
function getScore(address _user) public view returns(uint256) {
return scores[_user];
}
}

This code will need to be compile into byte code in order to be able to deploy to the Seele Main Net. In this case, we are using Remix . Compile using 0.4.24 compiler, EVM Homestead option. Then click Deploy button. Once it’s deploy, copy the input section, this is the compile bytecode of the contract, please save the contract bytecode and the ABI. Next we’ll need to deploy the contract byte code to the Seele main net. At this point you should have an Seele main net account already, if not don’t worry the process is easy. We’ll be using Seele wallet for both the account creation and contract deployment. The Seele wallet could be download from here, please refer to the Seele wallet documentation on how to create account and import it into the wallet with the instructions here. Once the private key is loaded, the account will show up on the main page. Click on the Deploy|Employ button of the account. Once the transaction form shows up, leave the To field blank and fill in the password of your key file that got created. Then select the CONTRACT BYTE CODE button and paste into the field and click the Deploy button to deploy the contract. Once the contract is deploy at the main menu click on the Hash number and copy it then click on the View receipt button to bring up a form and paste the hash code to find out the contract address. If the result show up then the contract is deployed to the Seele main net, please save the string under the word contract:, this is the contract address.

Javascript to post and get score from the deployed contract in the Seele main net.

Here’s the script to post score to the Seele main net

const sle = require(‘seele-sdk-javascript’)
const Web3= require(‘web3’)
var web3 = new Web3()
var web = new sle.rpc(‘http://117.50.97.136:8038',2) //shard 2
var contractAddress = “FILL_IN_THE_CONTRACT_ADDRESS” //contract address on Seele main net
const abi = require(‘./contract_abi.json’)
var from=”YOUR_ACCOUNT_PUBLIC_KEY”
const private_key = ‘YOUR_ACCOUNT_PRIVATE_KEY’
var contract = new sle.contract(contractAddress, abi)
var score = req.param(FILL_IN_SCORE);
async function updateScore(score){
const result = contract.update(from, score, “AN_IDENTIFIER”);
const initTx = sle.signature.initTxn(from, contractAddress, 0);
initTx.Payload = result.byteCode;
initTx.GasLimit = 700000;
var nonce = await web.getAccountNonce(from, ‘’, -1);
initTx.AccountNonce = nonce;
const signTx = sle.signature.signTxn(private_key, initTx);
txResult=await web.addTx(signTx);
var info = await web.getInfo()
}
updateScore(score)

Here’s the script to get score from the Seele main net

const sle = require(‘seele-sdk-javascript’)
const Web3= require(‘web3’)
var web3 = new Web3()
var web = new sle.rpc(‘http://117.50.97.136:8038',2)
var contract_add = “CONTRACT_ADDRESS” //contract address
const abi = require(‘./contract_abi.json’)
var contract = new sle.contract(contract_add, abi)
var getScore = function(address) {
var result = contract.getScore(“PUBLIC_KEY_ADDRESS”)
web.call(contract_add, result.byteCode, -1).then(d => {
r=web3.eth.abi.decodeParameters(result.methodInfo.outputs, d.result)
console.log(r[0].toString());
}).catch(e=>{
console.log(e.toString());
})
}
getScore()

The demo product is presented in the form of a html5 game located at here, it’s a free game from https://www.emanueleferonato.com/2019/12/11/commercial-html5-math-game-drawsum-source-code-available-for-free/, the score will be posted to the Seele main net after hitting the replay button after the current game is finished. The game is developed using the Phaser framework https://phaser.io/ with Express https://expressjs.com/ in the middle connecting the dots.

Thank you for your support!

Comment 0

delete

Are you sure you want to delete this post?