@atomicport/evm
Version:
Support Cross-Chain-Swap with HTLC on any blockchains
49 lines (48 loc) • 2.22 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.EvmErc20Htlc = void 0;
const ERC20_json_1 = __importDefault(require("./abis/ERC20.json"));
const HashedTimelockERC20_json_1 = __importDefault(require("./abis/HashedTimelockERC20.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 EvmErc20Htlc extends BaseHtlc_1.BaseHTLCService {
contractAddress;
constructor(providerEndpoint, contractAddress) {
super(providerEndpoint, contractAddress, HashedTimelockERC20_json_1.default.abi);
this.contractAddress = contractAddress;
}
/**
* Issue HTLC and obtain the key at the time of issue
*/
async lock(recipientAddress, senderAddress, secret, amount, tokenAddress, options) {
// Pre-register before issuing a transaction
const erc20TokenContract = new this.web3.eth.Contract(ERC20_json_1.default.abi, tokenAddress);
const gas = options?.gasLimit ?? 1000000;
const value = this.web3.utils.toWei(this.web3.utils.toBN(amount), 'finney');
await erc20TokenContract.methods
.approve(this.contractAddress, value)
.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, value)
.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 };
}
}
exports.EvmErc20Htlc = EvmErc20Htlc;