Last active
January 23, 2024 00:19
-
-
Save namankumar/2ebd9dd157b606c60c9bce3c580ea15b to your computer and use it in GitHub Desktop.
Resource Calculation on Soroban
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var SorobanClient = require('soroban-client'); | |
var server = new SorobanClient.Server('https://rpc-futurenet.stellar.org'); | |
(async function main() { | |
const account = await server.getAccount("GAN2NFRHWTY5ZNF6OQOWHOZWISA6HFFNFCX2XQMU4FJ5GMG36O7GJHVN"); | |
const fee = 100; | |
const contract = new SorobanClient.Contract("CAO4K2VSHMLIKSE2PWZKD5HJH4I4VLT66T7IONQCWKTPZN2IG7GPNXQU"); | |
let transaction = new SorobanClient.TransactionBuilder(account, { | |
fee, networkPassphrase: SorobanClient.Networks.FUTURENET | |
}) | |
.addOperation( | |
contract.call("increment") | |
) | |
.setTimeout(30) | |
.build(); | |
const result = await server.simulateTransaction(transaction); | |
console.log(result) | |
const simulation_resp_json = await server._simulateTransaction(transaction); | |
parseCost(simulation_resp_json) | |
})() | |
function parseCost(content) { | |
const events = content.events.map((event) => { | |
const e = SorobanClient.xdr.DiagnosticEvent.fromXDR(event, 'base64') | |
if (e.event().type().name === 'diagnostic') | |
return 0 | |
return e.toXDR().length | |
}) | |
const events_and_return_value_size = ( | |
events.reduce((accumulator, currentValue) => accumulator + currentValue, 0) // events | |
+ Buffer.from(content.results[0].xdr, 'base64').length // return value size | |
) | |
const sorobanTransactionData = SorobanClient.xdr.SorobanTransactionData.fromXDR(content.transactionData, 'base64') | |
console.log({ | |
//the first two lines are incorrect. use sorobanTransactionData instead of `cost` | |
CPU_instructions: Number(content.cost.cpuInsns), | |
RAM: Number(content.cost.memBytes), | |
minResourceFee: content.minResourceFee, | |
ledger_read_bytes: sorobanTransactionData.resources().readBytes(), | |
ledger_write_bytes: sorobanTransactionData.resources().writeBytes(), | |
ledger_entry_reads: sorobanTransactionData.resources().footprint().readOnly().length, | |
ledger_entry_writes: sorobanTransactionData.resources().footprint().readWrite().length, | |
events_and_return_value_size, | |
transaction_size: Buffer.from(content.transactionData, 'base64').length, | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment