UNPKG

@subasshrestha/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.

274 lines (267 loc) 12.8 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"); /** * A client implementation for zebec evm bridge contract */ class ZebecEthBridgeClient { // todo: fee is hard coded ArbitaryFee = _ethers.ethers.utils.parseEther("0.0000000000000001"); /** * Chain id of zebec eth bridge client */ /** * 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._contract = _Messenger__factory.Messenger__factory.connect(contractAddress, signer); this._chainId = sourceChain; } async registerUser(address, overrides) { this.checkEthAddress(address, this._chainId); const addressBuf = Buffer.from((0, _wormholeSdk.tryNativeToUint8Array)(address, this._chainId)); const _overrides = overrides && overrides.gasLimit ? overrides : { gasLimit: "200000" }; return (await this._contract.initialize_pda(addressBuf, this.ArbitaryFee, { ..._overrides, value: this.ArbitaryFee })).wait(); } async createTokenAccount(owner, tokenMint, overrides) { const ownerBuf = Buffer.from((0, _wormholeSdk.tryNativeToUint8Array)(owner, this._chainId)); const tokenMintBuf = Buffer.from((0, _wormholeSdk.tryNativeToUint8Array)(tokenMint, "solana")); const _overrides = overrides && overrides.gasLimit ? overrides : { gasLimit: "200000" }; return (await this._contract.initialize_token_account(ownerBuf, tokenMintBuf, this.ArbitaryFee, { ..._overrides, value: this.ArbitaryFee })).wait(); } async initStream(startTime, endTime, amount, receiver, sender, canCancel, canUpdate, tokenMint, overrides) { 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); const _overrides = overrides && overrides.gasLimit ? overrides : { gasLimit: "200000" }; return (await this._contract.process_token_stream(parsedStartTime, parsedEndTime, parsedAmount, receiverBuf, senderBuf, parsedCanCancel, parsedCanUpdate, tokenMintBuf, this.ArbitaryFee, { ..._overrides, value: this.ArbitaryFee })).wait(); } async deposit(amount, depositor, tokenMint, overrides) { 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); const _overrides = overrides && overrides.gasLimit ? overrides : { gasLimit: "200000" }; return (await this._contract.process_deposit_token(parsedAmount, depositorBuf, tokenMintBuf, this.ArbitaryFee, { ..._overrides, value: this.ArbitaryFee })).wait(); } async withdraw(amount, sender, tokenMint, overrides) { 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")); const _overrides = overrides && overrides.gasLimit ? overrides : { gasLimit: "200000" }; return (await this._contract.process_token_withdrawal(parsedAmount, senderBuf, tokenMintBuf, this.ArbitaryFee, { ..._overrides, value: this.ArbitaryFee })).wait(); } async updateStream(startTime, endTime, amount, receiver, sender, tokenMint, streamDataAccount, overrides) { this.checkEthAddress(receiver, this._chainId); this.checkEthAddress(sender, this._chainId); this.checkAmount(parseFloat(amount)); this.checkSolanaAddress(tokenMint); this.checkSolanaAddress(streamDataAccount); 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 streamDataAccountBuf = Buffer.from((0, _wormholeSdk.tryNativeToUint8Array)(streamDataAccount, "solana")); const _overrides = overrides && overrides.gasLimit ? overrides : { gasLimit: "200000" }; return (await this._contract.process_token_stream_update(parsedStartTime, parsedEndTime, parsedAmount, receiverBuf, senderBuf, tokenMintBuf, streamDataAccountBuf, this.ArbitaryFee, { ..._overrides, value: this.ArbitaryFee })).wait(); } async withdrawStreamed(sender, withdrawer, tokenMint, streamDataAccount, overrides) { this.checkEthAddress(sender, this._chainId); this.checkEthAddress(withdrawer, this._chainId); this.checkSolanaAddress(streamDataAccount); 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 streamDataAccountBuf = Buffer.from((0, _wormholeSdk.tryNativeToUint8Array)(streamDataAccount, "solana")); const _overrides = overrides && overrides.gasLimit ? overrides : { gasLimit: "200000" }; return (await this._contract.process_token_withdraw_stream(withdrawerBuf, tokenMintBuf, senderBuf, streamDataAccountBuf, this.ArbitaryFee, { ..._overrides, value: this.ArbitaryFee })).wait(); } async cancelStream(sender, receiver, tokenMint, streamDataAccount, overrides) { this.checkEthAddress(sender, this._chainId); this.checkEthAddress(receiver, this._chainId); this.checkSolanaAddress(streamDataAccount); 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 streamDataAccountBuf = Buffer.from((0, _wormholeSdk.tryNativeToUint8Array)(streamDataAccount, "solana")); const tokenMintBuf = Buffer.from((0, _wormholeSdk.tryNativeToUint8Array)(tokenMint, "solana")); const _overrides = overrides && overrides.gasLimit ? overrides : { gasLimit: "200000" }; return (await this._contract.process_cancel_token_stream(senderBuf, tokenMintBuf, receiverBuf, streamDataAccountBuf, this.ArbitaryFee, { ..._overrides, value: this.ArbitaryFee })).wait(); } async pauseResumeStream(sender, receiver, tokenMint, streamDataAccount, overrides) { this.checkEthAddress(sender, this._chainId); this.checkEthAddress(receiver, this._chainId); this.checkSolanaAddress(streamDataAccount); this.checkSolanaAddress(tokenMint); const senderBuf = Buffer.from((0, _wormholeSdk.tryNativeToUint8Array)(sender, this._chainId)); const receiverBuf = Buffer.from((0, _wormholeSdk.tryNativeToUint8Array)(receiver, this._chainId)); const streamDataAccountBuf = Buffer.from((0, _wormholeSdk.tryNativeToUint8Array)(streamDataAccount, "solana")); const tokenMintBuf = Buffer.from((0, _wormholeSdk.tryNativeToUint8Array)(tokenMint, "solana")); const _overrides = overrides && overrides.gasLimit ? overrides : { gasLimit: "200000" }; return (await this._contract.process_pause_token_stream(senderBuf, tokenMintBuf, receiverBuf, streamDataAccountBuf, this.ArbitaryFee, { ..._overrides, value: this.ArbitaryFee })).wait(); } async instantTransfer(amount, sender, withdrawer, tokenMint, overrides) { 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")); const _overrides = overrides && overrides.gasLimit ? overrides : { gasLimit: "200000" }; return (await this._contract.process_instant_token_transfer(parsedAmount, senderBuf, withdrawerBuf, tokenMintBuf, this.ArbitaryFee, { ..._overrides, value: this.ArbitaryFee })).wait(); } async directTransfer(amount, sender, tokenMint, receiver, overrides) { 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)); const _overrides = overrides && overrides.gasLimit ? overrides : { gasLimit: "200000" }; return (await this._contract.process_direct_transfer(parsedAmount, senderBuf, tokenMintBuf, receiverBuf, this.ArbitaryFee, { ..._overrides, value: this.ArbitaryFee })).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