Changeset View
Changeset View
Standalone View
Standalone View
modules/chronik-client/test/test.ts
import * as chai from "chai" | import * as chai from 'chai'; | ||||
import chaiAsPromised from "chai-as-promised" | import chaiAsPromised from 'chai-as-promised'; | ||||
import { | import { | ||||
BlockDetails, | BlockDetails, | ||||
BlockInfo, | BlockInfo, | ||||
ChronikClient, | ChronikClient, | ||||
SlpTokenTxData, | SlpTokenTxData, | ||||
SubscribeMsg, | SubscribeMsg, | ||||
Tx, | Tx, | ||||
Utxo, | Utxo, | ||||
UtxoState, | UtxoState, | ||||
} from "../index" | } from '../index'; | ||||
const expect = chai.expect | const expect = chai.expect; | ||||
const assert = chai.assert | const assert = chai.assert; | ||||
chai.use(chaiAsPromised) | chai.use(chaiAsPromised); | ||||
const TEST_URL = "https://chronik.be.cash/xec" | const TEST_URL = 'https://chronik.be.cash/xec'; | ||||
const GENESIS_PK = | const GENESIS_PK = | ||||
"04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc" + | '04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc' + | ||||
"3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f" | '3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f'; | ||||
const GENESIS_BLOCK_INFO: BlockInfo = { | const GENESIS_BLOCK_INFO: BlockInfo = { | ||||
hash: "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f", | hash: '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f', | ||||
prevHash: "0000000000000000000000000000000000000000000000000000000000000000", | prevHash: | ||||
'0000000000000000000000000000000000000000000000000000000000000000', | |||||
height: 0, | height: 0, | ||||
nBits: 0x1d00ffff, | nBits: 0x1d00ffff, | ||||
timestamp: "1231006505", | timestamp: '1231006505', | ||||
blockSize: "285", | blockSize: '285', | ||||
numTxs: "1", | numTxs: '1', | ||||
numInputs: "1", | numInputs: '1', | ||||
numOutputs: "1", | numOutputs: '1', | ||||
sumInputSats: "0", | sumInputSats: '0', | ||||
sumCoinbaseOutputSats: "5000000000", | sumCoinbaseOutputSats: '5000000000', | ||||
sumNormalOutputSats: "0", | sumNormalOutputSats: '0', | ||||
sumBurnedSats: "0", | sumBurnedSats: '0', | ||||
} | }; | ||||
const GENESIS_BLOCK_DETAILS: BlockDetails = { | const GENESIS_BLOCK_DETAILS: BlockDetails = { | ||||
version: 1, | version: 1, | ||||
merkleRoot: | merkleRoot: | ||||
"4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b", | '4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b', | ||||
nonce: "2083236893", | nonce: '2083236893', | ||||
medianTimestamp: "1231006505", | medianTimestamp: '1231006505', | ||||
} | }; | ||||
const GENESIS_TX: Tx = { | const GENESIS_TX: Tx = { | ||||
txid: "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b", | txid: '4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b', | ||||
version: 1, | version: 1, | ||||
inputs: [ | inputs: [ | ||||
{ | { | ||||
prevOut: { | prevOut: { | ||||
txid: "0000000000000000000000000000000000000000000000000000000000000000", | txid: '0000000000000000000000000000000000000000000000000000000000000000', | ||||
outIdx: 0xffffffff, | outIdx: 0xffffffff, | ||||
}, | }, | ||||
inputScript: | inputScript: | ||||
"04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e" + | '04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e' + | ||||
"63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420" + | '63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420' + | ||||
"666f722062616e6b73", | '666f722062616e6b73', | ||||
outputScript: undefined, | outputScript: undefined, | ||||
value: "0", | value: '0', | ||||
sequenceNo: 0xffffffff, | sequenceNo: 0xffffffff, | ||||
slpBurn: undefined, | slpBurn: undefined, | ||||
slpToken: undefined, | slpToken: undefined, | ||||
}, | }, | ||||
], | ], | ||||
outputs: [ | outputs: [ | ||||
{ | { | ||||
value: "5000000000", | value: '5000000000', | ||||
outputScript: "41" + GENESIS_PK + "ac", | outputScript: '41' + GENESIS_PK + 'ac', | ||||
slpToken: undefined, | slpToken: undefined, | ||||
spentBy: undefined, | spentBy: undefined, | ||||
}, | }, | ||||
], | ], | ||||
lockTime: 0, | lockTime: 0, | ||||
slpTxData: undefined, | slpTxData: undefined, | ||||
slpErrorMsg: undefined, | slpErrorMsg: undefined, | ||||
block: { | block: { | ||||
hash: "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f", | hash: '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f', | ||||
height: 0, | height: 0, | ||||
timestamp: "1231006505", | timestamp: '1231006505', | ||||
}, | }, | ||||
size: 204, | size: 204, | ||||
isCoinbase: true, | isCoinbase: true, | ||||
timeFirstSeen: "0", | timeFirstSeen: '0', | ||||
network: "XEC", | network: 'XEC', | ||||
} | }; | ||||
const GENESIS_UTXO: Utxo = { | const GENESIS_UTXO: Utxo = { | ||||
outpoint: { txid: GENESIS_TX.txid, outIdx: 0 }, | outpoint: { txid: GENESIS_TX.txid, outIdx: 0 }, | ||||
blockHeight: 0, | blockHeight: 0, | ||||
isCoinbase: true, | isCoinbase: true, | ||||
value: "5000000000", | value: '5000000000', | ||||
slpMeta: undefined, | slpMeta: undefined, | ||||
slpToken: undefined, | slpToken: undefined, | ||||
network: "XEC", | network: 'XEC', | ||||
} | }; | ||||
describe("new ChronikClient", () => { | describe('new ChronikClient', () => { | ||||
it("throws if url ends with a slash", () => { | it('throws if url ends with a slash', () => { | ||||
expect(() => new ChronikClient("https://chronik.be.cash/xec/")).to.throw( | expect( | ||||
() => new ChronikClient('https://chronik.be.cash/xec/'), | |||||
).to.throw( | |||||
"`url` cannot end with '/', got: https://chronik.be.cash/xec/", | "`url` cannot end with '/', got: https://chronik.be.cash/xec/", | ||||
) | ); | ||||
}) | }); | ||||
it("throws if url has wrong schema", () => { | it('throws if url has wrong schema', () => { | ||||
expect(() => new ChronikClient("soap://chronik.be.cash/xec")).to.throw( | expect(() => new ChronikClient('soap://chronik.be.cash/xec')).to.throw( | ||||
"`url` must start with 'https://' or 'http://', got: " + | "`url` must start with 'https://' or 'http://', got: " + | ||||
"soap://chronik.be.cash/xec", | 'soap://chronik.be.cash/xec', | ||||
) | ); | ||||
}) | }); | ||||
}) | }); | ||||
describe("/broadcast-tx", () => { | describe('/broadcast-tx', () => { | ||||
const chronik = new ChronikClient(TEST_URL) | const chronik = new ChronikClient(TEST_URL); | ||||
it("throws a decode error", async () => { | it('throws a decode error', async () => { | ||||
assert.isRejected( | assert.isRejected( | ||||
chronik.broadcastTx("00000000"), | chronik.broadcastTx('00000000'), | ||||
Error, | Error, | ||||
"Failed getting /broadcast-tx (invalid-tx-encoding): Invalid tx " + | 'Failed getting /broadcast-tx (invalid-tx-encoding): Invalid tx ' + | ||||
"encoding: Bytes error: Index 1 is out of bounds for array with " + | 'encoding: Bytes error: Index 1 is out of bounds for array with ' + | ||||
"length 0", | 'length 0', | ||||
) | ); | ||||
}) | }); | ||||
}) | }); | ||||
describe("/blockchain-info", () => { | describe('/blockchain-info', () => { | ||||
const chronik = new ChronikClient(TEST_URL) | const chronik = new ChronikClient(TEST_URL); | ||||
it("gives us the blockchain info", async () => { | it('gives us the blockchain info', async () => { | ||||
const blockchainInfo = await chronik.blockchainInfo() | const blockchainInfo = await chronik.blockchainInfo(); | ||||
expect(blockchainInfo.tipHash.length).to.eql(64) | expect(blockchainInfo.tipHash.length).to.eql(64); | ||||
expect(blockchainInfo.tipHeight).to.gte(739039) | expect(blockchainInfo.tipHeight).to.gte(739039); | ||||
}) | }); | ||||
}) | }); | ||||
describe("/block/:hash", () => { | describe('/block/:hash', () => { | ||||
const chronik = new ChronikClient(TEST_URL) | const chronik = new ChronikClient(TEST_URL); | ||||
it("gives us the Genesis block by hash", async () => { | it('gives us the Genesis block by hash', async () => { | ||||
const block = await chronik.block( | const block = await chronik.block( | ||||
"000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f", | '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f', | ||||
) | ); | ||||
expect(block.blockInfo).to.eql(GENESIS_BLOCK_INFO) | expect(block.blockInfo).to.eql(GENESIS_BLOCK_INFO); | ||||
expect(block.blockDetails).to.eql(GENESIS_BLOCK_DETAILS) | expect(block.blockDetails).to.eql(GENESIS_BLOCK_DETAILS); | ||||
expect(block.txs).to.eql([GENESIS_TX]) | expect(block.txs).to.eql([GENESIS_TX]); | ||||
}) | }); | ||||
it("gives us the Genesis block by height", async () => { | it('gives us the Genesis block by height', async () => { | ||||
const block = await chronik.block(0) | const block = await chronik.block(0); | ||||
expect(block.blockInfo).to.eql(GENESIS_BLOCK_INFO) | expect(block.blockInfo).to.eql(GENESIS_BLOCK_INFO); | ||||
expect(block.blockDetails).to.eql(GENESIS_BLOCK_DETAILS) | expect(block.blockDetails).to.eql(GENESIS_BLOCK_DETAILS); | ||||
expect(block.txs).to.eql([GENESIS_TX]) | expect(block.txs).to.eql([GENESIS_TX]); | ||||
}) | }); | ||||
}) | }); | ||||
describe("/blocks/:start/:end", () => { | describe('/blocks/:start/:end', () => { | ||||
const chronik = new ChronikClient(TEST_URL) | const chronik = new ChronikClient(TEST_URL); | ||||
it("gives us the first few blocks", async () => { | it('gives us the first few blocks', async () => { | ||||
const blocks = await chronik.blocks(0, 10) | const blocks = await chronik.blocks(0, 10); | ||||
expect(blocks.length).to.equal(11) | expect(blocks.length).to.equal(11); | ||||
const block0: BlockInfo = GENESIS_BLOCK_INFO | const block0: BlockInfo = GENESIS_BLOCK_INFO; | ||||
expect(blocks[0]).to.eql(block0) | expect(blocks[0]).to.eql(block0); | ||||
const block1: BlockInfo = { | const block1: BlockInfo = { | ||||
...block0, | ...block0, | ||||
hash: "00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048", | hash: '00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048', | ||||
prevHash: block0.hash, | prevHash: block0.hash, | ||||
height: 1, | height: 1, | ||||
timestamp: "1231469665", | timestamp: '1231469665', | ||||
blockSize: "215", | blockSize: '215', | ||||
} | }; | ||||
expect(blocks[1]).to.eql(block1) | expect(blocks[1]).to.eql(block1); | ||||
const block2: BlockInfo = { | const block2: BlockInfo = { | ||||
...block1, | ...block1, | ||||
hash: "000000006a625f06636b8bb6ac7b960a8d03705d1ace08b1a19da3fdcc99ddbd", | hash: '000000006a625f06636b8bb6ac7b960a8d03705d1ace08b1a19da3fdcc99ddbd', | ||||
prevHash: block1.hash, | prevHash: block1.hash, | ||||
height: 2, | height: 2, | ||||
timestamp: "1231469744", | timestamp: '1231469744', | ||||
} | }; | ||||
expect(blocks[2]).to.eql(block2) | expect(blocks[2]).to.eql(block2); | ||||
const block3: BlockInfo = { | const block3: BlockInfo = { | ||||
...block2, | ...block2, | ||||
hash: "0000000082b5015589a3fdf2d4baff403e6f0be035a5d9742c1cae6295464449", | hash: '0000000082b5015589a3fdf2d4baff403e6f0be035a5d9742c1cae6295464449', | ||||
prevHash: block2.hash, | prevHash: block2.hash, | ||||
height: 3, | height: 3, | ||||
timestamp: "1231470173", | timestamp: '1231470173', | ||||
} | }; | ||||
expect(blocks[3]).to.eql(block3) | expect(blocks[3]).to.eql(block3); | ||||
}) | }); | ||||
}) | }); | ||||
describe("/tx/:txid", () => { | describe('/tx/:txid', () => { | ||||
const chronik = new ChronikClient(TEST_URL) | const chronik = new ChronikClient(TEST_URL); | ||||
it("results in Not Found", async () => { | it('results in Not Found', async () => { | ||||
assert.isRejected( | assert.isRejected( | ||||
chronik.tx( | chronik.tx( | ||||
"0000000000000000000000000000000000000000000000000000000000000000", | '0000000000000000000000000000000000000000000000000000000000000000', | ||||
), | ), | ||||
Error, | Error, | ||||
"Failed getting tx " + | 'Failed getting tx ' + | ||||
"0000000000000000000000000000000000000000000000000000000000000000: " + | '0000000000000000000000000000000000000000000000000000000000000000: ' + | ||||
"Txid not found: " + | 'Txid not found: ' + | ||||
"0000000000000000000000000000000000000000000000000000000000000000", | '0000000000000000000000000000000000000000000000000000000000000000', | ||||
) | ); | ||||
}) | }); | ||||
it("results in the right tx", async () => { | it('results in the right tx', async () => { | ||||
const tx = await chronik.tx( | const tx = await chronik.tx( | ||||
"0f3c3908a2ddec8dea91d2fe1f77295bbbb158af869bff345d44ae800f0a5498", | '0f3c3908a2ddec8dea91d2fe1f77295bbbb158af869bff345d44ae800f0a5498', | ||||
) | ); | ||||
expect(tx.txid).to.eql( | expect(tx.txid).to.eql( | ||||
"0f3c3908a2ddec8dea91d2fe1f77295bbbb158af869bff345d44ae800f0a5498", | '0f3c3908a2ddec8dea91d2fe1f77295bbbb158af869bff345d44ae800f0a5498', | ||||
) | ); | ||||
expect(tx.version).to.eql(2) | expect(tx.version).to.eql(2); | ||||
expect(tx.inputs).to.eql([ | expect(tx.inputs).to.eql([ | ||||
{ | { | ||||
inputScript: | inputScript: | ||||
"473044022042349e5e9e58c4c7b1fc9cbcdd1a1c9774b2ae95b7704ce40c7058ef" + | '473044022042349e5e9e58c4c7b1fc9cbcdd1a1c9774b2ae95b7704ce40c7058ef' + | ||||
"14ba2854022022ba0cd4ead86982e7fc090ad06569a72c165efa3634feac4e722e" + | '14ba2854022022ba0cd4ead86982e7fc090ad06569a72c165efa3634feac4e722e' + | ||||
"ee405b92674121022d577b731fb05971d54951e4cb8bd11120327eba3e0fdd5a4d" + | 'ee405b92674121022d577b731fb05971d54951e4cb8bd11120327eba3e0fdd5a4d' + | ||||
"18f74a882df1a4", | '18f74a882df1a4', | ||||
outputScript: "76a914d15b9793d6af77663f8acf7e2c884f114ef901da88ac", | outputScript: | ||||
'76a914d15b9793d6af77663f8acf7e2c884f114ef901da88ac', | |||||
prevOut: { | prevOut: { | ||||
txid: "fbe9a326ea525013e11e3f6341b3e03d6ad189bac30f94d5ceaf24ef4388e069", | txid: 'fbe9a326ea525013e11e3f6341b3e03d6ad189bac30f94d5ceaf24ef4388e069', | ||||
outIdx: 1, | outIdx: 1, | ||||
}, | }, | ||||
sequenceNo: 4294967295, | sequenceNo: 4294967295, | ||||
slpBurn: undefined, | slpBurn: undefined, | ||||
slpToken: { | slpToken: { | ||||
amount: "50000000", | amount: '50000000', | ||||
isMintBaton: false, | isMintBaton: false, | ||||
}, | }, | ||||
value: "546", | value: '546', | ||||
}, | }, | ||||
{ | { | ||||
inputScript: | inputScript: | ||||
"473044022076e0c764b7d5a5f738304fec1df97db4ba6fc35f8ccb2438de8aabff" + | '473044022076e0c764b7d5a5f738304fec1df97db4ba6fc35f8ccb2438de8aabff' + | ||||
"781edb5c022002016344d4432a5837569b9c764b88aa71f98dbfb76c48cbd0ad9f" + | '781edb5c022002016344d4432a5837569b9c764b88aa71f98dbfb76c48cbd0ad9f' + | ||||
"18dc15be0e4121022d577b731fb05971d54951e4cb8bd11120327eba3e0fdd5a4d" + | '18dc15be0e4121022d577b731fb05971d54951e4cb8bd11120327eba3e0fdd5a4d' + | ||||
"18f74a882df1a4", | '18f74a882df1a4', | ||||
outputScript: "76a914d15b9793d6af77663f8acf7e2c884f114ef901da88ac", | outputScript: | ||||
'76a914d15b9793d6af77663f8acf7e2c884f114ef901da88ac', | |||||
prevOut: { | prevOut: { | ||||
txid: "7ded3b52bbe20fc40d147a4e799fda91e589d29e017789ab6361e462dc972094", | txid: '7ded3b52bbe20fc40d147a4e799fda91e589d29e017789ab6361e462dc972094', | ||||
outIdx: 0, | outIdx: 0, | ||||
}, | }, | ||||
sequenceNo: 4294967295, | sequenceNo: 4294967295, | ||||
slpBurn: undefined, | slpBurn: undefined, | ||||
slpToken: undefined, | slpToken: undefined, | ||||
value: "100000", | value: '100000', | ||||
}, | }, | ||||
]) | ]); | ||||
expect(tx.outputs).to.eql([ | expect(tx.outputs).to.eql([ | ||||
{ | { | ||||
outputScript: | outputScript: | ||||
"6a04534c500001010453454e44200daf200e3418f2df1158efef36fbb507f12928" + | '6a04534c500001010453454e44200daf200e3418f2df1158efef36fbb507f12928' + | ||||
"f1fdcf3543703e64e75a4a90730800000000004c4b40080000000002aea540", | 'f1fdcf3543703e64e75a4a90730800000000004c4b40080000000002aea540', | ||||
slpToken: undefined, | slpToken: undefined, | ||||
spentBy: undefined, | spentBy: undefined, | ||||
value: "0", | value: '0', | ||||
}, | }, | ||||
{ | { | ||||
outputScript: "76a9149c371def7e7cf89b30a62d658147937e679a965388ac", | outputScript: | ||||
'76a9149c371def7e7cf89b30a62d658147937e679a965388ac', | |||||
slpToken: { | slpToken: { | ||||
amount: "5000000", | amount: '5000000', | ||||
isMintBaton: false, | isMintBaton: false, | ||||
}, | }, | ||||
spentBy: { | spentBy: { | ||||
txid: "962ace9db1a36d06c129dffbe9a92bcf2eafe37d1a44aedfc6f957d2be69f149", | txid: '962ace9db1a36d06c129dffbe9a92bcf2eafe37d1a44aedfc6f957d2be69f149', | ||||
outIdx: 0, | outIdx: 0, | ||||
}, | }, | ||||
value: "546", | value: '546', | ||||
}, | }, | ||||
{ | { | ||||
outputScript: "76a914e7b4f63ec550ada1aed74960ddc4e0e107cd6cd188ac", | outputScript: | ||||
'76a914e7b4f63ec550ada1aed74960ddc4e0e107cd6cd188ac', | |||||
slpToken: { | slpToken: { | ||||
amount: "45000000", | amount: '45000000', | ||||
isMintBaton: false, | isMintBaton: false, | ||||
}, | }, | ||||
spentBy: { | spentBy: { | ||||
txid: "11ce5e4249c5b43927810129d887ce0df3bbde46d036998dff0180f94d2df6f8", | txid: '11ce5e4249c5b43927810129d887ce0df3bbde46d036998dff0180f94d2df6f8', | ||||
outIdx: 0, | outIdx: 0, | ||||
}, | }, | ||||
value: "546", | value: '546', | ||||
}, | }, | ||||
{ | { | ||||
outputScript: "76a914d15b9793d6af77663f8acf7e2c884f114ef901da88ac", | outputScript: | ||||
'76a914d15b9793d6af77663f8acf7e2c884f114ef901da88ac', | |||||
slpToken: undefined, | slpToken: undefined, | ||||
spentBy: { | spentBy: { | ||||
txid: "11ce5e4249c5b43927810129d887ce0df3bbde46d036998dff0180f94d2df6f8", | txid: '11ce5e4249c5b43927810129d887ce0df3bbde46d036998dff0180f94d2df6f8', | ||||
outIdx: 1, | outIdx: 1, | ||||
}, | }, | ||||
value: "98938", | value: '98938', | ||||
}, | }, | ||||
]) | ]); | ||||
expect(tx.lockTime).to.eql(0) | expect(tx.lockTime).to.eql(0); | ||||
expect(tx.slpTxData).to.eql({ | expect(tx.slpTxData).to.eql({ | ||||
genesisInfo: undefined, | genesisInfo: undefined, | ||||
slpMeta: { | slpMeta: { | ||||
tokenId: | tokenId: | ||||
"0daf200e3418f2df1158efef36fbb507f12928f1fdcf3543703e64e75a4a9073", | '0daf200e3418f2df1158efef36fbb507f12928f1fdcf3543703e64e75a4a9073', | ||||
tokenType: "FUNGIBLE", | tokenType: 'FUNGIBLE', | ||||
txType: "SEND", | txType: 'SEND', | ||||
groupTokenId: undefined, | groupTokenId: undefined, | ||||
}, | }, | ||||
}) | }); | ||||
expect(tx.slpErrorMsg).to.eql(undefined) | expect(tx.slpErrorMsg).to.eql(undefined); | ||||
expect(tx.block).to.eql({ | expect(tx.block).to.eql({ | ||||
hash: "0000000000000000452f19532a6297ea194eaacac6d3bbcbf7c08a74cad84b44", | hash: '0000000000000000452f19532a6297ea194eaacac6d3bbcbf7c08a74cad84b44', | ||||
height: 697728, | height: 697728, | ||||
timestamp: "1627790415", | timestamp: '1627790415', | ||||
}) | }); | ||||
expect(tx.timeFirstSeen).to.eql("0") | expect(tx.timeFirstSeen).to.eql('0'); | ||||
expect(tx.network).to.eql("XEC") | expect(tx.network).to.eql('XEC'); | ||||
}) | }); | ||||
}) | }); | ||||
describe("/token/:tokenId", () => { | describe('/token/:tokenId', () => { | ||||
const chronik = new ChronikClient(TEST_URL) | const chronik = new ChronikClient(TEST_URL); | ||||
it("results in Not Found", async () => { | it('results in Not Found', async () => { | ||||
assert.isRejected( | assert.isRejected( | ||||
chronik.token( | chronik.token( | ||||
"0000000000000000000000000000000000000000000000000000000000000000", | '0000000000000000000000000000000000000000000000000000000000000000', | ||||
), | ), | ||||
Error, | Error, | ||||
) | ); | ||||
}) | }); | ||||
it("results in Not Found", async () => { | it('results in Not Found', async () => { | ||||
assert.isRejected( | assert.isRejected( | ||||
chronik.token( | chronik.token( | ||||
"0f3c3908a2ddec8dea91d2fe1f77295bbbb158af869bff345d44ae800f0a5498", | '0f3c3908a2ddec8dea91d2fe1f77295bbbb158af869bff345d44ae800f0a5498', | ||||
), | ), | ||||
Error, | Error, | ||||
) | ); | ||||
}) | }); | ||||
it("gives us a token", async () => { | it('gives us a token', async () => { | ||||
const token = await chronik.token( | const token = await chronik.token( | ||||
"0daf200e3418f2df1158efef36fbb507f12928f1fdcf3543703e64e75a4a9073", | '0daf200e3418f2df1158efef36fbb507f12928f1fdcf3543703e64e75a4a9073', | ||||
) | ); | ||||
expect(token.slpTxData).to.eql({ | expect(token.slpTxData).to.eql({ | ||||
genesisInfo: { | genesisInfo: { | ||||
decimals: 4, | decimals: 4, | ||||
tokenDocumentHash: "", | tokenDocumentHash: '', | ||||
tokenDocumentUrl: "https://www.raiusd.co/etoken", | tokenDocumentUrl: 'https://www.raiusd.co/etoken', | ||||
tokenName: "RaiUSD", | tokenName: 'RaiUSD', | ||||
tokenTicker: "USDR", | tokenTicker: 'USDR', | ||||
}, | }, | ||||
slpMeta: { | slpMeta: { | ||||
groupTokenId: undefined, | groupTokenId: undefined, | ||||
tokenId: | tokenId: | ||||
"0daf200e3418f2df1158efef36fbb507f12928f1fdcf3543703e64e75a4a9073", | '0daf200e3418f2df1158efef36fbb507f12928f1fdcf3543703e64e75a4a9073', | ||||
tokenType: "FUNGIBLE", | tokenType: 'FUNGIBLE', | ||||
txType: "GENESIS", | txType: 'GENESIS', | ||||
}, | }, | ||||
} as SlpTokenTxData) | } as SlpTokenTxData); | ||||
expect(token.block).to.eql({ | expect(token.block).to.eql({ | ||||
hash: "00000000000000002686aa5ffa8401c7ed67338fb9475561b2fa9817d6571da8", | hash: '00000000000000002686aa5ffa8401c7ed67338fb9475561b2fa9817d6571da8', | ||||
height: 697721, | height: 697721, | ||||
timestamp: "1627783243", | timestamp: '1627783243', | ||||
}) | }); | ||||
expect(token.timeFirstSeen).to.eql("0") | expect(token.timeFirstSeen).to.eql('0'); | ||||
expect(token.initialTokenQuantity).to.eql("0") | expect(token.initialTokenQuantity).to.eql('0'); | ||||
expect(token.containsBaton).to.eql(true) | expect(token.containsBaton).to.eql(true); | ||||
expect(token.network).to.eql("XEC") | expect(token.network).to.eql('XEC'); | ||||
}) | }); | ||||
}) | }); | ||||
describe("/validate-utxos", () => { | describe('/validate-utxos', () => { | ||||
const chronik = new ChronikClient(TEST_URL) | const chronik = new ChronikClient(TEST_URL); | ||||
it("validates the UTXOs", async () => { | it('validates the UTXOs', async () => { | ||||
const validationResult = await chronik.validateUtxos([ | const validationResult = await chronik.validateUtxos([ | ||||
{ | { | ||||
txid: "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b", | txid: '4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b', | ||||
outIdx: 0, | outIdx: 0, | ||||
}, | }, | ||||
{ | { | ||||
txid: "0f3c3908a2ddec8dea91d2fe1f77295bbbb158af869bff345d44ae800f0a5498", | txid: '0f3c3908a2ddec8dea91d2fe1f77295bbbb158af869bff345d44ae800f0a5498', | ||||
outIdx: 1, | outIdx: 1, | ||||
}, | }, | ||||
{ | { | ||||
txid: "0f3c3908a2ddec8dea91d2fe1f77295bbbb158af869bff345d44ae800f0a5498", | txid: '0f3c3908a2ddec8dea91d2fe1f77295bbbb158af869bff345d44ae800f0a5498', | ||||
outIdx: 100, | outIdx: 100, | ||||
}, | }, | ||||
{ | { | ||||
txid: "0000000000000000000000000000000000000000000000000000000000000000", | txid: '0000000000000000000000000000000000000000000000000000000000000000', | ||||
outIdx: 100, | outIdx: 100, | ||||
}, | }, | ||||
]) | ]); | ||||
const expectedResult: UtxoState[] = [ | const expectedResult: UtxoState[] = [ | ||||
{ | { | ||||
height: 0, | height: 0, | ||||
isConfirmed: true, | isConfirmed: true, | ||||
state: "UNSPENT", | state: 'UNSPENT', | ||||
}, | }, | ||||
{ | { | ||||
height: 697728, | height: 697728, | ||||
isConfirmed: true, | isConfirmed: true, | ||||
state: "SPENT", | state: 'SPENT', | ||||
}, | }, | ||||
{ | { | ||||
height: 697728, | height: 697728, | ||||
isConfirmed: true, | isConfirmed: true, | ||||
state: "NO_SUCH_OUTPUT", | state: 'NO_SUCH_OUTPUT', | ||||
}, | }, | ||||
{ | { | ||||
height: -1, | height: -1, | ||||
isConfirmed: false, | isConfirmed: false, | ||||
state: "NO_SUCH_TX", | state: 'NO_SUCH_TX', | ||||
}, | }, | ||||
] | ]; | ||||
expect(validationResult).to.eql(expectedResult) | expect(validationResult).to.eql(expectedResult); | ||||
}) | }); | ||||
}) | }); | ||||
describe("/script/:type/:payload/history", () => { | describe('/script/:type/:payload/history', () => { | ||||
const chronik = new ChronikClient(TEST_URL) | const chronik = new ChronikClient(TEST_URL); | ||||
it("gives us the first page", async () => { | it('gives us the first page', async () => { | ||||
const script = chronik.script("p2pk", GENESIS_PK) | const script = chronik.script('p2pk', GENESIS_PK); | ||||
const history = await script.history() | const history = await script.history(); | ||||
expect(history.numPages).to.equal(1) | expect(history.numPages).to.equal(1); | ||||
expect(history.txs[history.txs.length - 1]).to.eql(GENESIS_TX) | expect(history.txs[history.txs.length - 1]).to.eql(GENESIS_TX); | ||||
}) | }); | ||||
it("gives us an empty page for ?page=1", async () => { | it('gives us an empty page for ?page=1', async () => { | ||||
const script = chronik.script("p2pk", GENESIS_PK) | const script = chronik.script('p2pk', GENESIS_PK); | ||||
const history = await script.history(1) | const history = await script.history(1); | ||||
expect(history.numPages).to.equal(1) | expect(history.numPages).to.equal(1); | ||||
expect(history.txs).to.eql([]) | expect(history.txs).to.eql([]); | ||||
}) | }); | ||||
it("gives us just one tx for ?page_size=1", async () => { | it('gives us just one tx for ?page_size=1', async () => { | ||||
const script = chronik.script("p2pk", GENESIS_PK) | const script = chronik.script('p2pk', GENESIS_PK); | ||||
const history = await script.history(undefined, 1) | const history = await script.history(undefined, 1); | ||||
expect(history.numPages).to.equal(2) | expect(history.numPages).to.equal(2); | ||||
expect(history.txs.length).to.equal(1) | expect(history.txs.length).to.equal(1); | ||||
}) | }); | ||||
it("gives us the Genesis tx for ?page=1&page_size=1", async () => { | it('gives us the Genesis tx for ?page=1&page_size=1', async () => { | ||||
const script = chronik.script("p2pk", GENESIS_PK) | const script = chronik.script('p2pk', GENESIS_PK); | ||||
const history = await script.history(1, 1) | const history = await script.history(1, 1); | ||||
expect(history.numPages).to.equal(2) | expect(history.numPages).to.equal(2); | ||||
expect(history.txs).to.eql([GENESIS_TX]) | expect(history.txs).to.eql([GENESIS_TX]); | ||||
}) | }); | ||||
}) | }); | ||||
describe("/script/:type/:payload/utxos", () => { | describe('/script/:type/:payload/utxos', () => { | ||||
const chronik = new ChronikClient(TEST_URL) | const chronik = new ChronikClient(TEST_URL); | ||||
it("gives us the UTXOs", async () => { | it('gives us the UTXOs', async () => { | ||||
const script = chronik.script("p2pk", GENESIS_PK) | const script = chronik.script('p2pk', GENESIS_PK); | ||||
const utxos = await script.utxos() | const utxos = await script.utxos(); | ||||
expect(utxos.length).to.equal(1) | expect(utxos.length).to.equal(1); | ||||
expect(utxos[0].outputScript).to.eql("41" + GENESIS_PK + "ac") | expect(utxos[0].outputScript).to.eql('41' + GENESIS_PK + 'ac'); | ||||
expect(utxos[0].utxos[0]).to.eql(GENESIS_UTXO) | expect(utxos[0].utxos[0]).to.eql(GENESIS_UTXO); | ||||
}) | }); | ||||
}) | }); | ||||
describe("/ws", () => { | describe('/ws', () => { | ||||
const chronik = new ChronikClient("https://chronik.be.cash/xpi") | const chronik = new ChronikClient('https://chronik.be.cash/xpi'); | ||||
xit("gives us a confirmation", async () => { | xit('gives us a confirmation', async () => { | ||||
const promise = new Promise((resolve: (msg: SubscribeMsg) => void) => { | const promise = new Promise((resolve: (msg: SubscribeMsg) => void) => { | ||||
const ws = chronik.ws({ | const ws = chronik.ws({ | ||||
onMessage: msg => { | onMessage: msg => { | ||||
resolve(msg) | resolve(msg); | ||||
ws.close() | ws.close(); | ||||
}, | }, | ||||
}) | }); | ||||
ws.subscribe("p2pkh", "b8ae1c47effb58f72f7bca819fe7fc252f9e852e") | ws.subscribe('p2pkh', 'b8ae1c47effb58f72f7bca819fe7fc252f9e852e'); | ||||
}) | }); | ||||
const msg = await promise | const msg = await promise; | ||||
expect(msg.type).to.eql("Confirmed") | expect(msg.type).to.eql('Confirmed'); | ||||
}) | }); | ||||
it("connects to the ws", async () => { | it('connects to the ws', async () => { | ||||
const promise = new Promise(resolve => { | const promise = new Promise(resolve => { | ||||
const ws = chronik.ws({}) | const ws = chronik.ws({}); | ||||
ws.waitForOpen().then(() => { | ws.waitForOpen().then(() => { | ||||
resolve({}) | resolve({}); | ||||
ws.close() | ws.close(); | ||||
}) | }); | ||||
}) | }); | ||||
await promise | await promise; | ||||
}) | }); | ||||
}) | }); |