UNPKG

@atomicport/evm

Version:

Support Cross-Chain-Swap with HTLC on any blockchains

33 lines (32 loc) 1.37 kB
import HashedTimelockAbi from './abis/HashedTimelock.json'; import { BaseHTLCService } from './models/BaseHtlc'; /** * HTLC operations on the Ethereum Test Net. * Passing a value to the constructor will overwrite the specified value. */ export class EvmHtlc extends BaseHTLCService { constructor(providerEndpoint, contractAddress) { super(providerEndpoint, contractAddress, HashedTimelockAbi.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 }; } }