ethers.js/packages/abi/lib/coders/number.js

64 lines
2.7 KiB
JavaScript
Raw Permalink Normal View History

"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
2021-03-07 18:24:04 -05:00
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
2021-03-07 18:24:04 -05:00
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
2021-03-07 18:24:04 -05:00
exports.NumberCoder = void 0;
var bignumber_1 = require("@ethersproject/bignumber");
var constants_1 = require("@ethersproject/constants");
var abstract_coder_1 = require("./abstract-coder");
var NumberCoder = /** @class */ (function (_super) {
__extends(NumberCoder, _super);
function NumberCoder(size, signed, localName) {
var _this = this;
var name = ((signed ? "int" : "uint") + (size * 8));
_this = _super.call(this, name, name, localName, false) || this;
_this.size = size;
_this.signed = signed;
return _this;
}
2020-11-23 03:43:28 -05:00
NumberCoder.prototype.defaultValue = function () {
return 0;
};
NumberCoder.prototype.encode = function (writer, value) {
var v = bignumber_1.BigNumber.from(value);
// Check bounds are safe for encoding
2020-04-15 18:28:04 -04:00
var maxUintValue = constants_1.MaxUint256.mask(writer.wordSize * 8);
if (this.signed) {
2020-04-15 18:28:04 -04:00
var bounds = maxUintValue.mask(this.size * 8 - 1);
if (v.gt(bounds) || v.lt(bounds.add(constants_1.One).mul(constants_1.NegativeOne))) {
this._throwError("value out-of-bounds", value);
}
}
2020-04-15 18:28:04 -04:00
else if (v.lt(constants_1.Zero) || v.gt(maxUintValue.mask(this.size * 8))) {
this._throwError("value out-of-bounds", value);
}
2020-04-15 18:28:04 -04:00
v = v.toTwos(this.size * 8).mask(this.size * 8);
if (this.signed) {
v = v.fromTwos(this.size * 8).toTwos(8 * writer.wordSize);
}
return writer.writeValue(v);
};
NumberCoder.prototype.decode = function (reader) {
2020-04-15 18:28:04 -04:00
var value = reader.readValue().mask(this.size * 8);
if (this.signed) {
value = value.fromTwos(this.size * 8);
}
return reader.coerce(this.name, value);
};
return NumberCoder;
}(abstract_coder_1.Coder));
exports.NumberCoder = NumberCoder;
2020-07-13 08:03:56 -04:00
//# sourceMappingURL=number.js.map