Skip to content

Commit ca5d551

Browse files
committed
add get block and tx in datastructure
1 parent 9be36af commit ca5d551

File tree

2 files changed

+77
-2
lines changed

2 files changed

+77
-2
lines changed

docs/dataStructure.md

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,18 @@ const block = await unichain.api.getBlockByNumber(blockNumber)
5555
console.log('Block data:', block)
5656
```
5757

58+
#### Try It yourself
59+
<div class="u_center">
60+
<input id="blockID" class="u_input u_full" placeholder=" Input block ID or block hash" ></input>
61+
<p id="blockError" style="color:red"></p>
62+
<div>
63+
<textarea id="blockRes" style="visibility: hidden; width: 100%;margin-top:5px;padding:5px" disabled>
64+
</textarea>
65+
</div>
66+
<input type="button" class="u_button u_button_primary u_margin_top_10" onClick="getBlock()" value="Get Block Info"></input>
67+
</div>
68+
69+
5870
### Transaction format
5971
The transaction will have the following structure:
6072
```js
@@ -114,9 +126,71 @@ Please note that the transaction and block will be finalized after ⅔ of the wi
114126
curl -X POST http://{host}/walletsolidity/gettransactionbyid -d '{"value": "transaction_hash"}'
115127
```
116128

129+
#### Try It yourself
130+
<div class="u_center">
131+
<input id="txid" class="u_input u_full" placeholder=" Input transaction ID" ></input>
132+
<p id="txError" style="color:red"></p>
133+
<div>
134+
<textarea id="txRes" style="visibility: hidden; width: 100%;margin-top:5px;padding:5px" disabled>
135+
</textarea>
136+
</div>
137+
<input type="button" class="u_button u_button_primary u_margin_top_10" onClick="getTx()" value="Get Transaction Info"></input>
138+
</div>
139+
117140
### Related resources:
118141
- [How to run UniChain node](./getStarted)
119142
- [How to use UniChainJS library](./docs/tutorials/tutorial-002)
120143

121144

122-
145+
<script type="text/javascript">
146+
function isNumeric(value) {
147+
return /^-?\d+$/.test(value);
148+
}
149+
function isHex (hexString) {
150+
const re = /[0-9A-Fa-f]{64}/g
151+
return re.test(hexString)
152+
}
153+
function getBlock() {
154+
const blockId = document.getElementById("blockID").value
155+
//TODO: validate
156+
let url = 'https://seed-1.unichain.world/wallet/'
157+
let data = {}
158+
console.log(blockId)
159+
if (isNumeric(blockId)) {
160+
url += 'getblockbynum'
161+
data = {num: parseInt(blockId)}
162+
} else if (isHex(blockId)) { //check hex string
163+
url += 'getblockbyid'
164+
data = {value: blockId}
165+
} else {
166+
document.getElementById("blockError").innerHTML = 'Invalid block ID or block hash'
167+
return
168+
}
169+
axios.post(url, data).then(res => {
170+
console.log(res.data)
171+
document.getElementById("blockRes").innerHTML = JSON.stringify(res.data)
172+
document.getElementById("blockRes").rows = 10
173+
document.getElementById("blockRes").style.visibility = 'visible'
174+
}).catch(err => {
175+
document.getElementById("blockError").innerHTML = JSON.stringify(err)
176+
})
177+
}
178+
function getTx() {
179+
const txid = document.getElementById("txid").value
180+
let url = 'https://seed-1.unichain.world/wallet/gettransactionbyid'
181+
console.log(txid)
182+
if (txid.length != 64 || !isHex(txid)) {
183+
document.getElementById("txError").innerHTML = 'Invalid transaction hash'
184+
return
185+
}
186+
axios.post(url, {value: txid}).then(res => {
187+
console.log(res.data)
188+
document.getElementById("txRes").innerHTML = JSON.stringify(res.data)
189+
document.getElementById("txRes").rows = 10
190+
document.getElementById("txRes").style.visibility = 'visible'
191+
}).catch(err => {
192+
console.log(err)
193+
document.getElementById("txError").innerHTML = JSON.stringify(err)
194+
})
195+
}
196+
</script>

website/siteConfig.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ const siteConfig = {
8585
'https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.10/clipboard.min.js',
8686
'/js/demo.js',
8787
'/js/custom.js',
88-
'https://cdn.jsdelivr.net/npm/@uniworld/unichain-js@1.0.9/dist/UnichainJS.js'
88+
'https://cdn.jsdelivr.net/npm/@uniworld/unichain-js@1.0.9/dist/UnichainJS.js',
89+
'https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js'
8990
],
9091
stylesheets: ['/css/custom.css', 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css'],
9192

0 commit comments

Comments
 (0)