Skip to content

Commit

Permalink
Merge pull request #1612 from Emurgo/collateral-tx-support
Browse files Browse the repository at this point in the history
[feature] Handling non-native script txs
  • Loading branch information
stackchain committed Sep 28, 2021
2 parents 0517936 + da631dc commit 44ac048
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 12 deletions.
1 change: 0 additions & 1 deletion src/api/shelley/api.js
Expand Up @@ -126,7 +126,6 @@ export const bulkGetAccountState = async (
config: BackendConfig,
): Promise<AccountStateResponse> => {
const chunks = _.chunk(addresses, config.FETCH_UTXOS_MAX_ADDRESSES)

const responses = await Promise.all(chunks.map((addrs) => getAccountState({addresses: addrs}, config)))
return Object.assign({}, ...responses)
}
Expand Down
12 changes: 12 additions & 0 deletions src/api/shelley/facade.js
Expand Up @@ -118,5 +118,17 @@ export const checkAndFacadeTransactionAsync = async (tx: RawTransaction): Promis
slot: tx.slot,
withdrawals: tx.withdrawals,
certificates: tx.certificates,
validContract: tx.valid_contract,
scriptSize: tx.script_size,
collateralInputs: (tx.collateral_inputs ?? []).map((i) => ({
address: i.address,
amount: i.amount,
assets: (i.assets ?? []).map((a) => ({
amount: a.amount,
assetId: a.assetId,
policyId: a.policyId,
name: a.name,
})),
})),
}
}
3 changes: 3 additions & 0 deletions src/api/types.js
Expand Up @@ -217,6 +217,9 @@ export type RemoteTxInfo = {|
amount: string,
|}>,
+certificates: Array<RemoteCertificateMeta>,
+valid_contract?: boolean,
+script_size?: number,
+collateral_inputs?: Array<RemoteTransactionInput>,
|}

export type RawTransaction = {|
Expand Down
38 changes: 27 additions & 11 deletions src/crypto/processTransactions.js
Expand Up @@ -79,15 +79,26 @@ export const processTxHistoryData = (
): TransactionInfo => {
const _strToDefaultMultiAsset = (amount: string) => strToDefaultMultiAsset(amount, networkId)

// rename to match yoroi extension notation
const utxoInputs = tx.inputs
const utxoOutputs = tx.outputs
const accountingInputs = tx.withdrawals.map((w) => ({
address: w.address,
amount: w.amount,
assets: [],
}))
// const accountingOutpus // eventually
// collateral
const collateral = tx.collateralInputs || []
const isNonNativeScriptExecution = Number(tx.scriptSize) > 0 || collateral.length > 0
const isInvalidScriptExecution = isNonNativeScriptExecution && !tx.validContract
// TODO: check if is it possible to have not owned address in collateral inputs
// NOTE: only add the tx inputs to account it if the execution has failed
const ownUtxoCollateralInputs = isInvalidScriptExecution
? collateral.filter(({address}) => ownAddresses.includes(address))
: []

// NOTE: will ignore the inputs and outputs if the tx script execution failed
const utxoInputs = isInvalidScriptExecution ? [] : tx.inputs
const utxoOutputs = isInvalidScriptExecution ? [] : tx.outputs
const accountingInputs = isInvalidScriptExecution
? []
: tx.withdrawals.map((w) => ({
address: w.address,
amount: w.amount,
assets: [],
}))

const ownUtxoInputs = utxoInputs.filter(({address}) => ownAddresses.includes(address))
const ownUtxoOutputs = utxoOutputs.filter(({address}) => ownAddresses.includes(address))
Expand All @@ -113,7 +124,7 @@ export const processTxHistoryData = (
return _strToDefaultMultiAsset('0')
})()

const unifiedInputs = [...utxoInputs, ...accountingInputs]
const unifiedInputs = [...utxoInputs, ...accountingInputs, ...ownUtxoCollateralInputs]
const unifiedOutputs = [
...utxoOutputs,
// ...accountingOutpus,
Expand Down Expand Up @@ -187,7 +198,12 @@ export const processTxHistoryData = (
let fee
const remoteFee = tx.fee != null ? _strToDefaultMultiAsset(new BigNumber(tx.fee).times(-1).toString()) : null
let direction
if (isIntraWallet) {
if (isInvalidScriptExecution) {
direction = TRANSACTION_DIRECTION.SELF
amount = brutto
// NOTE: the collateral is the fee when it has failed
fee = null
} else if (isIntraWallet) {
direction = TRANSACTION_DIRECTION.SELF
amount = _strToDefaultMultiAsset('0')
fee = remoteFee ?? totalFee
Expand Down
3 changes: 3 additions & 0 deletions src/types/HistoryTransaction.js
Expand Up @@ -119,4 +119,7 @@ export type Transaction = {|
amount: string,
|}>,
certificates: Array<RemoteCertificateMeta>,
+validContract?: boolean,
+scriptSize?: number,
+collateralInputs?: Array<{address: string, amount: string, assets: Array<BaseAsset>}>,
|}

0 comments on commit 44ac048

Please sign in to comment.