UNPKG

@factorial-finance/blueprint-node

Version:

blueprint-node-plugin

121 lines (120 loc) 4.18 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.parseStack = parseStack; exports.serializeStack = serializeStack; exports.convertTransaction = convertTransaction; exports.addrToBigInt = addrToBigInt; const core_1 = require("@ton/core"); function parseStack(stack) { const stackItems = stack ? stack.map((item) => { const [type, value] = item; if (type === 'num') { return { type: 'int', value: BigInt(value) }; } else if (type === 'tvm.Cell') { return { type: 'cell', cell: core_1.Cell.fromBoc(Buffer.from(value, 'base64'))[0] }; } else if (type === 'tvm.Slice') { return { type: 'slice', cell: core_1.Cell.fromBoc(Buffer.from(value, 'base64'))[0] }; } else if (type === 'tvm.Builder') { return { type: 'builder', cell: core_1.Cell.fromBoc(Buffer.from(value, 'base64'))[0] }; } throw new Error('Unsupported stack item type: ' + type); }) : []; return stackItems; } function serializeStack(src) { const stack = []; for (const s of src) { if (s.type === 'int') { stack.push(['num', s.value.toString()]); } else if (s.type === 'cell') { stack.push(['cell', { bytes: s.cell.toBoc().toString('base64') }]); } else if (s.type === 'slice') { stack.push(['slice', { bytes: s.cell.toBoc().toString('base64') }]); } else if (s.type === 'builder') { stack.push(['builder', { bytes: s.cell.toBoc().toString('base64') }]); } else if (s.type === 'null') { stack.push(['null']); } else { throw new Error('Unsupported stack item type: ' + s.type); } } return stack; } function convertTransaction(tx) { // Convert blockchain transaction to API format const inMsg = tx.inMessage ? convertMessage(tx.inMessage) : undefined; const outMsgs = tx.outMessages ? Array.from(tx.outMessages.values()).map(convertMessage).filter(msg => !!msg) : []; return { data: tx.raw.toBoc().toString("base64"), utime: tx.now, transaction_id: { lt: tx.lt.toString(), hash: tx.hash().toString("base64") }, fee: tx.totalFees.coins.toString(), storage_fee: "0", other_fee: "0", in_msg: inMsg, out_msgs: outMsgs }; } function convertMessage(msg) { if (!msg) return undefined; if (msg.info.type === "external-in") { return { source: msg.info.src?.toString() || "", destination: msg.info.dest?.toString() || "", value: "0", fwd_fee: "0", ihr_fee: "0", created_lt: "0", body_hash: msg.body.hash().toString("base64"), msg_data: { '@type': 'msg.dataRaw', 'body': msg.body.toBoc().toString("base64") }, message: msg.body.toBoc().toString("base64"), }; } else if (msg.info.type === "internal") { return { source: msg.info.src?.toString() || "", destination: msg.info.dest?.toString() || "", value: msg.info.value?.coins.toString() || "0", fwd_fee: msg.info.forwardFee.toString() || "0", ihr_fee: msg.info.ihrFee.toString() || "0", created_lt: msg.info.createdLt?.toString() || "0", body_hash: msg.body.hash().toString("base64"), msg_data: { '@type': 'msg.dataRaw', 'body': msg.body.toBoc().toString("base64") }, message: msg.body.toBoc().toString("base64"), }; } return undefined; } function addrToBigInt(address) { if (typeof address === 'string') { return BigInt("0x" + core_1.Address.parse(address).hash.toString('hex')); ; } else if (typeof address === 'bigint') { return address; } else if (address instanceof core_1.Address) { return BigInt("0x" + address.hash.toString("hex")); } else { throw Error(""); } }