@xchainjs/xchain-evm
Advanced tools
Comparing version 0.3.2 to 0.3.4
import { Provider, TransactionResponse } from '@ethersproject/abstract-provider'; | ||
import { AssetInfo, Balance, BaseXChainClient, ExplorerProviders, FeeOption, Fees, Network, OnlineDataProviders, Tx, TxHash, TxHistoryParams, TxParams, TxsPage, XChainClient, XChainClientParams } from '@xchainjs/xchain-client'; | ||
import { AssetInfo, Balance, BaseXChainClient, ExplorerProviders, FeeOption, Fees, Network, OnlineDataProviders, PreparedTx, Tx, TxHash, TxHistoryParams, TxParams, TxsPage, XChainClient, XChainClientParams } from '@xchainjs/xchain-client'; | ||
import { Address, Asset, BaseAmount, Chain } from '@xchainjs/xchain-util'; | ||
@@ -255,3 +255,5 @@ import { BigNumber, Signer, ethers } from 'ethers'; | ||
*/ | ||
estimateGasLimit({ asset, recipient, amount, memo }: TxParams): Promise<BigNumber>; | ||
estimateGasLimit({ asset, recipient, amount, memo, from }: TxParams & { | ||
from?: Address; | ||
}): Promise<BigNumber>; | ||
private isGasAsset; | ||
@@ -278,3 +280,24 @@ /** | ||
protected roundRobinGetTransactions(params: TxHistoryParams): Promise<TxsPage>; | ||
/** | ||
* Prepare transfer. | ||
* | ||
* @param {TxParams&Address&FeeOption&BaseAmount&BigNumber} params The transfer options. | ||
* @returns {PreparedTx} The raw unsigned transaction. | ||
*/ | ||
prepareTx({ sender, asset, memo, amount, recipient, }: TxParams & { | ||
sender: Address; | ||
feeOption?: FeeOption; | ||
gasPrice?: BaseAmount; | ||
gasLimit?: BigNumber; | ||
}): Promise<PreparedTx>; | ||
/** | ||
* Prepare transfer. | ||
* | ||
* @param {ApproveParams&Address&FeeOption&BaseAmount&BigNumber} params The transfer options. | ||
* @returns {PreparedTx} The raw unsigned transaction. | ||
*/ | ||
prepareApprove({ contractAddress, spenderAddress, amount, sender, }: ApproveParams & { | ||
sender: string; | ||
}): Promise<PreparedTx>; | ||
} | ||
export { Client }; |
@@ -674,2 +674,3 @@ import { BaseXChainClient, Network, FeeOption, checkFeeBounds, standardFeeRates, FeeType } from '@xchainjs/xchain-client'; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const sender = this.getAddress(walletIndex || 0); | ||
const gasPrice = BigNumber.from((yield this.estimateGasPrices().then((prices) => prices[feeOption])) | ||
@@ -679,8 +680,7 @@ // .catch(() => getDefaultGasPrices()[feeOption]) | ||
.toFixed()); | ||
const signer = txSigner || this.getWallet(walletIndex); | ||
const fromAddress = yield signer.getAddress(); | ||
checkFeeBounds(this.feeBounds, gasPrice.toNumber()); | ||
const gasLimit = yield this.estimateApprove({ | ||
spenderAddress, | ||
contractAddress, | ||
fromAddress, | ||
fromAddress: sender, | ||
amount, | ||
@@ -690,9 +690,18 @@ }).catch(() => { | ||
}); | ||
checkFeeBounds(this.feeBounds, gasPrice.toNumber()); | ||
const valueToApprove = getApprovalAmount(amount); | ||
const contract = new ethers.Contract(contractAddress, erc20ABI, this.getProvider()); | ||
/* as same as ethers.TransactionResponse expected by `sendTransaction` */ | ||
const unsignedTx = yield contract.populateTransaction.approve(spenderAddress, valueToApprove); | ||
const result = yield signer.sendTransaction(Object.assign(Object.assign({}, unsignedTx), { from: fromAddress, gasPrice, | ||
gasLimit })); | ||
const { rawUnsignedTx } = yield this.prepareApprove({ | ||
contractAddress, | ||
spenderAddress, | ||
amount, | ||
sender, | ||
}); | ||
const transaction = ethers.utils.parseTransaction(rawUnsignedTx); | ||
const signer = txSigner || this.getWallet(walletIndex); | ||
const result = yield signer.sendTransaction({ | ||
from: transaction.from, | ||
to: transaction.to, | ||
value: transaction.value, | ||
data: transaction.data, | ||
gasPrice, | ||
gasLimit, | ||
}); | ||
return result; | ||
@@ -740,5 +749,2 @@ }); | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (asset.chain !== this.chain) | ||
throw Error(`this client can only transfer assets on chain: ${this.chain}. Bad asset: ${assetToString(asset)}`); | ||
const isGasAsset = this.isGasAsset(asset); | ||
const txGasPrice = gasPrice | ||
@@ -749,12 +755,12 @@ ? BigNumber.from(gasPrice.amount().toFixed()) | ||
.then((gp) => BigNumber.from(gp.amount().toFixed())); | ||
const defaultGasLimit = isGasAsset | ||
? this.defaults[this.network].transferGasAssetGasLimit | ||
: this.defaults[this.network].transferTokenGasLimit; | ||
const sender = this.getAddress(walletIndex || 0); | ||
let txGasLimit; | ||
if (!gasLimit) { | ||
try { | ||
txGasLimit = yield this.estimateGasLimit({ asset, recipient, amount, memo }); | ||
txGasLimit = yield this.estimateGasLimit({ asset, recipient, amount, memo, from: sender }); | ||
} | ||
catch (error) { | ||
txGasLimit = defaultGasLimit; | ||
txGasLimit = this.isGasAsset(asset) | ||
? this.defaults[this.network].transferGasAssetGasLimit | ||
: this.defaults[this.network].transferTokenGasLimit; | ||
} | ||
@@ -770,24 +776,13 @@ } | ||
checkFeeBounds(this.feeBounds, overrides.gasPrice.toNumber()); | ||
const { rawUnsignedTx } = yield this.prepareTx({ | ||
sender, | ||
recipient, | ||
amount, | ||
asset, | ||
memo, | ||
}); | ||
const transactionRequest = ethers.utils.parseTransaction(rawUnsignedTx); | ||
const signer = txSigner || this.getWallet(walletIndex); | ||
const txAmount = BigNumber.from(amount.amount().toFixed()); | ||
// Transfer ETH | ||
if (isGasAsset) { | ||
const transactionRequest = Object.assign({ to: recipient, value: txAmount }, Object.assign(Object.assign({}, overrides), { data: memo ? toUtf8Bytes(memo) : undefined })); | ||
const { hash } = yield signer.sendTransaction(transactionRequest); | ||
return hash; | ||
} | ||
else { | ||
const assetAddress = getTokenAddress(asset); | ||
if (!assetAddress) | ||
throw Error(`Can't parse address from asset ${assetToString(asset)}`); | ||
// Transfer ERC20 | ||
const { hash } = yield this.call({ | ||
signer, | ||
contractAddress: assetAddress, | ||
abi: erc20ABI, | ||
funcName: 'transfer', | ||
funcParams: [recipient, txAmount, Object.assign({}, overrides)], | ||
}); | ||
return hash; | ||
} | ||
const { hash } = yield signer.sendTransaction(Object.assign({ from: transactionRequest.from, to: transactionRequest.to, data: transactionRequest.data, value: transactionRequest.value }, overrides)); | ||
return hash; | ||
}); | ||
@@ -853,3 +848,3 @@ } | ||
*/ | ||
estimateGasLimit({ asset, recipient, amount, memo }) { | ||
estimateGasLimit({ asset, recipient, amount, memo, from }) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
@@ -866,3 +861,3 @@ const txAmount = BigNumber.from(amount.amount().toFixed()); | ||
gasEstimate = yield contract.estimateGas.transfer(recipient, txAmount, { | ||
from: this.getAddress(), | ||
from: from || this.getAddress(), | ||
}); | ||
@@ -873,3 +868,3 @@ } | ||
const transactionRequest = { | ||
from: this.getAddress(), | ||
from: from || this.getAddress(), | ||
to: recipient, | ||
@@ -971,2 +966,56 @@ value: txAmount, | ||
} | ||
/** | ||
* Prepare transfer. | ||
* | ||
* @param {TxParams&Address&FeeOption&BaseAmount&BigNumber} params The transfer options. | ||
* @returns {PreparedTx} The raw unsigned transaction. | ||
*/ | ||
prepareTx({ sender, asset = this.gasAsset, memo, amount, recipient, }) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (asset.chain !== this.chain) | ||
throw Error(`This client can only prepare transactions on chain: ${this.chain}. Bad asset: ${asset.chain}`); | ||
if (!this.validateAddress(sender)) | ||
throw Error('Invalid sender address'); | ||
if (!this.validateAddress(recipient)) | ||
throw Error('Invalid recipient address'); | ||
if (this.isGasAsset(asset)) { | ||
return { | ||
rawUnsignedTx: ethers.utils.serializeTransaction({ | ||
to: recipient, | ||
value: BigNumber.from(amount.amount().toFixed()), | ||
data: memo ? toUtf8Bytes(memo) : undefined, | ||
}), | ||
}; | ||
} | ||
else { | ||
const assetAddress = getTokenAddress(asset); | ||
if (!assetAddress) | ||
throw Error(`Can't parse address from asset ${assetToString(asset)}`); | ||
const contract = new ethers.Contract(assetAddress, erc20ABI, this.getProvider()); | ||
/* as same as ethers.TransactionResponse expected by `sendTransaction` */ | ||
const unsignedTx = yield contract.populateTransaction.transfer(recipient, BigNumber.from(amount.amount().toFixed())); | ||
return { rawUnsignedTx: ethers.utils.serializeTransaction(unsignedTx) }; | ||
} | ||
}); | ||
} | ||
/** | ||
* Prepare transfer. | ||
* | ||
* @param {ApproveParams&Address&FeeOption&BaseAmount&BigNumber} params The transfer options. | ||
* @returns {PreparedTx} The raw unsigned transaction. | ||
*/ | ||
prepareApprove({ contractAddress, spenderAddress, amount, sender, }) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (!this.validateAddress(contractAddress)) | ||
throw Error('Invalid contractAddress address'); | ||
if (!this.validateAddress(spenderAddress)) | ||
throw Error('Invalid spenderAddress address'); | ||
if (!this.validateAddress(sender)) | ||
throw Error('Invalid sender address'); | ||
const contract = new ethers.Contract(contractAddress, erc20ABI, this.getProvider()); | ||
const valueToApprove = getApprovalAmount(amount); | ||
const unsignedTx = yield contract.populateTransaction.approve(spenderAddress, valueToApprove); | ||
return { rawUnsignedTx: ethers.utils.serializeTransaction(unsignedTx) }; | ||
}); | ||
} | ||
} | ||
@@ -973,0 +1022,0 @@ |
133
lib/index.js
@@ -678,2 +678,3 @@ 'use strict'; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const sender = this.getAddress(walletIndex || 0); | ||
const gasPrice = ethers.BigNumber.from((yield this.estimateGasPrices().then((prices) => prices[feeOption])) | ||
@@ -683,8 +684,7 @@ // .catch(() => getDefaultGasPrices()[feeOption]) | ||
.toFixed()); | ||
const signer = txSigner || this.getWallet(walletIndex); | ||
const fromAddress = yield signer.getAddress(); | ||
xchainClient.checkFeeBounds(this.feeBounds, gasPrice.toNumber()); | ||
const gasLimit = yield this.estimateApprove({ | ||
spenderAddress, | ||
contractAddress, | ||
fromAddress, | ||
fromAddress: sender, | ||
amount, | ||
@@ -694,9 +694,18 @@ }).catch(() => { | ||
}); | ||
xchainClient.checkFeeBounds(this.feeBounds, gasPrice.toNumber()); | ||
const valueToApprove = getApprovalAmount(amount); | ||
const contract = new ethers.ethers.Contract(contractAddress, erc20ABI, this.getProvider()); | ||
/* as same as ethers.TransactionResponse expected by `sendTransaction` */ | ||
const unsignedTx = yield contract.populateTransaction.approve(spenderAddress, valueToApprove); | ||
const result = yield signer.sendTransaction(Object.assign(Object.assign({}, unsignedTx), { from: fromAddress, gasPrice, | ||
gasLimit })); | ||
const { rawUnsignedTx } = yield this.prepareApprove({ | ||
contractAddress, | ||
spenderAddress, | ||
amount, | ||
sender, | ||
}); | ||
const transaction = ethers.ethers.utils.parseTransaction(rawUnsignedTx); | ||
const signer = txSigner || this.getWallet(walletIndex); | ||
const result = yield signer.sendTransaction({ | ||
from: transaction.from, | ||
to: transaction.to, | ||
value: transaction.value, | ||
data: transaction.data, | ||
gasPrice, | ||
gasLimit, | ||
}); | ||
return result; | ||
@@ -744,5 +753,2 @@ }); | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (asset.chain !== this.chain) | ||
throw Error(`this client can only transfer assets on chain: ${this.chain}. Bad asset: ${xchainUtil.assetToString(asset)}`); | ||
const isGasAsset = this.isGasAsset(asset); | ||
const txGasPrice = gasPrice | ||
@@ -753,12 +759,12 @@ ? ethers.BigNumber.from(gasPrice.amount().toFixed()) | ||
.then((gp) => ethers.BigNumber.from(gp.amount().toFixed())); | ||
const defaultGasLimit = isGasAsset | ||
? this.defaults[this.network].transferGasAssetGasLimit | ||
: this.defaults[this.network].transferTokenGasLimit; | ||
const sender = this.getAddress(walletIndex || 0); | ||
let txGasLimit; | ||
if (!gasLimit) { | ||
try { | ||
txGasLimit = yield this.estimateGasLimit({ asset, recipient, amount, memo }); | ||
txGasLimit = yield this.estimateGasLimit({ asset, recipient, amount, memo, from: sender }); | ||
} | ||
catch (error) { | ||
txGasLimit = defaultGasLimit; | ||
txGasLimit = this.isGasAsset(asset) | ||
? this.defaults[this.network].transferGasAssetGasLimit | ||
: this.defaults[this.network].transferTokenGasLimit; | ||
} | ||
@@ -774,24 +780,13 @@ } | ||
xchainClient.checkFeeBounds(this.feeBounds, overrides.gasPrice.toNumber()); | ||
const { rawUnsignedTx } = yield this.prepareTx({ | ||
sender, | ||
recipient, | ||
amount, | ||
asset, | ||
memo, | ||
}); | ||
const transactionRequest = ethers.ethers.utils.parseTransaction(rawUnsignedTx); | ||
const signer = txSigner || this.getWallet(walletIndex); | ||
const txAmount = ethers.BigNumber.from(amount.amount().toFixed()); | ||
// Transfer ETH | ||
if (isGasAsset) { | ||
const transactionRequest = Object.assign({ to: recipient, value: txAmount }, Object.assign(Object.assign({}, overrides), { data: memo ? utils.toUtf8Bytes(memo) : undefined })); | ||
const { hash } = yield signer.sendTransaction(transactionRequest); | ||
return hash; | ||
} | ||
else { | ||
const assetAddress = getTokenAddress(asset); | ||
if (!assetAddress) | ||
throw Error(`Can't parse address from asset ${xchainUtil.assetToString(asset)}`); | ||
// Transfer ERC20 | ||
const { hash } = yield this.call({ | ||
signer, | ||
contractAddress: assetAddress, | ||
abi: erc20ABI, | ||
funcName: 'transfer', | ||
funcParams: [recipient, txAmount, Object.assign({}, overrides)], | ||
}); | ||
return hash; | ||
} | ||
const { hash } = yield signer.sendTransaction(Object.assign({ from: transactionRequest.from, to: transactionRequest.to, data: transactionRequest.data, value: transactionRequest.value }, overrides)); | ||
return hash; | ||
}); | ||
@@ -857,3 +852,3 @@ } | ||
*/ | ||
estimateGasLimit({ asset, recipient, amount, memo }) { | ||
estimateGasLimit({ asset, recipient, amount, memo, from }) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
@@ -870,3 +865,3 @@ const txAmount = ethers.BigNumber.from(amount.amount().toFixed()); | ||
gasEstimate = yield contract.estimateGas.transfer(recipient, txAmount, { | ||
from: this.getAddress(), | ||
from: from || this.getAddress(), | ||
}); | ||
@@ -877,3 +872,3 @@ } | ||
const transactionRequest = { | ||
from: this.getAddress(), | ||
from: from || this.getAddress(), | ||
to: recipient, | ||
@@ -975,2 +970,56 @@ value: txAmount, | ||
} | ||
/** | ||
* Prepare transfer. | ||
* | ||
* @param {TxParams&Address&FeeOption&BaseAmount&BigNumber} params The transfer options. | ||
* @returns {PreparedTx} The raw unsigned transaction. | ||
*/ | ||
prepareTx({ sender, asset = this.gasAsset, memo, amount, recipient, }) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (asset.chain !== this.chain) | ||
throw Error(`This client can only prepare transactions on chain: ${this.chain}. Bad asset: ${asset.chain}`); | ||
if (!this.validateAddress(sender)) | ||
throw Error('Invalid sender address'); | ||
if (!this.validateAddress(recipient)) | ||
throw Error('Invalid recipient address'); | ||
if (this.isGasAsset(asset)) { | ||
return { | ||
rawUnsignedTx: ethers.ethers.utils.serializeTransaction({ | ||
to: recipient, | ||
value: ethers.BigNumber.from(amount.amount().toFixed()), | ||
data: memo ? utils.toUtf8Bytes(memo) : undefined, | ||
}), | ||
}; | ||
} | ||
else { | ||
const assetAddress = getTokenAddress(asset); | ||
if (!assetAddress) | ||
throw Error(`Can't parse address from asset ${xchainUtil.assetToString(asset)}`); | ||
const contract = new ethers.ethers.Contract(assetAddress, erc20ABI, this.getProvider()); | ||
/* as same as ethers.TransactionResponse expected by `sendTransaction` */ | ||
const unsignedTx = yield contract.populateTransaction.transfer(recipient, ethers.BigNumber.from(amount.amount().toFixed())); | ||
return { rawUnsignedTx: ethers.ethers.utils.serializeTransaction(unsignedTx) }; | ||
} | ||
}); | ||
} | ||
/** | ||
* Prepare transfer. | ||
* | ||
* @param {ApproveParams&Address&FeeOption&BaseAmount&BigNumber} params The transfer options. | ||
* @returns {PreparedTx} The raw unsigned transaction. | ||
*/ | ||
prepareApprove({ contractAddress, spenderAddress, amount, sender, }) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (!this.validateAddress(contractAddress)) | ||
throw Error('Invalid contractAddress address'); | ||
if (!this.validateAddress(spenderAddress)) | ||
throw Error('Invalid spenderAddress address'); | ||
if (!this.validateAddress(sender)) | ||
throw Error('Invalid sender address'); | ||
const contract = new ethers.ethers.Contract(contractAddress, erc20ABI, this.getProvider()); | ||
const valueToApprove = getApprovalAmount(amount); | ||
const unsignedTx = yield contract.populateTransaction.approve(spenderAddress, valueToApprove); | ||
return { rawUnsignedTx: ethers.ethers.utils.serializeTransaction(unsignedTx) }; | ||
}); | ||
} | ||
} | ||
@@ -977,0 +1026,0 @@ |
{ | ||
"name": "@xchainjs/xchain-evm", | ||
"version": "0.3.2", | ||
"version": "0.3.4", | ||
"description": "Genereic EVM client for XChainJS", | ||
@@ -37,3 +37,3 @@ "keywords": [ | ||
"devDependencies": { | ||
"@xchainjs/xchain-client": "^0.14.2", | ||
"@xchainjs/xchain-client": "^0.15.1", | ||
"@xchainjs/xchain-crypto": "^0.3.0", | ||
@@ -46,3 +46,3 @@ "@xchainjs/xchain-util": "^0.13.1", | ||
"peerDependencies": { | ||
"@xchainjs/xchain-client": "^0.14.2", | ||
"@xchainjs/xchain-client": "^0.15.1", | ||
"@xchainjs/xchain-crypto": "^0.3.0", | ||
@@ -53,2 +53,2 @@ "@xchainjs/xchain-util": "^0.13.1", | ||
} | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
219046
3572