|
| 1 | +<template> |
| 2 | + <div> |
| 3 | + <div class="utxos"> |
| 4 | + <tx-history-value |
| 5 | + v-for="(amount, assetId) in valList" |
| 6 | + :key="assetId" |
| 7 | + :amount="amount" |
| 8 | + :type="type" |
| 9 | + :asset-id="assetId" |
| 10 | + :is-income="false" |
| 11 | + :operation-color="operationColor" |
| 12 | + :operation-direction="operationDirection" |
| 13 | + ></tx-history-value> |
| 14 | + <div class="nfts"> |
| 15 | + <div v-for="(groupIDs, assetID) in nftGroups" :key="assetID"> |
| 16 | + <tx-history-nft-family-group |
| 17 | + v-for="(payloads, id) in groupIDs" |
| 18 | + :key="id" |
| 19 | + :payloads="payloads" |
| 20 | + :assetID="assetID" |
| 21 | + class="group" |
| 22 | + ></tx-history-nft-family-group> |
| 23 | + </div> |
| 24 | + </div> |
| 25 | + </div> |
| 26 | + </div> |
| 27 | +</template> |
| 28 | +<script lang="ts"> |
| 29 | +import { Vue, Component, Prop } from 'vue-property-decorator' |
| 30 | +import { ITransactionData, UTXO } from '@/store/modules/history/types' |
| 31 | +import { TransactionValueDict } from '@/components/SidePanels/types' |
| 32 | +import { PayloadBase, PayloadTypes } from 'avalanche/dist/utils' |
| 33 | +import { Buffer } from 'avalanche' |
| 34 | +import { WalletType } from '@/store/types' |
| 35 | +import { avm, pChain } from '@/AVA' |
| 36 | +
|
| 37 | +import TxHistoryValue from '@/components/SidePanels/TxHistoryValue.vue' |
| 38 | +import TxHistoryNftFamilyGroup from '@/components/SidePanels/TxHistoryNftFamilyGroup.vue' |
| 39 | +
|
| 40 | +let payloadtypes = PayloadTypes.getInstance() |
| 41 | +
|
| 42 | +@Component({ |
| 43 | + components: { |
| 44 | + TxHistoryValue, |
| 45 | + TxHistoryNftFamilyGroup, |
| 46 | + }, |
| 47 | +}) |
| 48 | +export default class BaseTx extends Vue { |
| 49 | + @Prop() transaction!: ITransactionData |
| 50 | +
|
| 51 | + get addresses() { |
| 52 | + let wallet: WalletType = this.$store.state.activeWallet |
| 53 | + if (!wallet) return [] |
| 54 | +
|
| 55 | + return wallet.getHistoryAddresses() |
| 56 | + } |
| 57 | +
|
| 58 | + get addrsRaw() { |
| 59 | + let addrs: string[] = this.addresses |
| 60 | + return addrs.map((addr) => addr.split('-')[1]) |
| 61 | + } |
| 62 | +
|
| 63 | + get type() { |
| 64 | + return this.transaction.type |
| 65 | + } |
| 66 | +
|
| 67 | + // What did I lose? |
| 68 | + get inValues() { |
| 69 | + let addrs: string[] = this.addresses |
| 70 | + let addrsRaw = addrs.map((addr) => addr.split('-')[1]) |
| 71 | +
|
| 72 | + let ins = this.transaction.inputs |
| 73 | + let res: TransactionValueDict = {} // asset id -> value dict |
| 74 | +
|
| 75 | + // if empty |
| 76 | + if (!ins) { |
| 77 | + return res |
| 78 | + } |
| 79 | +
|
| 80 | + ins.forEach((inputUtxo) => { |
| 81 | + const include = this.includeUtxo(inputUtxo.output, true) |
| 82 | + const assetId = inputUtxo.output.assetID |
| 83 | + const amt = inputUtxo.output.amount |
| 84 | +
|
| 85 | + if (include) { |
| 86 | + if (res[assetId]) { |
| 87 | + res[assetId] += parseInt(amt) |
| 88 | + } else { |
| 89 | + res[assetId] = parseInt(amt) |
| 90 | + } |
| 91 | + } |
| 92 | + }) |
| 93 | +
|
| 94 | + return res |
| 95 | + } |
| 96 | +
|
| 97 | + includeUtxo(utxo: UTXO, isInput?: boolean) { |
| 98 | + const isIncludes = |
| 99 | + utxo.addresses.filter((value) => this.addrsRaw.includes(value)).length > 0 |
| 100 | +
|
| 101 | + switch (this.transaction.type) { |
| 102 | + case 'export': |
| 103 | + return utxo.chainID === avm.getBlockchainID() |
| 104 | + case 'pvm_export': |
| 105 | + return utxo.chainID === pChain.getBlockchainID() |
| 106 | + case 'pvm_import': |
| 107 | + case 'import': |
| 108 | + if (isInput) return false |
| 109 | + return isIncludes |
| 110 | + case 'add_validator': |
| 111 | + case 'add_delegator': |
| 112 | + return !isInput && utxo.stake |
| 113 | + case 'operation': |
| 114 | + // if no payload it is avax |
| 115 | + // check if it is from wallet |
| 116 | + if (!utxo.payload && !isIncludes) return false |
| 117 | + return true |
| 118 | + // default just return original logic |
| 119 | + // might need to be changed in the future as |
| 120 | + // more tx types are added |
| 121 | + case 'base': |
| 122 | + default: |
| 123 | + return isIncludes |
| 124 | + } |
| 125 | +
|
| 126 | + return false |
| 127 | + } |
| 128 | +
|
| 129 | + // what did I gain? |
| 130 | + get outValues() { |
| 131 | + let addrs: string[] = this.addresses |
| 132 | + let addrsRaw = addrs.map((addr) => addr.split('-')[1]) |
| 133 | + let outs = this.transaction.outputs |
| 134 | + let res: TransactionValueDict = {} // asset id -> value dict |
| 135 | +
|
| 136 | + // if empty |
| 137 | + if (!outs) { |
| 138 | + return res |
| 139 | + } |
| 140 | +
|
| 141 | + outs.forEach((utxoOut) => { |
| 142 | + let utxoAddrs = utxoOut.addresses |
| 143 | + let assetId = utxoOut.assetID |
| 144 | + let amt = utxoOut.amount |
| 145 | +
|
| 146 | + const include = this.includeUtxo(utxoOut) |
| 147 | +
|
| 148 | + if (include) { |
| 149 | + if (res[assetId]) { |
| 150 | + res[assetId] += parseInt(amt) |
| 151 | + } else { |
| 152 | + res[assetId] = parseInt(amt) |
| 153 | + } |
| 154 | + } |
| 155 | + }) |
| 156 | +
|
| 157 | + return res |
| 158 | + } |
| 159 | +
|
| 160 | + get valList() { |
| 161 | + let ins = this.inValues |
| 162 | + let outs = this.outValues |
| 163 | + let res = JSON.parse(JSON.stringify(outs)) |
| 164 | +
|
| 165 | + for (var assetId in ins) { |
| 166 | + let inAmount = ins[assetId] || 0 |
| 167 | + if (res[assetId]) { |
| 168 | + res[assetId] -= inAmount |
| 169 | + } else { |
| 170 | + res[assetId] = -1 * inAmount |
| 171 | + } |
| 172 | + } |
| 173 | +
|
| 174 | + return res |
| 175 | + } |
| 176 | +
|
| 177 | + get nftGroups() { |
| 178 | + let addrs: string[] = this.addresses |
| 179 | + let addrsRaw = addrs.map((addr) => addr.split('-')[1]) |
| 180 | +
|
| 181 | + let ins = this.transaction.inputs || {} |
| 182 | + let outs = this.transaction.outputs || {} |
| 183 | + let res: { [key in string]: { [key in string]: PayloadBase[] } } = {} |
| 184 | +
|
| 185 | + // res = { |
| 186 | + // 'asset id': { ['group id']: 'payload' }, |
| 187 | + // } |
| 188 | +
|
| 189 | + const pushPayload = (rawPayload: string, assetID: string, groupID: number) => { |
| 190 | + let payload = Buffer.from(rawPayload, 'base64') |
| 191 | + payload = Buffer.concat([new Buffer(4).fill(payload.length), payload]) |
| 192 | +
|
| 193 | + try { |
| 194 | + let typeId = payloadtypes.getTypeID(payload) |
| 195 | + let pl: Buffer = payloadtypes.getContent(payload) |
| 196 | + let payloadbase: PayloadBase = payloadtypes.select(typeId, pl) |
| 197 | +
|
| 198 | + if (res[assetID]) { |
| 199 | + if (res[assetID][groupID]) { |
| 200 | + res[assetID][groupID].push(payloadbase) |
| 201 | + } else { |
| 202 | + res[assetID] = { |
| 203 | + [groupID]: [payloadbase], |
| 204 | + } |
| 205 | + } |
| 206 | + } else { |
| 207 | + res[assetID] = { |
| 208 | + [groupID]: [payloadbase], |
| 209 | + } |
| 210 | + } |
| 211 | + } catch (e) { |
| 212 | + // console.error(e) |
| 213 | + } |
| 214 | + } |
| 215 | +
|
| 216 | + ins.forEach((inputUtxo) => { |
| 217 | + const groupID = inputUtxo.output.groupID |
| 218 | + const assetID = inputUtxo.output.assetID |
| 219 | +
|
| 220 | + if (inputUtxo.output.payload) { |
| 221 | + pushPayload(inputUtxo.output.payload, assetID, groupID) |
| 222 | + } |
| 223 | + }) |
| 224 | + outs.forEach((utxoOut) => { |
| 225 | + let groupID = utxoOut.groupID |
| 226 | + let assetID = utxoOut.assetID |
| 227 | +
|
| 228 | + if (utxoOut.payload) { |
| 229 | + pushPayload(utxoOut.payload, assetID, groupID) |
| 230 | + } |
| 231 | + }) |
| 232 | +
|
| 233 | + return res |
| 234 | + } |
| 235 | +
|
| 236 | + get operationDirection() { |
| 237 | + if (this.type !== 'operation') return 'N/A' |
| 238 | +
|
| 239 | + let addrs: string[] = this.addresses |
| 240 | + let addrsRaw = addrs.map((addr) => addr.split('-')[1]) |
| 241 | +
|
| 242 | + const isFromWallet = this.transaction.inputs.find((input) => { |
| 243 | + return input.output.addresses.find((value) => { |
| 244 | + return addrsRaw.includes(value) |
| 245 | + }) |
| 246 | + }) |
| 247 | +
|
| 248 | + return isFromWallet ? 'Sent' : 'Received' |
| 249 | + } |
| 250 | +
|
| 251 | + get operationColor() { |
| 252 | + return this.operationDirection === 'Received' ? 'success' : 'sent' |
| 253 | + } |
| 254 | +} |
| 255 | +</script> |
| 256 | +<style scoped lang="scss"> |
| 257 | +.nfts { |
| 258 | + display: flex; |
| 259 | + flex-wrap: wrap; |
| 260 | + margin-top: 5px; |
| 261 | + justify-content: flex-end; |
| 262 | + > div { |
| 263 | + margin-left: 5px; |
| 264 | + } |
| 265 | +} |
| 266 | +</style> |
0 commit comments