與智能合約交互(3):傳統商業程序數據上鍊,積分轉幣系統(終)

同步發佈簡書鏈接https://www.jianshu.com/writer#/notebooks/18826917/notes/33273306mysql

上篇介紹了積分轉幣系統的技術架構和程序邏輯流程及部分代碼,本篇將繼續介紹代碼部分,同時做者也發現 了在簡書發佈代碼存在一個問題只能用截圖,並且這樣效率比較不高,好的一點就是防止考貝,保持原創.下面進入正題:web

4、程序代碼(續):

3.API接口的數據庫部分:sql

表結構圖示意:數據庫

                         
 

4。API積分換幣部分:限於篇幅只有部分截圖,重在程序結構示意明瞭api

下面代碼說明:右側三個函數被封裝後導出供外部調用,意在實現積分按時更新,插入,並兌換成代幣瀏覽器

                         
 

 

                         
 

5.代幣上鍊部分:服務器

也封裝成一 個接口模塊,程序說明:代碼截圖中右側是函數功能模塊圖,實現環境要求win10環境下命令行窗口:微信

如圖:網絡

                         
 

須要啓動geth程序命令打開本地節點:geth命令以下:架構

geth --testnet --fast --cache=1024 --maxpeers 10000 --datadir="e:\EthereumTestdata" --rpc --rpcaddr 127.0.0.1 --rpcport 8545 --rpcapi web3,eth,personal --port 3000 console

注意: 所打開的本地節點,要求達到和以太坊網絡數據徹底同步,塊高度爲最新,(blocks=1)不然依然不能,把本地要上鍊數據同步到以太坊鏈上

 

                         
 

 

                         
 

 

                         
 

6.上鍊主程序:

                         
 

5、運行效果

以太坊瀏覽器查看地址https://ropsten.etherscan.io/address/0x133dc9a905455354892687a725c4c274cdbc017e#tokentxns

 

                         
 

本地mysql數據庫代幣彙總表

                         
 

6、運行過程

同時啓動三個命令行窗口

1.本地以太以太坊節點geth窗口,上面已有截圖

2.UI接口服務器程序監聽窗口

                         
 

3.上鍊轉幣主調程序窗口

                         
 4.附件智能合約測試代碼:smartTraderToken.sol文件
pragma solidity ^0.4.16;
/* 修改微信yyy99966*/
contract Token{

    function balanceOf(address _owner) public constant returns (uint256 balance);

    function transfer(address _to, uint256 _value) public returns (bool success);

    function transferFrom(address _from, address _to, uint256 _value) public returns (bool success);

    function approve(address _spender, uint256 _value) public returns (bool success);

    function allowance(address _owner, address _spender) public constant returns (uint256 remaining);

    event Transfer(address indexed _from, address indexed _to, uint256 _value);

    event Approval(address indexed _owner, address indexed _spender, uint256 _value);
}

contract SmartTraderToken is Token {
    uint256 public totalSupply;
    string  public name;
    uint8   public decimals;
    string  public symbol;

    constructor(uint256 initialAmount, string tokenName, uint8 decimalUnits, string tokenSymbol) public {
        totalSupply = initialAmount * 10 ** uint256(decimalUnits);
        balances[msg.sender] = totalSupply;

        name = tokenName;
        decimals = decimalUnits;
        symbol = tokenSymbol;
    }

    function transfer(address _to, uint256 _value) public returns (bool success) {
        require(balances[msg.sender] >= _value && balances[_to] + _value > balances[_to]);
        require(_to != 0x0);
        balances[msg.sender] -= _value;            //從消息發送者帳戶中減去token數量_value
        balances[_to] += _value;                //往接收帳戶增長token數量_value
        emit Transfer(msg.sender, _to, _value);        //觸發轉幣交易事件
        return true;
    }

    function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {
        require(balances[_from] >= _value && allowed[_from][msg.sender] >= _value && _value > 0);
        balances[_to] += _value;                //接收帳戶增長token數量_value
        balances[_from] -= _value;                 //支出帳戶_from減去token數量_value
        allowed[_from][msg.sender] -= _value;    //消息發送者能夠從帳戶_from中轉出的數量減小_value
        emit Transfer(_from, _to, _value);            //觸發轉幣交易事件
        return true;
    }
    function balanceOf(address _owner) public constant returns (uint256 balance) {
        return balances[_owner];
    }

    function approve(address _spender, uint256 _value) public returns (bool success)
    {
        allowed[msg.sender][_spender] = _value;
        emit Approval(msg.sender, _spender, _value);
        return true;
    }

    function allowance(address _owner, address _spender) public constant returns (uint256 remaining) {
        return allowed[_owner][_spender];        //容許_spender從_owner中轉出的token數,也就是受權
    }
    function hello()  public  constant returns (string){
    return 'Hello World';
    }
    mapping (address => uint256) balances;
    mapping (address => mapping (address => uint256)) allowed;
    
}

 

 

積分轉幣程序介紹(完)

相關文章
相關標籤/搜索