项目中用到usdt充值,由于走的是区块充值,节点服务器上是给每个用户都分配了一个usdt钱包地址,所以需要把可客户充值在每个钱包地址上的usdt归集到一块。
Omnicore提供了两个新的api omni_funded_send
和omni_funded_sendall
,这种方式的好处在于可以指定手续费的支付方,所有的令牌交易都可以使用统一的地址进行支付比特币手续费,而不需要发送者自身拥有比特币。但这里并未设定手续费的具体数量,系统将根据在配置文件中的关于手续费的配置文件进行动态设定。
调用参数
/**
* 方法:转账
* @param $from
* @param $to
* @param $amount
* @return array
*/
function sendTransaction($from,$to,$amount)
{
try{
$info = $this->usdt->omni_funded_send($from, $to ,1, $amount,'填写你需要支付手续费的比特币地址');
// halt($info);
}catch (BitcoindException $e){
return ['msg' => '转出失败', 'status' => 201];
}
if ($info=='JSON value is not a string as expected'){
return ['msg' => '请预留充足的BTC作为手续费', 'status' => 202];
}
if (strlen($info)<30 || $info=='Error choosing inputs for the send transaction'){
return ['msg' => '转出异常,稍后重试', 'status' => 203];
}
return ['msg' => '转账信息发送成功,等待区块确认', 'status' => 200,'data'=>$info];
}
然后利用定时任务将节点服务器中的各个账号中的usdt转账到同一账号中
setName('summary')
->setDescription('归集');
}
protected function execute(Input $input, Output $output){
//每隔3小时触发一次
$id = \swoole_timer_tick(60000, function ($timer_id) {
file_put_contents('summary.log','开始'.PHP_EOL,FILE_APPEND);
// $ptsummary=Db::name('ptsummary')->where(['status'=>1,'user_id'=>['in',[161,185]]])->order('id','asc')->select();
$ptsummary=Db::name('ptsummary')->where(['status'=>1])->order('id','asc')->select();
file_put_contents('summary.log',json_encode($ptsummary).PHP_EOL,FILE_APPEND);
$recipient=getConfigValue('recipient');
$commission=getConfigValue('commission');
foreach ($ptsummary as $k=>$v){
sleep(1);
$usdt=(new Coins())->getCoins()->settxfee('0.0005');
$usdt=(new Coins())->getCoins()->omni_funded_send($v['send'],$recipient,31,(string)$v['money'],$v['send']);
file_put_contents('summary.log','tx_id--'.$usdt.'--'.$v['send'].PHP_EOL,FILE_APPEND);
if(strpos($usdt,'wallet may be locked or fees may not be sufficient') !== false){
$usdt=(new Coins())->getCoins()->omni_funded_send($v['send'],$recipient,31,(string)$v['money'],$commission);
file_put_contents('summary.log','tx_id1--'.$usdt.'---'.$commission.PHP_EOL,FILE_APPEND);
if(strpos($usdt,'wallet may be locked or fees may not be sufficient') !== false){
continue;
}
}
if (strpos($usdt,'Error choosing inputs for the send transaction')!==false){
continue;
}
if (strpos($usdt,'Error committing transaction')!==false){
continue;
}
$update[]=[
'id'=>$v['id'],
'txid'=>$usdt,
'recipient'=>$recipient,
'status'=>2,
'update_time'=>date('YmdHis')
];
file_put_contents('summary.log','tx_id1--'.json_encode($update).PHP_EOL,FILE_APPEND);
}
$lastptsummary=Db::name('ptsummary')->where(['status'=>2])->select();
foreach ($lastptsummary as $kk=>$vv){
sleep(1);
$usdt_last=(new Coins())->gettransaction($vv['txid']);
if (is_array($usdt_last)){
if ($usdt_last['ismine']){
if(isset($usdt_last['valid']) && $usdt_last['valid']==true && $usdt_last['confirmations']>=2){
$last_update[]=[
'id'=>$vv['id'],
'status'=>3,
'update_time'=>date('YmdHis')
];
}
}
}
}
$ptsummary= new Ptsummary();
if (!empty($update)){
file_put_contents('summary.log',json_encode($update).PHP_EOL,FILE_APPEND);
$ptsummary->isUpdate(true)->saveAll($update);
}
if (!empty($last_update)){
file_put_contents('summary.log',json_encode($last_update).PHP_EOL,FILE_APPEND);
$ptsummary->isUpdate(true)->saveAll($last_update);
}
file_put_contents('summary.log','结束'.PHP_EOL,FILE_APPEND);
});
}
}
*/
namespace app\admin\controller\usdts;
class Coins
{
protected $usdt;
public function __construct()
{
$this->usdt=new Usdtcion('账号','密码','ip','端口');
}
/**
* 方法:转账
* @param $from
* @param $to
* @param $amount
* @return array
*/
function sendTransaction($from,$to,$amount)
{
try{
$info = $this->usdt->omni_funded_send($from, $to ,31, $amount,'14PEfEATtjvG8ZPtjCnAp88HFETRbUXyvW');
}catch (BitcoindException $e){
return ['msg' => '转出失败', 'status' => 201];
}
if ($info=='JSON value is not a string as expected'){
return ['msg' => '请预留充足的BTC作为手续费', 'status' => 202];
}
if (strlen($info)<30 || $info=='Error choosing inputs for the send transaction'){
return ['msg' => '转出异常,稍后重试', 'status' => 203];
}
return ['msg' => '转账信息发送成功,等待区块确认', 'status' => 200,'data'=>$info];
}
function sendBtc($from,$to,$amount)
{
$connect=$this->usdt;
$result = $connect->listunspent(3, 99999999, [$from]);
if (empty($result)){
$creater=[];
}else{
foreach ($result as $k=>$v){
$txid = $v['txid'];
$vout = $v['vout'];
$creater[]=["txid" => $txid, "vout" => $vout];
}
}
$raw_trans = $connect->createrawtransaction(
$creater,
["$to" => $amount]
);
$fund_raw = $connect->fundrawtransaction(
$raw_trans,
[
"changeAddress" => $from, //找零地址
'feeRate' => 0.0004 //最高手续费
]
);
if ($fund_raw=='Insufficient funds'){
return ['msg' => '请预留充足的BTC作为手续费', 'status' => 202];
}
$sign = $connect->signrawtransaction($fund_raw['hex']);
$result = $connect->sendrawtransaction($sign['hex']);
if (is_array($result) || strlen($result)<30){
return ['msg' => '转出失败', 'status' => 201];
}
return ['msg' => '转账信息发送成功,等待区块确认', 'status' => 200,'data'=>$result];
}
/**
* 方法:生成USDT地址
* @param $user_id
* @return bool
*/
public function usdtcoin($user_id)
{
$lists=$this->usdt->getaddressesbyaccount("$user_id");
if (count($lists)>0){
return $lists[0];
}
$address=$this->usdt->getnewaddress("$user_id");
if (strlen($address) != 34) {
return false;
}
return $address;
}
/**
* 方法:获取余额
* @param $address
* @return int
*/
function getBalance($address)
{
return $this->usdt->omni_getbalance($address,31)??0;
}
function getCoins()
{
return $this->usdt;
}
}