@0xcert/ethereum-value-ledger
Version:
Value ledger module for currency management on the Ethereum blockchain.
77 lines • 4 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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const ethereum_generic_provider_1 = require("@0xcert/ethereum-generic-provider");
const ethereum_sandbox_1 = require("@0xcert/ethereum-sandbox");
const spec_1 = require("@specron/spec");
const ledger_1 = require("../../../core/ledger");
const spec = new spec_1.Spec();
spec.before((stage) => __awaiter(void 0, void 0, void 0, function* () {
const protocol = new ethereum_sandbox_1.Protocol(stage.web3);
stage.set('protocol', yield protocol.deploy());
}));
spec.before((stage) => __awaiter(void 0, void 0, void 0, function* () {
const accounts = yield stage.web3.eth.getAccounts();
stage.set('coinbase', accounts[0]);
stage.set('bob', accounts[1]);
stage.set('jane', accounts[2]);
stage.set('sara', accounts[3]);
}));
spec.before((stage) => __awaiter(void 0, void 0, void 0, function* () {
const provider = new ethereum_generic_provider_1.GenericProvider({
client: stage.web3,
accountId: stage.get('coinbase'),
requiredConfirmations: 0,
});
stage.set('provider', provider);
}));
spec.before((stage) => __awaiter(void 0, void 0, void 0, function* () {
const provider = stage.get('provider');
const ledgerId = stage.get('protocol').erc20.instance.options.address;
const actionsGatewayId = stage.get('protocol').actionsGateway.instance.options.address;
stage.set('ledger', new ledger_1.ValueLedger(provider, ledgerId));
}));
spec.test('approves account for value transfer', (ctx) => __awaiter(void 0, void 0, void 0, function* () {
const ledger = ctx.get('ledger');
const coinbase = ctx.get('coinbase');
const bob = ctx.get('bob');
const token = ctx.get('protocol').erc20;
const value = '300000000000000000000000';
const mutation = yield ledger.approveValue(value, bob);
yield mutation.complete();
ctx.is(mutation.logs[0].event, 'Approval');
ctx.is(yield token.instance.methods.allowance(coinbase, bob).call(), value);
}));
spec.test('fails to reapprove account without reseting approval', (ctx) => __awaiter(void 0, void 0, void 0, function* () {
const ledger = ctx.get('ledger');
const coinbase = ctx.get('coinbase');
const jane = ctx.get('jane');
const token = ctx.get('protocol').erc20;
const value = '300000000000000000000000';
yield ledger.approveValue(value, jane);
ctx.is(yield token.instance.methods.allowance(coinbase, jane).call(), value);
yield ctx.throws(() => ledger.approveValue(value, jane));
}));
spec.test('reapprove account by reseting approval', (ctx) => __awaiter(void 0, void 0, void 0, function* () {
const ledger = ctx.get('ledger');
const coinbase = ctx.get('coinbase');
const sara = ctx.get('sara');
const token = ctx.get('protocol').erc20;
const value = '300000000000000000000000';
yield ledger.approveValue(value, sara);
ctx.is(yield token.instance.methods.allowance(coinbase, sara).call(), value);
yield ledger.approveValue('0', sara);
ctx.is(yield token.instance.methods.allowance(coinbase, sara).call(), '0');
yield ledger.approveValue(value, sara);
ctx.is(yield token.instance.methods.allowance(coinbase, sara).call(), value);
}));
exports.default = spec;
//# sourceMappingURL=approve-value-instance-method.test.js.map