@helium/transactions
Version:
Construct and serialize Helium blockchain transaction primatives
72 lines • 3.54 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const proto_1 = __importDefault(require("@helium/proto"));
const Transaction_1 = __importDefault(require("./Transaction"));
const utils_1 = require("./utils");
class SubnetworkRewardsV1 extends Transaction_1.default {
constructor(opts) {
super();
this.type = 'subnetwork_rewards_v1';
this.tokenType = opts.tokenType;
this.startEpoch = opts.startEpoch;
this.endEpoch = opts.endEpoch;
this.rewardServerSignature = opts.rewardServerSignature;
this.rewards = opts.rewards;
}
serialize() {
const Txn = proto_1.default.helium.blockchain_txn;
const subnetworkRewards = this.toProto();
const txn = Txn.create({ subnetworkRewards });
return Txn.encode(txn).finish();
}
message() {
const SubnetworkRewards = proto_1.default.helium.blockchain_txn_subnetwork_rewards_v1;
const subnetworkRewards = this.toProto(true);
return SubnetworkRewards.encode(subnetworkRewards).finish();
}
async sign({ keypair }) {
const serialized = this.message();
this.rewardServerSignature = await keypair.sign(serialized);
return this;
}
static fromString(serializedTxnString) {
var _a, _b, _c, _d, _e;
const buf = Buffer.from(serializedTxnString, 'base64');
const decoded = proto_1.default.helium.blockchain_txn.decode(buf);
const tokenType = (0, utils_1.toTicker)((0, utils_1.toNumber)((_a = decoded.subnetworkRewards) === null || _a === void 0 ? void 0 : _a.tokenType));
const startEpoch = (0, utils_1.toNumber)((_b = decoded.subnetworkRewards) === null || _b === void 0 ? void 0 : _b.startEpoch) || 0;
const endEpoch = (0, utils_1.toNumber)((_c = decoded.subnetworkRewards) === null || _c === void 0 ? void 0 : _c.endEpoch) || 0;
const rewards = (((_d = decoded.subnetworkRewards) === null || _d === void 0 ? void 0 : _d.rewards) || []).map((p) => ({
account: (0, utils_1.toAddressable)(p.account),
amount: (0, utils_1.toNumber)(p.amount),
}));
const rewardServerSignature = (0, utils_1.toUint8Array)((_e = decoded.subnetworkRewards) === null || _e === void 0 ? void 0 : _e.rewardServerSignature);
return new SubnetworkRewardsV1({
tokenType,
startEpoch,
endEpoch,
rewards,
rewardServerSignature,
});
}
toProto(forSigning = false) {
const SubnetworkRewards = proto_1.default.helium.blockchain_txn_subnetwork_rewards_v1;
const SubnetworkReward = proto_1.default.helium.subnetwork_reward;
const rewards = this.rewards.map(({ account, amount }) => SubnetworkReward.create({
account: (0, utils_1.toUint8Array)(account === null || account === void 0 ? void 0 : account.bin),
amount,
}));
return SubnetworkRewards.create({
tokenType: (0, utils_1.toTokenType)({ ticker: this.tokenType }),
startEpoch: this.startEpoch,
endEpoch: this.endEpoch,
rewards,
rewardServerSignature: this.rewardServerSignature && !forSigning ? (0, utils_1.toUint8Array)(this.rewardServerSignature) : null,
});
}
}
exports.default = SubnetworkRewardsV1;
//# sourceMappingURL=SubnetworkRewardsV1.js.map