Skip to content

Commit

Permalink
fix: Update post conditions input in transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
janniks committed Oct 23, 2024
1 parent 2b264b5 commit f239c21
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 deletions.
19 changes: 9 additions & 10 deletions packages/transactions/src/builders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ export type BaseContractDeployOptions = {
* transfered assets */
postConditionMode?: PostConditionModeName | PostConditionMode;
/** a list of post conditions to add to the transaction */
postConditions?: PostConditionWire[];
postConditions?: PostCondition[] | PostConditionWire[];
/** set to true if another account is sponsoring the transaction (covering the transaction fee) */
sponsored?: boolean;
} & NetworkClientParam;
Expand Down Expand Up @@ -370,12 +370,10 @@ export async function makeUnsignedContractDeploy(
? createSponsoredAuth(spendingCondition)
: createStandardAuth(spendingCondition);

const postConditions: PostConditionWire[] = [];
if (options.postConditions && options.postConditions.length > 0) {
options.postConditions.forEach(postCondition => {
postConditions.push(postCondition);
});
}
const postConditions: PostConditionWire[] = (options.postConditions ?? []).map(pc => {
if (typeof pc.type === 'string') return postConditionToWire(pc);
return pc;

Check warning on line 375 in packages/transactions/src/builders.ts

View check run for this annotation

Codecov / codecov/patch

packages/transactions/src/builders.ts#L375

Added line #L375 was not covered by tests
});
const lpPostConditions = createLPList(postConditions);

const transaction = new StacksTransactionWire({
Expand Down Expand Up @@ -522,9 +520,10 @@ export async function makeUnsignedContractCall(
? createSponsoredAuth(spendingCondition)
: createStandardAuth(spendingCondition);

const postConditions = (options.postConditions ?? []).map(
postConditionToWire as (post: PostCondition) => PostConditionWire
);
const postConditions: PostConditionWire[] = (options.postConditions ?? []).map(pc => {
if (typeof pc.type === 'string') return postConditionToWire(pc);
return pc;

Check warning on line 525 in packages/transactions/src/builders.ts

View check run for this annotation

Codecov / codecov/patch

packages/transactions/src/builders.ts#L525

Added line #L525 was not covered by tests
});
const lpPostConditions = createLPList(postConditions);

const transaction = new StacksTransactionWire({
Expand Down
4 changes: 2 additions & 2 deletions packages/transactions/src/postcondition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
StacksWireType,
parseAssetString,
parsePrincipalString,
serializePostCondition,
serializePostConditionWire,
} from './wire';

const FUNGIBLE_COMPARATOR_MAPPING = {
Expand Down Expand Up @@ -90,7 +90,7 @@ export function postConditionToWire(postcondition: PostCondition): PostCondition
*/
export function postConditionToHex(postcondition: PostCondition): string {
const wire = postConditionToWire(postcondition);
return serializePostCondition(wire);
return serializePostConditionWire(wire);
}

/** @internal */
Expand Down
9 changes: 5 additions & 4 deletions packages/transactions/src/wire/serialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export function serializeStacksWireBytes(wire: StacksWire): Uint8Array {
case StacksWireType.Asset:
return serializeAssetBytes(wire);
case StacksWireType.PostCondition:
return serializePostConditionBytes(wire);
return serializePostConditionWireBytes(wire);
case StacksWireType.PublicKey:
return serializePublicKeyBytes(wire);
case StacksWireType.LengthPrefixedList:
Expand Down Expand Up @@ -341,11 +341,12 @@ export function deserializeLPList(
return createLPList(l, lengthPrefixBytes);
}

export function serializePostCondition(postCondition: PostConditionWire): string {
return bytesToHex(serializePostConditionBytes(postCondition));
export function serializePostConditionWire(postCondition: PostConditionWire): string {
return bytesToHex(serializePostConditionWireBytes(postCondition));
}

/** @internal */
export function serializePostConditionBytes(postCondition: PostConditionWire): Uint8Array {
export function serializePostConditionWireBytes(postCondition: PostConditionWire): Uint8Array {
const bytesArray = [];
bytesArray.push(postCondition.conditionType);
bytesArray.push(serializePrincipalBytes(postCondition.principal));
Expand Down
6 changes: 3 additions & 3 deletions packages/transactions/tests/builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import {
publicKeyIsCompressed,
publicKeyToHex,
serializePayloadBytes,
serializePostConditionBytes,
serializePostConditionWireBytes,
serializePublicKeyBytes,
} from '../src';
import { BytesReader } from '../src/BytesReader';
Expand Down Expand Up @@ -2146,11 +2146,11 @@ test('Post-conditions with amount larger than 8 bytes throw an error', () => {
};

expect(() => {
serializePostConditionBytes(postConditionToWire(stxPc));
serializePostConditionWireBytes(postConditionToWire(stxPc));
}).toThrowError('The post-condition amount may not be larger than 8 bytes');

expect(() => {
serializePostConditionBytes(postConditionToWire(fungiblePc));
serializePostConditionWireBytes(postConditionToWire(fungiblePc));
}).toThrowError('The post-condition amount may not be larger than 8 bytes');
});

Expand Down

0 comments on commit f239c21

Please sign in to comment.