-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Samuel Sramko
committed
Aug 30, 2021
1 parent
81438bf
commit 1c58878
Showing
9 changed files
with
65 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import BigNumber from 'bignumber.js'; | ||
import {validateBody} from '../connector/tatum'; | ||
import token_abi from '../contracts/erc20/token_abi'; | ||
import {helperBroadcastTx, helperPrepareSCCall} from '../helpers'; | ||
import {ApproveErc20, Currency} from '../model'; | ||
import {getBscBep20ContractDecimals, getCeloErc20ContractDecimals, getEthErc20ContractDecimals, getOne20ContractDecimals, getPolygonErc20ContractDecimals} from '../transaction'; | ||
|
||
/** | ||
* Approve ERC20 transfer for spender. | ||
* @param testnet chain to work with | ||
* @param body request data | ||
* @param provider optional provider to enter. if not present, Tatum Web3 will be used. | ||
* @returns {txId: string} Transaction ID of the operation, or signatureID in case of Tatum KMS | ||
*/ | ||
export const sendApproveErc20 = async (testnet: boolean, body: ApproveErc20, provider?: string) => | ||
helperBroadcastTx(body.chain, await prepareApproveErc20(testnet, body, provider), body.signatureId); | ||
|
||
/** | ||
* Prepare approve ERC20 signed transaction. | ||
* @param testnet if we are on testnet or not | ||
* @param body body of the approve operation | ||
* @param provider optional Web3 provider | ||
*/ | ||
export const prepareApproveErc20 = async (testnet: boolean, body: ApproveErc20, provider?: string) => { | ||
await validateBody(body, ApproveErc20); | ||
let amount; | ||
switch (body.chain) { | ||
case Currency.CELO: | ||
amount = new BigNumber(body.amount).multipliedBy(new BigNumber(10).pow(await getCeloErc20ContractDecimals(testnet, body.contractAddress, provider))).toString(16); | ||
break; | ||
case Currency.ONE: | ||
amount = new BigNumber(body.amount).multipliedBy(new BigNumber(10).pow(await getOne20ContractDecimals(testnet, body.contractAddress, provider))).toString(16); | ||
break; | ||
case Currency.ETH: | ||
amount = new BigNumber(body.amount).multipliedBy(new BigNumber(10).pow(await getEthErc20ContractDecimals(testnet, body.contractAddress, provider))).toString(16); | ||
break; | ||
case Currency.BSC: | ||
amount = new BigNumber(body.amount).multipliedBy(new BigNumber(10).pow(await getBscBep20ContractDecimals(testnet, body.contractAddress, provider))).toString(16); | ||
break; | ||
case Currency.MATIC: | ||
amount = new BigNumber(body.amount).multipliedBy(new BigNumber(10).pow(await getPolygonErc20ContractDecimals(testnet, body.contractAddress, provider))).toString(16); | ||
break; | ||
default: | ||
throw new Error('Unsupported combination of inputs.'); | ||
} | ||
const params = [body.spender.trim(), `0x${amount}`]; | ||
body.amount = '0'; | ||
return await helperPrepareSCCall(testnet, body, ApproveErc20, 'approve', params, undefined, provider, token_abi); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters