A small key-value index contract

It should be trivial to create a simple key-value database anyone could write to… for a fee.

The below contract should allow anyone to create an entry by sending their data in an NFT commitment of any CashToken category. The party creating the record my specify how long they’d like the record to persist by specifying the number of blocks as sats.

Rather than collect data from a custom indexer, dapps could retrieve data from any Bitcoin Cash indexer by getting the NFT balance of the address for the contract corresponding to the key of interest.

Obviously, records would be limited to the current commitment length (currently 40 bytes). And a minimum storage period of 800 blocks (or the current token output minimum value).

The burning mechanism is based on @dagurval 's TokenBurner contract.

pragma cashscript 0.11.0;

// Small Index 
// 
// A subscription based key-value index 
//

contract index(bytes key) {
    
    // Secure outputs with data in token commitments for a given key.
    // 
    // All entries pay a fixed storage fee of 1 satoshi per block. 
    //
    // If an entry has expired, miners may drop it.
    //
    // Miners can drop many expired entires at once.
    //

    function drop() {

        // Assure the key is not empty.
        // OP_SIZE OP_NIP OP_VERIFY
        require(bool(key.length));

        // Require each input age be higher than the utxo value
        // OP_INPUTINDEX OP_UTXOVALUE OP_CHECKSEQUENCEVERIFY OP_DROP
        require(tx.age >= tx.inputs[this.activeInputIndex].value);

        // See TokenBurner - Dagur Valberg Johannsson
        //
        // Require a single output
        // OP_TXOUTPUTCOUNT OP_1 OP_NUMEQUALVERIFY 
        require(tx.outputs.length == 1);

        // Without BCH
        // OP_0 OP_OUTPUTVALUE OP_0 OP_NUMEQUALVERIFY
        require(tx.outputs[0].value == 0);

        // Without tokens
        // OP_0 OP_OUTPUTTOKENCATEGORY OP_0 OP_EQUALVERIFY
        require(tx.outputs[0].tokenCategory == 0x);

        // As an empty OP_RETURN
        // OP_0 OP_OUTPUTBYTECODE 6a OP_EQUAL
        require(tx.outputs[0].lockingBytecode == 0x6a);
    }
    
}
2 Likes

Example Application - Dex order book aggregator

Given a number of decentralized exchange protocols, it should be easy for anyone to get the entire market for a token, if they have all the parameters of all open contracts.

Given four (4) decentralized exchange protocols, each denoted by two letter identifiers (CQ, TS, FB & FS), then given some token id, it should be feasible to store the swap protocol, version and parameters on NFT outputs within the current 40-byte commitment length.

Requesting the values stored on a token category with some wrapper library like so:

get(242f6ecedb404c743477e35b09733a56cacae34f3109d5cee1cbc1d5630affd7)

might return something like

[
 024351010114ce8a106e7b04dc2b2c920324bbf01cf5e6d1cb06.
 0254530100034ff305 14 eb31564cd4969f028be7ced216543f1550bbae3404f458c600,
 024642010014f1cb613a35a9d2c030290f81d7c311ffabd86bfc04f0b9f505,
 024653010014f1cb613a35a9d2c030290f81d7c311ffabd86bfc0418ddf505,
]

which would be transformable to :

protocol version parameters length (bytes)
02 4653 01 00 14 f1cb613a35a9d2c030290f81d7c311ffabd86bfc 04 18ddf505 32
02 4642 01 00 14 f1cb613a35a9d2c030290f81d7c311ffabd86bfc 04 f0b9f505 32
02 4351 01 01 14 ce8a106e7b04dc2b2c920324bbf01cf5e6d1cb06 26
02 5453 01 00 03 4ff305 14 eb31564cd4969f028be7ced216543f1550bbae34 04 f458c600 36

Given the above data, and the specifications of the dex protocols referred to, user software could then calculate the current address for the four swap contracts above and the query the state of those swaps by getting the current balances from any CashToken enabled indexer software.

Software could craft an aggregated order book with all common dexes without interacting with custom queries, apis or indexers of any particular dex.

Users (and arbitrage software) would be able to generate a total market view using all swaps orders stored on the index contract for the token category.

Which might look something like:

DEX Spot Price BCH Token
TS v1 0.00018000 0 5000
FS v0 0.00001600 0 130000
CQ v1 0.00001055 1 1200
CQ v1 0.00001024 11 12000
CQ v1 0.00001010 0.6 1500
FB v0 0.00000800 12 0
FB v0 0.00000080 1 0
FS v0 NaN 0.60 0
FB v0 NaN 0 10210