UNPKG

cic-client

Version:

Typescript libraries for building CIC client applications

320 lines (319 loc) 14.6 kB
"use strict"; /** * Provides processing of Ethereum transaction receipts relevant for the CIC network. * * @module helper */ 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 __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; exports.__esModule = true; exports.TokenDeclarationHelper = exports.TransactionHelper = exports.DeclaratorHelper = void 0; var erc20_1 = require("./common/erc20"); var convert_1 = require("./bancor/convert"); var solidity_1 = require("./solidity"); var const_1 = require("./const"); /** * Reduced, concrete view of a transaction receipt. * * @todo Is there a way of importing the web3 type instead? */ var Receipt = /** @class */ (function () { function Receipt() { } return Receipt; }()); /** * Receipt processor within context of a given registry. * */ var TransactionHelper = /** @class */ (function () { /** * * @param registry The registry context to use */ function TransactionHelper(w3, registry) { this.w3 = w3; this.registry = registry; this.ontransfer = function (t) { console.debug('transfer ', t); }; this.onconversion = function (c) { console.debug('convert ', c); }; } /** * Processes a single receipt to find conversion and token transfer events. * * If a transfer is found, ontransfer callback will be called with a Transfer instance. * If a conversion is found, onconvsrsion callback will be called with a Conversion instance. * * @param r A Web3 transaction receipt. * @todo The from-amount of convert is currently wrong, but be retrieved from the initial transfer log entry instead. */ TransactionHelper.prototype.processReceipt = function (r) { return __awaiter(this, void 0, void 0, function () { var self, logs, convert_log, token_txs, i, contractAddress, t, e_1, d, t, e_2, e_3, conversion, _i, token_txs_1, a, transfer; return __generator(this, function (_a) { switch (_a.label) { case 0: self = this; logs = r.logs; convert_log = undefined; token_txs = []; i = 0; _a.label = 1; case 1: if (!(i < logs.length)) return [3 /*break*/, 15]; contractAddress = logs[i].address; _a.label = 2; case 2: _a.trys.push([2, 4, , 14]); return [4 /*yield*/, this.registry.getToken(contractAddress)]; case 3: t = _a.sent(); token_txs.push([r.status, t, logs[i]]); return [3 /*break*/, 14]; case 4: e_1 = _a.sent(); _a.label = 5; case 5: _a.trys.push([5, 8, , 13]); return [4 /*yield*/, this.registry.declaratorHelper.getTrustedTokenDeclaration('AddressDeclarator', contractAddress)]; case 6: d = _a.sent(); console.debug('trust record for ' + contractAddress + ' found', d); return [4 /*yield*/, this.registry.addToken(contractAddress)]; case 7: t = _a.sent(); token_txs.push([r.status, t, logs[i]]); return [3 /*break*/, 13]; case 8: e_2 = _a.sent(); _a.label = 9; case 9: _a.trys.push([9, 11, , 12]); return [4 /*yield*/, this.registry.getContract(contractAddress)]; case 10: _a.sent(); convert_log = logs[i]; return [3 /*break*/, 15]; case 11: e_3 = _a.sent(); return [3 /*break*/, 14]; case 12: return [3 /*break*/, 13]; case 13: return [3 /*break*/, 14]; case 14: i++; return [3 /*break*/, 1]; case 15: if (!(convert_log !== undefined)) return [3 /*break*/, 17]; return [4 /*yield*/, convert_1.Conversion.processLog(this.w3, this.registry, r.status, convert_log)]; case 16: conversion = _a.sent(); if (conversion !== undefined) { this.onconversion(conversion); } return [3 /*break*/, 21]; case 17: _i = 0, token_txs_1 = token_txs; _a.label = 18; case 18: if (!(_i < token_txs_1.length)) return [3 /*break*/, 21]; a = token_txs_1[_i]; return [4 /*yield*/, erc20_1.Transfer.processLog(this.w3, a[0], a[1], a[2])]; case 19: transfer = _a.sent(); if (transfer !== undefined) { this.ontransfer(transfer); } _a.label = 20; case 20: _i++; return [3 /*break*/, 18]; case 21: return [2 /*return*/]; } }); }); }; return TransactionHelper; }()); exports.TransactionHelper = TransactionHelper; /** * Token query within the context of a registry. */ var TokenDeclarationHelper = /** @class */ (function () { /** * * @param w3 * @param registry The registry context to use. */ function TokenDeclarationHelper(w3, registry) { this.registry = registry; this.w3 = w3; } /** * Check if a token fits the ERC20 standard. * * @param tokenAddress The address of the token to be checked. */ TokenDeclarationHelper.prototype.assertERC20TokenType = function (tokenAddress) { return __awaiter(this, void 0, void 0, function () { var erc20Abi, tokenContract, tokenSymbol, e_4; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.registry.getAbi('ERC20')]; case 1: erc20Abi = _a.sent(); _a.label = 2; case 2: _a.trys.push([2, 4, , 5]); tokenContract = new this.w3.eth.Contract(erc20Abi, tokenAddress); return [4 /*yield*/, tokenContract.methods.symbol().call()]; case 3: tokenSymbol = _a.sent(); if (tokenSymbol !== undefined) { return [2 /*return*/, true]; } return [3 /*break*/, 5]; case 4: e_4 = _a.sent(); console.log('contract ' + tokenAddress + ' is not an ERC20 token'); return [3 /*break*/, 5]; case 5: return [2 /*return*/, false]; } }); }); }; return TokenDeclarationHelper; }()); exports.TokenDeclarationHelper = TokenDeclarationHelper; /** * Declarator query within the context of a registry. */ var DeclaratorHelper = /** @class */ (function () { /** * * @param w3 * @param registry The registry context to use. */ function DeclaratorHelper(w3, registry) { this.registry = registry; this.trusts = []; this.tokenHelper = new TokenDeclarationHelper(w3, registry); } /** * Add an address to list of declarators to trust. * * @param address The address to be added to the trust list */ DeclaratorHelper.prototype.addTrust = function (address) { this.trusts.push(address); }; /** * Check for trust records of a token * * if there are no trust records, an error will be thrown. * * @param tokenRegistryContractName The name of the token in the token registry. * @param tokenAddress The address of the contract used to deploy the token. * @param checkInterface A boolean on whether to to check the interface. */ DeclaratorHelper.prototype.getTrustedTokenDeclaration = function (tokenRegistryContractName, tokenAddress, checkInterface) { if (checkInterface === void 0) { checkInterface = false; } return __awaiter(this, void 0, void 0, function () { var i, isERC20, _a; return __generator(this, function (_b) { switch (_b.label) { case 0: i = 0; _b.label = 1; case 1: if (!(i < this.trusts.length)) return [3 /*break*/, 7]; console.debug('checking for trust record by ' + this.trusts[i] + ' for token ' + tokenAddress); _b.label = 2; case 2: _b.trys.push([2, 5, , 6]); return [4 /*yield*/, this.tokenHelper.assertERC20TokenType(tokenAddress)]; case 3: isERC20 = _b.sent(); if (isERC20) { console.log('contract ' + tokenAddress + ' is an ERC20 token'); } return [4 /*yield*/, this.getTokenDeclaration(tokenRegistryContractName, this.trusts[i], tokenAddress, checkInterface)]; case 4: return [2 /*return*/, _b.sent()]; case 5: _a = _b.sent(); return [3 /*break*/, 6]; case 6: i++; return [3 /*break*/, 1]; case 7: throw new Error('no trusted records for token ' + tokenAddress); } }); }); }; /** * Check for trust records for a token by a declarator. * * @param tokenRegistryContractName The name of the token in the token registry. * @param declarator The address of the declarator used to check for trust. * @param tokenAddress The address of the contract used to deploy the token. * @param checkInterface A boolean on whether to to check the interface. */ DeclaratorHelper.prototype.getTokenDeclaration = function (tokenRegistryContractName, declarator, tokenAddress, checkInterface) { if (checkInterface === void 0) { checkInterface = false; } return __awaiter(this, void 0, void 0, function () { var tokenRegistryContract, declarationParts; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.registry.getContractByName(tokenRegistryContractName, 'AddressDeclarator', [solidity_1.interfaceCodes.Declarator])]; case 1: tokenRegistryContract = _a.sent(); return [4 /*yield*/, tokenRegistryContract.methods.declaration(declarator, tokenAddress).call()]; case 2: declarationParts = _a.sent(); if (declarationParts.length == 1 && declarationParts[0] == const_1.zeroAddress) { throw new Error('no declarations found for declarator "' + declarator + '" address "' + tokenAddress + '"'); } return [2 /*return*/, declarationParts]; } }); }); }; return DeclaratorHelper; }()); exports.DeclaratorHelper = DeclaratorHelper;