Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: transaction template #808

Open
wants to merge 29 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
cbb5490
feat: POC implementation
r4mmer Nov 11, 2024
9755aa8
feat: initial implementation
r4mmer Nov 11, 2024
a342ab7
feat: add support for create token transaction
r4mmer Dec 16, 2024
edb3894
feat: add balance management for created token reference
r4mmer Dec 17, 2024
250105d
tests: instruction tests
r4mmer Dec 20, 2024
90dab94
feat: complete instruction
r4mmer Dec 23, 2024
13efa80
feat: complete instruction
r4mmer Dec 23, 2024
fec87f6
feat: executor tests
r4mmer Dec 26, 2024
fff192b
tests: executor and interpreter tests
r4mmer Dec 26, 2024
d46c571
tests(integration): custom token integration template tests
r4mmer Dec 26, 2024
6f91f9e
chore: linter changes
r4mmer Dec 27, 2024
3a3b9ea
chore: linter changes
r4mmer Dec 27, 2024
30f1764
tests(integration): change injectFunds options
r4mmer Dec 27, 2024
3b2ae37
tests(integration): create token tx have different serialization
r4mmer Dec 27, 2024
7099194
tests(integration): include change outputs in checks
r4mmer Dec 27, 2024
edd8234
tests(integration): change output orderr
r4mmer Dec 27, 2024
d120896
tests(integration): add change value to send operation
r4mmer Dec 27, 2024
a50620a
chore: remove helper types to use inferred types from zod
r4mmer Dec 27, 2024
ee5edf4
tests(integration): add change and complete tests
r4mmer Dec 27, 2024
ab0b78e
tests(integration): wrong complete instruction type
r4mmer Dec 30, 2024
49ad6d3
fix: change would add invalid tokens on transaction
r4mmer Dec 31, 2024
674aa2d
tests(unit): mock balance properly
r4mmer Dec 31, 2024
c7e968c
feat: expose transaction template and helper method on the wallet
r4mmer Jan 7, 2025
61b9502
chore: linter changes
r4mmer Jan 8, 2025
ba4110d
test(integration): increase coverage on integration tests
r4mmer Jan 8, 2025
1d8cb3f
tests(integration): change position of tests
r4mmer Jan 8, 2025
b2cd834
tests(integration): change tx version
r4mmer Jan 8, 2025
d82aa19
feat: make tx signing optional when building with the facade
r4mmer Jan 9, 2025
33591c3
feat: correctly pass the arguments to build template
r4mmer Jan 9, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore: remove helper types to use inferred types from zod
  • Loading branch information
r4mmer committed Dec 27, 2024
commit a50620a844767d982aa47eee14392bb9839aa7fb
10 changes: 4 additions & 6 deletions src/template/transaction/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import { JSONBigInt } from '../../utils/bigint';
import {
TxTemplateInstruction,
TransactionTemplate,
TransactionTemplateType,
TxTemplateInstructionType,
RawInputInstruction,
UtxoSelectInstruction,
AuthoritySelectInstruction,
Expand Down Expand Up @@ -41,28 +39,28 @@ const ConfigInsArgs = ConfigInstruction.omit({ type: true });
const SetVarInsArgs = SetVarInstruction.omit({ type: true });

export class TransactionTemplateBuilder {
template: TransactionTemplateType;
template: z.infer<typeof TransactionTemplate>;

constructor() {
this.template = [];
}

static from(instructions: TransactionTemplateType): TransactionTemplateBuilder {
static from(instructions: z.input<typeof TransactionTemplate>): TransactionTemplateBuilder {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Obs: z.input<T> means that this accepts the "unparsed" instruction.
When the type is z.infer<T> it means the output of the parsed data.

const parsedTemplate = TransactionTemplate.parse(instructions);
const tt = new TransactionTemplateBuilder();
tt.template = parsedTemplate;
return tt;
}

build(): TransactionTemplateType {
build(): z.infer<typeof TransactionTemplate> {
return this.template;
}

export(space: number = 2): string {
return JSONBigInt.stringify(this.template, space);
}

addInstruction(ins: TxTemplateInstructionType): TransactionTemplateBuilder {
addInstruction(ins: z.input<typeof TxTemplateInstruction>): TransactionTemplateBuilder {
this.template.push(TxTemplateInstruction.parse(ins));
return this;
}
Expand Down
3 changes: 1 addition & 2 deletions src/template/transaction/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
ShuffleInstruction,
TokenOutputInstruction,
TxTemplateInstruction,
TxTemplateInstructionType,
UtxoSelectInstruction,
getVariable,
} from './instructions';
Expand All @@ -48,7 +47,7 @@ import { getWalletAddress, getWalletBalance } from './setvarcommands';
export async function runInstruction(
interpreter: ITxTemplateInterpreter,
ctx: TxTemplateContext,
ins: TxTemplateInstructionType
ins: z.infer<typeof TxTemplateInstruction>
) {
const instructionExecutor = findInstructionExecution(ins);
await instructionExecutor(interpreter, ctx, ins);
Expand Down
2 changes: 0 additions & 2 deletions src/template/transaction/instructions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,5 @@ export const TxTemplateInstruction = z.discriminatedUnion('type', [
ConfigInstruction,
SetVarInstruction,
]);
export type TxTemplateInstructionType = z.infer<typeof TxTemplateInstruction>;

export const TransactionTemplate = z.array(TxTemplateInstruction);
export type TransactionTemplateType = z.infer<typeof TransactionTemplate>;
7 changes: 4 additions & 3 deletions src/template/transaction/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
* LICENSE file in the root directory of this source tree.
*/

import { TransactionTemplateType } from './instructions';
import { z } from 'zod';
import { TransactionTemplate } from './instructions';
import { runInstruction } from './executor';
import { TxTemplateContext } from './context';
import {
Expand Down Expand Up @@ -40,10 +41,10 @@ export class WalletTxTemplateInterpreter implements ITxTemplateInterpreter {
this.txCache = {};
}

async build(instructions: TransactionTemplateType, debug: boolean = false): Promise<Transaction> {
async build(instructions: z.infer<typeof TransactionTemplate>, debug: boolean = false): Promise<Transaction> {
const context = new TxTemplateContext(this.wallet.logger, debug);

for (const ins of instructions) {
for (const ins of TransactionTemplate.parse(instructions)) {
await runInstruction(this, context, ins);
}

Expand Down
5 changes: 3 additions & 2 deletions src/template/transaction/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
* LICENSE file in the root directory of this source tree.
*/

import { TransactionTemplateType } from './instructions';
import { TransactionTemplate } from './instructions';
import { TxTemplateContext } from './context';
import { IHistoryTx, ITokenBalance, ITokenData, OutputValueType } from '../../types';
import Transaction from '../../models/transaction';
import Network from '../../models/network';
import { Utxo } from '../../wallet/types';
import { z } from 'zod';

export interface IGetUtxosOptions {
token?: string;
Expand Down Expand Up @@ -47,7 +48,7 @@ export interface IWalletBalanceData {
}

export interface ITxTemplateInterpreter {
build(instructions: TransactionTemplateType, debug: boolean): Promise<Transaction>;
build(instructions: z.infer<typeof TransactionTemplate>, debug: boolean): Promise<Transaction>;
getAddress(markAsUsed?: boolean): Promise<string>;
getAddressAtIndex(index: number): Promise<string>;
getBalance(token: string): Promise<IWalletBalanceData>;
Expand Down