@0xcert/ethereum-value-ledger
Version:
Value ledger module for currency management on the Ethereum blockchain.
141 lines • 6.25 kB
JavaScript
"use strict";
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 });
exports.ValueLedger = void 0;
const ethereum_generic_provider_1 = require("@0xcert/ethereum-generic-provider");
const ethereum_utils_1 = require("@0xcert/ethereum-utils");
const scaffold_1 = require("@0xcert/scaffold");
const approve_account_1 = require("../mutations/approve-account");
const deploy_1 = require("../mutations/deploy");
const transfer_1 = require("../mutations/transfer");
const transfer_from_1 = require("../mutations/transfer-from");
const get_allowance_1 = require("../queries/get-allowance");
const get_balance_1 = require("../queries/get-balance");
const get_info_1 = require("../queries/get-info");
class ValueLedger {
constructor(provider, id) {
this._provider = provider;
this._id = this._provider.encoder.normalizeAddress(id);
}
static deploy(provider, recipe) {
return __awaiter(this, void 0, void 0, function* () {
return deploy_1.default(provider, recipe);
});
}
static getInstance(provider, id) {
return new this(provider, id);
}
get id() {
return this._id;
}
get provider() {
return this._provider;
}
getApprovedValue(accountId, spenderId) {
return __awaiter(this, void 0, void 0, function* () {
accountId = this._provider.encoder.normalizeAddress(accountId);
spenderId = this._provider.encoder.normalizeAddress(spenderId);
return get_allowance_1.default(this, accountId, spenderId);
});
}
getBalance(accountId) {
return __awaiter(this, void 0, void 0, function* () {
accountId = this._provider.encoder.normalizeAddress(accountId);
return get_balance_1.default(this, accountId);
});
}
getInfo() {
return __awaiter(this, void 0, void 0, function* () {
return get_info_1.default(this);
});
}
isApprovedValue(value, accountId, spenderId) {
return __awaiter(this, void 0, void 0, function* () {
accountId = this._provider.encoder.normalizeAddress(accountId);
spenderId = this._provider.encoder.normalizeAddress(spenderId);
const approved = yield get_allowance_1.default(this, accountId, spenderId);
return ethereum_utils_1.bigNumberify(approved).gte(ethereum_utils_1.bigNumberify(value));
});
}
approveValue(value, accountId) {
return __awaiter(this, void 0, void 0, function* () {
accountId = this._provider.encoder.normalizeAddress(accountId);
const approvedValue = yield this.getApprovedValue(this.provider.accountId, accountId);
if (!ethereum_utils_1.bigNumberify(value).isZero() && !ethereum_utils_1.bigNumberify(approvedValue).isZero()) {
throw new scaffold_1.ProviderError(scaffold_1.ProviderIssue.ERC20_APPROVAL_RACE_CONDITION);
}
return approve_account_1.default(this, accountId, value);
});
}
disapproveValue(accountId) {
return __awaiter(this, void 0, void 0, function* () {
accountId = this._provider.encoder.normalizeAddress(accountId);
return approve_account_1.default(this, accountId, '0');
});
}
transferValue(recipe) {
return __awaiter(this, void 0, void 0, function* () {
const senderId = this._provider.encoder.normalizeAddress(recipe.senderId);
const receiverId = this._provider.encoder.normalizeAddress(recipe.receiverId);
return recipe.senderId
? transfer_from_1.default(this, senderId, receiverId, recipe.value)
: transfer_1.default(this, receiverId, recipe.value);
});
}
getContext() {
return [
{
name: 'Transfer',
topic: '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
types: [
{
kind: ethereum_generic_provider_1.MutationEventTypeKind.INDEXED,
name: 'from',
type: 'address',
},
{
kind: ethereum_generic_provider_1.MutationEventTypeKind.INDEXED,
name: 'to',
type: 'address',
},
{
kind: ethereum_generic_provider_1.MutationEventTypeKind.NORMAL,
name: 'value',
type: 'uint256',
},
],
},
{
name: 'Approval',
topic: '0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925',
types: [
{
kind: ethereum_generic_provider_1.MutationEventTypeKind.INDEXED,
name: 'owner',
type: 'address',
},
{
kind: ethereum_generic_provider_1.MutationEventTypeKind.INDEXED,
name: 'spender',
type: 'address',
},
{
kind: ethereum_generic_provider_1.MutationEventTypeKind.NORMAL,
name: 'value',
type: 'uint256',
},
],
},
];
}
}
exports.ValueLedger = ValueLedger;
//# sourceMappingURL=ledger.js.map