Examples: query, "exact match", wildcard*, wild?ard, wild*rd
Fuzzy search: cake~ (finds cakes, bake)
Term boost: "red velvet"^4, chocolate^2
Field grouping: tags:(+work -"fun-stuff")
Escape special characters +-&|!(){}[]^"~*?:\ - e.g. \+ \* \!
Range search: properties.timestamp:[1587729413488 TO *] (inclusive), properties.title:{A TO Z}(excluding A and Z)
Combinations: chocolate AND vanilla, chocolate OR vanilla, (chocolate OR vanilla) NOT "vanilla pudding"
Field search: properties.title:"The Title" AND text
Back to post

Revisions 3

one year ago
Slava Fomin
77 × 2 Administrator
How to decode data of TON smart-contract?
How to decode data of TON smart-contract?
There is no single data-storage format in TON. Each smart contract could have it's own way of storing/encoding it's data. Take the [NFT item smart contract](https://github.com/ton-blockchain/token-contract/blob/main/nft/nft-item.fc#L19-L47) as an example. It stores it's data in the following format: ``` ;; ;; Storage ;; ;; uint64 index ;; MsgAddressInt collection_address ;; MsgAddressInt owner_address ;; cell content ;; ``` And the contract has two internal functions to actually load and persist it's data: ```func (int, int, slice, slice, cell) load_data() { slice ds = get_data().begin_parse(); var (index, collection_address) = (ds~load_uint(64), ds~load_msg_addr()); if (ds.slice_bits() > 0) { return (-1, index, collection_address, ds~load_msg_addr(), ds~load_ref()); } else { return (0, index, collection_address, null(), null()); ;; nft not initialized yet } } () store_data(int index, slice collection_address, slice owner_address, cell content) impure { set_data( begin_cell() .store_uint(index, 64) .store_slice(collection_address) .store_slice(owner_address) .store_ref(content) .end_cell() ); } ``` The data is stored as "cell" that could have an arbitrary format and could reference other nested cells. Look for the **Bag of Cells** concept in the [white paper](https://ton-blockchain.github.io/docs/ton.pdf). However, you should not try to decode smart contract data directly, even if you know the format of the stored data. The proper way would be to use the "public API" of the contract by means of calling it's get methods. The NFT item contract has a [get method](https://github.com/ton-blockchain/token-contract/blob/main/nft/nft-item.fc#L137-L144) for exactly this purpose: ```func ;; ;; GET Methods ;; (int, int, slice, slice, cell) get_nft_data() method_id { (int init?, int index, slice collection_address, slice owner_address, cell content) = load_data(); return (init?, index, collection_address, owner_address, content); } ``` It will return all the needed data for you in the decoded fashion. And [here](https://github.com/slavafomin/tonweb/blob/typescripted/src/contract/token/nft/nft-item.ts#L83-L109) is how you would parse the result of this get method: ```typescript // Calling the get-method const result = await this.provider.call2( myAddress.toString(), 'get_nft_data' ); // Parsing the data returned by it const isInitialized = ( (expectBN(result[0]).toNumber() === -1) ); const index = expectBN(result[1]).toNumber(); const collectionAddress = ( parseAddressFromCell(result[2]) ); const ownerAddress = (isInitialized ? parseAddressFromCell(result[3]) : null ); const contentCell = result[4]; // Single NFT without a collection const contentUri = ((isInitialized && !collectionAddress) ? parseOffchainUriCell(contentCell) : null ); ``` Also, some peaces of data is stored as binary strings and require some non-trivial parsing using the [TL-B schemas](https://www.tonspace.co/learn/overviews/TL-B). Each library would give you some utilities to use. One of such tools is usually called a `CellSlice` that allows you to read and decode the bits of the bit-string by hand. Consider this example from the [TonWeb](https://github.com/slavafomin/tonweb): ```ts // Encoding some data as a cell const cell = new Cell(); cell.bits.writeUint('100500', 32); cell.bits.writeString('Hello World'); // Reading the data from cell (bit-string) const slice = new CellSlice(cell); slice.loadUint(32); // 100500 slice.loadString(); // Hello World slice.isEmpty(); // true ``` This should get you started. Just make sure to study all the linked concepts.
There is no single data-storage format in TON. Each smart contract could have it's own way of storing/encoding it's data. Take the [NFT item smart contract](https://github.com/ton-blockchain/token-contract/blob/main/nft/nft-item.fc#L19-L47) as an example. It stores it's data in the following format: ``` ;; ;; Storage ;; ;; uint64 index ;; MsgAddressInt collection_address ;; MsgAddressInt owner_address ;; cell content ;; ``` And the contract has two internal functions to actually load and persist it's data: ```func (int, int, slice, slice, cell) load_data() { slice ds = get_data().begin_parse(); var (index, collection_address) = (ds~load_uint(64), ds~load_msg_addr()); if (ds.slice_bits() > 0) { return (-1, index, collection_address, ds~load_msg_addr(), ds~load_ref()); } else { return (0, index, collection_address, null(), null()); ;; nft not initialized yet } } () store_data(int index, slice collection_address, slice owner_address, cell content) impure { set_data( begin_cell() .store_uint(index, 64) .store_slice(collection_address) .store_slice(owner_address) .store_ref(content) .end_cell() ); } ``` The data is stored as "cell" that could have an arbitrary format and could reference other nested cells. Look for the **Bag of Cells** concept in the [white paper](https://ton-blockchain.github.io/docs/ton.pdf). However, you should not try to decode smart contract data directly, even if you know the format of the stored data. The proper way would be to use the "public API" of the contract by means of calling it's get methods. The NFT item contract has a [get method](https://github.com/ton-blockchain/token-contract/blob/main/nft/nft-item.fc#L137-L144) for exactly this purpose: ```func ;; ;; GET Methods ;; (int, int, slice, slice, cell) get_nft_data() method_id { (int init?, int index, slice collection_address, slice owner_address, cell content) = load_data(); return (init?, index, collection_address, owner_address, content); } ``` It will return all the needed data for you in the decoded fashion. And [here](https://github.com/slavafomin/tonweb/blob/typescripted/src/contract/token/nft/nft-item.ts#L83-L109) is how you would parse the result of this get method: ```typescript // Calling the get-method const result = await this.provider.call2( myAddress.toString(), 'get_nft_data' ); // Parsing the data returned by it const isInitialized = ( (expectBN(result[0]).toNumber() === -1) ); const index = expectBN(result[1]).toNumber(); const collectionAddress = ( parseAddressFromCell(result[2]) ); const ownerAddress = (isInitialized ? parseAddressFromCell(result[3]) : null ); const contentCell = result[4]; // Single NFT without a collection const contentUri = ((isInitialized && !collectionAddress) ? parseOffchainUriCell(contentCell) : null ); ``` Also, some peaces of data is stored as binary strings and require some non-trivial parsing using the [TL-B schemas](https://www.tonspace.co/learn/overviews/TL-B). Each library would give you some utilities to use. One of such tools is usually called a `CellSlice` that allows you to read and decode the bits of the bit-string by hand. Consider this example from the [TonWeb](https://github.com/slavafomin/tonweb): ```ts // Encoding some data as a cell const cell = new Cell(); cell.bits.writeUint('100500', 32); cell.bits.writeString('Hello World'); // Reading the data from cell bit-string const slice = new CellSlice(cell); slice.loadUint(32); // 100500 slice.loadString(); // Hello World slice.isEmpty(); // true ``` This should get you started. Just make sure to study all the linked concepts.
one year ago
Slava Fomin
77 × 2 Administrator
How to decode data of TON smart-contract?
How to decode data of TON smart-contract?
There is no single data-storage format in TON. Each smart contract could have it's own way of storing/encoding it's data. Take the [NFT item smart contract](https://github.com/ton-blockchain/token-contract/blob/main/nft/nft-item.fc#L19-L47) as an example. It stores it's data in the following format: ``` ;; ;; Storage ;; ;; uint64 index ;; MsgAddressInt collection_address ;; MsgAddressInt owner_address ;; cell content ;; ``` And the contract has two internal functions to actually load and persist it's data: ```func (int, int, slice, slice, cell) load_data() { slice ds = get_data().begin_parse(); var (index, collection_address) = (ds~load_uint(64), ds~load_msg_addr()); if (ds.slice_bits() > 0) { return (-1, index, collection_address, ds~load_msg_addr(), ds~load_ref()); } else { return (0, index, collection_address, null(), null()); ;; nft not initialized yet } } () store_data(int index, slice collection_address, slice owner_address, cell content) impure { set_data( begin_cell() .store_uint(index, 64) .store_slice(collection_address) .store_slice(owner_address) .store_ref(content) .end_cell() ); } ``` The data is stored as "cell" that could have an arbitrary format and could reference other nested cells. Look for the **Bag of Cells** concept in the [white paper](https://ton-blockchain.github.io/docs/ton.pdf). However, you should not try to decode smart contract data directly, even if you know the format of the stored data. The proper way would be to use the "public API" of the contract by means of calling it's get methods. The NFT item contract has a [get method](https://github.com/ton-blockchain/token-contract/blob/main/nft/nft-item.fc#L137-L144) for exactly this purpose: ```func ;; ;; GET Methods ;; (int, int, slice, slice, cell) get_nft_data() method_id { (int init?, int index, slice collection_address, slice owner_address, cell content) = load_data(); return (init?, index, collection_address, owner_address, content); } ``` It will return all the needed data for you in the decoded fashion. And [here](https://github.com/slavafomin/tonweb/blob/typescripted/src/contract/token/nft/nft-item.ts#L83-L109) is how you would parse the result of this get method: ```typescript // Calling the get-method const result = await this.provider.call2( myAddress.toString(), 'get_nft_data' ); // Parsing the data returned by it const isInitialized = ( (expectBN(result[0]).toNumber() === -1) ); const index = expectBN(result[1]).toNumber(); const collectionAddress = ( parseAddressFromCell(result[2]) ); const ownerAddress = (isInitialized ? parseAddressFromCell(result[3]) : null ); const contentCell = result[4]; // Single NFT without a collection const contentUri = ((isInitialized && !collectionAddress) ? parseOffchainUriCell(contentCell) : null ); ``` Also, some peaces of data is stored as binary strings and require some non-trivial parsing using the [TL-B schemas](https://www.tonspace.co/learn/overviews/TL-B). Each library would give you some utilities to use. One of such tools is usually called a `CellSlice` that allows you to read and decode the bits of the bit-string by hand. Consider this example from the [TonWeb](https://github.com/slavafomin/tonweb): ```ts // Encoding some data as a cell const cell = new Cell(); cell.bits.writeUint('100500', 32); cell.bits.writeString('Hello World'); // Reading the data from cell bit-string const slice = new CellSlice(cell); slice.loadUint(32); // 100500 slice.loadString(); // Hello World slice.isEmpty(); // true ``` This should get you started. Just make sure to study all the linked concepts.
There is no single data-storage format in TON. Each smart contract could have it's own way of storing/encoding it's data. Take the [NFT item smart contract](https://github.com/ton-blockchain/token-contract/blob/main/nft/nft-item.fc#L19-L47) as an example. It stores it's data in the following format: ``` ;; ;; Storage ;; ;; uint64 index ;; MsgAddressInt collection_address ;; MsgAddressInt owner_address ;; cell content ;; ``` And the contract has two internal functions to actually load and persist it's data: ```func (int, int, slice, slice, cell) load_data() { slice ds = get_data().begin_parse(); var (index, collection_address) = (ds~load_uint(64), ds~load_msg_addr()); if (ds.slice_bits() > 0) { return (-1, index, collection_address, ds~load_msg_addr(), ds~load_ref()); } else { return (0, index, collection_address, null(), null()); ;; nft not initialized yet } } () store_data(int index, slice collection_address, slice owner_address, cell content) impure { set_data( begin_cell() .store_uint(index, 64) .store_slice(collection_address) .store_slice(owner_address) .store_ref(content) .end_cell() ); } ``` The data is stored as "cell" that could have an arbitrary format and could reference other nested cells. Look for the **Bag of Cells** concept in the [white paper](https://ton-blockchain.github.io/docs/ton.pdf). However, you should not try to decode smart contract data directly, even if you know the format of the stored data. The proper way would be to use the "public API" of the contract by means of calling it's get methods. The NFT item contract has a [get method](https://github.com/ton-blockchain/token-contract/blob/main/nft/nft-item.fc#L137-L144) for exactly this purpose: ```func ;; ;; GET Methods ;; (int, int, slice, slice, cell) get_nft_data() method_id { (int init?, int index, slice collection_address, slice owner_address, cell content) = load_data(); return (init?, index, collection_address, owner_address, content); } ``` It will return all the needed data for you in the decoded fashion. And [here](https://github.com/slavafomin/tonweb/blob/typescripted/src/contract/token/nft/nft-item.ts#L83-L109) is how you would parse the result of this get method: ```typescript // Calling the get-method const result = await this.provider.call2( myAddress.toString(), 'get_nft_data' ); // Parsing the data returned by it const isInitialized = ( (expectBN(result[0]).toNumber() === -1) ); const index = expectBN(result[1]).toNumber(); const collectionAddress = ( parseAddressFromCell(result[2]) ); const ownerAddress = (isInitialized ? parseAddressFromCell(result[3]) : null ); const contentCell = result[4]; // Single NFT without a collection const contentUri = ((isInitialized && !collectionAddress) ? parseOffchainUriCell(contentCell) : null ); ```
one year ago
Original
Slava Fomin
77 × 2 Administrator
How to decode data of TON smart-contract?

There is no single data-storage format in TON. Each smart contract could have it's own way of storing/encoding it's data. Take the [NFT item smart contract](https://github.com/ton-blockchain/token-contract/blob/main/nft/nft-item.fc#L19-L47) as an example. It stores it's data in the following format: ``` ;; ;; Storage ;; ;; uint64 index ;; MsgAddressInt collection_address ;; MsgAddressInt owner_address ;; cell content ;; ``` And the contract has two internal functions to actually load and persist it's data: ```func (int, int, slice, slice, cell) load_data() { slice ds = get_data().begin_parse(); var (index, collection_address) = (ds~load_uint(64), ds~load_msg_addr()); if (ds.slice_bits() > 0) { return (-1, index, collection_address, ds~load_msg_addr(), ds~load_ref()); } else { return (0, index, collection_address, null(), null()); ;; nft not initialized yet } } () store_data(int index, slice collection_address, slice owner_address, cell content) impure { set_data( begin_cell() .store_uint(index, 64) .store_slice(collection_address) .store_slice(owner_address) .store_ref(content) .end_cell() ); } ``` The data is stored as "cell" that could have an arbitrary format and could reference other nested cells. Look for the **Bag of Cells** concept in the [white paper](https://ton-blockchain.github.io/docs/ton.pdf). However, you should not try to decode smart contract data directly, even if you know the format of the stored data. The proper way would be to use the "public API" of the contract by means of calling it's get methods. The NFT item contract has a [get method](https://github.com/ton-blockchain/token-contract/blob/main/nft/nft-item.fc#L137-L144) for exactly this purpose: ```func ;; ;; GET Methods ;; (int, int, slice, slice, cell) get_nft_data() method_id { (int init?, int index, slice collection_address, slice owner_address, cell content) = load_data(); return (init?, index, collection_address, owner_address, content); } ``` It will return all the needed data for you in the decoded fashion. And [here](https://github.com/slavafomin/tonweb/blob/typescripted/src/contract/token/nft/nft-item.ts#L83-L109) is how you would parse the result of this get method: ```typescript // Calling the get-method const result = await this.provider.call2( myAddress.toString(), 'get_nft_data' ); // Parsing the data returned by it const isInitialized = ( (expectBN(result[0]).toNumber() === -1) ); const index = expectBN(result[1]).toNumber(); const collectionAddress = ( parseAddressFromCell(result[2]) ); const ownerAddress = (isInitialized ? parseAddressFromCell(result[3]) : null ); const contentCell = result[4]; // Single NFT without a collection const contentUri = ((isInitialized && !collectionAddress) ? parseOffchainUriCell(contentCell) : null ); ```