Development
...
Smart Contracts
Read Contracts State
1 min
as explained in docid\ smt0kwoldxbpwgbu1i1ht , the data stack has a fixed layout and can be read in 8 byte blocks the following example shows how to read parts of a https //github com/signum network/sips/blob/master/sip/sip 40 md contract, which are deployed through the https //signumart io this full example in the https //github com/signum network/signumjs/blob/main/examples/nodejs/advanced/contracts/viewcontractdata js const {address} = require("@signumjs/core"); const {contractdataview} = require("@signumjs/contracts"); const {amount} = require("@signumjs/util"); / this is an example of how to read data from a contract usually, contracts maintain a certain internal state, like ownership, paid dividends or whatever these data is publicly accessible and can be read relatively easily using contractdataview the requirement is that you know the positions of the values in the contracts data stack the data stack is divided into 8 byte segments and can be addressed using indices the following example uses parts of the layout of the nftsrc40 contract if you use your custom contracts, you might want to use the contract inspector to see you data stack https //contracts inspector ohager vercel app/ / const nftcontractdataindex = { owner 0, status 1, currentprice 2, platformaddress 3, platformfee 4, royaltiesfee 5, royaltiesowner 6, highestbidder 19, totaltimessold 27, totalbidsreceived 28, totalroyaltiesfee 29, totalplatformfee 30, totallikes 31, } // using a dedicated class for contract data view is a good practice class nftdataview { constructor(contract) { this id = contract at; // contractdataview gives us easy access to the 8 byte blocks this view = new contractdataview(contract); } getid() { return this id; } getownerid() { return this view\ getvariableasdecimal(nftcontractdataindex owner); } getroyaltiesownerid() { return this view\ getvariableasdecimal(nftcontractdataindex royaltiesowner); } getplatformid() { return this view\ getvariableasdecimal(nftcontractdataindex platformaddress); } getplatformfee() { return parseint( this view\ getvariableasdecimal(nftcontractdataindex platformfee), 10 ); } / and so on, and so forth / } const ledgerhosturls = { testnet 'http //localhost 6876', mainnet 'https //europe signum network', } function asaddress(id, prefix){ return address fromnumericid(id, prefix) getreedsolomonaddress() } / this advanced example shows how to interact with smart contracts, i e how to call methods / async function shownftdata({ledgerchoice, nft}) { try { const ledger = ledgerclientfactory create({ nodehost ledgerhosturls\[ledgertype]) } // this way we not only convert the contracts address to its id, but this method also asserts // that we have a formally valid address it's not guaranteed that the address really exists in the chain const nftid = address create(nft) getnumericid() const nftcontract = await ledger contract getcontract(nftid) const explorerurl = ledgerchoice === 'testnet' ? `https //t chain signum network/at/${nftid}` `https //chain signum network/at/${nftid}` const prefix = ledgerchoice === 'testnet'? 'ts' 's' const nftdataview = new nftdataview(nftcontract) console info(`stats of nft '${asaddress(nftid, prefix)}' ${explorerurl}`) console info(' ') console info('creator ', nftcontract creatorrs) console info('current owner ', asaddress(nftdataview\ getownerid(),prefix)) console info('current price ', nftdataview\ getcurrentprice() getsigna(), 'signa') console info('current status ', nftdataview\ getstatus()) console info('number of sales ', nftdataview\ gettotaltimessold()) console info('likes ', nftdataview\ gettotallikes()) } catch (e) { console error(e); } } (async () => { await shownftdata({ ledgerchoice 'testnet', nft '5704341721881819656' }); })();