Browse Source

Merge pull request #188 from Philreact/add-fee-to-publish

add fees to publish
pull/190/head
AlphaX-Projects 1 year ago committed by GitHub
parent
commit
2ad1ed10da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      core/language/us.json
  2. 40
      plugins/plugins/core/qdn/browser/browser.src.js
  3. 34
      plugins/plugins/core/qdn/publish/publish.src.js
  4. 30
      plugins/plugins/utils/publish-image.js

3
core/language/us.json

@ -719,7 +719,8 @@
"bchange43": "Do you give this application permission to add to this list?",
"bchange44": "Do you give this application permission to delete from this list?",
"bchange45": "Encrypt",
"bchange46": "Do you give this application permission to save the following file"
"bchange46": "Do you give this application permission to save the following file",
"bchange47": "Instant publish - requires"
},
"datapage": {
"dchange1": "Data Management",

40
plugins/plugins/core/qdn/browser/browser.src.js

@ -425,6 +425,22 @@ class WebBrowser extends LitElement {
const joinFee = (Number(data) / 1e8).toFixed(8)
return joinFee
}
async getArbitraryFee (){
const timestamp = Date.now()
const myNode = window.parent.reduxStore.getState().app.nodeConfig.knownNodes[window.parent.reduxStore.getState().app.nodeConfig.node]
const nodeUrl = myNode.protocol + '://' + myNode.domain + ':' + myNode.port
const url = `${nodeUrl}/transactions/unitfee?txType=ARBITRARY&timestamp=${timestamp}`
const response = await fetch(url)
if (!response.ok) {
throw new Error('Error when fetching arbitrary fee');
}
const data = await response.json()
const arbitraryFee = (Number(data) / 1e8).toFixed(8)
return {
timestamp,
fee : arbitraryFee
}
}
async sendQortFee() {
const myNode = window.parent.reduxStore.getState().app.nodeConfig.knownNodes[window.parent.reduxStore.getState().app.nodeConfig.node]
const nodeUrl = myNode.protocol + '://' + myNode.domain + ':' + myNode.port
@ -975,6 +991,7 @@ class WebBrowser extends LitElement {
const tag3 = data.tag3;
const tag4 = data.tag4;
const tag5 = data.tag5;
let feeAmount = null
if (data.identifier == null) {
identifier = 'default';
}
@ -994,6 +1011,8 @@ class WebBrowser extends LitElement {
if (data.file) {
data64 = await fileToBase64(data.file)
}
const getArbitraryFee = await this.getArbitraryFee()
feeAmount = getArbitraryFee.fee
if (data.encrypt) {
try {
@ -1014,6 +1033,7 @@ class WebBrowser extends LitElement {
}
const res2 = await showModalAndWait(
@ -1022,7 +1042,8 @@ class WebBrowser extends LitElement {
name,
identifier,
service,
encrypt: data.encrypt
encrypt: data.encrypt,
feeAmount
}
);
if (res2.action === 'accept') {
@ -1052,7 +1073,8 @@ class WebBrowser extends LitElement {
tag4,
tag5,
apiVersion: 2,
withFee: res2.userData.isWithFee === true ? true : false
withFee: res2.userData.isWithFee === true ? true : false,
feeAmount: feeAmount
});
response = JSON.stringify(resPublish);
@ -1080,7 +1102,7 @@ class WebBrowser extends LitElement {
case actions.PUBLISH_MULTIPLE_QDN_RESOURCES: {
const requiredFields = ['resources'];
const missingFields = [];
let feeAmount = null
requiredFields.forEach((field) => {
if (!data[field]) {
missingFields.push(field);
@ -1114,11 +1136,14 @@ class WebBrowser extends LitElement {
response = JSON.stringify(data);
break
}
const getArbitraryFee = await this.getArbitraryFee()
feeAmount = getArbitraryFee.fee
const res2 = await showModalAndWait(
actions.PUBLISH_MULTIPLE_QDN_RESOURCES,
{
resources,
encrypt: data.encrypt
encrypt: data.encrypt,
feeAmount
}
);
@ -1217,7 +1242,8 @@ class WebBrowser extends LitElement {
tag4,
tag5,
apiVersion: 2,
withFee: res2.userData.isWithFee === true ? true : false
withFee: res2.userData.isWithFee === true ? true : false,
feeAmount: feeAmount
});
worker.terminate();
@ -3001,7 +3027,7 @@ async function showModalAndWait(type, data) {
</table>
<div class="checkbox-row">
<label for="isWithFee" id="isWithFeeLabel" style="color: var(--black);">
${get('browserpage.bchange33')} ${data.resources.length * 0.001} QORT fee
${get('browserpage.bchange33')} ${data.resources.length * data.feeAmount} QORT fee
</label>
<mwc-checkbox checked style="margin-right: -15px;" id="isWithFee"></mwc-checkbox>
</div>
@ -3017,7 +3043,7 @@ async function showModalAndWait(type, data) {
<p style="font-size: 16px;overflow-wrap: anywhere;" class="modal-paragraph"><span style="font-weight: bold">${get("browserpage.bchange45")}:</span> ${data.encrypt ? true : false}</p>
<div class="checkbox-row">
<label for="isWithFee" id="isWithFeeLabel" style="color: var(--black);">
${get('browserpage.bchange29')}
${get('browserpage.bchange47')} ${data.feeAmount} QORT fee
</label>
<mwc-checkbox checked style="margin-right: -15px;" id="isWithFee"></mwc-checkbox>
</div>

34
plugins/plugins/core/qdn/publish/publish.src.js

@ -488,6 +488,16 @@ class PublishData extends LitElement {
this.successMessage = ''
console.error(errorMessage)
}
const getArbitraryFee = async () => {
const timestamp = Date.now()
let fee = await parentEpml.request('apiCall', {
url: `/transactions/unitfee?txType=ARBITRARY&timestamp=${timestamp}`
})
return {
timestamp,
fee : (Number(fee) / 1e8).toFixed(8)
}
}
const validate = async () => {
let validNameRes = await validateName(registeredName)
@ -501,8 +511,17 @@ class PublishData extends LitElement {
this.generalMessage = `${err6string}`
let transactionBytes
let previewUrlPath
let uploadDataRes = await uploadData(registeredName, path, file, preview, fee)
let feeAmount = null
if(fee){
const res = await getArbitraryFee()
if(res.fee){
feeAmount= res.fee
} else {
throw new Error('unable to get fee')
}
}
let uploadDataRes = await uploadData(registeredName, path, file, preview, fee, feeAmount)
if (uploadDataRes.error) {
let err7string = get("publishpage.pchange20")
@ -531,12 +550,13 @@ class PublishData extends LitElement {
if (fee) {
let err9string = get("publishpage.pchange26")
this.generalMessage = `${err9string}`
} else {
let err9string = get("publishpage.pchange22")
this.generalMessage = `${err9string}`
}
let signAndProcessRes = await signAndProcess(transactionBytes, fee)
let signAndProcessRes = await signAndProcess(transactionBytes, fee, feeAmount)
if (signAndProcessRes.error) {
let err10string = get("publishpage.pchange20")
@ -554,7 +574,9 @@ class PublishData extends LitElement {
this.successMessage = `${err11string}`
}
const uploadData = async (registeredName, path, file, preview, fee) => {
const uploadData = async (registeredName, path, file, preview, fee, feeAmount) => {
let postBody = path
let urlSuffix = ""
if (file != null) {
@ -592,9 +614,9 @@ class PublishData extends LitElement {
uploadDataUrl = `/arbitrary/${service}/${registeredName}/${this.identifier}${urlSuffix}?${metadataQueryString}&apiKey=${this.getApiKey()}&preview=${new Boolean(preview).toString()}`
}
} else if (fee) {
uploadDataUrl = `/arbitrary/${this.service}/${registeredName}${urlSuffix}?${metadataQueryString}&fee=100000&apiKey=${this.getApiKey()}`
uploadDataUrl = `/arbitrary/${this.service}/${registeredName}${urlSuffix}?${metadataQueryString}&fee=${feeAmount}&apiKey=${this.getApiKey()}`
if (identifier != null && identifier.trim().length > 0) {
uploadDataUrl = `/arbitrary/${service}/${registeredName}/${this.identifier}${urlSuffix}?${metadataQueryString}&fee=100000&apiKey=${this.getApiKey()}`
uploadDataUrl = `/arbitrary/${service}/${registeredName}/${this.identifier}${urlSuffix}?${metadataQueryString}&fee=${feeAmount}&apiKey=${this.getApiKey()}`
}
} else {
uploadDataUrl = `/arbitrary/${this.service}/${registeredName}${urlSuffix}?${metadataQueryString}&apiKey=${this.getApiKey()}`

30
plugins/plugins/utils/publish-image.js

@ -28,7 +28,8 @@ export const publishData = async ({
tag2,
tag3,
tag4,
tag5
tag5,
feeAmount
}) => {
const validateName = async (receiverName) => {
let nameRes = await parentEpml.request("apiCall", {
@ -48,6 +49,16 @@ export const publishData = async ({
})
return convertedBytes
}
const getArbitraryFee = async () => {
const timestamp = Date.now()
let fee = await parentEpml.request('apiCall', {
url: `/transactions/unitfee?txType=ARBITRARY&timestamp=${timestamp}`
})
return {
timestamp,
fee : (Number(fee) / 1e8).toFixed(8)
}
}
const signAndProcess = async (transactionBytesBase58) => {
let convertedBytesBase58 = await convertBytesForSigning(
@ -125,7 +136,18 @@ export const publishData = async ({
if (validNameRes.error) {
throw new Error('Name not found');
}
let transactionBytes = await uploadData(registeredName, path, file)
let fee = null
if(withFee && feeAmount){
fee= feeAmount
} else if(withFee){
const res = await getArbitraryFee()
if(res.fee){
fee= res.fee
} else {
throw new Error('unable to get fee')
}
}
let transactionBytes = await uploadData(registeredName, path, file, fee)
if (transactionBytes.error) {
throw new Error(transactionBytes.message || 'Error when uploading');
} else if (
@ -149,7 +171,7 @@ export const publishData = async ({
return signAndProcessRes
}
const uploadData = async (registeredName, path, file) => {
const uploadData = async (registeredName, path, file, fee) => {
if (identifier != null && identifier.trim().length > 0) {
let postBody = path
let urlSuffix = ""
@ -181,7 +203,7 @@ export const publishData = async ({
}
if(withFee){
uploadDataUrl = uploadDataUrl + '&fee=100000'
uploadDataUrl = uploadDataUrl + `&fee=${fee}`
}
if(filename != null && filename != "undefined"){

Loading…
Cancel
Save