Adding contract triggering UI

This commit is contained in:
sairaj mote 2023-04-28 00:56:27 +05:30
parent 2117134496
commit 44e7f86f8e

View File

@ -452,7 +452,7 @@
<span>Update price</span>
</a>
</li>
<li class="hidden">
<li>
<a class="button smart-contract-action" href="#/smartcontracts/trigger">
<svg class="icon" xmlns="http://www.w3.org/2000/svg"
enable-background="new 0 0 24 24" height="24px" viewBox="0 0 24 24" width="24px"
@ -1450,42 +1450,45 @@
break;
}
case 'trigger': {
const filteredSmartContracts = filterSmartContracts({ type: 'one-time-event', subType: 'external-event' })
const filteredSmartContracts = filterSmartContracts({ type: 'one-time-event', subType: 'external-trigger' })
if (filteredSmartContracts.length) {
let selectedSmartContract = filteredSmartContracts[0] || {}
if (scName && scAddress)
selectedSmartContract = getScDetails(scName, scAddress)
const { contractName, contractAddress, price, tokenIdentification, userChoices } = selectedSmartContract
history.replaceState(null, null, `#/smartcontracts/trigger?scName=${contractName}&scAddress=${contractAddress}`)
renderElem(getRef('smart_contract_trigger_form'), html`
<div class="grid gap-0-5">
<span class="label">FLO private key</span>
<sm-input class="password-field" type="password" error-text="Invalid private key" data-private-key="" required="">
<label slot="right" class="interact">
<input type="checkbox" class="hidden" readonly="" onchange="togglePrivateKeyVisibility(this)">
<svg class="icon invisible" xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 0 24 24" width="24px" fill="#000000"> <title>Hide password</title> <path d="M0 0h24v24H0zm0 0h24v24H0zm0 0h24v24H0zm0 0h24v24H0z" fill="none"></path> <path d="M12 7c2.76 0 5 2.24 5 5 0 .65-.13 1.26-.36 1.83l2.92 2.92c1.51-1.26 2.7-2.89 3.43-4.75-1.73-4.39-6-7.5-11-7.5-1.4 0-2.74.25-3.98.7l2.16 2.16C10.74 7.13 11.35 7 12 7zM2 4.27l2.28 2.28.46.46C3.08 8.3 1.78 10.02 1 12c1.73 4.39 6 7.5 11 7.5 1.55 0 3.03-.3 4.38-.84l.42.42L19.73 22 21 20.73 3.27 3 2 4.27zM7.53 9.8l1.55 1.55c-.05.21-.08.43-.08.65 0 1.66 1.34 3 3 3 .22 0 .44-.03.65-.08l1.55 1.55c-.67.33-1.41.53-2.2.53-2.76 0-5-2.24-5-5 0-.79.2-1.53.53-2.2zm4.31-.78l3.15 3.15.02-.16c0-1.66-1.34-3-3-3l-.17.01z"></path> </svg>
<svg class="icon visible" xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 0 24 24" width="24px" fill="#000000"> <title>Show password</title> <path d="M0 0h24v24H0z" fill="none"></path> <path d="M12 4.5C7 4.5 2.73 7.61 1 12c1.73 4.39 6 7.5 11 7.5s9.27-3.11 11-7.5c-1.73-4.39-6-7.5-11-7.5zM12 17c-2.76 0-5-2.24-5-5s2.24-5 5-5 5 2.24 5 5-2.24 5-5 5zm0-8c-1.66 0-3 1.34-3 3s1.34 3 3 3 3-1.34 3-3-1.34-3-3-3z"></path> </svg>
</label>
</sm-input>
</div>
<div class="grid gap-0-5">
<span class="label">Select smart contract</span>
<sm-select>
${render.availableSmartContractOptions(filteredSmartContracts)}
</sm-select>
</div>
<fieldset>
<legend>Select outcome</legend>
<div class="grid gap-0-5">
<label>
<input type="radio" name="outcome" value="1" checked>
<span>Outcome 1</span>
</label>
<label>
<input type="radio" name="outcome" value="2">
<span>Outcome 2</span>
</label>
<span class="label">Select smart contract</span>
<sm-select id="selected_smart_contract" onchange="handleSmartContractSelection('trigger')">
${render.availableSmartContractOptions(filteredSmartContracts, `${contractName}_${contractAddress}`)}
</sm-select>
</div>
</fieldset>
<div class="multi-state-button">
<button class="button button--primary" type="submit" disabled>Trigger</button>
</div>
`)
<fieldset>
<legend>Select outcome</legend>
<div class="grid gap-0-5">
${userChoices.map(choice => html`
<label class="flex align-center">
<input type="radio" name="outcome" value=${choice} required>
<span class="capitalize">${choice}</span>
</label>
`)}
</div>
</fieldset>
<div class="grid gap-0-5">
<span class="label">Contract address FLO private key</span>
<sm-input id="trigger_private_key" class="password-field" type="password" error-text="Invalid private key" data-private-key="" required="">
<label slot="right" class="interact">
<input type="checkbox" class="hidden" readonly="" onchange="togglePrivateKeyVisibility(this)">
<svg class="icon invisible" xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 0 24 24" width="24px" fill="#000000"> <title>Hide password</title> <path d="M0 0h24v24H0zm0 0h24v24H0zm0 0h24v24H0zm0 0h24v24H0z" fill="none"></path> <path d="M12 7c2.76 0 5 2.24 5 5 0 .65-.13 1.26-.36 1.83l2.92 2.92c1.51-1.26 2.7-2.89 3.43-4.75-1.73-4.39-6-7.5-11-7.5-1.4 0-2.74.25-3.98.7l2.16 2.16C10.74 7.13 11.35 7 12 7zM2 4.27l2.28 2.28.46.46C3.08 8.3 1.78 10.02 1 12c1.73 4.39 6 7.5 11 7.5 1.55 0 3.03-.3 4.38-.84l.42.42L19.73 22 21 20.73 3.27 3 2 4.27zM7.53 9.8l1.55 1.55c-.05.21-.08.43-.08.65 0 1.66 1.34 3 3 3 .22 0 .44-.03.65-.08l1.55 1.55c-.67.33-1.41.53-2.2.53-2.76 0-5-2.24-5-5 0-.79.2-1.53.53-2.2zm4.31-.78l3.15 3.15.02-.16c0-1.66-1.34-3-3-3l-.17.01z"></path> </svg>
<svg class="icon visible" xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 0 24 24" width="24px" fill="#000000"> <title>Show password</title> <path d="M0 0h24v24H0z" fill="none"></path> <path d="M12 4.5C7 4.5 2.73 7.61 1 12c1.73 4.39 6 7.5 11 7.5s9.27-3.11 11-7.5c-1.73-4.39-6-7.5-11-7.5zM12 17c-2.76 0-5-2.24-5-5s2.24-5 5-5 5 2.24 5 5-2.24 5-5 5zm0-8c-1.66 0-3 1.34-3 3s1.34 3 3 3 3-1.34 3-3-1.34-3-3-3z"></path> </svg>
</label>
</sm-input>
</div>
<div class="multi-state-button">
<button id="trigger_contract_button" class="button button--primary" onclick=${triggerContract} type="submit" disabled>Trigger</button>
</div>
`)
} else {
renderElem(getRef('smart_contract_trigger_form'), html`
<div class="grid justify-items-center gap-0-5">
@ -2610,67 +2613,67 @@
'--redish-orange',
]
// async function getContractInfo(name, address) {
// return new Promise((resolve, reject) => {
// if (!name) {
// name = floGlobals.smartContracts[0].contractName
// address = floGlobals.smartContracts[0].contractAddress
// }
// fetchJSON(`https://ranchimallflo.duckdns.org/api/v2/smartContractInfo?contractName=${name}&contractAddress=${address}`)
// .then(info => {
// console.log(info)
// const {
// contractInfo: {
// contractType,
// numberOfDeposits,
// numberOfParticipants,
// priceType,
// oracle_address,
// contractSubtype,
// status,
// expiryTime,
// payeeAddress,
// userChoices,
// tokenIdentification,
// acceptingToken,
// sellingToken,
// contractAmount,
// minimumsubscriptionamount,
// maximumsubscriptionamount,
// totalHonorAmount,
// totalParticipationAmount,
// price
// }, contractAddress,
// contractName
// } = info
// resolve({
// contractName,
// contractAddress,
// contractType,
// contractSubtype,
// status,
// expiryTime,
// payeeAddress,
// userChoices,
// token: tokenIdentification,
// acceptingToken,
// sellingToken,
// participationFees: contractAmount,
// minimumsubscriptionamount,
// maximumsubscriptionamount,
// numberOfDeposits,
// numberOfParticipants,
// priceType,
// oracle_address,
// totalHonorAmount,
// totalParticipationAmount,
// price
// })
// }).catch(error => {
// reject(error)
// })
// })
// }
async function getContractInfo(name, address) {
return new Promise((resolve, reject) => {
if (!name) {
name = floGlobals.smartContracts[0].contractName
address = floGlobals.smartContracts[0].contractAddress
}
fetchJSON(`${floGlobals.tokenApiUrl}/api/v2/smartContractInfo?contractName=${name}&contractAddress=${address}`)
.then(info => {
console.log(info)
const {
contractInfo: {
contractType,
numberOfDeposits,
numberOfParticipants,
priceType,
oracle_address,
contractSubtype,
status,
expiryTime,
payeeAddress,
userChoices,
tokenIdentification,
acceptingToken,
sellingToken,
contractAmount,
minimumsubscriptionamount,
maximumsubscriptionamount,
totalHonorAmount,
totalParticipationAmount,
price
}, contractAddress,
contractName
} = info
resolve({
contractName,
contractAddress,
contractType,
contractSubtype,
status,
expiryTime,
payeeAddress,
userChoices,
token: tokenIdentification,
acceptingToken,
sellingToken,
participationFees: contractAmount,
minimumsubscriptionamount,
maximumsubscriptionamount,
numberOfDeposits,
numberOfParticipants,
priceType,
oracle_address,
totalHonorAmount,
totalParticipationAmount,
price
})
}).catch(error => {
reject(error)
})
})
}
function getScDetails(name, address) {
return floGlobals.smartContracts[floGlobals.scMap.get(`${name}_${address}`)] || {}
@ -2814,7 +2817,7 @@
function updatePrice(e) {
const selectedSmartContract = document.getElementById('selected_smart_contract').value
const [contractName, contractAddress] = (selectedSmartContract).split('_')
const oraclePrivateKey = document.getElementById('oracle_private_key').value
const oraclePrivateKey = document.getElementById('oracle_private_key').value.trim()
const oracleAddress = document.getElementById('oracle_address').value
if (!floCrypto.verifyPrivKey(oraclePrivateKey, oracleAddress)) {
return notify(`Private key doesn't match with Oracle address`, 'error')
@ -2844,6 +2847,39 @@
})
}
function triggerContract(e) {
const selectedSmartContract = document.getElementById('selected_smart_contract').value
const [contractName, contractAddress] = (selectedSmartContract).split('_')
const triggerPrivateKey = document.getElementById('trigger_private_key').value.trim()
const triggerAddress = floCrypto.getFloID(triggerPrivateKey)
const triggerOutcome = getRef('smart_contract_trigger_form').querySelector('input[name="outcome"]:checked').value
const floData = `${contractName}@ triggerCondition:"${triggerOutcome}"`
console.log(floData)
if (contractAddress !== triggerAddress) {
return notify(`Private key doesn't match with contract trigger address`, 'error')
}
getConfirmation('Trigger contract', {
message: `Triggering ${contractName} with outcome: ${triggerOutcome}`,
confirmText: 'Trigger',
cancelText: 'Cancel'
}).then((res) => {
if (!res) return
buttonLoader('trigger_contract_button', true)
floBlockchainAPI.writeData(contractAddress, floData, triggerPrivateKey, contractAddress).then((txid) => {
showTransactionResult(true, txid, {
title: 'Contract trigger initiated',
})
getRef('smart_contract_trigger_form').reset()
document.getElementById('trigger_contract_button').disabled = true
}).catch((error) => {
showTransactionResult(false, error)
console.error(error)
}).finally(() => {
buttonLoader('trigger_contract_button', false)
})
})
}
function handlePriceTypeChange(e) {
switch (e.target.value) {
case 'static':