UNPKG

@lucoadam/zebec-wormhole-sdk

Version:

This sdk can be use to transfer assets across chains and to interact with the Zebec's xchain bridge smart contracts for passing message from EVM chain to solana specially to utilize the features of Zebec Streaming and Zebec Multisig Streaming protocol.

307 lines (302 loc) 16.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ZebecEthBridgeClient = void 0; var _ethers = require("ethers"); var _wormholeSdk = require("@certusone/wormhole-sdk"); var _utils = require("../utils"); var _Messenger__factory = require("./factory/Messenger__factory"); class ZebecEthBridgeClient { // todo: fee is hard coded ArbitaryFee = _ethers.ethers.utils.parseEther("0.0000000000000001"); get contractAddress() { return this._contractAddress; } /** * Constructs an client instance to interact with evm bridge contract * @param contractAddress address of the bridge contract address * @param signer evm signer * @param sourceChain wormhole chain id of deployed evm contract */ constructor(contractAddress, signer, sourceChain) { this._contractAddress = contractAddress; this._contract = _Messenger__factory.Messenger__factory.connect(this._contractAddress, signer); this._chainId = sourceChain; } async registerEmitterAddress(chainId, emitterAddress) { const BufferChainId = _ethers.BigNumber.from(chainId); const emitterAddressBuf = Buffer.from(emitterAddress); return (await this._contract.registerApplicationContracts(BufferChainId, emitterAddressBuf)).wait(); } async depositSol(amount, depositor) { this.checkEthAddress(depositor, this._chainId); this.checkAmount(parseFloat(amount)); const depositorBuf = Buffer.from(depositor); const parsedAmount = _ethers.ethers.utils.parseUnits(amount, _wormholeSdk.WSOL_DECIMALS); return (await this._contract.process_deposit_sol(parsedAmount, depositorBuf, this.ArbitaryFee)).wait(); } async depositToken(amount, depositor, tokenMint) { this.checkEthAddress(depositor, this._chainId); this.checkSolanaAddress(tokenMint); this.checkAmount(parseFloat(amount)); const depositorBuf = Buffer.from((0, _wormholeSdk.tryNativeToUint8Array)(depositor, this._chainId)); const tokenMintBuf = Buffer.from((0, _wormholeSdk.tryNativeToUint8Array)(tokenMint, "solana")); const decimals = await (0, _utils.getDecimals)(tokenMint); const parsedAmount = _ethers.ethers.utils.parseUnits(amount, decimals); return (await this._contract.process_deposit_token(parsedAmount, depositorBuf, tokenMintBuf, this.ArbitaryFee, { gasLimit: "200000", value: this.ArbitaryFee })).wait(); } async withdrawSol(amount, sender) { this.checkAmount(parseFloat(amount)); this.checkEthAddress(sender, this._chainId); const senderBuf = Buffer.from((0, _wormholeSdk.tryNativeToUint8Array)(sender, this._chainId)); const parsedAmount = _ethers.ethers.utils.parseUnits(amount, _wormholeSdk.WSOL_DECIMALS); return (await this._contract.process_native_withdrawal(parsedAmount, senderBuf, this.ArbitaryFee, { value: this.ArbitaryFee })).wait(); } async withdrawToken(amount, sender, tokenMint) { this.checkEthAddress(sender, this._chainId); this.checkSolanaAddress(tokenMint); const decimals = await (0, _utils.getDecimals)(tokenMint); const parsedAmount = _ethers.ethers.utils.parseUnits(amount, decimals); const senderBuf = Buffer.from((0, _wormholeSdk.tryNativeToUint8Array)(sender, this._chainId)); const tokenMintBuf = Buffer.from((0, _wormholeSdk.tryNativeToUint8Array)(tokenMint, "solana")); return (await this._contract.process_token_withdrawal(parsedAmount, senderBuf, tokenMintBuf, this.ArbitaryFee, { value: this.ArbitaryFee })).wait(); } async startSolStream(startTime, endTime, amount, receiver, sender, canCancel, canUpdate) { this.checkEthAddress(receiver, this._chainId); this.checkEthAddress(sender, this._chainId); this.checkAmount(parseFloat(amount)); this.checkStartTimeAndEndTime(parseInt(startTime), parseInt(endTime)); const receiverBuf = Buffer.from((0, _wormholeSdk.tryNativeToUint8Array)(receiver, this._chainId)); const senderBuf = Buffer.from((0, _wormholeSdk.tryNativeToUint8Array)(sender, this._chainId)); const parsedAmount = _ethers.ethers.utils.parseUnits(amount, _wormholeSdk.WSOL_DECIMALS); const parsedStartTime = _ethers.BigNumber.from(startTime); const parsedEndTime = _ethers.BigNumber.from(endTime); const cancel = _ethers.BigNumber.from(+canCancel); const update = _ethers.BigNumber.from(+canUpdate); return (await this._contract.process_native_stream(parsedStartTime, parsedEndTime, parsedAmount, receiverBuf, senderBuf, cancel, update, this.ArbitaryFee, { value: this.ArbitaryFee })).wait(); } async startTokenStream(startTime, endTime, amount, receiver, sender, canCancel, canUpdate, tokenMint) { // this.checkEthAddress(receiver, this._chainId); // this.checkEthAddress(sender, this._chainId); this.checkSolanaAddress(tokenMint); this.checkAmount(parseFloat(amount)); this.checkStartTimeAndEndTime(parseInt(startTime), parseInt(endTime)); const receiverBuf = Buffer.from((0, _wormholeSdk.tryNativeToUint8Array)(receiver, this._chainId)); const senderBuf = Buffer.from((0, _wormholeSdk.tryNativeToUint8Array)(sender, this._chainId)); const tokenMintBuf = Buffer.from((0, _wormholeSdk.tryNativeToUint8Array)(tokenMint, "solana")); const decimals = await (0, _utils.getDecimals)(tokenMint); const parsedAmount = _ethers.ethers.utils.parseUnits(amount, decimals); const parsedStartTime = _ethers.BigNumber.from(startTime); const parsedEndTime = _ethers.BigNumber.from(endTime); const parsedCanCancel = _ethers.BigNumber.from(+canCancel); const parsedCanUpdate = _ethers.BigNumber.from(+canUpdate); return (await this._contract.process_token_stream(parsedStartTime, parsedEndTime, parsedAmount, receiverBuf, senderBuf, parsedCanCancel, parsedCanUpdate, tokenMintBuf, this.ArbitaryFee, { gasLimit: "200000", value: this.ArbitaryFee })).wait(); } async updateSolStream(startTime, endTime, amount, receiver, sender, dataAccount) { this.checkEthAddress(receiver, this._chainId); this.checkEthAddress(sender, this._chainId); this.checkSolanaAddress(dataAccount); this.checkAmount(parseFloat(amount)); this.checkStartTimeAndEndTime(parseInt(startTime), parseInt(endTime)); const receiverBuf = Buffer.from((0, _wormholeSdk.tryNativeToUint8Array)(receiver, this._chainId)); const senderBuf = Buffer.from((0, _wormholeSdk.tryNativeToUint8Array)(sender, this._chainId)); const dataAccountBuf = Buffer.from((0, _wormholeSdk.tryNativeToUint8Array)(dataAccount, "solana")); // dataAccount is required but it is not implemented in solana client side const parsedAmount = _ethers.ethers.utils.parseUnits(amount); const parsedStartTime = _ethers.BigNumber.from(startTime); const parsedEndTime = _ethers.BigNumber.from(endTime); return (await this._contract.process_native_stream_update(parsedStartTime, parsedEndTime, parsedAmount, receiverBuf, senderBuf, // dataAccountBuf this.ArbitaryFee, { value: this.ArbitaryFee })).wait(); } async updateTokenStream(startTime, endTime, amount, receiver, sender, tokenMint, dataAccount) { this.checkEthAddress(receiver, this._chainId); this.checkEthAddress(sender, this._chainId); this.checkAmount(parseFloat(amount)); this.checkSolanaAddress(tokenMint); this.checkSolanaAddress(dataAccount); const parsedStartTime = _ethers.BigNumber.from(startTime); const parsedEndTime = _ethers.BigNumber.from(endTime); const decimals = await (0, _utils.getDecimals)(tokenMint); const parsedAmount = _ethers.ethers.utils.parseUnits(amount, decimals); const receiverBuf = Buffer.from((0, _wormholeSdk.tryNativeToUint8Array)(receiver, this._chainId)); const senderBuf = Buffer.from((0, _wormholeSdk.tryNativeToUint8Array)(sender, this._chainId)); const tokenMintBuf = Buffer.from((0, _wormholeSdk.tryNativeToUint8Array)(tokenMint, "solana")); const dataAccountBuf = Buffer.from((0, _wormholeSdk.tryNativeToUint8Array)(dataAccount, "solana")); return (await this._contract.process_token_stream_update(parsedStartTime, parsedEndTime, parsedAmount, receiverBuf, senderBuf, tokenMintBuf, dataAccountBuf, this.ArbitaryFee, { value: this.ArbitaryFee })).wait(); } async withdrawFromSolStream(sender, withdrawer, dataAccount) { this.checkEthAddress(sender, this._chainId); this.checkEthAddress(withdrawer, this._chainId); this.checkSolanaAddress(dataAccount); const senderBuf = Buffer.from((0, _wormholeSdk.tryNativeToUint8Array)(sender, this._chainId)); const withdrawerBuf = Buffer.from((0, _wormholeSdk.tryNativeToUint8Array)(withdrawer, this._chainId)); const dataAccountBuf = Buffer.from((0, _wormholeSdk.tryNativeToUint8Array)(dataAccount, "solana")); return (await this._contract.process_native_withdraw_stream(withdrawerBuf, this.ArbitaryFee)).wait(); } async withdrawFromTokenStream(sender, withdrawer, tokenMint, dataAccount) { this.checkEthAddress(sender, this._chainId); this.checkEthAddress(withdrawer, this._chainId); this.checkSolanaAddress(dataAccount); this.checkSolanaAddress(tokenMint); const senderBuf = Buffer.from((0, _wormholeSdk.tryNativeToUint8Array)(sender, this._chainId)); const withdrawerBuf = Buffer.from((0, _wormholeSdk.tryNativeToUint8Array)(withdrawer, this._chainId)); const tokenMintBuf = Buffer.from((0, _wormholeSdk.tryNativeToUint8Array)(tokenMint, "solana")); const dataAccountBuf = Buffer.from((0, _wormholeSdk.tryNativeToUint8Array)(dataAccount, "solana")); return (await this._contract.process_token_withdraw_stream(withdrawerBuf, tokenMintBuf, senderBuf, dataAccountBuf, this.ArbitaryFee, { gasLimit: "100000", value: this.ArbitaryFee })).wait(); } async cancelSolStream(sender, dataAccount) { this.checkEthAddress(sender, this._chainId); this.checkSolanaAddress(dataAccount); const senderBuf = Buffer.from((0, _wormholeSdk.tryNativeToUint8Array)(sender, this._chainId)); const dataAccountBuf = Buffer.from((0, _wormholeSdk.tryNativeToUint8Array)(dataAccount, "solana")); return (await this._contract.process_cancel_native_stream(senderBuf, this.ArbitaryFee)).wait(); } async cancelTokenStream(sender, receiver, tokenMint, dataAccount) { this.checkEthAddress(sender, this._chainId); this.checkEthAddress(receiver, this._chainId); this.checkSolanaAddress(dataAccount); this.checkSolanaAddress(tokenMint); this.checkEthAddress(sender, this._chainId); const senderBuf = Buffer.from((0, _wormholeSdk.tryNativeToUint8Array)(sender, this._chainId)); const receiverBuf = Buffer.from((0, _wormholeSdk.tryNativeToUint8Array)(receiver, this._chainId)); const dataAccountBuf = Buffer.from((0, _wormholeSdk.tryNativeToUint8Array)(dataAccount, "solana")); const tokenMintBuf = Buffer.from((0, _wormholeSdk.tryNativeToUint8Array)(tokenMint, "solana")); return (await this._contract.process_cancel_token_stream(senderBuf, tokenMintBuf, receiverBuf, dataAccountBuf, this.ArbitaryFee, { value: this.ArbitaryFee })).wait(); } async pauseSolStream(sender, dataAccount) { this.checkEthAddress(sender, this._chainId); this.checkSolanaAddress(dataAccount); const senderBuf = Buffer.from((0, _wormholeSdk.tryNativeToUint8Array)(sender, this._chainId)); const dataAccountBuf = Buffer.from((0, _wormholeSdk.tryNativeToUint8Array)(dataAccount, "solana")); return (await this._contract.process_pause_native_stream(senderBuf, this.ArbitaryFee, { value: this.ArbitaryFee })).wait(); } async pauseTokenStream(sender, receiver, tokenMint, dataAccount) { this.checkEthAddress(sender, this._chainId); this.checkEthAddress(receiver, this._chainId); this.checkSolanaAddress(dataAccount); this.checkSolanaAddress(tokenMint); const senderBuf = Buffer.from((0, _wormholeSdk.tryNativeToUint8Array)(sender, this._chainId)); const receiverBuf = Buffer.from((0, _wormholeSdk.tryNativeToUint8Array)(receiver, this._chainId)); const dataAccountBuf = Buffer.from((0, _wormholeSdk.tryNativeToUint8Array)(dataAccount, "solana")); const tokenMintBuf = Buffer.from((0, _wormholeSdk.tryNativeToUint8Array)(tokenMint, "solana")); return (await this._contract.process_pause_token_stream(senderBuf, tokenMintBuf, receiverBuf, dataAccountBuf, this.ArbitaryFee, { value: this.ArbitaryFee })).wait(); } async instantSolTransfer(amount, sender, withdrawer) { this.checkAmount(parseFloat(amount)); this.checkEthAddress(sender, this._chainId); this.checkEthAddress(withdrawer, this._chainId); const parsedAmount = _ethers.ethers.utils.parseUnits(amount, _wormholeSdk.WSOL_DECIMALS); const senderBuf = Buffer.from((0, _wormholeSdk.tryNativeToUint8Array)(sender, this._chainId)); const withdrawerBuf = Buffer.from((0, _wormholeSdk.tryNativeToUint8Array)(withdrawer, this._chainId)); return (await this._contract.process_instant_native_transfer(parsedAmount, senderBuf, withdrawerBuf, this.ArbitaryFee, { value: this.ArbitaryFee })).wait(); } async instantTokenTransfer(amount, sender, withdrawer, tokenMint) { this.checkAmount(parseFloat(amount)); this.checkEthAddress(sender, this._chainId); this.checkEthAddress(withdrawer, this._chainId); this.checkSolanaAddress(tokenMint); const decimals = await (0, _utils.getDecimals)(tokenMint); const parsedAmount = _ethers.ethers.utils.parseUnits(amount, decimals); const senderBuf = Buffer.from((0, _wormholeSdk.tryNativeToUint8Array)(sender, this._chainId)); const withdrawerBuf = Buffer.from((0, _wormholeSdk.tryNativeToUint8Array)(withdrawer, this._chainId)); const tokenMintBuf = Buffer.from((0, _wormholeSdk.tryNativeToUint8Array)(tokenMint, "solana")); return (await this._contract.process_instant_token_transfer(parsedAmount, senderBuf, withdrawerBuf, tokenMintBuf, this.ArbitaryFee, { value: this.ArbitaryFee })).wait(); } async directTokenTransfer(amount, sender, tokenMint, receiver) { this.checkAmount(parseFloat(amount)); this.checkEthAddress(sender, this._chainId); this.checkEthAddress(receiver, this._chainId); this.checkSolanaAddress(tokenMint); const decimals = await (0, _utils.getDecimals)(tokenMint); const parsedAmount = _ethers.ethers.utils.parseUnits(amount, decimals); const senderBuf = Buffer.from((0, _wormholeSdk.tryNativeToUint8Array)(sender, this._chainId)); const tokenMintBuf = Buffer.from((0, _wormholeSdk.tryNativeToUint8Array)(tokenMint, "solana")); const receiverBuf = Buffer.from((0, _wormholeSdk.tryNativeToUint8Array)(receiver, this._chainId)); return (await this._contract.process_direct_transfer(parsedAmount, senderBuf, tokenMintBuf, receiverBuf, this.ArbitaryFee, { value: this.ArbitaryFee, gasLimit: "200000" })).wait(); } /** * Checks if given amount is valid * @param amount * @returns */ checkAmount(amount) { if (amount <= 0) { throw new _utils.InvalidArgumentError(amount); } return; } /** * Checks if given address is valid evm address * @param address * @param chainId * @returns */ checkEthAddress(address, chainId) { if (!(0, _utils.isValidAddress)(address, (0, _wormholeSdk.toChainName)(chainId))) { throw new _utils.InvalidArgumentError(address); } return; } /** * Checks if given address is valid solana address * @param address * @returns */ checkSolanaAddress(address) { if (!(0, _utils.isValidAddress)(address, "solana")) { throw new _utils.InvalidArgumentError(address); } return; } /** * Checks if given start time and end time are valid * @param startTime * @param endTime * @returns */ checkStartTimeAndEndTime(startTime, endTime) { const now = Math.floor(Date.now() / 1000); if (startTime < now) { throw new _utils.InvalidArgumentError(startTime); } if (endTime <= startTime) { throw new _utils.InvalidArgumentError(endTime); } return; } } exports.ZebecEthBridgeClient = ZebecEthBridgeClient; //# sourceMappingURL=client.js.map