-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cc6a62b
commit 5af4215
Showing
10 changed files
with
139 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,2 @@ | ||
export * from './generateNonce'; | ||
export * from './mergeSignatures'; | ||
export * from './readPlutusData'; | ||
export * from './readTransaction'; |
This file was deleted.
Oops, something went wrong.
260 changes: 130 additions & 130 deletions
260
packages/module/src/core/CPS-009.ts → packages/module/src/core/CPS0009.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,130 +1,130 @@ | ||
import type { Quantity, UTxO, Unit } from '@mesh/common/types'; | ||
|
||
export const selectUtxos = ( | ||
inputs: UTxO[], | ||
requiredAssets: Map<Unit, Quantity>, | ||
threshold: Quantity | ||
): UTxO[] => { | ||
const totalRequiredAssets = new Map<Unit, Quantity>(requiredAssets); | ||
totalRequiredAssets.set( | ||
'lovelace', | ||
String(Number(totalRequiredAssets.get('lovelace')) + Number(threshold)) | ||
); | ||
const utxoMap = new Map<number, UTxO>(); | ||
for (let i = 0; i < inputs.length; i++) { | ||
utxoMap.set(i, inputs[i]); | ||
} | ||
const selectedInputs = new Set<number>(); | ||
const onlyLovelace = new Set<number>(); | ||
const singletons = new Set<number>(); | ||
const pairs = new Set<number>(); | ||
const rest = new Set<number>(); | ||
for (let i = 0; i < inputs.length; i++) { | ||
switch (inputs[i].output.amount.length) { | ||
case 1: { | ||
onlyLovelace.add(i); | ||
break; | ||
} | ||
case 2: { | ||
singletons.add(i); | ||
break; | ||
} | ||
case 3: { | ||
pairs.add(i); | ||
break; | ||
} | ||
default: { | ||
rest.add(i); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
const addUtxoWithAssetAmount = ( | ||
inputIndex: number, | ||
assetUnit: string, | ||
set: Set<number> | ||
) => { | ||
const utxo = utxoMap.get(inputIndex); | ||
if (!utxo) return; | ||
|
||
const amount = getAssetAmount(utxo, assetUnit); | ||
if (Number(amount) > 0) { | ||
selectedInputs.add(inputIndex); | ||
set.delete(inputIndex); | ||
for (const asset of utxo.output.amount) { | ||
totalRequiredAssets.set( | ||
asset.unit, | ||
String( | ||
Number(totalRequiredAssets.get(asset.unit)) - Number(asset.quantity) | ||
) | ||
); | ||
} | ||
} | ||
}; | ||
|
||
for (const assetUnit of totalRequiredAssets.keys()) { | ||
if (assetUnit == 'lovelace') continue; | ||
for (const inputIndex of singletons) { | ||
const assetRequired = totalRequiredAssets.get(assetUnit); | ||
if (!assetRequired || Number(assetRequired) <= 0) break; | ||
addUtxoWithAssetAmount(inputIndex, assetUnit, singletons); | ||
} | ||
|
||
for (const inputIndex of pairs) { | ||
const assetRequired = totalRequiredAssets.get(assetUnit); | ||
if (!assetRequired || Number(assetRequired) <= 0) break; | ||
addUtxoWithAssetAmount(inputIndex, assetUnit, pairs); | ||
} | ||
|
||
for (const inputIndex of rest) { | ||
const assetRequired = totalRequiredAssets.get(assetUnit); | ||
if (!assetRequired || Number(assetRequired) <= 0) break; | ||
addUtxoWithAssetAmount(inputIndex, assetUnit, rest); | ||
} | ||
} | ||
|
||
for (const inputIndex of onlyLovelace) { | ||
const assetRequired = totalRequiredAssets.get('lovelace'); | ||
if (!assetRequired || Number(assetRequired) <= 0) break; | ||
addUtxoWithAssetAmount(inputIndex, 'lovelace', onlyLovelace); | ||
} | ||
|
||
for (const inputIndex of singletons) { | ||
const assetRequired = totalRequiredAssets.get('lovelace'); | ||
if (!assetRequired || Number(assetRequired) <= 0) break; | ||
addUtxoWithAssetAmount(inputIndex, 'lovelace', singletons); | ||
} | ||
|
||
for (const inputIndex of pairs) { | ||
const assetRequired = totalRequiredAssets.get('lovelace'); | ||
if (!assetRequired || Number(assetRequired) <= 0) break; | ||
addUtxoWithAssetAmount(inputIndex, 'lovelace', pairs); | ||
} | ||
|
||
for (const inputIndex of rest) { | ||
const assetRequired = totalRequiredAssets.get('lovelace'); | ||
if (!assetRequired || Number(assetRequired) <= 0) break; | ||
addUtxoWithAssetAmount(inputIndex, 'lovelace', rest); | ||
} | ||
|
||
for (const assetUnit of totalRequiredAssets.keys()) { | ||
if (Number(totalRequiredAssets.get(assetUnit)) > 0) return []; | ||
} | ||
|
||
const selectedUtxos: UTxO[] = []; | ||
for (const inputIndex of selectedInputs) { | ||
const utxo = utxoMap.get(inputIndex); | ||
if (utxo) { | ||
selectedUtxos.push(utxo); | ||
} | ||
} | ||
return selectedUtxos; | ||
}; | ||
|
||
const getAssetAmount = (utxo: UTxO, assetUnit: string): string => { | ||
for (const utxoAsset of utxo.output.amount) { | ||
if (utxoAsset.unit == assetUnit) return utxoAsset.quantity; | ||
} | ||
return '0'; | ||
}; | ||
import type { Quantity, UTxO, Unit } from '@mesh/common/types'; | ||
|
||
export const selectUtxos = ( | ||
inputs: UTxO[], | ||
requiredAssets: Map<Unit, Quantity>, | ||
threshold: Quantity | ||
): UTxO[] => { | ||
const totalRequiredAssets = new Map<Unit, Quantity>(requiredAssets); | ||
totalRequiredAssets.set( | ||
'lovelace', | ||
String(Number(totalRequiredAssets.get('lovelace')) + Number(threshold)) | ||
); | ||
const utxoMap = new Map<number, UTxO>(); | ||
for (let i = 0; i < inputs.length; i++) { | ||
utxoMap.set(i, inputs[i]); | ||
} | ||
const selectedInputs = new Set<number>(); | ||
const onlyLovelace = new Set<number>(); | ||
const singletons = new Set<number>(); | ||
const pairs = new Set<number>(); | ||
const rest = new Set<number>(); | ||
for (let i = 0; i < inputs.length; i++) { | ||
switch (inputs[i].output.amount.length) { | ||
case 1: { | ||
onlyLovelace.add(i); | ||
break; | ||
} | ||
case 2: { | ||
singletons.add(i); | ||
break; | ||
} | ||
case 3: { | ||
pairs.add(i); | ||
break; | ||
} | ||
default: { | ||
rest.add(i); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
const addUtxoWithAssetAmount = ( | ||
inputIndex: number, | ||
assetUnit: string, | ||
set: Set<number> | ||
) => { | ||
const utxo = utxoMap.get(inputIndex); | ||
if (!utxo) return; | ||
|
||
const amount = getAssetAmount(utxo, assetUnit); | ||
if (Number(amount) > 0) { | ||
selectedInputs.add(inputIndex); | ||
set.delete(inputIndex); | ||
for (const asset of utxo.output.amount) { | ||
totalRequiredAssets.set( | ||
asset.unit, | ||
String( | ||
Number(totalRequiredAssets.get(asset.unit)) - Number(asset.quantity) | ||
) | ||
); | ||
} | ||
} | ||
}; | ||
|
||
for (const assetUnit of totalRequiredAssets.keys()) { | ||
if (assetUnit == 'lovelace') continue; | ||
for (const inputIndex of singletons) { | ||
const assetRequired = totalRequiredAssets.get(assetUnit); | ||
if (!assetRequired || Number(assetRequired) <= 0) break; | ||
addUtxoWithAssetAmount(inputIndex, assetUnit, singletons); | ||
} | ||
|
||
for (const inputIndex of pairs) { | ||
const assetRequired = totalRequiredAssets.get(assetUnit); | ||
if (!assetRequired || Number(assetRequired) <= 0) break; | ||
addUtxoWithAssetAmount(inputIndex, assetUnit, pairs); | ||
} | ||
|
||
for (const inputIndex of rest) { | ||
const assetRequired = totalRequiredAssets.get(assetUnit); | ||
if (!assetRequired || Number(assetRequired) <= 0) break; | ||
addUtxoWithAssetAmount(inputIndex, assetUnit, rest); | ||
} | ||
} | ||
|
||
for (const inputIndex of onlyLovelace) { | ||
const assetRequired = totalRequiredAssets.get('lovelace'); | ||
if (!assetRequired || Number(assetRequired) <= 0) break; | ||
addUtxoWithAssetAmount(inputIndex, 'lovelace', onlyLovelace); | ||
} | ||
|
||
for (const inputIndex of singletons) { | ||
const assetRequired = totalRequiredAssets.get('lovelace'); | ||
if (!assetRequired || Number(assetRequired) <= 0) break; | ||
addUtxoWithAssetAmount(inputIndex, 'lovelace', singletons); | ||
} | ||
|
||
for (const inputIndex of pairs) { | ||
const assetRequired = totalRequiredAssets.get('lovelace'); | ||
if (!assetRequired || Number(assetRequired) <= 0) break; | ||
addUtxoWithAssetAmount(inputIndex, 'lovelace', pairs); | ||
} | ||
|
||
for (const inputIndex of rest) { | ||
const assetRequired = totalRequiredAssets.get('lovelace'); | ||
if (!assetRequired || Number(assetRequired) <= 0) break; | ||
addUtxoWithAssetAmount(inputIndex, 'lovelace', rest); | ||
} | ||
|
||
for (const assetUnit of totalRequiredAssets.keys()) { | ||
if (Number(totalRequiredAssets.get(assetUnit)) > 0) return []; | ||
} | ||
|
||
const selectedUtxos: UTxO[] = []; | ||
for (const inputIndex of selectedInputs) { | ||
const utxo = utxoMap.get(inputIndex); | ||
if (utxo) { | ||
selectedUtxos.push(utxo); | ||
} | ||
} | ||
return selectedUtxos; | ||
}; | ||
|
||
const getAssetAmount = (utxo: UTxO, assetUnit: string): string => { | ||
for (const utxoAsset of utxo.output.amount) { | ||
if (utxoAsset.unit == assetUnit) return utxoAsset.quantity; | ||
} | ||
return '0'; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './signatures'; |
7 changes: 3 additions & 4 deletions
7
...ule/src/common/helpers/mergeSignatures.ts → packages/module/src/serializer/signatures.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters