Skip to content

Commit

Permalink
add approve ERC20 fungible endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel Sramko committed Aug 30, 2021
1 parent 81438bf commit 1c58878
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 119 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.22.16",
"version": "1.23.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
49 changes: 49 additions & 0 deletions src/fungible/index.ts
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);
};
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export * from './tatum';
export * from './security';
export * from './storage';
export * from './offchain';
export * from './fungible';
export * from './blockchain';
export * from './transaction';
export * from './record'
Expand Down
38 changes: 0 additions & 38 deletions src/model/request/ApproveMarketplaceErc20Spending.ts

This file was deleted.

16 changes: 0 additions & 16 deletions src/model/request/ApproveTronMarketplaceErc20Spending.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/model/request/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ export * from './ApproveErc20';
export * from './InvokeAuctionOperation';
export * from './CreateAuction';
export * from './UpdateAuctionFee';
export * from './ApproveMarketplaceErc20Spending';
export * from './ApproveTronMarketplaceErc20Spending';
export * from './InvokeMarketplaceListingOperation';
export * from './InvokeTronMarketplaceListingOperation';
export * from './UpdateTronMarketplaceFeeRecipient';
Expand Down
5 changes: 2 additions & 3 deletions src/nft/marketplace/auction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {get, validateBody} from '../../connector/tatum';
import erc1155_abi from '../../contracts/erc1155/erc1155_abi';
import erc721_abi from '../../contracts/erc721/erc721_abi';
import {auction} from '../../contracts/marketplace';
import {prepareApproveErc20} from '../../fungible';
import {helperBroadcastTx, helperPrepareSCCall} from '../../helpers';
import {ApproveErc20, ApproveNftTransfer, CreateAuction, Currency, DeployNftAuction, InvokeAuctionOperation, UpdateAuctionFee, UpdateMarketplaceFeeRecipient,} from '../../model';
import {
Expand All @@ -12,7 +13,6 @@ import {
prepareOneDeployAuctionSignedTransaction,
preparePolygonDeployAuctionSignedTransaction
} from '../../transaction';
import {prepareMarketplaceApproveErc20Spending} from './listing';

export interface Auction {
/*
Expand Down Expand Up @@ -176,8 +176,7 @@ export const prepareAuctionApproveNftTransfer = async (testnet: boolean, body: A
* @returns {txId: string} Transaction ID of the operation, or signatureID in case of Tatum KMS
*/
export const prepareAuctionApproveErc20Transfer = async (testnet: boolean, body: ApproveErc20, provider?: string) => {
await validateBody(body, ApproveErc20);
return prepareMarketplaceApproveErc20Spending(testnet, {...body, marketplaceAddress: body.spender}, provider);
return prepareApproveErc20(testnet, body, provider);
};

/**
Expand Down
21 changes: 7 additions & 14 deletions src/nft/marketplace/listing.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import {tronBroadcast} from '../../blockchain';
import {listing} from '../../contracts/marketplace';
import {
ApproveMarketplaceErc20Spending,
CreateMarketplaceListing,
Currency,
DeployMarketplaceListing,
InvokeMarketplaceListingOperation,
SmartContractReadMethodInvocation
} from '../../model';
import {ApproveErc20, CreateMarketplaceListing, Currency, DeployMarketplaceListing, InvokeMarketplaceListingOperation, SmartContractReadMethodInvocation} from '../../model';
import {
sendBscSmartContractMethodInvocationTransaction,
sendBscSmartContractReadMethodInvocationTransaction,
Expand Down Expand Up @@ -102,10 +95,10 @@ describe('Marketplace Listing tests', () => {

it('should buy listing erc20', async () => {

const approve = new ApproveMarketplaceErc20Spending()
approve.contractAddress = '0x874069Fa1Eb16D44d622F2e0Ca25eeA172369bC1'
approve.marketplaceAddress = '0x8cb76aEd9C5e336ef961265c6079C14e9cD3D2eA'
approve.chain = Currency.CELO
const approve = new ApproveErc20();
approve.contractAddress = '0x874069Fa1Eb16D44d622F2e0Ca25eeA172369bC1';
approve.spender = '0x8cb76aEd9C5e336ef961265c6079C14e9cD3D2eA';
approve.chain = Currency.CELO;
approve.feeCurrency = Currency.CELO
approve.amount = '1.015'
approve.fromPrivateKey = '0x4874827a55d87f2309c55b835af509e3427aa4d52321eeb49a2b93b5c0f8edfb'
Expand Down Expand Up @@ -253,9 +246,9 @@ describe('Marketplace Listing tests', () => {
})

it('should approve erc20', async () => {
const approve = new ApproveMarketplaceErc20Spending();
const approve = new ApproveErc20();
approve.contractAddress = '0x326c977e6efc84e512bb9c30f76e30c160ed06fb';
approve.marketplaceAddress = '0x4153B909f55B0Ec43c11e980dF09b853477D9F79';
approve.spender = '0x4153B909f55B0Ec43c11e980dF09b853477D9F79';
approve.chain = Currency.MATIC;
approve.amount = '0.002';
approve.fromPrivateKey = '0xf09110a0aae3dddba3d722c6c629fb08082963d8ed38afaf25cfce084c22e3d2';
Expand Down
50 changes: 5 additions & 45 deletions src/nft/marketplace/listing.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import BigNumber from 'bignumber.js';
import {get, validateBody} from '../../connector/tatum';
import token_abi from '../../contracts/erc20/token_abi';
import {prepareApproveErc20} from '../../fungible';
import {helperBroadcastTx, helperPrepareSCCall} from '../../helpers';
import {
ApproveMarketplaceErc20Spending,
ApproveTronMarketplaceErc20Spending,
ApproveErc20,
CreateMarketplaceListing,
CreateTronMarketplaceListing,
Currency,
Expand All @@ -18,11 +17,6 @@ import {
UpdateTronMarketplaceFeeRecipient
} from '../../model';
import {
getBscBep20ContractDecimals,
getCeloErc20ContractDecimals,
getEthErc20ContractDecimals,
getOne20ContractDecimals,
getPolygonErc20ContractDecimals,
prepareBscDeployMarketplaceListingSignedTransaction,
prepareCeloDeployMarketplaceListingSignedTransaction,
prepareEthDeployMarketplaceListingSignedTransaction,
Expand Down Expand Up @@ -226,42 +220,8 @@ export const prepareMarketplaceUpdateFeeRecipient = async (testnet: boolean, bod
* @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 prepareMarketplaceApproveErc20Spending = async (testnet: boolean, body: ApproveMarketplaceErc20Spending | ApproveTronMarketplaceErc20Spending, provider?: string) => {
await validateBody(body, body.chain === Currency.TRON ? ApproveTronMarketplaceErc20Spending : ApproveMarketplaceErc20Spending);
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;
// case Currency.TRON:
// amount = new BigNumber(body.amount).multipliedBy(new BigNumber(10).pow(await getTronTrc20ContractDecimals(testnet, body.contractAddress, provider))).toString(16)
// break
default:
throw new Error('Unsupported combination of inputs.');
}
const params = [body.marketplaceAddress, `0x${amount}`];
body.amount = '0';
// if (body.chain === Currency.TRON) {
// return await helperPrepareSCCall(testnet, body, ApproveTronMarketplaceErc20Spending, 'approve',
// [
// {type: 'address', value: convertAddressToHex(params[0])},
// {type: 'uint256', value: params[1]},
// ], 'approve(address,uint256)', provider, token_abi)
// } else {
return await helperPrepareSCCall(testnet, body, ApproveMarketplaceErc20Spending, 'approve', params, undefined, provider, token_abi);
// }
export const prepareMarketplaceApproveErc20Spending = async (testnet: boolean, body: ApproveErc20, provider?: string) => {
return prepareApproveErc20(testnet, body, provider);
};

/**
Expand Down Expand Up @@ -372,7 +332,7 @@ export const sendMarketplaceUpdateFeeRecipient = async (testnet: boolean, body:
* @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 sendMarketplaceApproveErc20Spending = async (testnet: boolean, body: ApproveMarketplaceErc20Spending | ApproveTronMarketplaceErc20Spending, provider?: string) =>
export const sendMarketplaceApproveErc20Spending = async (testnet: boolean, body: ApproveErc20, provider?: string) =>
helperBroadcastTx(body.chain, await prepareMarketplaceApproveErc20Spending(testnet, body, provider), body.signatureId);
/**
* Create new listing on the marketplace.
Expand Down

0 comments on commit 1c58878

Please sign in to comment.