FISCO BCOS Solidity 智能合约 return string[] This type is only supported in the new experimental ABI

最开始引入:

pragma experimental ABIEncoderV2;

 

pragma solidity ^0.4.25;
pragma experimental ABIEncoderV2;

// 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);
		}
 
	}
	
	// 查询某个学生的某课成绩所有成绩
	function select_all_scores(address studentId, string courseName) public view 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);
 
		Entries entries = table.select(stuIdStr, condition);
 
        // 初始化数组大小
        int[] memory user_scores_list = new int[](uint256(entries.size()));
        
        // 给数组赋值
		for(int i=0; i<entries.size(); ++i) {
		    
            Entry entry = entries.get(i);
            // 添加数组元素
            user_scores_list[uint256(i)] = entry.getInt("score");

        }

        return user_scores_list;
 
	}
	
	
	// 查询某个学生的所有成绩
	function select_scores_info(address studentId) public view returns(string[], 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);
 
		Entries entries = table.select(stuIdStr, condition);
 
        // 初始化数组大小
        string[] memory user_courses_list = new string[](uint256(entries.size()));
        int[] memory user_scores_list = new int[](uint256(entries.size()));
        
        // 给数组赋值
		for(int i=0; i<entries.size(); ++i) {
		    
            Entry entry = entries.get(i);
            // 添加数组元素
            user_courses_list[uint256(i)] = entry.getString("course_name");
            user_scores_list[uint256(i)] = entry.getInt("score");

        }

        return (user_courses_list, user_scores_list);
 
	}
 
 
 
}
 
 
 
// 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) {}
 
}

参考:https://ethereum.stackexchange.com/questions/69077/how-can-i-return-dynamic-string-array-in-solidity

软件工程小施同学 CSDN认证博客专家 学堂在线讲师
每一个伟大,都源于一个勇敢的开始(微博:“软件工程小施同学”,公号”微程序学堂“)
©️2020 CSDN 皮肤主题: 撸撸猫 设计师:设计师小姐姐 返回首页