/// <summary> /// Broadcast a ledger to blockchain network. /// </summary> /// <param name="record">A ledger to broadcast.</param> /// <param name="waitForConfirm">Wait for ledger to store in a blockchain (Optional, default is true)</param> /// <returns>None. Transaction ID will be attached to the ledger object instead.</returns> public async Task BroadcastLedger(Ledger record, bool waitForConfirm = true) { var hexData = record.GetOP_RETURN(); var transaction = await api.BuildTransaction(userPrivateKey, record.UtxoReceiver, record.TokenReceiver, hexData); var tx = await api.BroadcastTransaction(transaction); record.TxId = tx.txid; if (waitForConfirm) { var currentTx = new ApiTransaction(); while (currentTx.blocktime == 0) { currentTx = await api.GetTransactionInfo(tx.txid); record.Blockheight = currentTx.blockheight; Thread.Sleep(3000); } record.Status = ProcessStatus.NotProcessed; record.TokenSenderHash = userPrivateKey.PubKeyHash.ToBytes(); ledger.Upsert(record); ledger.EnsureIndex(l => l.TxId); ledger.EnsureIndex(l => l.Blockheight); ledger.EnsureIndex(l => l.Operation); ledger.EnsureIndex(l => l.TokenSenderHash); ledger.EnsureIndex(l => l.TokenReceiverHash); ledger.EnsureIndex(l => l.Time); } else { record.Status = ProcessStatus.NotDefined; } }
/// <summary> /// Publish a wallet contract to blockchain and save the published to local data store. /// </summary> /// <param name="newContract">A to-be-published wallet contract.</param> /// <param name="creator">Private key of the creator</param> /// <returns>Return transaction ID if published. Throw exception if there is an error.</returns> public async Task <string> PublishContract(WalletContract newContract, BitcoinSecret creator) { try { ValidateContract(newContract); } catch (Exception) { throw; } var Data = $"{DomainHex}{newContract.NameHex}{newContract.TokenHex}{BitConverterExtension.GetHexBytes(newContract.TotalSupply)}" + $"{newContract.NoOfDecimal.ToString("x4")}{newContract.Conditions.ByteArrayToString()}"; var transaction = await insightAPI.BuildTransaction(creator, creator.GetAddress(), creator.GetAddress(), Data.ToLower()); var txId = await insightAPI.BroadcastTransaction(transaction); newContract.ID = txId.txid; var status = await insightAPI.GetSync(); newContract.StartingBlock = status.blockChainHeight; newContract.LastSyncedBlock = status.blockChainHeight; UpdateContract(newContract, true); return(newContract.ID); }
/// <summary> /// Broadcast transaction to blockchain. /// </summary> /// <param name="transaction">Signed NBitcoin-compatible transaction</param> /// <returns>TransactionID of the published transaction.</returns> public Task <TxID> BroadCastTransaction(Transaction transaction) => api.BroadcastTransaction(transaction);