Skip to content

Commit

Permalink
add functions
Browse files Browse the repository at this point in the history
  • Loading branch information
anyxem committed Sep 24, 2024
1 parent 01913b0 commit 8b20386
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 116 deletions.
37 changes: 19 additions & 18 deletions src/pages/SendCustomOutput/SendCustomOutput.helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ export const getInfoAboutOutput = (output) => {
allFields: {
destination: parsedOutput.destination,
'value.amount.decimal': parsedOutput.value.amount.decimal,
}
},
}
}

if(parsedOutput.type === 'IssueFungibleToken') {
if (parsedOutput.type === 'IssueFungibleToken') {
return {
type: 'IssueFungibleToken',
requiredFields: ['authority'],
Expand All @@ -31,11 +31,11 @@ export const getInfoAboutOutput = (output) => {
'token_ticker.string': parsedOutput.token_ticker.string,
'total_supply.amount.decimal': parsedOutput.total_supply.amount.decimal,
'total_supply.type': parsedOutput.total_supply.type,
}
},
}
}

if(parsedOutput.type === 'IssueNft') {
if (parsedOutput.type === 'IssueNft') {
return {
type: 'IssueNft',
requiredFields: ['authority'],
Expand All @@ -45,63 +45,64 @@ export const getInfoAboutOutput = (output) => {
destination: parsedOutput.destination,
'data.name': parsedOutput.data.name,
'data.ticker': parsedOutput.data.ticker,
}
},
}
}

if(parsedOutput.type === 'DataDeposit') {
if (parsedOutput.type === 'DataDeposit') {
return {
type: 'DataDeposit',
requiredFields: ['authority'],
extraFee: 50 * 1e11, // Decimal
allFields: {
data: parsedOutput.data,
}
},
}
}

return {}
}

export const getTemplate = (name) => {
if(name === 'Transfer') {
if (name === 'Transfer') {
return {
type: 'Transfer',
value: {
type: 'Coin',
amount: {
atoms: '',
decimal: '',
}
},
},
destination: 'insert destination address',
}
}
if(name === 'IssueFungibleToken') {
if (name === 'IssueFungibleToken') {
return {
type: 'IssueFungibleToken',
//authority: '_AUTHORITY',
authority: 'tmt1q9r4gz3aevjm38yq8ycd6gl3kqd25xh4jqzjthdc',
is_freezable: false,
metadata_uri: {
hex: '',
string: 'ipfs://bafybeid7qggkecapxm5ysv7beococ5x7zdz3ro43jfj7oxhi327k62xhsq/TKNTNXM1.json'
string:
'ipfs://bafybeid7qggkecapxm5ysv7beococ5x7zdz3ro43jfj7oxhi327k62xhsq/TKNTNXM1.json',
},
number_of_decimals: 8,
token_ticker: {
hex: '',
string: 'KTNAA'
string: 'KTNAA',
},
total_supply: {
amount: {
atoms: '',
decimal: '100'
decimal: '100',
},
type: 'Fixed'
}
type: 'Unlimited',
},
}
}
if(name === 'IssueNft') {
if (name === 'IssueNft') {
return {
type: 'IssueNft',
token_id: '',
Expand All @@ -112,10 +113,10 @@ export const getTemplate = (name) => {
},
}
}
if(name === 'DataDeposit') {
if (name === 'DataDeposit') {
return {
type: 'DataDeposit',
data: ''
data: '',
}
}
}
Expand Down
70 changes: 47 additions & 23 deletions src/pages/SendCustomOutput/SendCustomOutput.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import React, { useContext, useEffect, useState } from 'react'
import styles from './SendCustomOutput.module.css'
import { validateOutput, getInfoAboutOutput, getTemplate, stringToHex } from './SendCustomOutput.helpers'
import {
validateOutput,
getInfoAboutOutput,
getTemplate,
stringToHex,
} from './SendCustomOutput.helpers'
import { AccountContext, SettingsContext } from '@Contexts'
import { AppInfo } from '@Constants'
import { useMlWalletInfo } from '@Hooks'
Expand All @@ -9,7 +14,10 @@ import { ML } from '@Cryptos'
import { MLTransaction } from '@Helpers'
import { Mintlayer } from '@APIs'
import { LocalStorageService } from '@Storage'
import { getTransactionUtxos, totalUtxosAmount } from '../../utils/Helpers/ML/MLTransaction'
import {
getTransactionUtxos,
totalUtxosAmount,
} from '../../utils/Helpers/ML/MLTransaction'

const SendCustomOutput = () => {
const { addresses, accountID } = useContext(AccountContext)
Expand Down Expand Up @@ -64,16 +72,16 @@ const SendCustomOutput = () => {
const parsedOutput = getInfoAboutOutput(customOutput)
let amountToSend = 0

if(!parsedOutput) {
if (!parsedOutput) {
setError('Invalid output')
return
}

if(parsedOutput.amountToSend) {
if (parsedOutput.amountToSend) {
amountToSend = parsedOutput.amountToSend * 1e11
}

if(parsedOutput.requiredFields) {
if (parsedOutput.requiredFields) {
// TODO go through the required fields and check and add info
}

Expand All @@ -84,18 +92,22 @@ const SendCustomOutput = () => {
const utxoCoin = utxos.filter((utxo) => utxo.utxo.value.type === 'Coin')
const inputs = getTransactionUtxos({
utxos: utxoCoin,
amount: + (parsedOutput.extraFee || 0) + amountToSend + fee,
amount: +(parsedOutput.extraFee || 0) + amountToSend + fee,
})
console.log('inputs', inputs)

setInputs(inputs)
const utxoBalance = totalUtxosAmount(utxoCoin)
const utxoBalance = totalUtxosAmount(inputs)
console.log('utxoBalance', utxoBalance)

const transactionFee = fee
const extraFee = parsedOutput.extraFee || 0

const amountToReturn = BigInt(utxoBalance) - BigInt(amountToSend) - BigInt(transactionFee) - BigInt(extraFee)
const amountToReturn =
BigInt(utxoBalance) -
BigInt(amountToSend) -
BigInt(transactionFee) -
BigInt(extraFee)

console.log('amountToReturn', amountToReturn)

Expand All @@ -109,18 +121,19 @@ const SendCustomOutput = () => {
atoms: amountToReturn.toString(),
decimal: amountToReturn.toString() / 1e11,
},
}
},
}

setOutputs([adjustedOutput, output])

// calculate fee
const transactionSize = await MLTransaction.calculateCustomTransactionSizeInBytes({
network: networkType,
inputs,
outputs,
currentHeight
})
const transactionSize =
await MLTransaction.calculateCustomTransactionSizeInBytes({
network: networkType,
inputs,
outputs,
currentHeight,
})

const new_fee = Math.ceil(feerate * (transactionSize / 1000))
setFee(new_fee)
Expand Down Expand Up @@ -180,9 +193,10 @@ const SendCustomOutput = () => {
fee: fee.toString(),
isConfirmed: false,
mode: 'mode',
usedUtxosOutpoints: inputs.map(
({ outpoint: { index, source_id } }) => ({ index, source_id }),
),
usedUtxosOutpoints: inputs.map(({ outpoint: { index, source_id } }) => ({
index,
source_id,
})),
})
LocalStorageService.setItem(
unconfirmedTransactionString,
Expand All @@ -193,7 +207,12 @@ const SendCustomOutput = () => {
}

const handleInsertTemplate = () => {
if(customOutput?.length > 0 && !window.confirm('Are you sure you want to insert a template? It will overwrite the current output')) {
if (
customOutput?.length > 0 &&
!window.confirm(
'Are you sure you want to insert a template? It will overwrite the current output',
)
) {
return
}
const templateName = tplRef.current.selectedOptions[0].value
Expand All @@ -206,7 +225,7 @@ const SendCustomOutput = () => {
}

useEffect(() => {
if(customOutput) {
if (customOutput) {
const allFields = getInfoAboutOutput(customOutput).allFields

const fields = Object.keys(allFields).map((key) => {
Expand All @@ -229,7 +248,9 @@ const SendCustomOutput = () => {
data['value']['amount']['atoms'] = (event.target.value * 1e11).toString()
} else if (id === 'total_supply.amount.decimal') {
data['total_supply']['amount']['decimal'] = event.target.value
data['total_supply']['amount']['atoms'] = (event.target.value * 1e11).toString()
data['total_supply']['amount']['atoms'] = (
event.target.value * 1e11
).toString()
} else if (id === 'token_ticker.string') {
data['token_ticker']['string'] = event.target.value
data['token_ticker']['hex'] = stringToHex(event.target.value)
Expand Down Expand Up @@ -319,14 +340,17 @@ const SendCustomOutput = () => {
<div className={styles.transactionHexPreview}>
<div className={styles.transactionHexPreviewInputs}>
<div>
password: <input
password:{' '}
<input
type="password"
ref={passwordRef}
/>
</div>
<button onClick={handleBuildTransaction}>Build transaction</button>
</div>
<div className={styles.transactionHexPreviewResult}>{transactionHex}</div>
<div className={styles.transactionHexPreviewResult}>
{transactionHex}
</div>
</div>
<div className={styles.broadcast}>
<button onClick={handleBroadcast}>Broadcast transaction</button>
Expand Down
Loading

0 comments on commit 8b20386

Please sign in to comment.