# EOS 智能合约 语法 --- **1. EOSIOLIB SDK** EOS为了方便大家做合约开发,提供了一个SDK库:eosiolib。在代码路径:eos/contract/eosiolib目录下可以找到。eosiolib定义了一系列的基本类型和扩张类型,方便大家来实现合约。 **2. EOS 基本数据类型** int基本类型 | 类型名 | 类型 | 说明 | | -------- | -------- | -------- | |account_name |uint64_t |账号名| |permission_name |uint64_t |权限名| |table_name |uint64_t |表名| |time |uint32_t |时间,单位为秒| |scope_name |uint64_t |域名,一般指一个合约| |action_name |uint64_t |action的名子| |weight_type |uint16_t |权限权重值| **3. 符合基本类型** 所有复合基础类型都是两字节对齐。 #define ALIGNED(X) __attribute__ ((aligned (16))) X 这里运用了LLVM集成GNU的关键字__attribute__和方法aligned,功能可以参考ANSI C的pack。比如这里: struct ALIGNED(checksum256) { uint8_t hash[32]; }; 其实和: #pragma pack(2) struct checksum256 { uint8_t hash[32]; }; 作用一致。 |类型名 |类型 |说明| | -------- | -------- | -------- | |public_key |34 bytes |公钥| |signature |66 bytes |签名| |checksum256 |256-bit |256位检验和| |checksum160 |160-bit |160位校验和| |checksum512 |512-bit |512位校验和| |transaction_id_type |checksum256 |transaction的ID| |block_id_type |checksum256 |block的ID| **4. 扩展类型** CPP在上面基础类型之上扩展了一个"name"的类型 struct name { account_name value = 0; } 其本质就是个"account_name"或者说是 "uint64_t"。 **5. EOS 函数方法** | 方法名 | 方法说明 | | -------- | -------- | |string_to_name|取字符串中每个字母的ASCII对应的4bit的数字,然后将其往高位挪,直到计算完所有的字符。| |to_string|从高位往地位取每4bit获得其对应的ascii表示的字符,将字符连接起来。| --- static constexpr uint64_t string_to_name( const char* str ) { uint32_t len = 0; while( str[len] ) ++len; uint64_t value = 0; for( uint32_t i = 0; i <= 12; ++i ) { uint64_t c = 0; if( i < len && i <= 12 ) c = uint64_t(char_to_symbol( str[i] )); if( i < 12 ) { c &= 0x1f; c <<= 64-5*(i+1); } else { c &= 0x0f; } value |= c; } return value; } --- std::string to_string() const { static const char* charmap=".12345abcdefghijklmnopqrstuvwxyz"; std::string str(13,'.'); uint64_t tmp = value; for( uint32_t i = 0; i <= 12; ++i ) { char c = charmap[tmp & (i == 0 ? 0x0f : 0x1f)]; str[12-i] = c; tmp >>= (i == 0 ? 4 : 5); } trim_right_dots( str ); return str; } **N("eosio.token"):合约名称转换成对应的name** **#define N(X) ::eosio::string_to_name(#X)** **to_string是对string_to_name的一个逆运算** **6. multi_index索引** - https://hackmd.io/GRNH0DlgSEmAcQldRQsE-w 这个是我自己整理的 -https://github.com/eoshackathon/eos_dapp_development_cn/blob/master/docs/db_module.md 这个是github上面古千峰整理的,这个还要感谢玉成的热心分享。 **7. eos 目录结构** ###### tags: `区块链` `EOS` `智能合约` `DAPP`