UNPKG

@atomicport/evm

Version:

Support Cross-Chain-Swap with HTLC on any blockchains

54 lines (53 loc) 2.27 kB
import ERC721Abi from './abis/ERC721.json'; import HashedTimelockERC721 from './abis/HashedTimelockERC721.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 EvmErc721Htlc extends BaseHTLCService { contractAddress; constructor(providerEndpoint, contractAddress) { super(providerEndpoint, contractAddress, HashedTimelockERC721.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(ERC721Abi.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(ERC721Abi.abi, tokenAddress); const res = await erc721TokenContract.methods .mint(senderAddress, tokenId) .send({ from: senderAddress, gas: (1000000).toString() }); return { tokenId: res.events.Transfer.returnValues.tokenId, }; } }