Adding floTokenAPI
- Token Operator to send/receive tokens via blockchain using token system API floBlockchainAPI v2.3.1 - Added options parameter to writeData . Options can have the following properties: . strict_utxo : passes strict_utxo parameter to sendTx (Default: true) . sendAmt : amount of FLO to send (Default: floGlobals.sendAmt)
This commit is contained in:
parent
94f90807f7
commit
99368151d5
@ -1,4 +1,4 @@
|
|||||||
(function(EXPORTS) { //floBlockchainAPI v2.3.0
|
(function(EXPORTS) { //floBlockchainAPI v2.3.1
|
||||||
/* FLO Blockchain Operator to send/receive data from blockchain using API calls*/
|
/* FLO Blockchain Operator to send/receive data from blockchain using API calls*/
|
||||||
'use strict';
|
'use strict';
|
||||||
const floBlockchainAPI = EXPORTS;
|
const floBlockchainAPI = EXPORTS;
|
||||||
@ -45,7 +45,7 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
//Promised function to get data from API
|
//Promised function to get data from API
|
||||||
const promisedAPI = floBlockchainAPI.promisedAPI = function(apicall) {
|
const promisedAPI = floBlockchainAPI.promisedAPI = floBlockchainAPI.fetch = function(apicall) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
//console.log(apicall);
|
//console.log(apicall);
|
||||||
fetch_api(apicall)
|
fetch_api(apicall)
|
||||||
@ -126,11 +126,13 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Write Data into blockchain
|
//Write Data into blockchain
|
||||||
floBlockchainAPI.writeData = function(senderAddr, data, privKey, receiverAddr = floGlobals.adminID, strict_utxo = true) {
|
floBlockchainAPI.writeData = function(senderAddr, data, privKey, receiverAddr = floGlobals.adminID, options = {}) {
|
||||||
|
let strict_utxo = options.strict_utxo === false ? false : true,
|
||||||
|
sendAmt = isNaN(options.sendAmt) ? floGlobals.sendAmt : options.sendAmt;
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (typeof data != "string")
|
if (typeof data != "string")
|
||||||
data = JSON.stringify(data);
|
data = JSON.stringify(data);
|
||||||
sendTx(senderAddr, receiverAddr, floGlobals.sendAmt, privKey, data, strict_utxo)
|
sendTx(senderAddr, receiverAddr, sendAmt, privKey, data, strict_utxo)
|
||||||
.then(txid => resolve(txid))
|
.then(txid => resolve(txid))
|
||||||
.catch(error => reject(error));
|
.catch(error => reject(error));
|
||||||
});
|
});
|
||||||
|
|||||||
56
floTokenAPI.js
Normal file
56
floTokenAPI.js
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
(function(EXPORTS) { //floTokenAPI v1.0.1
|
||||||
|
/* Token Operator to send/receive tokens via blockchain using API calls*/
|
||||||
|
'use strict';
|
||||||
|
const tokenAPI = EXPORTS;
|
||||||
|
|
||||||
|
const fetch_api = tokenAPI.fetch = function(apicall) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
console.log(floGlobals.tokenURL + apicall);
|
||||||
|
fetch(floGlobals.tokenURL + apicall).then(response => {
|
||||||
|
if (response.ok)
|
||||||
|
response.json().then(data => resolve(data));
|
||||||
|
else
|
||||||
|
reject(response)
|
||||||
|
}).catch(error => reject(error))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const getBalance = tokenAPI.getBalance = function(floID, token = floGlobals.currency) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
fetch_api(`api/v1.0/getFloAddressBalance?token=${token}&floAddress=${floID}`)
|
||||||
|
.then(result => resolve(result.balance || 0))
|
||||||
|
.catch(error => reject(error))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
tokenAPI.getTx = function(txID) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
fetch_api(`api/v1.0/getTransactionDetails/${txID}`).then(res => {
|
||||||
|
if (res.result === "error")
|
||||||
|
reject(res.description);
|
||||||
|
else if (!res.parsedFloData)
|
||||||
|
reject("Data piece (parsedFloData) missing");
|
||||||
|
else if (!res.transactionDetails)
|
||||||
|
reject("Data piece (transactionDetails) missing");
|
||||||
|
else
|
||||||
|
resolve(res);
|
||||||
|
}).catch(error => reject(error))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
tokenAPI.sendToken = function(privKey, amount, receiverID, message = "", token = floGlobals.currency, options = {}) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
let senderID = floCrypto.getFloID(privKey);
|
||||||
|
if (typeof amount !== "number" || amount <= 0)
|
||||||
|
return reject("Invalid amount");
|
||||||
|
getBalance(senderID, token).then(bal => {
|
||||||
|
if (amount > bal)
|
||||||
|
return reject(`Insufficient ${token}# balance`);
|
||||||
|
floBlockchainAPI.writeData(senderID, `send ${amount} ${token}# ${message}`, privKey, receiverID, options)
|
||||||
|
.then(txid => resolve(txid))
|
||||||
|
.catch(error => reject(error))
|
||||||
|
}).catch(error => reject(error))
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
})('object' === typeof module ? module.exports : window.floTokenAPI = {});
|
||||||
@ -10,7 +10,7 @@
|
|||||||
//Required for all
|
//Required for all
|
||||||
blockchain: "FLO",
|
blockchain: "FLO",
|
||||||
|
|
||||||
//Required for blockchain API operators
|
//Required for blockchain API operations
|
||||||
apiURL: {
|
apiURL: {
|
||||||
FLO: ['https://livenet.flocha.in/', 'https://flosight.duckdns.org/'],
|
FLO: ['https://livenet.flocha.in/', 'https://flosight.duckdns.org/'],
|
||||||
FLO_TEST: ['https://testnet-flosight.duckdns.org/', 'https://testnet.flocha.in/']
|
FLO_TEST: ['https://testnet-flosight.duckdns.org/', 'https://testnet.flocha.in/']
|
||||||
@ -19,6 +19,9 @@
|
|||||||
sendAmt: 0.001,
|
sendAmt: 0.001,
|
||||||
fee: 0.0005,
|
fee: 0.0005,
|
||||||
|
|
||||||
|
//Required for token API operations
|
||||||
|
tokenURL: "https://ranchimallflo.duckdns.org/",
|
||||||
|
|
||||||
//Required for Supernode operations
|
//Required for Supernode operations
|
||||||
SNStorageID: "FNaN9McoBAEFUjkRmNQRYLmBF8SpS7Tgfk",
|
SNStorageID: "FNaN9McoBAEFUjkRmNQRYLmBF8SpS7Tgfk",
|
||||||
supernodes: {}, //each supnernode must be stored as floID : {uri:<uri>,pubKey:<publicKey>}
|
supernodes: {}, //each supnernode must be stored as floID : {uri:<uri>,pubKey:<publicKey>}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user