UNPKG

@atomicport/evm

Version:

Support Cross-Chain-Swap with HTLC on any blockchains

61 lines (60 loc) 2.65 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.EvmErc721Htlc = void 0; const ERC721_json_1 = __importDefault(require("./abis/ERC721.json")); const HashedTimelockERC721_json_1 = __importDefault(require("./abis/HashedTimelockERC721.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 EvmErc721Htlc extends BaseHtlc_1.BaseHTLCService { contractAddress; constructor(providerEndpoint, contractAddress) { super(providerEndpoint, contractAddress, HashedTimelockERC721_json_1.default.abi); this.contractAddress = contractAddress; } /** * Issue HTLC and obtain the key at the time of issue */ async lock(recipientAddress, senderAddress, secret, tokenId, tokenAddress, options) { // Pre-register before issuing a transaction const erc721TokenContract = new this.web3.eth.Contract(ERC721_json_1.default.abi, tokenAddress); const gas = options?.gasLimit ?? 1000000; await erc721TokenContract.methods .approve(this.contractAddress, tokenId) .send({ from: senderAddress, gas: gas.toString() }); // Issue lock transaction const lockPeriod = Math.floor(Date.now() / 1000) + (options?.lockSeconds ?? 3600); return await this.contract.methods .newContract(recipientAddress, secret, lockPeriod, tokenAddress, tokenId) .send({ from: senderAddress, gas: gas.toString() }); } /** * Receive tokens stored under the key at the time of HTLC generation */ async withDraw(contractId, senderAddress, proof, gasLimit) { const gas = gasLimit ?? 1000000; const res = await this.contract.methods .withdraw(contractId, proof) .send({ from: senderAddress, gas: gas.toString() }); return { result: res }; } /** * for development * create erc721 token */ async createToken(tokenAddress, senderAddress, tokenId) { const erc721TokenContract = new this.web3.eth.Contract(ERC721_json_1.default.abi, tokenAddress); const res = await erc721TokenContract.methods .mint(senderAddress, tokenId) .send({ from: senderAddress, gas: (1000000).toString() }); return { tokenId: res.events.Transfer.returnValues.tokenId, }; } } exports.EvmErc721Htlc = EvmErc721Htlc;