调整了blockchain api
This commit is contained in:
parent
b1ef05e0be
commit
06a6794245
|
@ -895,66 +895,6 @@ class ApplicationController < ActionController::Base
|
|||
HotSearchKeyword.add(keyword)
|
||||
end
|
||||
|
||||
|
||||
# author: zxh
|
||||
# blockchain存证api
|
||||
#def invoke_blockchain_api(uri, params)
|
||||
# begin
|
||||
# uri = URI.parse(URI.encode(uri.strip))
|
||||
# res = Net::HTTP.start(uri.host, uri.port) do |http|
|
||||
# req = Net::HTTP::Post.new(uri)
|
||||
# req['Content-Type'] = 'application/json'
|
||||
# req.body = params
|
||||
# http.request(req)
|
||||
# end
|
||||
# if res.code.to_i != 200
|
||||
# puts '区块链接口请求失败.'
|
||||
# return false
|
||||
# else
|
||||
# res_body = JSON.parse(res.body)
|
||||
# if res_body.has_key?("data") && JSON.parse(res_body["data"]).has_key?("status") && JSON.parse(res_body["data"])['status'] == "Success"
|
||||
# else
|
||||
# puts '区块链接口请求出错.'
|
||||
# return false
|
||||
# end
|
||||
# end
|
||||
#
|
||||
# return true
|
||||
# rescue Exception => e
|
||||
# puts '区块链接口请求失败.'
|
||||
# return false
|
||||
# end
|
||||
#end
|
||||
|
||||
def invoke_blockchain_api(uri, params)
|
||||
begin
|
||||
uri = URI.parse(URI.encode(uri.strip))
|
||||
res = Net::HTTP.start(uri.host, uri.port) do |http|
|
||||
req = Net::HTTP::Post.new(uri)
|
||||
req['Content-Type'] = 'application/json'
|
||||
req.body = params
|
||||
http.request(req)
|
||||
end
|
||||
if res.code.to_i != 200
|
||||
puts '区块链接口请求失败.'
|
||||
return false
|
||||
else
|
||||
res_body = JSON.parse(res.body)
|
||||
if res_body.has_key?("status") && res_body["status"] == 0
|
||||
else
|
||||
puts '区块链接口请求出错.'
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
return true
|
||||
rescue Exception => e
|
||||
puts '区块链接口请求失败.'
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
# author: zxh
|
||||
# blockchain相关项目活动调用函数
|
||||
# return true: 表示上链操作成功; return false: 表示上链操作失败
|
||||
|
@ -987,7 +927,7 @@ class ApplicationController < ActionController::Base
|
|||
updated_at = model['updated_on']
|
||||
|
||||
# 调用区块链接口
|
||||
param = {
|
||||
params = {
|
||||
"request-type": "upload issue info",
|
||||
"issue_id": "gitlink-" + id.to_s,
|
||||
"repo_id": "gitlink-" + project_id.to_s,
|
||||
|
@ -1001,8 +941,12 @@ class ApplicationController < ActionController::Base
|
|||
"created_at": created_at,
|
||||
"updated_at": updated_at
|
||||
}.to_json
|
||||
success_blockchain = invoke_blockchain_api(Blockchain.blockchain_config[:api_url], param)
|
||||
return success_blockchain
|
||||
resp_body = Blockchain::InvokeBlockchainApi.call(params)
|
||||
if resp_body['status'] == 10
|
||||
raise Error, resp_body['message']
|
||||
elsif resp_body['status'] != 0
|
||||
raise Error, "区块链接口请求失败."
|
||||
end
|
||||
|
||||
elsif activity_type == "issue_comment_create"
|
||||
issue_comment_id = model['id']
|
||||
|
@ -1016,7 +960,7 @@ class ApplicationController < ActionController::Base
|
|||
|
||||
if project['use_blockchain'] == 0 || project['use_blockchain'] == false
|
||||
# 无需执行上链操作
|
||||
return true
|
||||
return
|
||||
end
|
||||
|
||||
identifier = project['identifier']
|
||||
|
@ -1034,7 +978,7 @@ class ApplicationController < ActionController::Base
|
|||
created_at = model['created_on']
|
||||
|
||||
if issue_classify == "issue"
|
||||
param = {
|
||||
params = {
|
||||
"request-type": "upload issue comment info",
|
||||
"issue_comment_id": "gitlink-" + issue_comment_id.to_s,
|
||||
"issue_comment_number": 0, # 暂时不需要
|
||||
|
@ -1050,7 +994,7 @@ class ApplicationController < ActionController::Base
|
|||
"created_at": created_at,
|
||||
}.to_json
|
||||
elsif issue_classify == "pull_request"
|
||||
param = {
|
||||
params = {
|
||||
"request-type": "upload pull request comment info",
|
||||
"pull_request_comment_id": "gitlink-" + issue_comment_id.to_s,
|
||||
"pull_request_comment_number": 0, # 不考虑该字段
|
||||
|
@ -1068,15 +1012,19 @@ class ApplicationController < ActionController::Base
|
|||
end
|
||||
|
||||
# 调用区块链接口
|
||||
success_blockchain = invoke_blockchain_api(Blockchain.blockchain_config[:api_url], param)
|
||||
return success_blockchain
|
||||
resp_body = Blockchain::InvokeBlockchainApi.call(params)
|
||||
if resp_body['status'] == 10
|
||||
raise Error, resp_body['message']
|
||||
elsif resp_body['status'] != 0
|
||||
raise Error, "区块链接口请求失败."
|
||||
end
|
||||
elsif activity_type == "pull_request_create"
|
||||
# 调用区块链接口
|
||||
project_id = model['project_id']
|
||||
project = Project.find(project_id)
|
||||
if project['use_blockchain'] == 0 || project['use_blockchain'] == false
|
||||
# 无需执行上链操作
|
||||
return true
|
||||
return
|
||||
end
|
||||
|
||||
pull_request_id = model['id']
|
||||
|
@ -1106,13 +1054,13 @@ class ApplicationController < ActionController::Base
|
|||
# 查询pull request对应的commit信息
|
||||
commits = Gitea::PullRequest::CommitsService.call(ownername, identifier, model['gitea_number'])
|
||||
if commits.nil?
|
||||
return false # 获取pr中变更的commit信息失败
|
||||
raise Error, "区块链接口请求失败" # 获取pr中变更的commit信息失败
|
||||
end
|
||||
commit_shas = []
|
||||
commits.each do |c|
|
||||
commit_shas << c["Sha"]
|
||||
end
|
||||
param = {
|
||||
params = {
|
||||
"request-type": "upload pull request info",
|
||||
"pull_request_id": "gitlink-" + pull_request_id.to_s,
|
||||
"pull_request_number": 0, # trustie没有该字段
|
||||
|
@ -1131,16 +1079,19 @@ class ApplicationController < ActionController::Base
|
|||
"created_at": created_at,
|
||||
"updated_at": updated_at
|
||||
}.to_json
|
||||
success_blockchain = invoke_blockchain_api(Blockchain.blockchain_config[:api_url], param)
|
||||
return success_blockchain
|
||||
resp_body = Blockchain::InvokeBlockchainApi.call(params)
|
||||
if resp_body['status'] == 9
|
||||
raise Error, resp_body['message']
|
||||
elsif resp_body['status'] != 0
|
||||
raise Error, "区块链接口请求失败."
|
||||
end
|
||||
elsif activity_type == "pull_request_merge"
|
||||
|
||||
# 调用区块链接口
|
||||
project_id = model['project_id']
|
||||
project = Project.find(project_id)
|
||||
if project['use_blockchain'] == 0 || project['use_blockchain'] == false
|
||||
# 无需执行上链操作
|
||||
return true
|
||||
return
|
||||
end
|
||||
|
||||
pull_request_id = model['id']
|
||||
|
@ -1157,7 +1108,7 @@ class ApplicationController < ActionController::Base
|
|||
# 查询pull request对应的commit信息
|
||||
commits = Gitea::PullRequest::CommitsService.call(ownername, identifier, model['gitea_number'])
|
||||
if commits.nil?
|
||||
return false # 获取pr中变更的commit信息失败
|
||||
raise Error, "区块链接口请求失败" # 获取pr中变更的commit信息失败
|
||||
end
|
||||
commit_shas = []
|
||||
commits.each do |c|
|
||||
|
@ -1165,7 +1116,7 @@ class ApplicationController < ActionController::Base
|
|||
end
|
||||
|
||||
# 将pull request相关信息写入链上
|
||||
param = {
|
||||
params = {
|
||||
"request-type": "upload pull request info",
|
||||
"pull_request_id": "gitlink-" + pull_request_id.to_s,
|
||||
"pull_request_number": 0, # trustie没有该字段
|
||||
|
@ -1184,14 +1135,19 @@ class ApplicationController < ActionController::Base
|
|||
"created_at": created_at,
|
||||
"updated_at": updated_at
|
||||
}.to_json
|
||||
success_blockchain = invoke_blockchain_api(Blockchain.blockchain_config[:api_url], param)
|
||||
resp_body = Blockchain::InvokeBlockchainApi.call(params)
|
||||
if resp_body['status'] == 9
|
||||
raise Error, resp_body['message']
|
||||
elsif resp_body['status'] != 0
|
||||
raise Error, "区块链接口请求失败."
|
||||
end
|
||||
|
||||
|
||||
# 将commit相关信息写入链上
|
||||
commit_shas.each do |commit_sha|
|
||||
commit_diff = Gitea::Commit::DiffService.call(ownername, identifier, commit_sha, owner['gitea_token'])
|
||||
commit = Gitea::Commit::InfoService.call(ownername, identifier, commit_sha, owner['gitea_token'])
|
||||
param = {
|
||||
params = {
|
||||
"request-type": "upload commit info",
|
||||
"commit_hash": commit_sha,
|
||||
"repo_id": "gitlink-" + project_id.to_s,
|
||||
|
@ -1204,10 +1160,13 @@ class ApplicationController < ActionController::Base
|
|||
"content": commit['commit']['message'],
|
||||
"commit_diff": commit_diff['Files'].to_s
|
||||
}.to_json
|
||||
success_blockchain_commit = invoke_blockchain_api(Blockchain.blockchain_config[:api_url], param)
|
||||
success_blockchain = success_blockchain & success_blockchain_commit
|
||||
resp_body = Blockchain::InvokeBlockchainApi.call(params)
|
||||
if resp_body['status'] == 7
|
||||
raise Error, resp_body['message']
|
||||
elsif resp_body['status'] != 0
|
||||
raise Error, "区块链接口请求失败."
|
||||
end
|
||||
end
|
||||
return success_blockchain
|
||||
|
||||
elsif activity_type == "pull_request_refuse"
|
||||
|
||||
|
@ -1228,7 +1187,7 @@ class ApplicationController < ActionController::Base
|
|||
action = 'refused'
|
||||
|
||||
# 将pull request相关信息写入链上
|
||||
param = {
|
||||
params = {
|
||||
"request-type": "upload pull request info",
|
||||
"pull_request_id": "gitlink-" + pull_request_id.to_s,
|
||||
"pull_request_number": 0, # trustie没有该字段
|
||||
|
@ -1247,8 +1206,12 @@ class ApplicationController < ActionController::Base
|
|||
"created_at": created_at,
|
||||
"updated_at": updated_at
|
||||
}.to_json
|
||||
success_blockchain = invoke_blockchain_api(Blockchain.blockchain_config[:api_url], param)
|
||||
return success_blockchain
|
||||
resp_body = Blockchain::InvokeBlockchainApi.call(params)
|
||||
if resp_body['status'] == 9
|
||||
raise Error, resp_body['message']
|
||||
elsif resp_body['status'] != 0
|
||||
raise Error, "区块链接口请求失败."
|
||||
end
|
||||
end
|
||||
end
|
||||
def find_atme_receivers
|
||||
|
|
|
@ -160,22 +160,14 @@ class IssuesController < ApplicationController
|
|||
|
||||
# author: zxh
|
||||
# 扣除发起人的token
|
||||
blockchain_result = Blockchain::CreateIssue.call(user_id: @issue.author_id, project_id: @issue.project_id, token_num: @issue.blockchain_token_num)
|
||||
if blockchain_result = false
|
||||
normal_status(-1, "创建失败")
|
||||
raise ActiveRecord::Rollback
|
||||
else
|
||||
end
|
||||
#render json: {status: 0, message: "创建成功", id: @issue.id}
|
||||
# 调用上链API存证
|
||||
success_blockchain = push_activity_2_blockchain("issue_create", @issue)
|
||||
if success_blockchain == false
|
||||
normal_status(-1, "创建失败")
|
||||
raise ActiveRecord::Rollback
|
||||
else
|
||||
render json: {status: 0, message: "创建成功", id: @issue.id}
|
||||
if @issue.blockchain_token_num > 0
|
||||
Blockchain::CreateIssue.call(user_id: @issue.author_id, project_id: @issue.project_id, token_num: @issue.blockchain_token_num)
|
||||
end
|
||||
|
||||
# 调用上链API存证
|
||||
push_activity_2_blockchain("issue_create", @issue)
|
||||
|
||||
render json: {status: 0, message: "创建成功", id: @issue.id}
|
||||
else
|
||||
normal_status(-1, "创建失败")
|
||||
end
|
||||
|
|
|
@ -51,14 +51,9 @@ class JournalsController < ApplicationController
|
|||
|
||||
# author: zxh
|
||||
# 调用上链API
|
||||
success_blockchain = push_activity_2_blockchain("issue_comment_create", journal)
|
||||
if success_blockchain == false
|
||||
normal_status(-1, "评论失败")
|
||||
raise ActiveRecord::Rollback
|
||||
end
|
||||
push_activity_2_blockchain("issue_comment_create", journal)
|
||||
|
||||
render :json => { status: 0, message: "评论成功", id: journal.id}
|
||||
# normal_status(0, "评论成功")
|
||||
else
|
||||
normal_status(-1, "评论失败")
|
||||
raise ActiveRecord::Rollback
|
||||
|
|
|
@ -78,11 +78,8 @@ class PullRequestsController < ApplicationController
|
|||
|
||||
# author: zxh
|
||||
# 调用上链API
|
||||
success_blockchain = push_activity_2_blockchain("pull_request_create", @pull_request)
|
||||
if success_blockchain == false
|
||||
render_error("create pull request error: cannot save to blockchain")
|
||||
raise ActiveRecord::Rollback
|
||||
end
|
||||
push_activity_2_blockchain("pull_request_create", @pull_request)
|
||||
|
||||
else
|
||||
render_error("create pull request error: #{@gitea_pull_request[:status]}")
|
||||
raise ActiveRecord::Rollback
|
||||
|
@ -166,20 +163,16 @@ class PullRequestsController < ApplicationController
|
|||
colsed = PullRequests::CloseService.call(@owner, @repository, @pull_request, current_user)
|
||||
# author: zxh
|
||||
# 调用上链API
|
||||
success_blockchain = push_activity_2_blockchain("pull_request_refuse", @pull_request)
|
||||
if success_blockchain == false
|
||||
normal_status(-1, "拒绝失败")
|
||||
raise ActiveRecord::Rollback
|
||||
push_activity_2_blockchain("pull_request_refuse", @pull_request)
|
||||
|
||||
if colsed === true
|
||||
@pull_request.project_trends.create!(user: current_user, project: @project,action_type: ProjectTrend::CLOSE)
|
||||
# 合并请求下issue处理为关闭
|
||||
@issue&.update_attributes!({status_id:5})
|
||||
SendTemplateMessageJob.perform_later('PullRequestClosed', current_user.id, @pull_request.id) if Site.has_notice_menu?
|
||||
normal_status(1, "已拒绝")
|
||||
else
|
||||
if colsed === true
|
||||
@pull_request.project_trends.create!(user: current_user, project: @project,action_type: ProjectTrend::CLOSE)
|
||||
# 合并请求下issue处理为关闭
|
||||
@issue&.update_attributes!({status_id:5})
|
||||
SendTemplateMessageJob.perform_later('PullRequestClosed', current_user.id, @pull_request.id) if Site.has_notice_menu?
|
||||
normal_status(1, "已拒绝")
|
||||
else
|
||||
normal_status(-1, '合并失败')
|
||||
end
|
||||
normal_status(-1, '合并失败')
|
||||
end
|
||||
rescue => e
|
||||
normal_status(-1, e.message)
|
||||
|
@ -224,13 +217,7 @@ class PullRequestsController < ApplicationController
|
|||
|
||||
# author: zxh
|
||||
# 调用上链API
|
||||
success_blockchain = push_activity_2_blockchain("pull_request_merge", @pull_request)
|
||||
if success_blockchain == false
|
||||
normal_status(-1, "合并失败")
|
||||
raise ActiveRecord::Rollback
|
||||
else
|
||||
end
|
||||
|
||||
push_activity_2_blockchain("pull_request_merge", @pull_request)
|
||||
|
||||
# 查看是否fix了相关issue,如果fix就转账
|
||||
if params["fix_issue_id"].nil? || params["fix_issue_id"] == ""
|
||||
|
@ -248,20 +235,17 @@ class PullRequestsController < ApplicationController
|
|||
normal_status(-1, "关联issue失败")
|
||||
raise ActiveRecord::Rollback
|
||||
else
|
||||
project = Project.find_by(user_id: owner.id, name: params["project_id"])
|
||||
project = Project.find_by(user_id: owner.id, identifier: params["project_id"])
|
||||
if project.nil?
|
||||
normal_status(-1, "关联issue失败")
|
||||
raise ActiveRecord::Rollback
|
||||
else
|
||||
author_id = pr.user_id
|
||||
result = Blockchain::FixIssue.call({user_id: author_id.to_s, project_id: project.id.to_s, token_num: token_num})
|
||||
if result == false
|
||||
normal_status(-1, "关联issue失败")
|
||||
raise ActiveRecord::Rollback
|
||||
else
|
||||
# update issue to state 5
|
||||
issue.update(status_id: 5)
|
||||
if token_num > 0
|
||||
Blockchain::FixIssue.call({user_id: author_id.to_s, project_id: project.id.to_s, token_num: token_num})
|
||||
end
|
||||
# update issue to state 5
|
||||
issue.update(status_id: 5)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -92,7 +92,7 @@ class UsersController < ApplicationController
|
|||
end
|
||||
|
||||
if @project['use_blockchain'] == true or @project['use_blockchain'] == 1
|
||||
balance_user = Blockchain::BalanceQueryOneProject.call({"user_id": user_id, "project_id": project_id})[:balance]
|
||||
balance_user = Blockchain::BalanceQueryOneProject.call({"user_id": user_id, "project_id": project_id})
|
||||
balance_all = Blockchain::RepoBasicInfo.call({"project_id": project_id})["cur_supply"]
|
||||
scores = {
|
||||
"final" => cal_perc(balance_user, balance_all),
|
||||
|
@ -440,12 +440,10 @@ class UsersController < ApplicationController
|
|||
normal_status(-1, "创建者无法找到")
|
||||
else
|
||||
p = Project.find_by(user_id: owner.id, identifier: params['project_identifier'])
|
||||
results = Blockchain::BalanceQueryOneProject.call({"user_id": params['user_id'].to_i, "project_id": p.id.to_i})
|
||||
render json: { status: results[:status], balance: results[:balance]}
|
||||
balance = Blockchain::BalanceQueryOneProject.call({"user_id": params['user_id'].to_i, "project_id": p.id.to_i})
|
||||
render json: { status: 0, balance: balance}
|
||||
end
|
||||
else
|
||||
puts "~!@~!@" + params['user_id'].to_s
|
||||
puts current_user.id.to_s
|
||||
normal_status(-1, "缺少权限")
|
||||
end
|
||||
|
||||
|
@ -455,16 +453,15 @@ class UsersController < ApplicationController
|
|||
def blockchain_transfer
|
||||
#is_current_admin_user = User.current.logged? && (current_user&.admin? || current_user.id == params['payer_id'].to_i)
|
||||
is_current_admin_user = true
|
||||
|
||||
if is_current_admin_user
|
||||
results = Blockchain::TransferService.call(params)
|
||||
if results == true
|
||||
render json: { status: 2 } # 重新查询余额
|
||||
else
|
||||
normal_status(-1, "转账失败")
|
||||
end
|
||||
Blockchain::TransferService.call(params)
|
||||
render json: {status: 2} # 重新查询余额
|
||||
else
|
||||
normal_status(-1, "缺少权限")
|
||||
end
|
||||
rescue Exception => e
|
||||
normal_status(-1, e.to_s)
|
||||
end
|
||||
|
||||
# exchange money
|
||||
|
@ -640,7 +637,12 @@ class UsersController < ApplicationController
|
|||
def blockchain_get_issue_token_num
|
||||
issue_id = params["issue_id"]['orderId'].to_i
|
||||
issue = Issue.find_by(id: issue_id)
|
||||
render json: {"blockchain_token_num": issue.blockchain_token_num}
|
||||
project = Project.find(issue.project_id)
|
||||
if project[:use_blockchain]
|
||||
render json: {"blockchain_token_num": issue.blockchain_token_num}
|
||||
else
|
||||
render json: {"blockchain_token_num": -1} # 未使用blockchain
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
|
|
@ -21,7 +21,8 @@ module TagChosenHelper
|
|||
"issue_tag": render_issue_tags(project),
|
||||
"issue_type": render_issue_species,
|
||||
"all_issues": all_issues,
|
||||
"branches": render_branches(project)
|
||||
"branches": render_branches(project),
|
||||
"use_blockchain": project['use_blockchain']
|
||||
}
|
||||
end
|
||||
|
||||
|
|
|
@ -42,7 +42,7 @@ module Watchable
|
|||
end
|
||||
|
||||
if @project['use_blockchain'] == true or @project['use_blockchain'] == 1
|
||||
balance_user = Blockchain::BalanceQueryOneProject.call({"user_id": @user.id, "project_id": @project.id})[:balance]
|
||||
balance_user = Blockchain::BalanceQueryOneProject.call({"user_id": @user.id, "project_id": @project.id})
|
||||
balance_all = Blockchain::RepoBasicInfo.call({"project_id": @project.id})["cur_supply"]
|
||||
score = cal_perc(balance_user, balance_all)
|
||||
else
|
||||
|
|
|
@ -7,70 +7,52 @@ class ApplicationQuery
|
|||
params[key].to_s.strip.presence
|
||||
end
|
||||
|
||||
|
||||
# author: zxh
|
||||
# add blockchain related functions in application_query
|
||||
|
||||
def invoke_blockchain_api(uri, params)
|
||||
begin
|
||||
uri = URI.parse(URI.encode(uri.strip))
|
||||
res = Net::HTTP.start(uri.host, uri.port) do |http|
|
||||
req = Net::HTTP::Post.new(uri)
|
||||
req['Content-Type'] = 'application/json'
|
||||
req.body = params
|
||||
http.request(req)
|
||||
end
|
||||
if res.code.to_i != 200
|
||||
puts '区块链接口请求失败.'
|
||||
return false
|
||||
else
|
||||
res_body = JSON.parse(res.body)
|
||||
if res_body.has_key?("status") && res_body["status"] == 0
|
||||
else
|
||||
puts '区块链接口请求出错.'
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
return res_body
|
||||
rescue Exception => e
|
||||
puts '区块链接口请求失败.'
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
# find all the repos that a user has tokens
|
||||
def find_repo_with_token(user_id)
|
||||
param = {
|
||||
params = {
|
||||
"request-type": "query user balance of all repos",
|
||||
"username": user_id.to_s
|
||||
}.to_json
|
||||
resp_body = invoke_blockchain_api(Blockchain.blockchain_config[:api_url], param)
|
||||
token_list = resp_body['UserBalanceList'].nil? ? [] : resp_body['UserBalanceList']
|
||||
return token_list
|
||||
resp_body = Blockchain::InvokeBlockchainApi.call(params)
|
||||
if resp_body['status'] != 0
|
||||
raise "区块链接口请求失败."
|
||||
else
|
||||
token_list = resp_body['UserBalanceList'].nil? ? [] : resp_body['UserBalanceList']
|
||||
return token_list
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
# find one repo that a user has tokens
|
||||
def find_one_balance(user_id, project_id)
|
||||
param = {
|
||||
# return 3 statuses: UnknownErr/ResUserNotExisted/Success
|
||||
params = {
|
||||
"request-type": "query user balance of single repo",
|
||||
"username": user_id.to_s,
|
||||
"token_name": project_id.to_s
|
||||
}.to_json
|
||||
resp_body = invoke_blockchain_api(Blockchain.blockchain_config[:api_url], param)
|
||||
return resp_body
|
||||
resp_body = Blockchain::InvokeBlockchainApi.call(params)
|
||||
if resp_body['status'] == 0
|
||||
return resp_body['balance']
|
||||
elsif resp_body['status'] == 100
|
||||
return 0 # 找不到用户返回0
|
||||
else
|
||||
raise "区块链接口请求失败."
|
||||
end
|
||||
end
|
||||
|
||||
# query the basic info of a repository
|
||||
def find_blockchain_repo_info(project_id)
|
||||
param = {
|
||||
params = {
|
||||
"request-type": "query repo basic info",
|
||||
"token_name": project_id.to_s
|
||||
}.to_json
|
||||
resp_body = invoke_blockchain_api(Blockchain.blockchain_config[:api_url], param)
|
||||
return resp_body
|
||||
resp_body = Blockchain::InvokeBlockchainApi.call(params)
|
||||
if resp_body['status'] == 0
|
||||
return resp_body
|
||||
else
|
||||
raise "区块链接口请求失败."
|
||||
end
|
||||
end
|
||||
|
||||
end
|
|
@ -7,12 +7,6 @@ class Blockchain::BalanceQueryOneProject < ApplicationQuery
|
|||
end
|
||||
|
||||
def call
|
||||
balance = find_one_balance(params[:user_id].to_s, params[:project_id].to_s)
|
||||
if balance == false
|
||||
return false
|
||||
else
|
||||
results = {"status": balance['status'], "balance": balance['balance']}
|
||||
results
|
||||
end
|
||||
find_one_balance(params[:user_id].to_s, params[:project_id].to_s)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -8,10 +8,6 @@ class Blockchain::RepoBasicInfo < ApplicationQuery
|
|||
|
||||
def call
|
||||
info = find_blockchain_repo_info(params[:project_id].to_s)
|
||||
if info == false
|
||||
return false
|
||||
else
|
||||
return info
|
||||
end
|
||||
return info
|
||||
end
|
||||
end
|
||||
|
|
|
@ -19,39 +19,6 @@ class ApplicationService
|
|||
ActiveModel::Type::Boolean.new.cast str
|
||||
end
|
||||
|
||||
|
||||
# author: zxh
|
||||
# blockchain创建项目相关api
|
||||
|
||||
def invoke_blockchain_api(uri, params)
|
||||
begin
|
||||
uri = URI.parse(URI.encode(uri.strip))
|
||||
res = Net::HTTP.start(uri.host, uri.port) do |http|
|
||||
req = Net::HTTP::Post.new(uri)
|
||||
req['Content-Type'] = 'application/json'
|
||||
req.body = params
|
||||
http.request(req)
|
||||
end
|
||||
if res.code.to_i != 200
|
||||
puts '区块链接口请求失败.'
|
||||
return false
|
||||
else
|
||||
res_body = JSON.parse(res.body)
|
||||
if res_body.has_key?("status") && res_body["status"] == 0
|
||||
else
|
||||
puts '区块链接口请求出错.'
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
return true
|
||||
rescue Exception => e
|
||||
puts '区块链接口请求失败.'
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
# params: params from index.js page
|
||||
def create_repo_on_blockchain(params, project)
|
||||
username = params['user_id'].to_s
|
||||
|
@ -59,54 +26,72 @@ class ApplicationService
|
|||
total_supply = params['blockchain_token_all'].to_i
|
||||
token_balance = [[username, (total_supply * params['blockchain_init_token'].to_i / 100).to_i]]
|
||||
|
||||
param = {
|
||||
params = {
|
||||
"request-type": "create repo",
|
||||
"username": username,
|
||||
"token_name": token_name,
|
||||
"total_supply": total_supply,
|
||||
"token_balance": token_balance
|
||||
}.to_json
|
||||
invoke_blockchain_api(Blockchain.blockchain_config[:api_url], param)
|
||||
resp_body = Blockchain::InvokeBlockchainApi.call(params)
|
||||
if resp_body['status'] != 0
|
||||
raise "区块链接口请求失败."
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def transfer_balance_on_blockchain(payer, payee, token_name, amount)
|
||||
|
||||
param = {
|
||||
params = {
|
||||
"request-type": "transfer amount",
|
||||
"payer": payer,
|
||||
"payee": payee,
|
||||
"token_name": token_name,
|
||||
"amount": amount
|
||||
}.to_json
|
||||
results = invoke_blockchain_api(Blockchain.blockchain_config[:api_url], param)
|
||||
return results
|
||||
resp_body = Blockchain::InvokeBlockchainApi.call(params)
|
||||
if resp_body['status'] == 5 or resp_body['status'] == 106 or resp_body['status'] == 105
|
||||
raise resp_body['message']
|
||||
elsif resp_body['status'] != 0
|
||||
raise "区块链接口请求失败."
|
||||
else
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def lock_balance_on_blockchain(username, tokenname, amount)
|
||||
|
||||
param = {
|
||||
params = {
|
||||
"request-type": "lock user balance",
|
||||
"username": username,
|
||||
"token_name": tokenname,
|
||||
"amount": amount
|
||||
}.to_json
|
||||
results = invoke_blockchain_api(Blockchain.blockchain_config[:api_url], param)
|
||||
return results
|
||||
resp_body = Blockchain::InvokeBlockchainApi.call(params)
|
||||
if resp_body['status'] == 5 or resp_body['status'] == 106 or resp_body['status'] == 103
|
||||
raise resp_body['message']
|
||||
elsif resp_body['status'] != 0
|
||||
raise "区块链接口请求失败."
|
||||
else
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def unlock_balance_on_blockchain(username, tokenname, amount)
|
||||
|
||||
param = {
|
||||
params = {
|
||||
"request-type": "unlock user balance",
|
||||
"username": username,
|
||||
"token_name": tokenname,
|
||||
"amount": amount
|
||||
}.to_json
|
||||
results = invoke_blockchain_api(Blockchain.blockchain_config[:api_url], param)
|
||||
return results
|
||||
resp_body = Blockchain::InvokeBlockchainApi.call(params)
|
||||
if resp_body['status'] == 5 or resp_body['status'] == 106 or resp_body['status'] == 104
|
||||
raise resp_body['message']
|
||||
elsif resp_body['status'] != 0
|
||||
raise "区块链接口请求失败."
|
||||
else
|
||||
end
|
||||
end
|
||||
def phone_mail_type value
|
||||
value =~ /^1\d{10}$/ ? 1 : 0
|
||||
|
|
|
@ -13,8 +13,7 @@ class Blockchain::CreateIssue < ApplicationService
|
|||
amount = @params[:token_num].to_i
|
||||
|
||||
# 调用token锁仓函数
|
||||
results = lock_balance_on_blockchain(username, token_name, amount)
|
||||
return results
|
||||
lock_balance_on_blockchain(username, token_name, amount)
|
||||
end
|
||||
rescue => e
|
||||
puts "转账失败: #{e.message}"
|
||||
|
|
|
@ -13,8 +13,7 @@ class Blockchain::FixIssue < ApplicationService
|
|||
amount = @params[:token_num].to_i
|
||||
|
||||
# 调用token锁仓函数
|
||||
results = unlock_balance_on_blockchain(username, token_name, amount)
|
||||
return results
|
||||
unlock_balance_on_blockchain(username, token_name, amount)
|
||||
end
|
||||
rescue => e
|
||||
puts "关联issue失败: #{e.message}"
|
||||
|
|
|
@ -0,0 +1,34 @@
|
|||
class Blockchain::InvokeBlockchainApi < ApplicationService
|
||||
# 调用blockchain
|
||||
|
||||
attr_reader :params
|
||||
|
||||
def initialize(params)
|
||||
@params = params
|
||||
end
|
||||
|
||||
def call
|
||||
begin
|
||||
uri = Blockchain.blockchain_config[:api_url]
|
||||
uri = URI.parse(URI.encode(uri.strip))
|
||||
res = Net::HTTP.start(uri.host, uri.port) do |http|
|
||||
req = Net::HTTP::Post.new(uri)
|
||||
req['Content-Type'] = 'application/json'
|
||||
req.body = @params
|
||||
http.request(req)
|
||||
end
|
||||
if res.code.to_i != 200
|
||||
raise "区块链接口请求失败."
|
||||
else
|
||||
res_body = JSON.parse(res.body)
|
||||
if res_body.has_key?("status")
|
||||
else
|
||||
raise "区块链接口请求失败."
|
||||
end
|
||||
end
|
||||
return res_body
|
||||
rescue Exception => e
|
||||
raise "区块链接口请求失败."
|
||||
end
|
||||
end
|
||||
end
|
|
@ -8,25 +8,23 @@ class Blockchain::TransferService < ApplicationService
|
|||
|
||||
def call
|
||||
ActiveRecord::Base.transaction do
|
||||
transfer_amount = params['transfer_amount']
|
||||
if (Float(transfer_amount) rescue false) == false or transfer_amount.to_i < 0 or Float(transfer_amount) != transfer_amount.to_i
|
||||
raise Error, "请输入正确的转账金额"
|
||||
end
|
||||
transfer_amount = params['transfer_amount'].to_i
|
||||
transfer_login = params['transfer_login']
|
||||
payer = params['payer_id'].to_s
|
||||
payee = User.find_by(login: transfer_login)
|
||||
if payee.nil?
|
||||
# normal_status(-1, "未找到用户")
|
||||
return false
|
||||
raise Error, "未找到接收转账的用户"
|
||||
else
|
||||
payee = payee.id.to_s
|
||||
token_name = params['project_id'].to_s
|
||||
# 调用token转移函数
|
||||
results = transfer_balance_on_blockchain(payer, payee, token_name, transfer_amount)
|
||||
return results
|
||||
transfer_balance_on_blockchain(payer, payee, token_name, transfer_amount)
|
||||
end
|
||||
end
|
||||
rescue => e
|
||||
puts "转账失败: #{e.message}"
|
||||
# normal_status(-1, e.message)
|
||||
return false
|
||||
end
|
||||
|
||||
private
|
||||
|
@ -34,4 +32,5 @@ class Blockchain::TransferService < ApplicationService
|
|||
def no_use
|
||||
puts "this function does not have any usage"
|
||||
end
|
||||
|
||||
end
|
Loading…
Reference in New Issue