Last active
June 10, 2020 21:16
-
-
Save DanCassiano/5f902a4a69b628b1c4a14c391651ff56 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> | |
<meta http-equiv="X-UA-Compatible" content="ie=edge"> | |
<title>Tronwallet-browser Example</title> | |
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" | |
integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous"> | |
<style> | |
* { | |
margin: 0; | |
padding: 0 | |
} | |
.column { | |
display: flex; | |
flex-direction: column; | |
} | |
.row { | |
display: flex; | |
flex-direction: row; | |
} | |
code { | |
font-family: "Lucida Console", Monaco, monospace; | |
white-space: normal; | |
font-size: 10px; | |
max-height: 200px; | |
overflow: auto; | |
} | |
.margin-top { | |
margin-top: 20px; | |
} | |
</style> | |
<script> | |
const ONE_TRX = 1000000 | |
function $(ele) { return document.querySelector(ele) } | |
function getAccount() { | |
if (window.tronWeb) { | |
window.tronWeb.trx.getAccount(window.tronWeb.defaultAddress.base58) | |
.then(data => { | |
document.querySelector("#viewCode").innerHTML = JSON.stringify(data, null, 4) | |
}) | |
} | |
} | |
function goBack() { | |
console.log("back press") | |
history.back() | |
} | |
async function send() { | |
const from = $("#fromInput").value | |
const to = $("#toInput").value | |
const amount = $("#amountInput").value | |
if (!from || !to || !amount) { | |
alert("you need to fill to, from and amount") | |
return | |
} | |
if (window.tronWeb && window.tronWeb.defaultAddress.base58) { | |
try { | |
var tronweb = window.tronWeb | |
var tx = await tronweb.transactionBuilder.sendTrx(to, Number(amount), from) | |
console.log(tx) | |
var signedTx = await tronweb.trx.sign(tx) | |
console.log(signedTx) | |
var broastTx = await tronweb.trx.sendRawTransaction(signedTx) | |
console.log(broastTx) | |
const r = $("#result") | |
r.innerHTML = JSON.stringify(broastTx, null, 2) | |
} catch (error) { | |
console.error(error) | |
} | |
} else { | |
console.error("tronweb not found") | |
} | |
} | |
window.onload = function () { | |
const sendForm = $("#sendForm") | |
sendForm.addEventListener('submit', function (e) { | |
e.preventDefault() | |
send() | |
return false | |
}) | |
$("#amountInput").addEventListener('input', function (e) { | |
console.log("aquiiiii") | |
// e.preventDefault() | |
const label = $("#amountHelp") | |
label.innerHTML = Number(e.target.value) / Number(ONE_TRX) | |
}) | |
if (window.tronWeb && window.tronWeb.defaultAddress.base58) { | |
$("#fromInput").value = window.tronWeb.defaultAddress.base58 | |
} | |
} | |
</script> | |
</head> | |
<body> | |
<div class="container"> | |
<div class="card margin-top"> | |
<div class="card-header"> | |
Account | |
</div> | |
<ul class="list-group list-group-flush"> | |
<li class="list-group-item"> | |
<div class="row"> | |
<div class="col"> | |
<button class="btn btn-primary" onclick="getAccount()">Get Acount</button> | |
</div> | |
</div> | |
<div class="row"> | |
<div class="col"> | |
<code id="viewCode" class="overflow-auto"></code> | |
</div> | |
</div> | |
</li> | |
</div> | |
<div class="card margin-top"> | |
<div class="card-header"> | |
<a href="https://developers.tron.network/docs/tronweb-transaction-builder" target="_blank">TX transaction</a> | |
</div> | |
<div class="card-body"> | |
<form id="sendForm"> | |
<div class="form-group"> | |
<label for="fromInput">From</label> | |
<input type="text" class="form-control" id="fromInput" aria-describedby="emailHelp" value=""> | |
</div> | |
<div class="form-group"> | |
<label for="toInput">To</label> | |
<input type="text" class="form-control" id="toInput" value="TN38wyf8XUhhC3Yy7qNyh8sCpfdqpFPjt2"> | |
</div> | |
<div class="form-group"> | |
<label for="amountInput">Amount (value = amount / <strong data-toggle="tooltip" data-placement="top" | |
title="1000000">1000000</strong>)</label> | |
<input type="text" class="form-control" id="amountInput" placeholder="0.0" value="1"> | |
<small class="form-text text-muted"><span id="amountHelp">0.000001</span></small> | |
</div> | |
<button type="submit" class="btn btn-primary">Send</button> | |
</form> | |
<code id="result"></code> | |
</div> | |
</div> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment