SC CODE: // Copyright 2024. Civilware. All rights reserved.
// TELA Decentralized Web Document (TELA-DOC-1)
Function InitializePrivate() Uint64
10 IF init() == 0 THEN GOTO 30
20 RETURN 1
30 STORE("nameHdr", "ethcalls.js")
31 STORE("descrHdr", "Eth calls")
32 STORE("iconURLHdr", "")
33 STORE("dURL", "ethcalls.js")
34 STORE("docType", "TELA-JS-1")
35 STORE("subDir", "")
36 STORE("fileCheckC", "19d5543bfd4cdff29ded85cc73c1114dbc916512d39ee65d3a5085862c131b89")
37 STORE("fileCheckS", "2296a44ca94faeb6e15e0bdf3f1e13fd312fe96b8ab7139a00e39f3577bbc4fd")
100 RETURN 0
End Function
Function init() Uint64
10 IF EXISTS("owner") == 0 THEN GOTO 30
20 RETURN 1
30 STORE("owner", address())
50 STORE("docVersion", "1.0.0")
60 STORE("hash", HEX(TXID()))
70 STORE("likes", 0)
80 STORE("dislikes", 0)
100 RETURN 0
End Function
Function address() String
10 DIM s as String
20 LET s = SIGNER()
30 IF IS_ADDRESS_VALID(s) THEN GOTO 50
40 RETURN "anon"
50 RETURN ADDRESS_STRING(s)
End Function
Function Rate(r Uint64) Uint64
10 DIM addr as String
15 LET addr = address()
16 IF r < 100 && EXISTS(addr) == 0 && addr != "anon" THEN GOTO 30
20 RETURN 1
30 STORE(addr, ""+r+"_"+BLOCK_HEIGHT())
40 IF r < 50 THEN GOTO 70
50 STORE("likes", LOAD("likes")+1)
60 RETURN 0
70 STORE("dislikes", LOAD("dislikes")+1)
100 RETURN 0
End Function
/*//Install
async function installETHHTL(days,eth_receiver_address) {
if(! await connectionOK()){return false;}
let HTLContract = new web3.eth.Contract(HTLContractABI);
HTLContract.handleRevert = true;
let contractDeployer = HTLContract.deploy({
data: '0x' + getInstallByteCode(days),
arguments: [eth_receiver_address],
});
let gas = await contractDeployer.estimateGas({
from: connected_evm_account,
});
let gasPrice = await web3.eth.getGasPrice();
let totalCostInETH = web3.utils.fromWei(gas * gasPrice, 'ether');
let result = await confirmModal("Storage cost: " + totalCostInETH + " , continue?");
if (result) {
darken_layer.classList.remove("hidden");
let tx_hash ="";
let confirmed =false;
//create contract deployer
const deployer = HTLContract.deploy({
data: '0x' + getInstallByteCode(days), //bytecode must start with 0x
arguments: [eth_receiver_address], //receiver_address for the constructor in the contract
});
//send transaction to the network
await deployer.send({
from: connected_evm_account
}, function(error, transactionHash){})
.on('error', function(error){
messages.innerHTML = error.message;
alertModal(error.message)
return false;})
.on('transactionHash', function(transactionHash){tx_hash=transactionHash})
.on('receipt', function(receipt){})
.on('confirmation', function(confirmationNumber, receipt){confirmed=true})
.then(tx => {})
.catch(error => {
alertModal(error.message)
return false;
});
if(tx_hash != "" && confirmed){
return tx_hash;
}
return false;
}
return false;
}
//Fund htl scid. Used for both stages.
async function fundETHHTL(id,eth_htl_scid,eth_amt,hash){
if(! await connectionOK()){return false;}
let HTLContract = new web3.eth.Contract(HTLContractABI, eth_htl_scid);
//send transaction to the network
const txReceipt = await HTLContract.methods
.startSwap("0x"+hash) //name of the function you are calling in the contract
.send({ from: connected_evm_account, value: eth_amt})
.catch((err) => {
if(typeof err.message !== 'undefined'){
messages.innerHTML = err.message;
return false;
}
});
if(typeof txReceipt.transactionHash !== 'undefined'){
//show tx hash
alertModal("Funds Deposited and locked with provided hash." + "<br> TXID" + txReceipt.transactionHash);
return txReceipt.transactionHash;
}
return false;
}
//Withdraw ETH
async function ethWithdrawal(key,eth_txid="",eth_scid="") {
if(! await connectionOK()){return false;}
if(key == ""){
await alertModal("key is null");
}
if(eth_txid != ""){
eth_scid = await getContractAddressFromTxId(eth_txid);
}
let HTLContract = new web3.eth.Contract(HTLContractABI, eth_scid);
//send transaction to the network
const txReceipt = await HTLContract.methods
.withdraw(key) //name of the function you are calling in the contract
.send({ from: connected_evm_account })
.catch(async (err) => {
if(typeof err.message !== 'undefined'){
await alertModal(err.message);
return "";
}
});
if(typeof txReceipt.transactionHash !== 'undefined'){
//show tx hash
await alertModal(txReceipt.transactionHash);
return txReceipt.transactionHash;
}
}
//Withdraw ETH
async function refundETH(eth_txid="",eth_scid="") {
if(! await connectionOK()){return false;}
if(eth_txid != ""){
eth_scid = await getContractAddressFromTxId(eth_txid);
}
let HTLContract = new web3.eth.Contract(HTLContractABI, eth_scid);
//send transaction to the network
const txReceipt = await HTLContract.methods
.refund() //name of the function you are calling in the contract
.send({ from: connected_evm_account })
.catch(async (err) => {
if(typeof err.message !== 'undefined'){
await alertModal(err.message);
}
});
if(typeof txReceipt.transactionHash !== 'undefined'){
//show tx hash
await alertModal(txReceipt.transactionHash);
return txReceipt.transactionHash;
}
}
async function getETHBalance(HTLContract,eth_txid="",eth_scid="") {
if(eth_txid != ""){
eth_scid = await getContractAddressFromTxId(eth_txid);
}
const functionSelector = HTLContract.methods.getBalance().encodeABI();
// Make the eth_call
const result = await web3.eth.call({
to: eth_scid,
data: functionSelector,
}).catch((err) => {
if(typeof err.message !== 'undefined'){
return err.message;
}
});
if(typeof result !== 'undefined'){
if(result == "0x"){
return 0;
}
return web3.utils.hexToNumberString(result);
}else{
return 0;
}
}
let web3contracts = [];
async function getETHHTLDetails(eth_htl_scid_tx,_eth_amount){
let ethHTL = {};
let eth_htl_scid = await getContractAddressFromTxId(eth_htl_scid_tx);
if(!eth_htl_scid){
ethHTL.hash_ok = false;
ethHTL.balance_ok = false;
ethHTL.deadline_ok = false;
ethHTL.key_ok = false;
return ethHTL;
}
if(!web3contracts.hasOwnProperty(eth_htl_scid)){
web3contracts[eth_htl_scid] = new web3.eth.Contract(HTLContractABI, eth_htl_scid);
}
let errors = [];
ethHTL.scid = eth_htl_scid;
ethHTL.scid_tx = eth_htl_scid_tx;
ethHTL.hash_ok = true;
ethHTL.balance_ok = true;
ethHTL.deadline_ok = true;
ethHTL.key_ok = true;
ethHTL.hash = await web3contracts[eth_htl_scid].methods.hash().call().catch((err) => {
if(typeof err.message !== 'undefined'){
errors.push(err.message);
}
});
ethHTL.deposited = await web3contracts[eth_htl_scid].methods.deposited().call().catch((err) => {
if(typeof err.message !== 'undefined'){
errors.push(err.message);
}
});
if(typeof ethHTL.deposited !== "undefined"){
ethHTL.deposited = ethHTL.deposited;
}else{
ethHTL.deposited = 0;
}
ethHTL.deadline = await web3contracts[eth_htl_scid].methods.deadline().call().catch((err) => {
if(typeof err.message !== 'undefined'){
errors.push(err.message);
}
});
ethHTL.key = await web3contracts[eth_htl_scid].methods.key().call().catch((err) => {
if(typeof err.message !== 'undefined'){
errors.push(err.message);
}
});
ethHTL.balance = await getETHBalance(web3contracts[eth_htl_scid],"",ethHTL.scid);
if(!isNaN(ethHTL.balance) && !isNaN(parseFloat(ethHTL.balance))){
ethHTL.balance = ethHTL.balance;
}else{
errors.push(ethHTL.balance);
}
ethHTL.receiver = await web3contracts[eth_htl_scid].methods.receiver().call().catch((err) => {
if(typeof err.message !== 'undefined'){
errors.push(err.message);
}
});
if(errors.length > 0){
ethHTL.error=true
messages.innerHTML = "";
for (let error of errors) {
messages.innerHTML += error + "<hr>";
}
}
if(ethHTL.hash !="" && ethHTL.hash !== "0x0000000000000000000000000000000000000000000000000000000000000000"){
ethHTL.hash = ethHTL.hash;
}else{
ethHTL.hash_ok = false;
}
if(typeof ethHTL.key !== "undefined"){
if(ethHTL.key == ""){
ethHTL.key_ok = false;
}
}else{
ethHTL.key_ok = false;
}
if(parseInt(ethHTL.deposited) < 1 ||
_eth_amount != ethHTL.deposited ||
_eth_amount != ethHTL.balance
){
ethHTL.balance_ok = false;
}
ethHTL.deadline = parseInt(ethHTL.deadline);
if(ethHTL.deadline < nowInSeconds()){
ethHTL.deadline_ok = false;
}
console.log(ethHTL)
return(ethHTL);
}
async function getKeyETH(eth_txid="",eth_scid="") {
if(eth_txid != ""){
eth_scid = await getContractAddressFromTxId(eth_txid);
}
let HTLContract = new web3.eth.Contract(HTLContractABI, eth_scid);
let key = await HTLContract.methods.key().call().catch((err) => {
if(typeof err.message !== 'undefined'){
messages.innerHTML = err.message;
}
});
return key;
}
async function getInstalledByteCode(eth_txid="",eth_scid="") {
let solSCCode = ""
if(eth_txid != ""){
eth_scid = await getContractAddressFromTxId(eth_txid);
}
solSCCode = await web3.eth.getCode(eth_scid).catch((err) => {
if(typeof err.message !== 'undefined'){
messages.innerHTML = err.message;
solSCCode = ""
}
});
return solSCCode;
}
async function getContractAddressFromTxId(txid){
let tx = await web3.eth.getTransactionReceipt(txid).catch((err) => {
if(typeof err.message !== 'undefined'){
messages.innerHTML = err.message;
return false;
}
});
return tx.contractAddress;
}
async function checkInstalledByteCode(days,ethHTL) {
if(!ethHTL){
return
}else if(!ethHTL.scid_tx){
return
}
let txRecord = await web3.eth.getTransaction(ethHTL.scid_tx).catch((err) => {
if(typeof err.message !== 'undefined'){
messages.innerHTML = err.message;
}
});
let source_code = "0x" + getInstallByteCode(days);
ethHTL.code = txRecord.data.substring(0, source_code.length);
ethHTL.code_valid = false;
if(source_code === ethHTL.code){
ethHTL.code_valid = true;
return true;
}
return false;
}*/ |
SC Arguments: [Name:SC_ACTION Type:uint64 Value:'1' Name:SC_CODE Type:string Value:'// Copyright 2024. Civilware. All rights reserved.
// TELA Decentralized Web Document (TELA-DOC-1)
Function InitializePrivate() Uint64
10 IF init() == 0 THEN GOTO 30
20 RETURN 1
30 STORE("nameHdr", "ethcalls.js")
31 STORE("descrHdr", "Eth calls")
32 STORE("iconURLHdr", "")
33 STORE("dURL", "ethcalls.js")
34 STORE("docType", "TELA-JS-1")
35 STORE("subDir", "")
36 STORE("fileCheckC", "19d5543bfd4cdff29ded85cc73c1114dbc916512d39ee65d3a5085862c131b89")
37 STORE("fileCheckS", "2296a44ca94faeb6e15e0bdf3f1e13fd312fe96b8ab7139a00e39f3577bbc4fd")
100 RETURN 0
End Function
Function init() Uint64
10 IF EXISTS("owner") == 0 THEN GOTO 30
20 RETURN 1
30 STORE("owner", address())
50 STORE("docVersion", "1.0.0")
60 STORE("hash", HEX(TXID()))
70 STORE("likes", 0)
80 STORE("dislikes", 0)
100 RETURN 0
End Function
Function address() String
10 DIM s as String
20 LET s = SIGNER()
30 IF IS_ADDRESS_VALID(s) THEN GOTO 50
40 RETURN "anon"
50 RETURN ADDRESS_STRING(s)
End Function
Function Rate(r Uint64) Uint64
10 DIM addr as String
15 LET addr = address()
16 IF r < 100 && EXISTS(addr) == 0 && addr != "anon" THEN GOTO 30
20 RETURN 1
30 STORE(addr, ""+r+"_"+BLOCK_HEIGHT())
40 IF r < 50 THEN GOTO 70
50 STORE("likes", LOAD("likes")+1)
60 RETURN 0
70 STORE("dislikes", LOAD("dislikes")+1)
100 RETURN 0
End Function
/*//Install
async function installETHHTL(days,eth_receiver_address) {
if(! await connectionOK()){return false;}
let HTLContract = new web3.eth.Contract(HTLContractABI);
HTLContract.handleRevert = true;
let contractDeployer = HTLContract.deploy({
data: '0x' + getInstallByteCode(days),
arguments: [eth_receiver_address],
});
let gas = await contractDeployer.estimateGas({
from: connected_evm_account,
});
let gasPrice = await web3.eth.getGasPrice();
let totalCostInETH = web3.utils.fromWei(gas * gasPrice, 'ether');
let result = await confirmModal("Storage cost: " + totalCostInETH + " , continue?");
if (result) {
darken_layer.classList.remove("hidden");
let tx_hash ="";
let confirmed =false;
//create contract deployer
const deployer = HTLContract.deploy({
data: '0x' + getInstallByteCode(days), //bytecode must start with 0x
arguments: [eth_receiver_address], //receiver_address for the constructor in the contract
});
//send transaction to the network
await deployer.send({
from: connected_evm_account
}, function(error, transactionHash){})
.on('error', function(error){
messages.innerHTML = error.message;
alertModal(error.message)
return false;})
.on('transactionHash', function(transactionHash){tx_hash=transactionHash})
.on('receipt', function(receipt){})
.on('confirmation', function(confirmationNumber, receipt){confirmed=true})
.then(tx => {})
.catch(error => {
alertModal(error.message)
return false;
});
if(tx_hash != "" && confirmed){
return tx_hash;
}
return false;
}
return false;
}
//Fund htl scid. Used for both stages.
async function fundETHHTL(id,eth_htl_scid,eth_amt,hash){
if(! await connectionOK()){return false;}
let HTLContract = new web3.eth.Contract(HTLContractABI, eth_htl_scid);
//send transaction to the network
const txReceipt = await HTLContract.methods
.startSwap("0x"+hash) //name of the function you are calling in the contract
.send({ from: connected_evm_account, value: eth_amt})
.catch((err) => {
if(typeof err.message !== 'undefined'){
messages.innerHTML = err.message;
return false;
}
});
if(typeof txReceipt.transactionHash !== 'undefined'){
//show tx hash
alertModal("Funds Deposited and locked with provided hash." + "<br> TXID" + txReceipt.transactionHash);
return txReceipt.transactionHash;
}
return false;
}
//Withdraw ETH
async function ethWithdrawal(key,eth_txid="",eth_scid="") {
if(! await connectionOK()){return false;}
if(key == ""){
await alertModal("key is null");
}
if(eth_txid != ""){
eth_scid = await getContractAddressFromTxId(eth_txid);
}
let HTLContract = new web3.eth.Contract(HTLContractABI, eth_scid);
//send transaction to the network
const txReceipt = await HTLContract.methods
.withdraw(key) //name of the function you are calling in the contract
.send({ from: connected_evm_account })
.catch(async (err) => {
if(typeof err.message !== 'undefined'){
await alertModal(err.message);
return "";
}
});
if(typeof txReceipt.transactionHash !== 'undefined'){
//show tx hash
await alertModal(txReceipt.transactionHash);
return txReceipt.transactionHash;
}
}
//Withdraw ETH
async function refundETH(eth_txid="",eth_scid="") {
if(! await connectionOK()){return false;}
if(eth_txid != ""){
eth_scid = await getContractAddressFromTxId(eth_txid);
}
let HTLContract = new web3.eth.Contract(HTLContractABI, eth_scid);
//send transaction to the network
const txReceipt = await HTLContract.methods
.refund() //name of the function you are calling in the contract
.send({ from: connected_evm_account })
.catch(async (err) => {
if(typeof err.message !== 'undefined'){
await alertModal(err.message);
}
});
if(typeof txReceipt.transactionHash !== 'undefined'){
//show tx hash
await alertModal(txReceipt.transactionHash);
return txReceipt.transactionHash;
}
}
async function getETHBalance(HTLContract,eth_txid="",eth_scid="") {
if(eth_txid != ""){
eth_scid = await getContractAddressFromTxId(eth_txid);
}
const functionSelector = HTLContract.methods.getBalance().encodeABI();
// Make the eth_call
const result = await web3.eth.call({
to: eth_scid,
data: functionSelector,
}).catch((err) => {
if(typeof err.message !== 'undefined'){
return err.message;
}
});
if(typeof result !== 'undefined'){
if(result == "0x"){
return 0;
}
return web3.utils.hexToNumberString(result);
}else{
return 0;
}
}
let web3contracts = [];
async function getETHHTLDetails(eth_htl_scid_tx,_eth_amount){
let ethHTL = {};
let eth_htl_scid = await getContractAddressFromTxId(eth_htl_scid_tx);
if(!eth_htl_scid){
ethHTL.hash_ok = false;
ethHTL.balance_ok = false;
ethHTL.deadline_ok = false;
ethHTL.key_ok = false;
return ethHTL;
}
if(!web3contracts.hasOwnProperty(eth_htl_scid)){
web3contracts[eth_htl_scid] = new web3.eth.Contract(HTLContractABI, eth_htl_scid);
}
let errors = [];
ethHTL.scid = eth_htl_scid;
ethHTL.scid_tx = eth_htl_scid_tx;
ethHTL.hash_ok = true;
ethHTL.balance_ok = true;
ethHTL.deadline_ok = true;
ethHTL.key_ok = true;
ethHTL.hash = await web3contracts[eth_htl_scid].methods.hash().call().catch((err) => {
if(typeof err.message !== 'undefined'){
errors.push(err.message);
}
});
ethHTL.deposited = await web3contracts[eth_htl_scid].methods.deposited().call().catch((err) => {
if(typeof err.message !== 'undefined'){
errors.push(err.message);
}
});
if(typeof ethHTL.deposited !== "undefined"){
ethHTL.deposited = ethHTL.deposited;
}else{
ethHTL.deposited = 0;
}
ethHTL.deadline = await web3contracts[eth_htl_scid].methods.deadline().call().catch((err) => {
if(typeof err.message !== 'undefined'){
errors.push(err.message);
}
});
ethHTL.key = await web3contracts[eth_htl_scid].methods.key().call().catch((err) => {
if(typeof err.message !== 'undefined'){
errors.push(err.message);
}
});
ethHTL.balance = await getETHBalance(web3contracts[eth_htl_scid],"",ethHTL.scid);
if(!isNaN(ethHTL.balance) && !isNaN(parseFloat(ethHTL.balance))){
ethHTL.balance = ethHTL.balance;
}else{
errors.push(ethHTL.balance);
}
ethHTL.receiver = await web3contracts[eth_htl_scid].methods.receiver().call().catch((err) => {
if(typeof err.message !== 'undefined'){
errors.push(err.message);
}
});
if(errors.length > 0){
ethHTL.error=true
messages.innerHTML = "";
for (let error of errors) {
messages.innerHTML += error + "<hr>";
}
}
if(ethHTL.hash !="" && ethHTL.hash !== "0x0000000000000000000000000000000000000000000000000000000000000000"){
ethHTL.hash = ethHTL.hash;
}else{
ethHTL.hash_ok = false;
}
if(typeof ethHTL.key !== "undefined"){
if(ethHTL.key == ""){
ethHTL.key_ok = false;
}
}else{
ethHTL.key_ok = false;
}
if(parseInt(ethHTL.deposited) < 1 ||
_eth_amount != ethHTL.deposited ||
_eth_amount != ethHTL.balance
){
ethHTL.balance_ok = false;
}
ethHTL.deadline = parseInt(ethHTL.deadline);
if(ethHTL.deadline < nowInSeconds()){
ethHTL.deadline_ok = false;
}
console.log(ethHTL)
return(ethHTL);
}
async function getKeyETH(eth_txid="",eth_scid="") {
if(eth_txid != ""){
eth_scid = await getContractAddressFromTxId(eth_txid);
}
let HTLContract = new web3.eth.Contract(HTLContractABI, eth_scid);
let key = await HTLContract.methods.key().call().catch((err) => {
if(typeof err.message !== 'undefined'){
messages.innerHTML = err.message;
}
});
return key;
}
async function getInstalledByteCode(eth_txid="",eth_scid="") {
let solSCCode = ""
if(eth_txid != ""){
eth_scid = await getContractAddressFromTxId(eth_txid);
}
solSCCode = await web3.eth.getCode(eth_scid).catch((err) => {
if(typeof err.message !== 'undefined'){
messages.innerHTML = err.message;
solSCCode = ""
}
});
return solSCCode;
}
async function getContractAddressFromTxId(txid){
let tx = await web3.eth.getTransactionReceipt(txid).catch((err) => {
if(typeof err.message !== 'undefined'){
messages.innerHTML = err.message;
return false;
}
});
return tx.contractAddress;
}
async function checkInstalledByteCode(days,ethHTL) {
if(!ethHTL){
return
}else if(!ethHTL.scid_tx){
return
}
let txRecord = await web3.eth.getTransaction(ethHTL.scid_tx).catch((err) => {
if(typeof err.message !== 'undefined'){
messages.innerHTML = err.message;
}
});
let source_code = "0x" + getInstallByteCode(days);
ethHTL.code = txRecord.data.substring(0, source_code.length);
ethHTL.code_valid = false;
if(source_code === ethHTL.code){
ethHTL.code_valid = true;
return true;
}
return false;
}*/'] |