Skip to content

Commit

Permalink
#NOISSUE - fix eth + bsc kms gas estimations
Browse files Browse the repository at this point in the history
fix marketplace listing for erc20 token with different decimals then 18
  • Loading branch information
Samuel Sramko committed Oct 6, 2021
1 parent 95fea8a commit 87a557a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tatumio/tatum",
"version": "1.26.8",
"version": "1.27.0",
"description": "Tatum API client allows browsers and Node.js clients to interact with Tatum API.",
"main": "dist/src/index.js",
"repository": "https://github.com/tatumio/tatum-js",
Expand Down
5 changes: 3 additions & 2 deletions src/nft/marketplace/listing.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import BigNumber from 'bignumber.js';
import {get, validateBody} from '../../connector/tatum';
import {prepareApproveErc20} from '../../fungible';
import {getErc20Decimals, prepareApproveErc20} from '../../fungible';
import {helperBroadcastTx, helperPrepareSCCall} from '../../helpers';
import {
ApproveErc20,
Expand Down Expand Up @@ -235,8 +235,9 @@ export const prepareMarketplaceApproveErc20Spending = async (testnet: boolean, b
*/
export const prepareMarketplaceCreateListing = async (testnet: boolean, body: CreateMarketplaceListing | CreateTronMarketplaceListing, provider?: string) => {
await validateBody(body, body.chain === Currency.TRON ? CreateTronMarketplaceListing : CreateMarketplaceListing);
const decimals = body.erc20Address ? await getErc20Decimals(testnet, body.chain, body.erc20Address, provider) : 18;
const params = [body.listingId, body.isErc721, body.nftAddress.trim(), `0x${new BigNumber(body.tokenId).toString(16)}`,
`0x${new BigNumber(body.price).multipliedBy(body.chain === Currency.TRON ? 1e6 : 1e18).toString(16)}`, body.seller.trim(), `0x${new BigNumber(body.amount || 0).toString(16)}`,
`0x${new BigNumber(body.price).multipliedBy(10 ** decimals).toString(16)}`, body.seller.trim(), `0x${new BigNumber(body.amount || 0).toString(16)}`,
body.erc20Address || '0x0000000000000000000000000000000000000000'];
if (body.chain === Currency.TRON) {
throw new Error('Unsupported chain');
Expand Down
17 changes: 10 additions & 7 deletions src/transaction/bsc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,21 @@ const prepareBscSignedTransactionAbstraction = async (
*/
export const signBscKMSTransaction = async (tx: TransactionKMS, fromPrivateKey: string, provider?: string) => {
if (tx.chain !== Currency.BSC) {
throw Error('Unsupported chain.')
throw Error('Unsupported chain.');
}
const client = getBscClient(provider, fromPrivateKey);
const transactionConfig = JSON.parse(tx.serializedTransaction);
const gas = await client.eth.estimateGas(transactionConfig);
if (!transactionConfig.gas) {
transactionConfig.gas = gas;
}
const client = getBscClient(provider, fromPrivateKey)
const transactionConfig = JSON.parse(tx.serializedTransaction)
transactionConfig.gas = await client.eth.estimateGas(transactionConfig)
if (!transactionConfig.nonce) {
transactionConfig.nonce = await bscGetTransactionsCount(client.eth.defaultAccount as string)
transactionConfig.nonce = await bscGetTransactionsCount(client.eth.defaultAccount as string);
}
if (!transactionConfig.gasPrice || transactionConfig.gasPrice === '0' || transactionConfig.gasPrice === 0 || transactionConfig.gasPrice === '0x0') {
transactionConfig.gasPrice = await bscGetGasPriceInWei()
transactionConfig.gasPrice = await bscGetGasPriceInWei();
}
return (await client.eth.accounts.signTransaction(transactionConfig, fromPrivateKey as string)).rawTransaction as string
return (await client.eth.accounts.signTransaction(transactionConfig, fromPrivateKey as string)).rawTransaction as string;
}

export const getBscBep20ContractDecimals = async (testnet: boolean, contractAddress: string, provider?: string) => {
Expand Down
17 changes: 10 additions & 7 deletions src/transaction/eth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,21 @@ export const getClient = (provider?: string, privateKey?: string) => {
*/
export const signEthKMSTransaction = async (tx: TransactionKMS, fromPrivateKey: string, provider?: string) => {
if (tx.chain !== Currency.ETH) {
throw Error('Unsupported chain.')
throw Error('Unsupported chain.');
}
const client = getClient(provider, fromPrivateKey);
const transactionConfig = JSON.parse(tx.serializedTransaction);
const gas = await client.eth.estimateGas(transactionConfig);
if (!transactionConfig.gas) {
transactionConfig.gas = gas;
}
const client = getClient(provider, fromPrivateKey)
const transactionConfig = JSON.parse(tx.serializedTransaction)
transactionConfig.gas = await client.eth.estimateGas(transactionConfig)
if (!transactionConfig.nonce) {
transactionConfig.nonce = await ethGetTransactionsCount(client.eth.defaultAccount as string)
transactionConfig.nonce = await ethGetTransactionsCount(client.eth.defaultAccount as string);
}
if (!transactionConfig.gasPrice || transactionConfig.gasPrice === '0' || transactionConfig.gasPrice === 0 || transactionConfig.gasPrice === '0x0') {
transactionConfig.gasPrice = await ethGetGasPriceInWei()
transactionConfig.gasPrice = await ethGetGasPriceInWei();
}
return (await client.eth.accounts.signTransaction(transactionConfig, fromPrivateKey as string)).rawTransaction as string
return (await client.eth.accounts.signTransaction(transactionConfig, fromPrivateKey as string)).rawTransaction as string;
}

/**
Expand Down

0 comments on commit 87a557a

Please sign in to comment.