"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Datawriter = void 0;
const xrpio_1 = require("xrpio");
class Datawriter {
    config;
    constructor(config) {
        this.config = config;
    }
    async callEndpoint(endpointName, contractAddress, parameter, fee) {
        const xrpio = new xrpio_1.xrpIO(this.config.xrpNode);
        await xrpio.connect();
        try {
            const dataHash = await xrpio.treeWrite(JSON.stringify(parameter), this.config.receiveAddress, this.config.sendWallet.secret);
            await xrpio.writeRaw({
                data: JSON.stringify({
                    endpoint: endpointName,
                    data: dataHash
                })
            }, contractAddress, this.config.sendWallet.secret, undefined, fee ? String(fee) : undefined);
        }
        catch (e) {
            console.log(e);
        }
        finally {
            await xrpio.disconnect();
        }
    }
}
exports.Datawriter = Datawriter;
(async () => {
    const owner_dw = new Datawriter({
        receiveAddress: "rDuXvMYNCEJCYDMFQykcafXhgi2NvMWqR",
        sendWallet: {
            address: "rUsPfG1hn6w6is28p5FUDWdMwvCo1iHrYq",
            secret: "sEdTMicaTVmfsVMLhxrufyAzEQSnsaP"
        },
        xrpNode: "wss://s.altnet.rippletest.net:51233"
    });
    //await owner_dw.callEndpoint('submit', 'rLaXUiYvW1EMns69PsAfwSLb2VgNtPpZwq', { title: "1", body: "2", from: "3" }, 100)
    const user_dw = new Datawriter({
        receiveAddress: "rwqCiEr3SLF43rAduhCChkR2K1XDhiqx5g",
        sendWallet: {
            address: "rHWN4X3hbodryX8H1EoPvmMV7AFHngEiBe",
            secret: "sEdTdLPWvz69UAUmt1zYijTyWheER9u"
        },
        xrpNode: "wss://s.altnet.rippletest.net:51233"
    });
    //await owner_dw.callEndpoint('submit', 'rLaXUiYvW1EMns69PsAfwSLb2VgNtPpZwq', { title: "1", body: "2", from: "3" }, 100)
    //await user_dw.callEndpoint('restricted', 'rLaXUiYvW1EMns69PsAfwSLb2VgNtPpZwq', { title: "user", body: "user", from: "user" })
    //await owner_dw.callEndpoint('restricted', 'rLaXUiYvW1EMns69PsAfwSLb2VgNtPpZwq', { title: "owner", body: "owner", from: "owner" })
    await user_dw.callEndpoint('setTns', 'rLaXUiYvW1EMns69PsAfwSLb2VgNtPpZwq', { hash: '01708abcF00636CE10E191FD782DBDC8F4076F28404BD88EBC31DE42DD084C0944B', name: 'test' });
})();
