博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
FISCO BCOS Solidity 使用Table合约CRUD接口 智能合约例子
阅读量:2038 次
发布时间:2019-04-28

本文共 6143 字,大约阅读时间需要 20 分钟。

1. 先安装webase-front,使用它的IDE编译智能合约(可以查看我其它文章)

2. 例子功能说明:

3. 源码

StudentScoreByCRUD.sol

pragma solidity ^0.4.25; // import "./Table.sol"; contract StudentScoreByCRUD{ 	address private _owner; 	modifier onlyOwner{		require(_owner == msg.sender, "Auth: only owner is authorized");		_;	} 	constructor () public {		_owner = msg.sender;	}   	event createEvent(address owner, string tableName);	event insertEvent(address studentId, string courseName, int score);	event updateEvent(address studentId, string courseName, int score);	event removeEvent(address studentId, string courseName);    	// 创建成绩表	function create() public onlyOwner returns(int){ 		TableFactory tf = TableFactory(0x1001);		int count = tf.createTable("stu_score", "student_id", "course_name, score");		emit createEvent(msg.sender, "stu_score");		return count; 	}  	// 插入成绩操作	function insert(address studentId, string courseName, int score) public onlyOwner returns(int){  		TableFactory tf = TableFactory(0x1001);		Table table = tf.openTable("stu_score"); 		string memory stuIdStr = addressToString(studentId);		Entry entry = table.newEntry();		entry.set("student_id", stuIdStr);		entry.set("course_name", courseName);		entry.set("score", score); 		int count = table.insert(stuIdStr, entry);		emit insertEvent(studentId, courseName, score);		return count; 	}  	function addressToString(address addr) private pure returns(string) {		// Convert addr to bytes		bytes20 value = bytes20(uint160(addr));		bytes memory strBytes = new bytes(42);		// Encode hex prefix		strBytes[0] = '0';		strBytes[1] = 'x'; 		// Encode bytes usig hex encoding		for(uint i = 0; i < 20; i++){			uint8 byteValue = uint8(value[i]);			strBytes[2 + (i<<1)] = encode(byteValue >> 4);			strBytes[3 + (i<<1)] = encode(byteValue & 0x0f);		} 		return string(strBytes);   	}  	function encode(uint8 num) private pure returns(byte){		// 0-9 -> 0-9		if(num >= 0 && num <= 9){			return byte(num + 48);		}		// 10-15 -> a-f		return byte(num + 87);	}  	// 更新成绩操作	function update(address studentId, string courseName, int newScore) public onlyOwner returns(int){ 		TableFactory tf = TableFactory(0x1001);		Table table = tf.openTable("stu_score"); 		Entry entry = table.newEntry();		entry.set("score", newScore); 		string memory stuIdStr = addressToString(studentId);		Condition condition = table.newCondition();		condition.EQ("student_id", stuIdStr);		condition.EQ("course_name", courseName); 		int count = table.update(stuIdStr, entry, condition);		emit updateEvent(studentId, courseName, newScore); 		return count; 	} 	// 删除成绩操作	function remove(address studentId, string courseName) public onlyOwner returns(int){ 		TableFactory tf = TableFactory(0x1001);		Table table = tf.openTable("stu_score"); 		string memory stuIdStr = addressToString(studentId);		Condition condition = table.newCondition();		condition.EQ("student_id", stuIdStr);		condition.EQ("course_name", courseName); 		int count = table.remove(stuIdStr, condition);		emit removeEvent(studentId, courseName); 		return count; 	} 	// 查询成绩操作	function select(address studentId, string courseName) public view returns(int, string){ 		TableFactory tf = TableFactory(0x1001);		Table table = tf.openTable("stu_score"); 		string memory stuIdStr = addressToString(studentId);		Condition condition = table.newCondition();		condition.EQ("student_id", stuIdStr);		condition.EQ("course_name", courseName); 		Entries entries = table.select(stuIdStr, condition); 		if(entries.size() == 0){		    return (0, stuIdStr);		}		else{		    		    return (entries.get(0).getInt("score"), stuIdStr);		} 	}   }   // table.sol,因为在webase-front的IDE中无法直接引用,所以将源码放到代码文件中 contract TableFactory {     function openTable(string memory) public view returns (Table) {} //open table     function createTable(string memory, string memory, string memory) public returns (int256) {} //create table }   //select condition contract Condition {     function EQ(string memory, int256) public {}     function EQ(string memory, string memory) public {}       function NE(string memory, int256) public {}     function NE(string memory, string memory) public {}       function GT(string memory, int256) public {}     function GE(string memory, int256) public {}       function LT(string memory, int256) public {}     function LE(string memory, int256) public {}       function limit(int256) public {}     function limit(int256, int256) public {} }   //one record contract Entry {     function getInt(string memory) public view returns (int256) {}     function getUInt(string memory) public view returns (int256) {}     function getAddress(string memory) public view returns (address) {}     function getBytes64(string memory) public view returns (bytes1[64] memory) {}     function getBytes32(string memory) public view returns (bytes32) {}     function getString(string memory) public view returns (string memory) {}       function set(string memory, int256) public {}     function set(string memory, uint256) public {}         function set(string memory, string memory) public {}     function set(string memory, address) public {} }   //record sets contract Entries {     function get(int256) public view returns (Entry) {}         function size() public view returns (int256) {} }   //Table main contract contract Table {     function select(string memory, Condition) public view returns (Entries) {}     function insert(string memory, Entry) public returns (int256) {}     function update(string memory, Entry, Condition) public returns (int256) {}     function remove(string memory, Condition) public returns (int256) {}           function newEntry() public view returns (Entry) {}     function newCondition() public view returns (Condition) {} }   contract KVTableFactory {     function openTable(string memory) public view returns (KVTable) {}     function createTable(string memory, string memory, string memory) public returns (int256) {} }   //KVTable per permiary key has only one Entry contract KVTable {     function get(string memory) public view returns (bool, Entry) {}     function set(string memory, Entry) public returns (int256) {}     function newEntry() public view returns (Entry) {} }

4. 结果

 

转载地址:http://svkof.baihongyu.com/

你可能感兴趣的文章
Springboot 之 自定义配置文件及读取配置文件
查看>>
服务器端判断request来自Ajax请求(异步)还是传统请求(同步)
查看>>
git adding files to index has encountered a problem
查看>>
git学习六:git提交忽略不必要的文件或文件夹
查看>>
springcloud(三):服务提供与调用
查看>>
Memcached 和 Redis 分布式锁方案
查看>>
乐视秒杀:每秒十万笔交易的数据架构解读
查看>>
如何解决秒杀的性能问题和超卖的讨论
查看>>
centos 卸载软件
查看>>
Linux下MySQL备份以及crontab定时备份(crontab表达式简介)
查看>>
powerdesigner: used normalization rules prevent from reusing this data item in a primary
查看>>
PowerDesigner最基础的使用方法入门学习
查看>>
eclipse maven 查找重复依赖
查看>>
MySQL数据表生成ER图 workbench使用
查看>>
创建Maven web项目时 出现 web.xml is missing and <failOnMissingWebXml> is set to true错误 pox.xml编译错误
查看>>
Maven几个常用的maven插件
查看>>
freemarker常见语法大全
查看>>
Feign真正正确的使用方法--供老项目调用
查看>>
hibernate两表关联分页查询
查看>>
maven snapshot快照仓库和release公布仓库区别
查看>>