Skip to content

Commit

Permalink
Fixed aggregate and hash used max fee. (#318)
Browse files Browse the repository at this point in the history
Fixed max fee and paid fee display for aggregate
  • Loading branch information
fboucquez committed Apr 30, 2020
1 parent 5195967 commit 5b5da90
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,20 @@ export class TransactionDetailsHeaderTs extends Vue {
*/
public getFeeAmount(): number {
if (!this.view) return 0
return this.view.values.get('effectiveFee') || this.view.values.get('maxFee') || 0
const effectiveFee = this.view.values.get('effectiveFee')
if (effectiveFee !== undefined)
{return effectiveFee}
const maxFee = this.view.values.get('maxFee')
if (maxFee !== undefined)
{return maxFee}
return 0
}

public getFeeKey(): string {
if (this.view && this.view.values.get('effectiveFee') != undefined){
return 'paid_fee'
}
return 'max_fee'
}

/**
Expand All @@ -96,7 +109,7 @@ export class TransactionDetailsHeaderTs extends Vue {
value: this.view.info ? this.view.info.hash : '-',
},
{
key: `${this.view.info ? 'paid_fee' : 'max_fee'}`,
key: this.getFeeKey(),
value: {
id: this.networkMosaic,
mosaicHex: this.networkMosaicTicker,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,14 @@ export class TransactionRowTs extends Vue {
}
// https://github.com/nemfoundation/nem2-desktop-account/issues/879
// We may want to show N/A instead of the paid fee
return this.view.values.get('effectiveFee') || this.view.values.get('maxFee') || 0
if (!this.view) return 0
const effectiveFee = this.view.values.get('effectiveFee')
if (effectiveFee !== undefined)
{return effectiveFee}
const maxFee = this.view.values.get('maxFee')
if (maxFee !== undefined)
{return maxFee}
return 0
}

/**
Expand Down
62 changes: 33 additions & 29 deletions src/services/TransactionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/
import {Store} from 'vuex'
import {Account, AccountAddressRestrictionTransaction, AccountLinkTransaction, AccountMetadataTransaction, AccountMosaicRestrictionTransaction, AccountOperationRestrictionTransaction, AddressAliasTransaction, AggregateTransaction, BlockInfo, CosignatureSignedTransaction, CosignatureTransaction, Deadline, HashLockTransaction, LockFundsTransaction, Mosaic, MosaicAddressRestrictionTransaction, MosaicAliasTransaction, MosaicDefinitionTransaction, MosaicGlobalRestrictionTransaction, MosaicMetadataTransaction, MosaicSupplyChangeTransaction, MultisigAccountModificationTransaction, NamespaceMetadataTransaction, NamespaceRegistrationTransaction, PublicAccount, SecretLockTransaction, SecretProofTransaction, SignedTransaction, Transaction, TransactionType, TransferTransaction, UInt64} from 'symbol-sdk'
import {Account, AccountAddressRestrictionTransaction, AccountLinkTransaction, AccountMetadataTransaction, AccountMosaicRestrictionTransaction, AccountOperationRestrictionTransaction, AddressAliasTransaction, AggregateTransaction, BlockInfo, CosignatureSignedTransaction, CosignatureTransaction, Deadline, HashLockTransaction, LockFundsTransaction, Mosaic, MosaicAddressRestrictionTransaction, MosaicAliasTransaction, MosaicDefinitionTransaction, MosaicGlobalRestrictionTransaction, MosaicId, MosaicMetadataTransaction, MosaicSupplyChangeTransaction, MultisigAccountModificationTransaction, NamespaceMetadataTransaction, NamespaceRegistrationTransaction, NetworkType, PublicAccount, SecretLockTransaction, SecretProofTransaction, SignedTransaction, Transaction, TransactionType, TransferTransaction, UInt64} from 'symbol-sdk'
// internal dependencies
import {AbstractService} from './AbstractService'
import {AccountModel} from '@/core/database/entities/AccountModel'
Expand Down Expand Up @@ -166,19 +166,13 @@ export class TransactionService extends AbstractService {
const height = transaction.transactionInfo ? transaction.transactionInfo.height : undefined
const block: BlockInfo = !height ? undefined : knownBlocks.find(k => k.height.equals(height))

const isAggregate = [
TransactionType.AGGREGATE_BONDED,
TransactionType.AGGREGATE_COMPLETE,
].includes(transaction.type)

// - set helper fields
view.values.set('isIncoming', false)
view.values.set('hasBlockInfo', undefined !== block)

// - initialize fee fields
view.values.set('maxFee', isAggregate ? 0 : transaction.maxFee.compact())
view.values.set('effectiveFee', 0)
if (!isAggregate && view.values.get('hasBlockInfo')) {
view.values.set('maxFee', transaction.maxFee.compact())
if (block) {
view.values.set('effectiveFee', transaction.size * block.feeMultiplier)
}

Expand All @@ -197,14 +191,14 @@ export class TransactionService extends AbstractService {
* transactions \a aggregateTx
*
* @param {SignedTransaction} aggregateTx
* @param {UInt64} the lock max fee.
* @return {LockFundsTransaction}
*/
public createHashLockTransaction(aggregateTx: SignedTransaction): LockFundsTransaction {
public createHashLockTransaction(aggregateTx: SignedTransaction, maxFee: UInt64): LockFundsTransaction {
// - shortcuts
const networkMosaic = this.$store.getters['mosaic/networkMosaic']
const networkType = this.$store.getters['network/networkType']
const networkConfiguration = this.$store.getters['network/networkConfiguration'] as NetworkConfigurationModel
const defaultFee = this.$store.getters['app/defaultFee']
const networkMosaic: MosaicId = this.$store.getters['mosaic/networkMosaic']
const networkType: NetworkType = this.$store.getters['network/networkType']
const networkConfiguration: NetworkConfigurationModel = this.$store.getters['network/networkConfiguration']

// - create hash lock
return LockFundsTransaction.create(
Expand All @@ -216,7 +210,7 @@ export class TransactionService extends AbstractService {
UInt64.fromUint(1000), // duration=1000
aggregateTx,
networkType,
UInt64.fromUint(defaultFee),
maxFee,
)
}

Expand Down Expand Up @@ -251,10 +245,9 @@ export class TransactionService extends AbstractService {
*/
public signStagedTransactions(account: Account): SignedTransaction[] {
// - shortcuts
const transactions = this.$store.getters['account/stagedTransactions']
const generationHash = this.$store.getters['network/generationHash']
const transactions: Transaction[] = this.$store.getters['account/stagedTransactions']
const generationHash: string = this.$store.getters['network/generationHash']
const signedTransactions = []

// - iterate transaction that are "on stage"
for (let i = 0, m = transactions.length; i < m; i ++) {
// - read transaction from stage
Expand Down Expand Up @@ -287,20 +280,26 @@ export class TransactionService extends AbstractService {
*/
public signAggregateStagedTransactions(account: Account): SignedTransaction[] {
// - shortcuts
const networkType = this.$store.getters['network/networkType']
const transactions = this.$store.getters['account/stagedTransactions']
const generationHash = this.$store.getters['network/generationHash']
const defaultFee = this.$store.getters['app/defaultFee']
const networkType: NetworkType = this.$store.getters['network/networkType']
const transactions: Transaction[] = this.$store.getters['account/stagedTransactions']
const generationHash: string = this.$store.getters['network/generationHash']
const signedTransactions = []

if (!transactions.length){
return signedTransactions
}

// - aggregate staged transactions
const maxFee = transactions.sort((a, b) => a.maxFee.compare(b.maxFee))[0].maxFee

// - aggregate staged transactions
const aggregateTx = AggregateTransaction.createComplete(
Deadline.create(),
// - format as `InnerTransaction`
transactions.map(t => t.toAggregate(account.publicAccount)),
networkType,
[],
UInt64.fromUint(defaultFee),
maxFee,
)

// - sign aggregate transaction
Expand Down Expand Up @@ -337,25 +336,30 @@ export class TransactionService extends AbstractService {
cosignatoryAccount: Account,
): SignedTransaction[] {
// - shortcuts
const networkType = this.$store.getters['network/networkType']
const transactions = this.$store.getters['account/stagedTransactions']
const generationHash = this.$store.getters['network/generationHash']
const defaultFee = this.$store.getters['app/defaultFee']
const networkType: NetworkType = this.$store.getters['network/networkType']
const transactions: Transaction[] = this.$store.getters['account/stagedTransactions']
const generationHash: string = this.$store.getters['network/generationHash']
const signedTransactions = []

if (!transactions.length){
return signedTransactions
}

// - aggregate staged transactions
const maxFee = transactions.sort((a, b) => a.maxFee.compare(b.maxFee))[0].maxFee

const aggregateTx = AggregateTransaction.createBonded(
Deadline.create(),
// - format as `InnerTransaction`
transactions.map(t => t.toAggregate(multisigAccount)),
networkType,
[],
UInt64.fromUint(defaultFee),
maxFee,
)

// - sign aggregate transaction and create lock
const signedTx = cosignatoryAccount.sign(aggregateTx, generationHash)
const hashLock = this.createHashLockTransaction(signedTx)
const hashLock = this.createHashLockTransaction(signedTx, maxFee)

// - sign hash lock and push
const signedLock = cosignatoryAccount.sign(hashLock, generationHash)
Expand Down
2 changes: 1 addition & 1 deletion src/store/Account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ export default {
* skipTransactions: boolean,
* }
*/
async initialize({commit, dispatch, getters}, {address}) {
async initialize({commit, getters}, {address}) {
const callback = async () => {
if (!address || !address.length) return
commit('setInitialized', true)
Expand Down

0 comments on commit 5b5da90

Please sign in to comment.