@vechain.energy/gas
Version:
calculate estimated gas usage for transactions
138 lines (137 loc) • 5.66 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const postNodeMock = jest.fn().mockResolvedValue([
{
"data": "0x000000000000000000000000000000000000000000000000000009184e72a000",
"events": [],
"transfers": [],
"gasUsed": 591,
"reverted": false,
"vmError": ""
},
{
"data": "0x000000000000000000000000000000000000000000000000000009184e72a000",
"events": [],
"transfers": [],
"gasUsed": 200,
"reverted": false,
"vmError": ""
}
]);
const bentMock = jest.fn().mockReturnValue(postNodeMock);
jest.mock('bent', () => bentMock, { virtual: true });
const vmPrice_1 = __importDefault(require("./vmPrice"));
const constants_1 = require("./constants");
describe('vmPrice(clauses, nodeOrConnex, caller)', () => {
it('returns a number', () => __awaiter(void 0, void 0, void 0, function* () {
const clauses = [
{
to: "0x0000000000000000000000000000456E65726779",
value: 1,
data: "0x"
}
];
const price = yield (0, vmPrice_1.default)(clauses, 'https://node.vechain.energy');
expect(typeof price).toBe('number');
}));
it('returns a non-negative number', () => __awaiter(void 0, void 0, void 0, function* () {
const clauses = [
{
to: "0x0000000000000000000000000000456E65726779",
value: 1,
data: "0x"
}
];
const price = yield (0, vmPrice_1.default)(clauses, 'https://node.vechain.energy');
expect(price).toBeGreaterThanOrEqual(0);
}));
it('returns the VM gas used with VM_GAS added', () => __awaiter(void 0, void 0, void 0, function* () {
const clauses = [
{
to: "0x0000000000000000000000000000456E65726779",
value: 1,
data: "0x"
}
];
const price = yield (0, vmPrice_1.default)(clauses, 'https://node.vechain.energy');
expect(price).toEqual(constants_1.VM_GAS + 791);
// make sure the HTTP request was made
expect(bentMock).toHaveBeenCalledWith('https://node.vechain.energy', 'POST', 'json', 200);
expect(postNodeMock).toHaveBeenCalledWith('/accounts/*', {
clauses: clauses,
caller: undefined
});
}));
it('returns the 0 if no VM gas is used', () => __awaiter(void 0, void 0, void 0, function* () {
const clauses = [
{
to: "0x0000000000000000000000000000456E65726779",
value: 1,
data: "0x"
}
];
postNodeMock.mockResolvedValueOnce([
{
"data": "0x000000000000000000000000000000000000000000000000000009184e72a000",
"events": [],
"transfers": [],
"gasUsed": 0,
"reverted": false,
"vmError": ""
}
]);
const price = yield (0, vmPrice_1.default)(clauses, 'https://node.vechain.energy');
expect(price).toEqual(0);
// make sure the HTTP request was made
expect(bentMock).toHaveBeenCalledWith('https://node.vechain.energy', 'POST', 'json', 200);
expect(postNodeMock).toHaveBeenCalledWith('/accounts/*', {
clauses: clauses,
caller: undefined
});
}));
it('passes the caller', () => __awaiter(void 0, void 0, void 0, function* () {
const clauses = [
{
to: "0x0000000000000000000000000000456E65726779",
value: 1,
data: "0x"
}
];
const caller = '0x0000000000000000000000000000000000000000';
yield (0, vmPrice_1.default)(clauses, 'https://node.vechain.energy', caller);
// make sure the HTTP request was made
expect(bentMock).toHaveBeenCalledWith('https://node.vechain.energy', 'POST', 'json', 200);
expect(postNodeMock).toHaveBeenCalledWith('/accounts/*', {
clauses: clauses,
caller
});
}));
it('sets the caller to 0x...01 as default for contract creation', () => __awaiter(void 0, void 0, void 0, function* () {
const clauses = [
{
to: null,
value: 1,
data: "0x"
}
];
yield (0, vmPrice_1.default)(clauses, 'https://node.vechain.energy');
// make sure the HTTP request was made
expect(bentMock).toHaveBeenCalledWith('https://node.vechain.energy', 'POST', 'json', 200);
expect(postNodeMock).toHaveBeenCalledWith('/accounts/*', {
clauses: clauses,
caller: '0x0000000000000000000000000000000000000001'
});
}));
});