Skip to content

Commit

Permalink
refactor: review comments and simplify builder in spots
Browse files Browse the repository at this point in the history
  • Loading branch information
erictaylor committed Sep 9, 2024
1 parent c56dd6b commit 35c2109
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 35 deletions.
43 changes: 10 additions & 33 deletions src/vms/pvm/etna-builder/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,7 @@ export const newBaseTx: TxBuilderFn<NewBaseTxProps> = (
[...fromAddressesBytes],
options,
);
const toBurn = new Map<string, bigint>([
[context.avaxAssetID, context.baseTxFee],
]);
const toBurn = new Map<string, bigint>();

outputs.forEach((out) => {
const assetId = out.assetId.value();
Expand Down Expand Up @@ -250,8 +248,7 @@ export const newImportTx: TxBuilderFn<NewImportTxProps> = (
matchOwners(
utxo.getOutputOwners(),
fromAddresses,
// TODO: Verify this.
options?.minIssuanceTime ?? BigInt(Math.ceil(Date.now() / 1_000)),
defaultedOptions.minIssuanceTime,
) || {};

if (inputSigIndices === undefined) {
Expand All @@ -275,6 +272,10 @@ export const newImportTx: TxBuilderFn<NewImportTxProps> = (
importedAmounts[assetId] = (importedAmounts[assetId] ?? 0n) + out.amount();
}

if (importedInputs.length === 0) {
throw new Error('no UTXOs available to import');
}

const importedAvax = importedAmounts[context.avaxAssetID];

importedInputs.sort(TransferableInput.compare);
Expand All @@ -285,10 +286,6 @@ export const newImportTx: TxBuilderFn<NewImportTxProps> = (
fromAddressesBytes,
);

if (importedInputs.length === 0) {
throw new Error('no UTXOs available to import');
}

const outputs: TransferableOutput[] = [];

for (const [assetID, amount] of Object.entries(importedAmounts)) {
Expand Down Expand Up @@ -321,18 +318,11 @@ export const newImportTx: TxBuilderFn<NewImportTxProps> = (
);

const toBurn = new Map<string, bigint>();
let excessAVAX = 0n;

if (importedAvax && importedAvax < context.baseTxFee) {
toBurn.set(context.avaxAssetID, context.baseTxFee - importedAvax);
} else {
excessAVAX = importedAvax - context.baseTxFee;
}

const [error, spendResults] = spend(
{
complexity,
excessAVAX,
excessAVAX: importedAvax,
fromAddresses,
ownerOverride: OutputOwners.fromNative(toAddresses, locktime, threshold),
spendOptions: defaultedOptions,
Expand Down Expand Up @@ -390,9 +380,7 @@ export const newExportTx: TxBuilderFn<NewExportTxProps> = (
const fromAddresses = addressesFromBytes(fromAddressesBytes);

const defaultedOptions = defaultSpendOptions(fromAddressesBytes, options);
const toBurn = new Map<string, bigint>([
[context.avaxAssetID, context.baseTxFee],
]);
const toBurn = new Map<string, bigint>();

outputs.forEach((output) => {
const assetId = output.assetId.value();
Expand Down Expand Up @@ -493,7 +481,6 @@ export const newCreateSubnetTx: TxBuilderFn<NewCreateSubnetTxProps> = (
excessAVAX: 0n,
fromAddresses: addressesFromBytes(fromAddressesBytes),
spendOptions: defaultedOptions,
toBurn: new Map([[context.avaxAssetID, context.createSubnetTxFee]]),
utxos,
},
context,
Expand Down Expand Up @@ -605,7 +592,6 @@ export const newCreateChainTx: TxBuilderFn<NewCreateChainTxProps> = (
excessAVAX: 0n,
fromAddresses: addressesFromBytes(fromAddressesBytes),
spendOptions: defaultedOptions,
toBurn: new Map([[context.avaxAssetID, context.createBlockchainTxFee]]),
utxos,
},
context,
Expand Down Expand Up @@ -699,7 +685,6 @@ export const newAddSubnetValidatorTx: TxBuilderFn<
excessAVAX: 0n,
fromAddresses: addressesFromBytes(fromAddressesBytes),
spendOptions: defaultedOptions,
toBurn: new Map([[context.avaxAssetID, context.addSubnetValidatorFee]]),
utxos,
},
context,
Expand Down Expand Up @@ -782,7 +767,6 @@ export const newRemoveSubnetValidatorTx: TxBuilderFn<
excessAVAX: 0n,
fromAddresses: addressesFromBytes(fromAddressesBytes),
spendOptions: defaultedOptions,
toBurn: new Map([[context.avaxAssetID, context.baseTxFee]]),
utxos,
},
context,
Expand Down Expand Up @@ -908,10 +892,7 @@ export const newAddPermissionlessValidatorTx: TxBuilderFn<
context,
) => {
const isPrimaryNetwork = subnetId === PrimaryNetworkID.toString();
const fee = isPrimaryNetwork
? context.addPrimaryNetworkValidatorFee
: context.addSubnetValidatorFee;
const toBurn = new Map<string, bigint>([[context.avaxAssetID, fee]]);
const toBurn = new Map<string, bigint>();

const assetId = stakingAssetId ?? context.avaxAssetID;

Expand Down Expand Up @@ -1070,17 +1051,14 @@ export const newAddPermissionlessDelegatorTx: TxBuilderFn<
context,
) => {
const isPrimaryNetwork = subnetId === PrimaryNetworkID.toString();
const fee = isPrimaryNetwork
? context.addPrimaryNetworkDelegatorFee
: context.addSubnetDelegatorFee;

const assetId = stakingAssetId ?? context.avaxAssetID;

// Check if we use correct asset if on primary network
if (isPrimaryNetwork && assetId !== context.avaxAssetID)
throw new Error('Staking asset ID must be AVAX for the primary network.');

const toBurn = new Map<string, bigint>([[context.avaxAssetID, fee]]);
const toBurn = new Map<string, bigint>();
const toStake = new Map<string, bigint>([[assetId, weight]]);

const defaultedOptions = defaultSpendOptions(fromAddressesBytes, options);
Expand Down Expand Up @@ -1221,7 +1199,6 @@ export const newTransferSubnetOwnershipTx: TxBuilderFn<
excessAVAX: 0n,
fromAddresses: addressesFromBytes(fromAddressesBytes),
spendOptions: defaultedOptions,
toBurn: new Map([[context.avaxAssetID, context.baseTxFee]]),
utxos,
},
context,
Expand Down
4 changes: 2 additions & 2 deletions src/vms/pvm/etna-builder/spend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ type SpendProps = Readonly<{
*
* Only unlocked UTXOs are able to be burned here.
*/
toBurn: Map<string, bigint>;
toBurn?: Map<string, bigint>;
/**
* Maps `assetID` to the amount of the asset to spend and place info
* the staked outputs. First locked UTXOs are attempted to be used for
Expand Down Expand Up @@ -191,7 +191,7 @@ export const spend = (
fromAddresses,
ownerOverride: _ownerOverride,
spendOptions,
toBurn,
toBurn = new Map(),
toStake = new Map(),
utxos,
}: SpendProps,
Expand Down

0 comments on commit 35c2109

Please sign in to comment.