UNPKG

@atomicport/evm

Version:

Support Cross-Chain-Swap with HTLC on any blockchains

40 lines (39 loc) 1.68 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.EvmHtlc = void 0; const HashedTimelock_json_1 = __importDefault(require("./abis/HashedTimelock.json")); const BaseHtlc_1 = require("./models/BaseHtlc"); /** * HTLC operations on the Ethereum Test Net. * Passing a value to the constructor will overwrite the specified value. */ class EvmHtlc extends BaseHtlc_1.BaseHTLCService { constructor(providerEndpoint, contractAddress) { super(providerEndpoint, contractAddress, HashedTimelock_json_1.default.abi); } /** * Issue HTLC and obtain the key at the time of issue */ async lock(recipientAddress, senderAddress, secret, amount, options) { const value = this.web3.utils.toWei(this.web3.utils.toBN(amount), 'finney'); const lockPeriod = Math.floor(Date.now() / 1000) + (options?.lockSeconds ?? 3600); const gas = options?.gasLimit ?? 1000000; return await this.contract.methods .newContract(recipientAddress, secret, lockPeriod) .send({ from: senderAddress, gas: gas.toString(), value }); } /** * Receive tokens stored under the key at the time of HTLC generation */ async withDraw(contractId, senderAddress, proof, gasLimit) { const gas = gasLimit ?? 1000000; const result = await this.contract.methods .withdraw(contractId, proof) .send({ from: senderAddress, gas: gas.toString() }); return { result: result }; } } exports.EvmHtlc = EvmHtlc;