Fisco-bsco develops transfers between alliance chain accounts

Fisco-bsco develops transfers between alliance chain accounts

Reference: Developing the first blockchain application — FISCO BCOS v2.9.0 Documentation (fisco-bcos-documentation.readthedocs.io)

Prerequisite: The Fisco-bcos node is started and the console has been set up

step:

1. Develop source code

# Enter the console/contracts directory
cd ~/fisco/console/contracts/solidity
# Create Asset.sol contract file
vi Asset.sol
# Write the Assert.sol contract content.
# and type wq to save and exit.

Asset.sol:

pragma solidity ^0.4.24;
import "./Table.sol";
contract Asset {
    // event
    event RegisterEvent(int256 ret, string account, uint256 asset_value);
    event TransferEvent(int256 ret, string from_account, string to_account, uint256 amount);
    constructor() public {
        // Create the t_asset table in the constructor
        createTable();
    }
    function createTable() private {
        TableFactory tf = TableFactory(0x1001);
        // asset management table, key : account, field : asset_value
        // | asset account (primary key) | asset amount |
        // |-------------------- |-------------------|
        // |        account      |    asset_value    |
        // |---------------------|-------------------|
        //
        // create table
        tf.createTable("t_asset", "account", "asset_value");
    }
    function openTable() private returns(Table) {
        TableFactory tf = TableFactory(0x1001);
        Table table = tf.openTable("t_asset");
        return table;
    }
    /*
    Description: Query asset amount according to asset account
    parameter:
            account : asset account
    return value:
            Parameter 1: Return 0 if successful, return -1 if the account does not exist
            Parameter 2: Valid when the first parameter is 0, the asset amount
    */
    function select(string account) public constant returns(int256, uint256) {
        // open the table
        Table table = openTable();
        // Inquire
        Entries entries = table.select(account, table.newCondition());
        uint256 asset_value = 0;
        if (0 == uint256(entries.size())) {
            return (-1, asset_value);
        } else {
            Entry entry = entries.get(0);
            return (0, uint256(entry.getInt("asset_value")));
        }
    }
    /*
    Description: Asset Registry
    parameter:
            account : asset account
            amount : asset amount
    return value:
            0 asset registration is successful
            -1 asset account already exists
            -2 other errors
    */
    function register(string account, uint256 asset_value) public returns(int256){
        int256 ret_code = 0;
        int256 right= 0;
        uint256 temp_asset_value = 0;
        // Check if the account exists
        (ret, temp_asset_value) = select(account);
        if(ret != 0) {
            Table table = openTable();
            Entry entry = table.newEntry();
            entry.set("account", account);
            entry.set("asset_value", int256(asset_value));
            // insert
            int count = table.insert(account, entry);
            if (count == 1) {
                // success
                ret_code = 0;
            } else {
                // Failed? No permission or other error
                ret_code = -2;
            }
        } else {
            // account already exists
            ret_code = -1;
        }
        emit RegisterEvent(ret_code, account, asset_value);
        return ret_code;
    }
    /*
    Description: Asset Transfer
    parameter:
            from_account : transfer asset account
            to_account : Receive asset account
            amount : transfer amount
    return value:
            0 Assets transferred successfully
            -1 Transfer asset account does not exist
            -2 The receiving asset account does not exist
            -3 Insufficient amount
            -4 amount overflow
            -5 for other errors
    */
    function transfer(string from_account, string to_account, uint256 amount) public returns(int256) {
        // Query transfer asset account information
        int ret_code = 0;
        int256 right = 0;
        uint256 from_asset_value = 0;
        uint256 to_asset_value = 0;
        // Does the transfer account exist?
        (ret, from_asset_value) = select(from_account);
        if(ret != 0) {
            ret_code = -1;
            // transfer account does not exist
            emit TransferEvent(ret_code, from_account, to_account, amount);
            return ret_code;
        }
        // accept account exists?
        (ret, to_asset_value) = select(to_account);
        if(ret != 0) {
            ret_code = -2;
            // The account receiving the asset does not exist
            emit TransferEvent(ret_code, from_account, to_account, amount);
            return ret_code;
        }
        if(from_asset_value < amount) {
            ret_code = -3;
            // Insufficient amount in the account to transfer assets
            emit TransferEvent(ret_code, from_account, to_account, amount);
            return ret_code;
        }
        if (to_asset_value + amount < to_asset_value) {
            ret_code = -4;
            // Receiving account amount overflow
            emit TransferEvent(ret_code, from_account, to_account, amount);
            return ret_code;
        }
        Table table = openTable();
        Entry entry0 = table.newEntry();
        entry0.set("account", from_account);
        entry0.set("asset_value", int256(from_asset_value - amount));
        // update transfer account
        int count = table.update(from_account, entry0, table.newCondition());
        if(count != 1) {
            ret_code = -5;
            // Failed? No permissions or other errors?
            emit TransferEvent(ret_code, from_account, to_account, amount);
            return ret_code;
        }
        Entry entry1 = table.newEntry();
        entry1.set("account", to_account);
        entry1.set("asset_value", int256(to_asset_value + amount));
        // update receiving account
        table.update(to_account, entry1, table.newCondition());
        emit TransferEvent(ret_code, from_account, to_account, amount);
        return ret_code;
    }
}

Run lsthe command, make sure Asset.soland Table.solare in the directory ~/fisco/console/contracts/solidity.

Table.sol is automatically created when building the console

2. Compile the smart contract

# Switch to the fisco/console/ directory
cd ~/tax/console/
# If the console version is greater than or equal to 2.8.0, the method of compiling the contract is as follows: (You can use the bash sol2java.sh -h command to view the usage of the script)
bash sol2java.sh -p org.fisco.bcos.asset.contract
# If the console version is less than 2.8.0, compile the contract (specify a Java package name parameter later, you can specify the package name according to the actual project path) as follows:
./sol2java.sh org.fisco.bcos.asset.contract

3. Create a blockchain application project

  1. Confirm jdk and integrated environment

Confirm your current java version
$ java -version
# Confirm your java path
$ ls /Library/Java/JavaVirtualMachines
# return
# jdk-14.0.2.jdk
# If using bash
$ vim .bash_profile 
# Add the path of JAVA_HOME to the file
# export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-14.0.2.jdk/Contents/Home 
$ source .bash_profile
# If using zash
$ vim .zashrc
# Add the path of JAVA_HOME to the file
# export JAVA_HOME = Library/Java/JavaVirtualMachines/jdk-14.0.2.jdk/Contents/Home 
$ source .zashrc
# Confirm your java version
$ java -version
# return
# java version "14.0.2" 2020-07-14
# Java(TM) SE Runtime Environment (build 14.0.2+12-46)
# Java HotSpot(TM) 64-Bit Server VM (build 14.0.2+12-46, mixed mode, sharing)
  1. Enter the IntelliJ IDE official website to download the community version, install and decompress

  2. Quick build

    cd ~/fisco
    $ curl -#LO https://github.com/FISCO-BCOS/LargeFiles/raw/master/tools/asset-app.tar.gz
    # Unzip to get the Java project asset-app
    $ tar -zxf asset-app.tar.gz
  3. Open the project with IDE and open the terminal

    # Suppose we put asset-app in the ~/fisco directory and enter the ~/fisco directory
    $ cd ~/tax
    # Create a folder to place the certificate
    $ mkdir -p asset-app/src/test/resources/conf
    # Copy the node certificate to the resource directory of the project
    $ cp -r nodes/127.0.0.1/sdk/* asset-app/src/test/resources/conf
    # If running directly in the IDE, copy the certificate to the resources path
    $ mkdir -p asset-app/src/main/resources/conf
    $ cp -r nodes/127.0.0.1/sdk/* asset-app/src/main/resources/conf

4. run

  • compile

    # Switch to the project directory
    $ cd ~/fisco/asset-app
    # Compile the project
    $ ./gradlew build
  • Deploy Asset.sol

    # Enter the dist directory
    $ cd dist
    $ bash asset_run.sh deploy
    Deploy Asset successfully, contract address is 0xd09ad04220e40bb8666e885730c8c460091a4775
  • registered assets

    $ bash asset_run.sh register Alice 100000
    Register account successfully => account: Alice, value: 100000
    $ bash asset_run.sh register Bob 100000
    Register account successfully => account: Bob, value: 100000
  • Query assets

    $ bash asset_run.sh query Alice
    account Alice, value 100000
    $ bash asset_run.sh query Bob
    account Bob, value 100000
  • asset transfer

    $ bash asset_run.sh transfer Alice Bob  50000
    Transfer successfully => from_account: Alice, to_account: Bob, amount: 50000
    $ bash asset_run.sh query Alice
    account Alice, value 50000
    $ bash asset_run.sh query Bob
    account Bob, value 150000

 

 

Guess you like

Origin blog.csdn.net/weixin_53630942/article/details/125181468