Skip to content

Commit

Permalink
fix: createTransaction and tests (#11)
Browse files Browse the repository at this point in the history
* fix: create-tx

* chore: changeset
  • Loading branch information
nickfrosty authored Jan 23, 2025
1 parent 0c03cb8 commit 5e0a77b
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
76 changes: 76 additions & 0 deletions packages/gill/src/__tests__/transactions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import assert from "node:assert";

import { createTransaction } from "../core/transactions";
import { blockhash, generateKeyPairSigner, isKeyPairSigner, KeyPairSigner } from "@solana/web3.js";

describe("createTransaction", () => {
let signer: KeyPairSigner;

beforeAll(async () => {
signer = await generateKeyPairSigner();
});

test("create a legacy transaction with a signer as the feePayer", () => {
const tx = createTransaction({
version: "legacy",
feePayer: signer,
instructions: [],
latestBlockhash: {
blockhash: blockhash("GK1nopeF3P8J46dGqq4KfaEWopZU7K65F6CKQXuUdr3z"),
lastValidBlockHeight: 0n,
},
});

assert.equal(tx.version, "legacy");
assert.equal(tx.feePayer.address, signer.address);
assert.equal(isKeyPairSigner(tx.feePayer), true);
assert.equal(tx.instructions.length, 0);
assert.equal(Object.hasOwn(tx, "lifetimeConstraint"), true);
});

test("create a version 0 transaction with a signer as the feePayer", () => {
const tx = createTransaction({
version: 0,
feePayer: signer,
instructions: [],
});

assert.equal(tx.version, 0);
assert.equal(tx.feePayer.address, signer.address);
assert.equal(isKeyPairSigner(tx.feePayer), true);
assert.equal(tx.instructions.length, 0);
assert.equal(Object.hasOwn(tx, "lifetimeConstraint"), false);
});

test("create a legacy transaction with an `Address` as the feePayer", () => {
const tx = createTransaction({
version: "legacy",
feePayer: signer.address,
instructions: [],
});

assert.equal(tx.version, "legacy");
assert.equal(tx.feePayer.address, signer.address);
assert.equal(isKeyPairSigner(tx.feePayer), false);
assert.equal(tx.instructions.length, 0);
assert.equal(Object.hasOwn(tx, "lifetimeConstraint"), false);
});

test("create a version 0 transaction with an `Address` as the feePayer", () => {
const tx = createTransaction({
version: 0,
feePayer: signer.address,
instructions: [],
latestBlockhash: {
blockhash: blockhash("GK1nopeF3P8J46dGqq4KfaEWopZU7K65F6CKQXuUdr3z"),
lastValidBlockHeight: 0n,
},
});

assert.equal(tx.version, 0);
assert.equal(tx.feePayer.address, signer.address);
assert.equal(isKeyPairSigner(tx.feePayer), false);
assert.equal(tx.instructions.length, 0);
assert.equal(Object.hasOwn(tx, "lifetimeConstraint"), true);
});
});
2 changes: 1 addition & 1 deletion packages/gill/src/core/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function createTransaction({
if (latestBlockhash) {
tx = setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, tx);
}
if ("address" in feePayer && isTransactionSigner(feePayer)) {
if (typeof feePayer !== "string" && "address" in feePayer && isTransactionSigner(feePayer)) {
return setTransactionMessageFeePayerSigner(feePayer, tx);
} else return setTransactionMessageFeePayer(feePayer, tx);
},
Expand Down
4 changes: 4 additions & 0 deletions packages/gill/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
export * from "./rpc";
export * from "./explorer";

export type Prettify<T> = {
[K in keyof T]: T[K];
} & {};

0 comments on commit 5e0a77b

Please sign in to comment.