UNPKG

@fleupold/dex-contracts

Version:

Contracts for dFusion multi-token batch auction exchange

67 lines (66 loc) 3.21 kB
"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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const bn_js_1 = __importDefault(require("bn.js")); const logging_1 = require("../src/logging"); const log = logging_1.factory.getLogger("scripts.deposit"); const BatchExchange = artifacts.require("BatchExchange"); const ERC20 = artifacts.require("ERC20"); const argv = require("yargs") .option("accountId", { describe: "Depositor's account index", }) .option("tokenId", { describe: "Token to deposit", }) .option("amount", { describe: "Amount in to deposit (in 10**18 WEI, e.g. 1 = 1 ETH)", }) .demand(["accountId", "tokenId", "amount"]) .help(false) .version(false).argv; const zero_address = "0x0000000000000000000000000000000000000000"; module.exports = (callback) => __awaiter(void 0, void 0, void 0, function* () { try { // Is is possible that this does not account for token.decimals? const amount = new bn_js_1.default(web3.utils.toWei(String(argv.amount))); const instance = yield BatchExchange.deployed(); const accounts = yield web3.eth.getAccounts(); const depositor = yield accounts[argv.accountId]; log.info(`Using account ${depositor}`); const token_address = yield instance.tokenIdToAddressMap(argv.tokenId); if (token_address === zero_address) { callback(`Error: No token registered at index ${argv.tokenId}`); } const token = yield ERC20.at(token_address); const depositorBalance = yield token.balanceOf(depositor); if (depositorBalance.lt(amount)) { callback(`Error: Insufficient balance (${depositorBalance.toString()} < ${amount.toString()}).`); } const allowance = yield token.allowance(depositor, instance.address); if (allowance.lt(amount)) { log.info(`Submitting approval for amount ${amount.sub(allowance).toString()}`); yield token.approve(instance.address, amount.sub(allowance), { from: depositor, }); } yield instance.deposit(token_address, amount, { from: depositor }); const tradeable_at = (yield instance.getPendingDeposit(depositor, token_address))[1]; log.info(`Deposit successful! Can be traded as of batch ${tradeable_at}`); callback(); } catch (error) { callback(error); } });