Skip to content

Commit

Permalink
Merge pull request #328 from mvs-org/0.8.3
Browse files Browse the repository at this point in the history
remove check prefix of token name.
  • Loading branch information
codrush authored Aug 31, 2018
2 parents 4eb8ef6 + 2399752 commit 4bb075f
Show file tree
Hide file tree
Showing 2 changed files with 200 additions and 43 deletions.
4 changes: 0 additions & 4 deletions src/lib/explorer/extensions/commands/swaptoken.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,6 @@ console_result swaptoken::invoke(Json::Value& jv_output,
// check asset symbol
check_asset_symbol(argument_.symbol);

if (argument_.symbol.compare(0, 4, "ERC.") != 0) {
throw argument_legality_exception{"Only support assets prefixed by 'ERC.'"};
}

attachment attach_asset, attach_fee;
const std::string&& to_address = get_address(argument_.to, attach_asset, false, blockchain);
const std::string&& swapfee_address = bc::get_developer_community_address(
Expand Down
239 changes: 200 additions & 39 deletions test/test-rpc-v3/tools/parse_cmd.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,86 @@
import os
import os, sys
import re

cmd_dir = '/home/czp/GitHub/nova/metaverse/include/metaverse/explorer/extensions/commands'
cmd_dir = '/home/czp/GitHub/metaverse/include/metaverse/explorer/extensions/commands'

rettypes = '''
getblockheader, blockheader
getblock, block
getnewaccount,account
getnewaddress,List<String>
listaddresses,List<String>
validateaddress,address
importaccount,accountEx
importkeyfile,String
dumpkeyfile,String
changepasswd,accountstatus
deleteaccount,accountstatus
getaccount,accountresult
shutdown,String
getinfo,info
getheight,UInt64
getpeerinfo,List<String>
getmininginfo,mining_info
startmining,String
stopmining,String
getwork,List<String>
addnode,String
submitwork,Boolean
getmemorypool,List<transaction>
getbalance,balances
listbalances,List<balance>
deposit,transaction
send,transaction
sendfrom,transaction
sendmore,transaction
gettx,transaction
listtxs,transactions
createasset,asset
deletelocalasset,delasset
issue,transaction
getaccountasset,List<asset>
getaddressasset,List<asset>
getasset,List<String>
listassets,List<asset>
sendasset,transaction
sendassetfrom,transaction
secondaryissue,transaction
issuecert,transaction
transfercert,transaction
burn,transaction
getnewmultisig,mulsignature
listmultisig,List<mulsignature>
deletemultisig,mulsignature
createmultisigtx,String
signmultisigtx,String
createrawtx,String
signrawtx,rawtx
decoderawtx,transaction
sendrawtx,String
registerdid,transaction
listdids,List<did>
didsend,transaction
didsendmore,transaction
didsendfrom,transaction
didsendasset,transaction
didsendassetfrom,transaction
didchangeaddress,transaction
getdid,List<didhistoryaddress>
registermit,transaction
transfermit,transaction
listmits,List<mit>
getmit,List<mit>
getpublickey,String
popblock,String
fetchheaderext,String
getaddressetp,String
setminingaccount,String
'''

func2type = {}
for i in rettypes.strip().split('\n'):
k,v = i.split(',')
func2type[k.strip()] = v.strip()

def get_paraname(line):
pattern = '"([/\w]+)(,\w+){0,1}"'
Expand Down Expand Up @@ -52,6 +131,8 @@ def macro_expand(input):

return convert.get(input, input)

special_cmds = []

def parse_cmd(filename):
starts = 'BX_HELP_VARIABLE ",h",'

Expand Down Expand Up @@ -106,18 +187,20 @@ def parse_cmd(filename):
param_details.append( (param_name, param_def, param_desc) )

# generate SDK
#generate_py_SDK(cmdname, pos_params, param_details)
generate_py_SDK(cmdname, pos_params, param_details)
#generate_go_SDK(cmdname, pos_params, param_details)
generate_DotNet_SDK(cmdname, pos_params, param_details)
#generate_DotNet_SDK(cmdname, pos_params, param_details)

def generate_py_SDK(cmdname, pos_params, params):
global special_cmds
template = '''
@mvs_api_v2
@mvs_api_v3
def %(cmdname)s(%(argument_defines)s):
\'\'\'
%(comments)s
\'\'\'
positional=[%(positional_arguments)s]
%(token_arguments)s
optional={
%(optional_arguments)s
}
Expand Down Expand Up @@ -157,20 +240,32 @@ def cpp2py_type(cpp_type):
argument_defines_2 = [] # with default value
optional_arguments = []
positional_arguments = []
token_arguments = []
for param_name, param_def, param_desc in params:
comments.append(" :param: %s(%s): %s" % (param_name, cpp2py_type(param_def["para_type"]), param_desc))
if param_def.get("must_give", False) == False:
argument_defines_2.append('%s=None' % param_name)
if "colon_delimited2_item" in param_def["para_type"]:
argument_defines_2.append('%s=(0,0)' % param_name)
special_cmds.append(cmdname)
else:
argument_defines_2.append('%s=None' % param_name)
else:
argument_defines_1.append('%s' % param_name)

if param_name in pos_params:
if param_def["para_type"] == 'std::vector<std::string>':
positional_arguments.append("' '.join(%s)" % param_name)
special_cmds.append(cmdname)
else:
positional_arguments.append(param_name)
continue

if param_def.get("just_token", False) == True:
assert param_def["para_type"] == "bool"
token_arguments.append(param_name)
special_cmds.append(cmdname)
continue

if "colon_delimited2_item" in param_def["para_type"]:
optional_arguments.append(
'"%s" : "%%s:%%s" %% (%s[0], %s[1]),' % (param_name, param_name, param_name)
Expand All @@ -184,6 +279,7 @@ def cpp2py_type(cpp_type):
'argument_defines': ', '.join(argument_defines_1 + argument_defines_2),
'optional_arguments': '\n '.join(optional_arguments),
'positional_arguments': ', '.join(positional_arguments),
'token_arguments': '\n '.join('if %s == True: positional.append("--%s")' % (i, i) for i in token_arguments),
}

print template % paras
Expand All @@ -195,11 +291,14 @@ def generate_go_SDK(cmdname, pos_params, params):
*/
func (r *RPCClient) %(func_name)s(%(argument_defines)s) (*JSONRpcResp, error) {
cmd := "%(cmdname)s"
positional := []interface{}{%(positional_arguments)s}
%(positional_defaults)s
optional := map[string]interface{}{
%(optional_arguments)s
%(optional_mustgive)s
}
args := []interface{}{%(positional_arguments)s optional}
%(token_arguments)s
%(optional_arguments)s
args := append(positional, optional)
return r.doPost(r.Url, cmd, args)
}
'''
Expand Down Expand Up @@ -244,33 +343,67 @@ def special_type_desc(cpp_type):

return convert.get(cpp_type, cpp_type)


def get_zero(go_type):
if '[2]uint64' == go_type:
return '[2]uint64{0, 0}'
if 'int' in go_type:
return '0'
if 'string' == go_type:
return '""'
if 'bool' == go_type:
return 'false'
if '[]string'== go_type:
return 'nil'
print go_type
assert (False)
comments = []
argument_defines = []
token_arguments = []
optional_arguments = []
optional_mustgive = []
positional_arguments = []
positional_defaults = []
for param_name, param_def, param_desc in params:
#print cmdname, param_name
comments.append(" :param: %s(%s): %s" % (param_name, special_type_desc( param_def["para_type"] ), param_desc) )
argument_defines.append( '%s %s' % (param_name, cpp2go_type( param_def["para_type"] ) ) )

if param_name in pos_params:
if param_def.get("must_give", False) == False:
zero = get_zero(cpp2go_type(param_def["para_type"]))
if param_def.has_key("default_value"):
if param_def["default_value"] == zero:
positional_defaults.append('if %s != %s {\n positional = append(positional, %s)\n }' % (
param_name, zero, param_name))
continue

if param_def["para_type"] == 'std::vector<std::string>':
positional_arguments.append('strings.Join(%s, " ")' % param_name)
else:
positional_arguments.append(param_name)
continue
if "colon_delimited2_item" in param_def["para_type"]:
optional_arguments.append('"%s" : strings.Join([]string{strconv.FormatUint(uint64(%s[0]), 10), strconv.FormatUint(uint64(%s[1]), 10)}, ":"),' % (param_name, param_name, param_name))
else:
optional_arguments.append('"%s" : %s,' % (param_name, param_name) )
if param_def.get("just_token", False) == True:
assert param_def["para_type"] == "bool"
token_arguments.append(param_name)
continue

if param_def.get("must_give", False) == False:
if "colon_delimited2_item" in param_def["para_type"]:
optional_arguments.append( (param_name, get_zero(cpp2go_type(param_def["para_type"])), 'strings.Join([]string{strconv.FormatUint(uint64(%s[0]), 10), strconv.FormatUint(uint64(%s[1]), 10)}, ":")' % ( param_name, param_name)))
else:
optional_arguments.append( (param_name, get_zero(cpp2go_type(param_def["para_type"])), param_name) )
else:
optional_mustgive.append('"%s":%s,' % (param_name, param_name))
paras = {
'comments' : '\n'.join(comments),
'func_name' : cmdname.title(),
'cmdname' : cmdname,
'argument_defines' : ', '.join(argument_defines),
'optional_arguments' : '\n '.join(optional_arguments),
'positional_arguments' : ' '.join([i+',' for i in positional_arguments]),
'token_arguments' : '\n '.join('if %s == true{\n positional=append(positional, "--%s")\n }' % (i, i) for i in token_arguments),
'optional_arguments' : '\n '.join(['if %s != %s {\n optional["%s"] = %s\n }' % (key, zero, key, value) for key, zero, value in optional_arguments]),
'optional_mustgive' : '\n '.join(optional_mustgive),
'positional_arguments' : ', '.join(positional_arguments),
'positional_defaults' : '\n '.join(positional_defaults),
}

print template % paras
Expand All @@ -281,31 +414,33 @@ def generate_DotNet_SDK(cmdname, pos_params, params):
/*
%(comments)s
*/
public String %(func_name)s(%(argument_defines)s)
public %(rettype)s %(func_name)s(%(argument_defines)s)
{
List<String> parameters = new List<String>() { %(positional_arguments)s };
%(positional_arguments_with_default_value)s
%(token_arguments)s
%(add_optional_parameters)s
return getResult<String>("%(func_name)s", parameters);
return getResult<%(rettype)s>("%(func_name)s", parameters);
}
'''
def cpp2dotnet_type(cpp_type):
convert = {
'uint8_t' : 'UInt8',
'uint16_t': 'UInt16',
'uint32_t': 'UInt32',
'std::uint32_t' : 'UInt32',
'uint64_t': 'UInt64',
'uint8_t' : 'UInt8?',
'uint16_t': 'UInt16?',
'uint32_t': 'UInt32?',
'std::uint32_t' : 'UInt32?',
'uint64_t': 'UInt64?',

'int8_t': 'Int8',
'int16_t': 'Int16',
'int32_t': 'Int32',
'std::int32_t': 'Int32',
'int64_t': 'Int64',
'int8_t': 'Int8?',
'int16_t': 'Int16?',
'int32_t': 'Int32?',
'std::int32_t': 'Int32?',
'int64_t': 'Int64?',

'non_negative_uint64' : 'UInt64',
'non_negative_uint64' : 'UInt64?',

'std::string' : 'String',
'bool': 'Boolean',
'bool': 'Boolean?',
'explorer::config::transaction' : 'String',
'std::vector<std::string>' : 'List<String>',
'explorer::config::language' : 'String',
Expand All @@ -329,43 +464,69 @@ def special_type_desc(cpp_type):

return convert.get(cpp_type, cpp_type)
comments = []
argument_defines = []
argument_defines_1 = [] # with no default valie
argument_defines_2 = [] # with default value
optional_arguments = []
positional_arguments = []
positional_arguments_with_default_value = []
token_arguments = []
for param_name, param_def, param_desc in params:
comments.append(" :param: %s(%s): %s" % (param_name, special_type_desc(param_def["para_type"]), param_desc))
argument_defines.append('%s %s' % (cpp2dotnet_type(param_def["para_type"]), param_name))
#argument_defines.append('%s %s' % (cpp2dotnet_type(param_def["para_type"]), param_name))
if param_def.get("must_give", False) == False:
argument_defines_2.append('%s %s=null' % (cpp2dotnet_type(param_def["para_type"]), param_name))
else:
argument_defines_1.append('%s %s' % (cpp2dotnet_type(param_def["para_type"]).replace('?', ''), param_name) )

if param_name in pos_params:
if param_def.get("must_give", False) == False:
target = positional_arguments_with_default_value
else:
target = positional_arguments

if cpp2dotnet_type(param_def["para_type"]) == 'String':
positional_arguments.append(param_name)
target.append((param_name, param_name))
elif cpp2dotnet_type(param_def["para_type"]) == 'List<String>':
positional_arguments.append('String.Join(" ", %s.ToArray())' % param_name)
target.append((param_name, 'String.Join(" ", %s.ToArray())' % param_name))
else:
positional_arguments.append('%s.ToString()' % param_name)
target.append((param_name, '%s.ToString()' % param_name))
continue

if param_def.get("just_token", False) == True:
assert param_def["para_type"] == "bool"
token_arguments.append(param_name)
continue

if "vector<" in param_def["para_type"]:
optional_arguments.append('foreach (var i in %s) {' % param_name)
optional_arguments.append(' parameters.AddRange(new List<String>{"--%s", i.ToString()});' % param_name)
optional_arguments.append('}')
else:
prefix = ""
if param_def.get("must_give", False) == False:
prefix = "if (%s != null) " % param_name

if "colon_delimited2_item" in param_def["para_type"]:
value = 'parameters.AddRange(new List<String>{"--%s", String.Format("{0}:{1}", %s.Item1, %s.Item2)});' % (param_name, param_name, param_name)
else:
value = 'parameters.AddRange(new List<String>{"--%s", %s.ToString()});' % (param_name, param_name)
optional_arguments.append(value)
optional_arguments.append(prefix + value)

paras = {
'comments': '\n'.join(comments),
'func_name': cmdname,
'argument_defines': ', '.join(argument_defines),
'argument_defines': ', '.join(argument_defines_1 + argument_defines_2),
'add_optional_parameters': '\n '.join(optional_arguments),
'positional_arguments': ', '.join(positional_arguments),
'positional_arguments': ', '.join([i[1] for i in positional_arguments]),
'positional_arguments_with_default_value' : '\n '.join([ 'if (%s != null) parameters.Add(%s);' % (i[0], i[1]) for i in positional_arguments_with_default_value ] ),
'rettype' : func2type[cmdname],
'token_arguments': '\n '.join([ 'if (%s == true) parameters.Add("--%s");' % (i, i) for i in token_arguments ])
}

print template % paras

if __name__ == "__main__":
for root, dirs, files in os.walk(cmd_dir):
for file in files:
parse_cmd(root + '/' + file)
parse_cmd(root + '/' + file)
print >> sys.stderr, set(special_cmds)

0 comments on commit 4bb075f

Please sign in to comment.