Skip to content

Commit

Permalink
chore: update flaky ordi test, remove unused estimate code, add doc c…
Browse files Browse the repository at this point in the history
…omments

Signed-off-by: Gregory Hill <[email protected]>
  • Loading branch information
gregdhill committed Jun 25, 2024
1 parent 99aa606 commit f412680
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 62 deletions.
16 changes: 5 additions & 11 deletions sdk/examples/gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async function createTxWithOpReturn(fromAddress: string, toAddress: string, amou
}
}

const unsignedTransaction = await createTransfer(
const unsignedTx = await createTransfer(
'mainnet',
addressType,
fromAddress,
Expand All @@ -50,19 +50,13 @@ async function createTxWithOpReturn(fromAddress: string, toAddress: string, amou
opReturn,
);

// Determine how many inputs to sign
const inputLength = unsignedTransaction.inputsLength;
const _inputsToSign = Array.from({ length: inputLength }, (_, i) => i);
const psbt = unsignedTx.toPSBT(0);
const psbtHex = hex.encode(psbt);

// Sign all inputs
const psbt = unsignedTransaction.toPSBT(0);
const _psbtHex = hex.encode(psbt);

// Sign all inputs with the payment address
const signedPsbtHex = ""; // TODO: sign PSBT
// TODO: sign PSBT
const signedPsbtHex = psbtHex;

const signedTx = SigTx.fromPSBT(bitcoin.Psbt.fromHex(signedPsbtHex).toBuffer());

signedTx.finalize();

return bitcoin.Transaction.fromBuffer(Buffer.from(signedTx.extract()));
Expand Down
35 changes: 7 additions & 28 deletions sdk/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,13 @@ export function getMerkleProof(block: Block, txHash: string, forWitness?: boolea
};
}

/**
* Estimate the tx inclusion fee for N P2WPKH inputs and 3 P2WPKH outputs.
*
* @param feeRate - The current rate for inclusion, satoshi per byte.
* @param numInputs - The number of inputs to estimate for.
* @returns The estimated fee for transaction inclusion.
*/
export function estimateTxFee(feeRate: number, numInputs: number = 1) {
const tx = new bitcoin.Transaction();
for (let i = 0; i < numInputs; i++) {
Expand All @@ -195,31 +202,3 @@ export function estimateTxFee(feeRate: number, numInputs: number = 1) {
const satoshis = feeRate * vsize;
return satoshis;
}

export function estimateTxFeeConst(feeRate: number, numInputs: number = 1) {
// hash (32 bytes)
// + index (4 bytes)
// + [scriptsig] length (1 byte)
// + sequence (4 bytes)
// + [witness] item count (1 byte)
// + [witness] signature length (1 byte)
// + [witness] signature (71 or 72 bytes)
// + [witness] pubkey length (1 byte)
// + [witness] pubkey (33 bytes)
const inputSize = 32 + 4 + 1 + 4 + (1 + 1 + 72 + 1 + 33) / 4;
// nValue (8 bytes)
// + scriptPubKey length (...)
// + scriptPubKey (P2WPKH = 22 bytes)
const outputSize = 8 + 1 + 22;
const opReturnSize = 8 + 1 + 1 + 20;
// nVersion (4 bytes)
// + input count (1 byte)
// + input(s)
// + output count (1 byte)
// + output(s)
// + nLockTime (4 bytes)
// + segwit marker & segwit flag (1 byte)
const vsize = 4 + 1 + inputSize * numInputs + 1 + outputSize + outputSize + opReturnSize + 4 + 1;
const satoshis = feeRate * vsize;
return satoshis;
}
36 changes: 18 additions & 18 deletions sdk/test/ordinal-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,40 @@ import {
import { assert, describe, it } from "vitest";

describe("Ordinal API Tests", () => {
// TODO: change to use ordi
it("should get inscription from id", async () => {
const client = new DefaultOrdinalsClient("testnet");
const client = new DefaultOrdinalsClient("mainnet");
// Deploy ORDI - BRC20
const inscriptionJson = await client.getInscriptionFromId({
txid: "74c86592f75716a14a534898913e6077fb5d7650cfc17600868964bbe2b7e512",
txid: "b61b0172d95e266c18aea0c624db987e971a5d6d4ebc2aaed85da4642d635735",
index: 0,
});

const expectedInscriptionJson: InscriptionJson<InscriptionId, SatPoint> = {
address: 'tb1qn50zg73kl8f8wkn8358n4z2drvwraxhl7zdzly',
address: 'bc1pxaneaf3w4d27hl2y93fuft2xk6m4u3wc4rafevc6slgd7f5tq2dqyfgy06',
charms: [],
children: [],
content_length: 868,
content_type: 'text/javascript',
effective_content_type: "text/javascript",
fee: 395,
height: 2537128,
id: InscriptionId.fromString('74c86592f75716a14a534898913e6077fb5d7650cfc17600868964bbe2b7e512i0'),
number: 560474,
next: InscriptionId.fromString('dd90d8222da2a6f3260109b1e4d1a2c341d999fce4707b1d77e49956a51a0305i0'),
content_length: 94,
content_type: 'text/plain;charset=utf-8',
effective_content_type: "text/plain;charset=utf-8",
fee: 4830,
height: 779832,
id: InscriptionId.fromString('b61b0172d95e266c18aea0c624db987e971a5d6d4ebc2aaed85da4642d635735i0'),
number: 348020,
next: InscriptionId.fromString('693bd98380ad6e58f83de6068c236c6eb9d629c825cc3342c2d93f24c6762c6di0'),
parent: null,
parents: [],
previous: InscriptionId.fromString('332d3fae125de51de29e97cd9e80aab7c63025d5094944a3dceb117c556c41cci0'),
previous: InscriptionId.fromString('4f0ff6259efa9d56b16664e6c5c9755c148818dc6bbca98f7f9166b277e4b7c0i0'),
rune: null,
sat: null,
satpoint: SatPoint.fromString('cdd084e3427c52b5e3777979521bb9030e680d521f267aedb6aef9d95e9e37b1:2:56181932'),
timestamp: 1699246476,
value: 116364085,
satpoint: SatPoint.fromString('b61b0172d95e266c18aea0c624db987e971a5d6d4ebc2aaed85da4642d635735:0:0'),
timestamp: 1678248991,
value: 10000,
};

assert.deepStrictEqual(inscriptionJson, expectedInscriptionJson);
});

it("should get inscriptions", async () => {
it("should get inscriptions", { timeout: 10000 }, async () => {
const client = new DefaultOrdinalsClient("testnet");
const inscriptionsJson = await client.getInscriptions();
// assert that inscriptionsJson is not null, undefined or empty
Expand Down Expand Up @@ -108,7 +108,7 @@ describe("Ordinal API Tests", () => {
assert.deepStrictEqual(satJson, expectedSatJson);
});

it("should get inscriptions from start block", async () => {
it("should get inscriptions from start block", { timeout: 10000 }, async () => {
const client = new DefaultOrdinalsClient("testnet");
const startBlock: number = 2537138;
const inscriptions = await client.getInscriptionsFromStartBlock(startBlock);
Expand Down
5 changes: 2 additions & 3 deletions sdk/test/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { assert, describe, it } from "vitest";
import { MAINNET_ESPLORA_BASE_PATH } from "../src/esplora";
import { Block } from "bitcoinjs-lib";
import { estimateTxFee, estimateTxFeeConst, getMerkleProof } from "../src/utils";
import { estimateTxFee, getMerkleProof } from "../src/utils";

describe("Utils Tests", () => {
// NOTE: this is a bit flaky due to slow response times from electrs
Expand All @@ -21,7 +21,6 @@ describe("Utils Tests", () => {
});

it("should estimate fee", async () => {
// NOTE: off by one but not sure why
assert.equal(estimateTxFee(1), estimateTxFeeConst(1) + 1);
assert.equal(estimateTxFee(1), 172);
});
});
3 changes: 1 addition & 2 deletions sdk/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
"outDir": "./dist"
},
"include": [
"src/**/*",
"examples/*"
"src/**/*"
],
"exclude": [
"./node_modules/*"
Expand Down

0 comments on commit f412680

Please sign in to comment.