eosjs常用代码
eosjs Startup 整理了eosjs常用代码,包括创建、查询EOS账号,EOS转账,购买RAM,出售RAM,抵押资源,取消抵押资源,生成密钥对等。
https://github.com/dabdevelop/eosjs-startup
eos rpc get_table_rows index_position
uint64_t read_only::get_table_index_name(const read_only::get_table_rows_params& p, bool& primary) {
using boost::algorithm::starts_with;
// see multi_index packing of index name
const uint64_t table = p.table;
uint64_t index = table & 0xFFFFFFFFFFFFFFF0ULL;
EOS_ASSERT( index == table, chain::contract_table_query_exception, "Unsupported table name: ${n}", ("n", p.table) );
primary = false;
uint64_t pos = 0;
if (p.index_position.empty() || p.index_position == "first" || p.index_position == "primary" || p.index_position == "one") {
primary = true;
} else if (starts_with(p.index_position, "sec") || p.index_position == "two") { // second, secondary
} else if (starts_with(p.index_position , "ter") || starts_with(p.index_position, "th")) { // tertiary, ternary, third, three
pos = 1;
} else if (starts_with(p.index_position, "fou")) { // four, fourth
pos = 2;
} else if (starts_with(p.index_position, "fi")) { // five, fifth
pos = 3;
} else if (starts_with(p.index_position, "six")) { // six, sixth
pos = 4;
} else if (starts_with(p.index_position, "sev")) { // seven, seventh
pos = 5;
} else if (starts_with(p.index_position, "eig")) { // eight, eighth
pos = 6;
} else if (starts_with(p.index_position, "nin")) { // nine, ninth
pos = 7;
} else if (starts_with(p.index_position, "ten")) { // ten, tenth
pos = 8;
} else {
try {
pos = fc::to_uint64( p.index_position );
} catch(...) {
EOS_ASSERT( false, chain::contract_table_query_exception, "Invalid index_position: ${p}", ("p", p.index_position));
}
if (pos < 2) {
primary = true;
pos = 0;
} else {
pos -= 2;
}
}
index |= (pos & 0x000000000000000FULL);
return index;
}
Position of the index used, accepted parameters primary, secondary, tertiary, fourth, fifth, sixth, seventh, eighth, ninth , tenth
index_position 可以以某前缀的名字指定对应多级索引,或者数字(从2开始)
EOS 合约 table primary key支持的类型
read_only::get_table_rows_result read_only::get_table_rows( const read_only::get_table_rows_params& p )const {
const abi_def abi = eosio::chain_apis::get_abi( db, p.code );
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
bool primary = false;
auto table_with_index = get_table_index_name( p, primary );
if( primary ) {
EOS_ASSERT( p.table == table_with_index, chain::contract_table_query_exception, "Invalid table name ${t}", ( "t", p.table ));
auto table_type = get_table_type( abi, p.table );
if( table_type == KEYi64 || p.key_type == "i64" || p.key_type == "name" ) {
return get_table_rows_ex<key_value_index>(p,abi);
}
EOS_ASSERT( false, chain::contract_table_query_exception, "Invalid table type ${type}", ("type",table_type)("abi",abi));
} else {
EOS_ASSERT( !p.key_type.empty(), chain::contract_table_query_exception, "key type required for non-primary index" );
if (p.key_type == chain_apis::i64 || p.key_type == "name") {
return get_table_rows_by_seckey<index64_index, uint64_t>(p, abi, [](uint64_t v)->uint64_t {
return v;
});
}
else if (p.key_type == chain_apis::i128) {
return get_table_rows_by_seckey<index128_index, uint128_t>(p, abi, [](uint128_t v)->uint128_t {
return v;
});
}
else if (p.key_type == chain_apis::i256) {
if ( p.encode_type == chain_apis::hex) {
using conv = keytype_converter<chain_apis::sha256,chain_apis::hex>;
return get_table_rows_by_seckey<conv::index_type, conv::input_type>(p, abi, conv::function());
}
using conv = keytype_converter<chain_apis::i256>;
return get_table_rows_by_seckey<conv::index_type, conv::input_type>(p, abi, conv::function());
}
结论
primary key 只支持uint64_t
和 capi_name
如果想支持其他类型只能用
二级索引支持了 index_position
+ key_type
官方解释
它们是多索引表,因为它们支持在数据上使用多个索引,主索引类型必须是uint64_t并且必须是唯一的,但其他次要索引可以具有重复项。最多可以有16个附加索引,字段类型可以是uint64_t,uint128_t,eosio::checksum256,double或long double
参考
https://developers.eos.io/eosio-cpp/docs/using-multi-index-tables#section-introduction
https://developers.eos.io/eosio-nodeos/reference#get_table_rows
https://eosio.stackexchange.com/questions/3091/is-it-possible-to-use-a-type-other-than-uint64-t-for-a-table-primary-key/3097#3097