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

Add reduce tx #89

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
54 changes: 54 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"@types/graphql-depth-limit": "^1.1.3",
"@types/ioredis": "^4.28.10",
"@types/jest": "^28.1.8",
"@types/json-bigint": "^1.0.4",
"@types/lodash": "^4.14.191",
"@types/node": "^18.13.0",
"@types/validator": "^13.7.12",
Expand All @@ -47,10 +48,12 @@
"axios": "^1.4.0",
"class-validator": "^0.14.0",
"dotenv": "^16.0.3",
"ergo-lib-wasm-nodejs": "^0.26.0",
"graphql": "^15.8.0",
"graphql-depth-limit": "^1.1.0",
"graphql-type-json": "^0.3.2",
"ioredis": "5.3.1",
"json-bigint": "^1.0.0",
"lodash": "^4.17.21",
"pg": "^8.11.0",
"reflect-metadata": "^0.1.13",
Expand Down
12 changes: 12 additions & 0 deletions src/graphql/input-types/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,15 @@ export class SignedTransactionInput {
@Field({ nullable: true })
size?: number;
}

@InputType("UnsignedTransaction")
export class UnsignedTransactionInput {
@Field(() => String)
unsignedTransaction!: string;

@Field(() => [String])
inputBoxes!: string[];

@Field(() => [String])
dataInputBoxes!: string[];
}
36 changes: 35 additions & 1 deletion src/graphql/resolvers/transaction-resolver.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
import { GraphQLResolveInfo } from "graphql";
import { Args, ArgsType, Ctx, Field, Info, Int, Query, Resolver } from "type-graphql";
import {
Arg,
Args,
ArgsType,
Ctx,
Field,
Info,
Int,
Mutation,
Query,
Resolver
} from "type-graphql";
import { Transaction } from "../objects/transaction";
import { removeUndefined } from "../../utils";
import { GraphQLContext } from "../context-type";
import { PaginationArguments } from "./pagination-arguments";
import { ArrayMaxSize } from "class-validator";
import { UnsignedTransactionInput } from "../input-types";
import * as wasm from "ergo-lib-wasm-nodejs";
import { nodeService } from "../../services";

@ArgsType()
class TransactionArguments {
Expand Down Expand Up @@ -72,4 +86,24 @@
take
});
}

@Mutation(() => String)
async reduceTransaction(@Arg("transaction") transaction: UnsignedTransactionInput) {
try {
const ctx = await nodeService.getStateContext();
const tx = wasm.UnsignedTransaction.from_json(transaction.unsignedTransaction);
const ergoBoxes = wasm.ErgoBoxes.from_boxes_json(transaction.inputBoxes);
const dataInputBoxes = wasm.ErgoBoxes.from_boxes_json([]);
const reducedTx = wasm.ReducedTransaction.from_unsigned_tx(
tx,
ergoBoxes,
dataInputBoxes,
ctx
);
return reducedTx.sigma_serialize_bytes().toString();
} catch (e: any) {

Check warning on line 104 in src/graphql/resolvers/transaction-resolver.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
console.error(e);
throw new Error(`Failed to reduce transaction! ${e}`);
}
}
}
7 changes: 7 additions & 0 deletions src/graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ type Mempool {

type Mutation {
checkTransaction(signedTransaction: SignedTransaction!): String!
reduceTransaction(transaction: UnsignedTransaction!): String!
submitTransaction(signedTransaction: SignedTransaction!): String!
}

Expand Down Expand Up @@ -369,3 +370,9 @@ type UnconfirmedTransaction implements ITransaction {
timestamp: String!
transactionId: String!
}

input UnsignedTransaction {
dataInputBoxes: [String!]!
inputBoxes: [String!]!
unsignedTransaction: String!
}
19 changes: 19 additions & 0 deletions src/services/node-service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import axios from "axios";
import { SignedTransactionInput } from "../graphql/input-types";
import * as wasm from "ergo-lib-wasm-nodejs";
import { JsonBI } from "../utils";

const HTTP_PREFIX_PATTERN = /^http(s?):\/\//;

Expand Down Expand Up @@ -30,6 +32,23 @@
public getNodeInfo() {
return axios.get(this._baseUrl + "/info");
}

public getStateContext = async () => {
try {
const lastHeaderResponse = await axios.get(this._baseUrl + "/blocks/lastHeaders/10");
const lastBlocks = lastHeaderResponse.data as any[];

Check warning on line 39 in src/services/node-service.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
const lastBlocksStrings = lastBlocks.map((header) => JsonBI.stringify(header));
const lastBlocksHeaders = wasm.BlockHeaders.from_json(lastBlocksStrings);
const lastBlockPreHeader = wasm.PreHeader.from_block_header(lastBlocksHeaders.get(0));

const stateContext = new wasm.ErgoStateContext(lastBlockPreHeader, lastBlocksHeaders);

return stateContext;
} catch (e) {
console.error(e);
throw new Error("Failed to get state context");
}
};
}

export const nodeService = new NodeService();
7 changes: 7 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import JSONBigInt from "json-bigint";

export function removeUndefined(value: Record<string, unknown>) {
const result: Record<string, unknown> = {};
for (const key in value) {
Expand All @@ -9,3 +11,8 @@ export function removeUndefined(value: Record<string, unknown>) {

return result;
}

export const JsonBI = JSONBigInt({
useNativeBigInt: true,
alwaysParseAsBig: true
});
Loading