Skip to content

Commit

Permalink
fix transfer erc20 token
Browse files Browse the repository at this point in the history
  • Loading branch information
samtatum committed Sep 22, 2021
1 parent aa042ea commit 82b98a7
Show file tree
Hide file tree
Showing 14 changed files with 95 additions and 126 deletions.
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "@tatumio/tatum",
"version": "1.26.0",
"version": "1.26.1",
"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
6 changes: 3 additions & 3 deletions src/model/request/OneTransfer20.ts
@@ -1,7 +1,7 @@
import {IsOptional, Min} from 'class-validator'
import {TransferCustomErc20} from './TransferCustomErc20'
import {IsOptional, Min} from 'class-validator';
import {TransferErc20} from './TransferErc20';

export class OneTransfer20 extends TransferCustomErc20 {
export class OneTransfer20 extends TransferErc20 {

@IsOptional()
@Min(0)
Expand Down
33 changes: 0 additions & 33 deletions src/model/request/TransferCustomErc20.ts

This file was deleted.

18 changes: 17 additions & 1 deletion src/model/request/TransferErc20.ts
@@ -1,5 +1,6 @@
import {Type} from 'class-transformer';
import {IsNotEmpty, IsNumberString, IsOptional, Length, Matches, MaxLength, Min, ValidateNested,} from 'class-validator';
import {IsIn, IsNotEmpty, IsNumberString, IsOptional, Length, Matches, Max, MaxLength, Min, ValidateIf, ValidateNested} from 'class-validator';
import {Currency, ETH_BASED_CURRENCIES, MATIC_BASED_CURRENCIES} from './Currency';
import {Fee} from './Fee';
import {PrivateKeyOrSignatureId} from './PrivateKeyOrSignatureId';

Expand All @@ -18,11 +19,26 @@ export class TransferErc20 extends PrivateKeyOrSignatureId {
@IsOptional()
public data?: string;

@ValidateIf(o => !o.contractAddress)
@IsNotEmpty()
@IsIn([...ETH_BASED_CURRENCIES, Currency.XDC, Currency.MATIC, ...MATIC_BASED_CURRENCIES])
public currency: Currency;

@ValidateIf(o => !o.currency)
@IsNotEmpty()
@Length(42, 43)
public contractAddress: string;

@IsOptional()
@Type(() => Fee)
@ValidateNested()
public fee?: Fee;

@ValidateIf(o => !o.currency)
@Min(1)
@Max(30)
public digits: number;

@Min(0)
@IsOptional()
public nonce?: number;
Expand Down
9 changes: 0 additions & 9 deletions src/model/request/TransferEthErc20.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/model/request/index.ts
Expand Up @@ -4,9 +4,7 @@ export * from './EstimateGasVet'
export * from './Fee'
export * from './TransferBchBlockchain'
export * from './TransferBtcBasedBlockchain'
export * from './TransferCustomErc20'
export * from './TransferErc20'
export * from './TransferEthErc20';
export * from './TransferFlowCustomTx';
export * from './OneBurn721';
export * from './OneBurnMultiToken'
Expand Down
10 changes: 5 additions & 5 deletions src/transaction/bsc.spec.ts
@@ -1,5 +1,5 @@
import Web3 from 'web3'
import {Currency, DeployErc20, TransferBscBep20, TransferCustomErc20} from '../model'
import Web3 from 'web3';
import {Currency, DeployErc20, TransferBscBep20, TransferErc20} from '../model';
import {
bscGetGasPriceInWei,
prepareBscOrBep20SignedTransaction,
Expand All @@ -12,7 +12,7 @@ import {
sendDeployBep721Transaction,
sendMintBep721Transaction,
sendMintMultipleBep721Transaction
} from './bsc'
} from './bsc';

describe('BSC transactions', () => {
jest.setTimeout(19999)
Expand Down Expand Up @@ -51,7 +51,7 @@ describe('BSC transactions', () => {
})

it('should test valid custom transaction ERC20 data', async () => {
const body = new TransferCustomErc20()
const body = new TransferErc20();
body.fromPrivateKey = '0x1a4344e55c562db08700dd32e52e62e7c40b1ef5e27c6ddd969de9891a899b29'
body.amount = '0'
body.contractAddress = '0x811DfbFF13ADFBC3Cf653dCc373C03616D3471c9'
Expand Down Expand Up @@ -93,7 +93,7 @@ describe('BSC transactions', () => {
})

it('should test invalid custom transaction ERC20 data, missing digits', async () => {
const body = new TransferCustomErc20()
const body = new TransferErc20();
body.fromPrivateKey = '0x1a4344e55c562db08700dd32e52e62e7c40b1ef5e27c6ddd969de9891a899b29'
body.amount = '0'
body.contractAddress = '0x811DfbFF13ADFBC3Cf653dCc373C03616D3471c9'
Expand Down
12 changes: 6 additions & 6 deletions src/transaction/bsc.ts
Expand Up @@ -37,7 +37,7 @@ import {
SmartContractReadMethodInvocation,
TransactionKMS,
TransferBscBep20,
TransferCustomErc20,
TransferErc20,
TransferMultiToken,
TransferMultiTokenBatch,
UpdateCashbackErc721
Expand Down Expand Up @@ -276,8 +276,8 @@ export const prepareBscOrBep20SignedTransaction = async (body: TransferBscBep20,
* @param provider url of the Bsc Server to connect to. If not set, default public server will be used.
* @returns transaction data to be broadcast to blockchain.
*/
export const prepareCustomBep20SignedTransaction = async (body: TransferCustomErc20, provider?: string) => {
await validateBody(body, TransferCustomErc20)
export const prepareCustomBep20SignedTransaction = async (body: TransferErc20, provider?: string) => {
await validateBody(body, TransferErc20);
const {
fromPrivateKey,
to,
Expand All @@ -287,7 +287,7 @@ export const prepareCustomBep20SignedTransaction = async (body: TransferCustomEr
fee,
nonce,
signatureId
} = body
} = body;

const client = getBscClient(provider, fromPrivateKey)

Expand Down Expand Up @@ -989,8 +989,8 @@ export const sendBscOrBep20Transaction = async (body: TransferBscBep20, provider
* @param provider url of the Bsc Server to connect to. If not set, default public server will be used.
* @returns transaction id of the transaction in the blockchain
*/
export const sendCustomBep20Transaction = async (body: TransferCustomErc20, provider?: string) =>
bscBroadcast(await prepareCustomBep20SignedTransaction(body, provider), body.signatureId)
export const sendCustomBep20Transaction = async (body: TransferErc20, provider?: string) =>
bscBroadcast(await prepareCustomBep20SignedTransaction(body, provider), body.signatureId);

/**
* Send Bsc deploy BEP20 transaction to the blockchain. This method broadcasts signed transaction to the blockchain.
Expand Down
30 changes: 15 additions & 15 deletions src/transaction/eth.spec.ts
@@ -1,5 +1,5 @@
import {ethEstimateGas} from '../blockchain'
import {BurnErc20, Currency, DeployErc20, MintErc20, TransferCustomErc20, TransferEthErc20} from '../model'
import {ethEstimateGas} from '../blockchain';
import {BurnErc20, Currency, DeployErc20, MintErc20, TransferErc20} from '../model';
import {
ethGetGasPriceInWei,
prepareCustomErc20SignedTransaction,
Expand All @@ -13,11 +13,11 @@ import {
sendMintErc721Transaction,
sendMintMultipleErc721Transaction,
sendSmartContractMethodInvocationTransaction,
} from './eth'
} from './eth';

describe('ETH transactions', () => {
it('should test valid transaction ETH data', async () => {
const body = new TransferEthErc20()
const body = new TransferErc20();
body.fromPrivateKey = '0x2dedb85f2a87f17e143dbd5e51a589f27b4c6acf6bf29ebff8eb5c32b5e9de05'
body.amount = '0'
body.currency = Currency.ETH
Expand All @@ -27,7 +27,7 @@ describe('ETH transactions', () => {
})

it('should test valid transaction ERC20 data', async () => {
const body = new TransferEthErc20()
const body = new TransferErc20();
body.fromPrivateKey = '0x2dedb85f2a87f17e143dbd5e51a589f27b4c6acf6bf29ebff8eb5c32b5e9de05'
body.amount = '0'
body.currency = Currency.PLTC
Expand All @@ -37,14 +37,14 @@ describe('ETH transactions', () => {
})

it('should test valid custom transaction ERC20 data', async () => {
const body = new TransferCustomErc20()
body.fromPrivateKey = '0x2dedb85f2a87f17e143dbd5e51a589f27b4c6acf6bf29ebff8eb5c32b5e9de05'
body.amount = '0'
body.contractAddress = '0x8cb76aed9c5e336ef961265c6079c14e9cd3d2ea'
body.to = '0x8cb76aed9c5e336ef961265c6079c14e9cd3d2ea'
body.digits = 10
const txData = await prepareCustomErc20SignedTransaction(body)
expect(txData).toContain('0x')
const body = new TransferErc20();
body.fromPrivateKey = '0x37b091fc4ce46a56da643f021254612551dbe0944679a6e09cb5724d3085c9ab';
body.amount = '3';
body.contractAddress = '0xa089e2375e315a911816dcf9ad482bd3bfc8ec11';
body.to = '0xfb99F8aE9b70A0C8Cd96aE665BBaf85A7E01a2ef';
body.digits = 18;
const txData = await prepareCustomErc20SignedTransaction(body);
expect(txData).toContain('0x');
})

it('should test valid custom deployment ERC20', async () => {
Expand Down Expand Up @@ -95,7 +95,7 @@ describe('ETH transactions', () => {
})

it('should test invalid custom transaction ERC20 data, missing digits', async () => {
const body = new TransferCustomErc20()
const body = new TransferErc20();
body.fromPrivateKey = '0x4874827a55d87f2309c55b835af509e3427aa4d52321eeb49a2b93b5c0f8edfb'
body.amount = '0'
body.contractAddress = '0x8cb76aed9c5e336ef961265c6079c14e9cd3d2ea'
Expand All @@ -109,7 +109,7 @@ describe('ETH transactions', () => {
})

it('should not test valid transaction data, missing currency', async () => {
const body = new TransferEthErc20()
const body = new TransferErc20();
body.fromPrivateKey = '0x4874827a55d87f2309c55b835af509e3427aa4d52321eeb49a2b93b5c0f8edfb'
body.amount = '0'
body.to = '0x8cb76aed9c5e336ef961265c6079c14e9cd3d2ea'
Expand Down
25 changes: 12 additions & 13 deletions src/transaction/eth.ts
Expand Up @@ -36,14 +36,13 @@ import {
SmartContractMethodInvocation,
SmartContractReadMethodInvocation,
TransactionKMS,
TransferCustomErc20,
TransferEthErc20,
TransferErc20,
TransferMultiToken,
TransferMultiTokenBatch,
UpdateCashbackErc721,
} from '../model';
import {mintNFT} from '../nft';
import {obtainCustodialAddressType} from '../wallet';
import { mintNFT } from '../nft'

/**
* Estimate Gas price for the transaction.
Expand Down Expand Up @@ -255,8 +254,8 @@ export const prepareEthBurnErc20SignedTransaction = async (body: BurnErc20, prov
* @param provider url of the Ethereum Server to connect to. If not set, default public server will be used.
* @returns transaction data to be broadcast to blockchain.
*/
export const prepareEthOrErc20SignedTransaction = async (body: TransferEthErc20, provider?: string) => {
await validateBody(body, TransferEthErc20)
export const prepareEthOrErc20SignedTransaction = async (body: TransferErc20, provider?: string) => {
await validateBody(body, TransferErc20);
const {
fromPrivateKey,
to,
Expand All @@ -266,7 +265,7 @@ export const prepareEthOrErc20SignedTransaction = async (body: TransferEthErc20,
data,
nonce,
signatureId
} = body
} = body;

const client = getClient(provider, fromPrivateKey)

Expand Down Expand Up @@ -300,8 +299,8 @@ export const prepareEthOrErc20SignedTransaction = async (body: TransferEthErc20,
* @param provider url of the Ethereum Server to connect to. If not set, default public server will be used.
* @returns transaction data to be broadcast to blockchain.
*/
export const prepareCustomErc20SignedTransaction = async (body: TransferCustomErc20, provider?: string) => {
await validateBody(body, TransferCustomErc20)
export const prepareCustomErc20SignedTransaction = async (body: TransferErc20, provider?: string) => {
await validateBody(body, TransferErc20);
const {
fromPrivateKey,
to,
Expand All @@ -311,7 +310,7 @@ export const prepareCustomErc20SignedTransaction = async (body: TransferCustomEr
fee,
nonce,
signatureId
} = body
} = body;

const client = getClient(provider, fromPrivateKey)

Expand Down Expand Up @@ -997,8 +996,8 @@ export const sendStoreDataTransaction = async (body: CreateRecord, provider?: st
* @param provider url of the Ethereum Server to connect to. If not set, default public server will be used.
* @returns transaction id of the transaction in the blockchain
*/
export const sendEthOrErc20Transaction = async (body: TransferEthErc20, provider?: string) =>
ethBroadcast(await prepareEthOrErc20SignedTransaction(body, provider), body.signatureId)
export const sendEthOrErc20Transaction = async (body: TransferErc20, provider?: string) =>
ethBroadcast(await prepareEthOrErc20SignedTransaction(body, provider), body.signatureId);

/**
* Send Ethereum custom ERC20 transaction to the blockchain. This method broadcasts signed transaction to the blockchain.
Expand All @@ -1007,8 +1006,8 @@ export const sendEthOrErc20Transaction = async (body: TransferEthErc20, provider
* @param provider url of the Ethereum Server to connect to. If not set, default public server will be used.
* @returns transaction id of the transaction in the blockchain
*/
export const sendCustomErc20Transaction = async (body: TransferCustomErc20, provider?: string) =>
ethBroadcast(await prepareCustomErc20SignedTransaction(body, provider), body.signatureId)
export const sendCustomErc20Transaction = async (body: TransferErc20, provider?: string) =>
ethBroadcast(await prepareCustomErc20SignedTransaction(body, provider), body.signatureId);

/**
* Send Ethereum deploy ERC20 transaction to the blockchain. This method broadcasts signed transaction to the blockchain.
Expand Down
6 changes: 3 additions & 3 deletions src/transaction/polygon.spec.ts
@@ -1,5 +1,5 @@
import Web3 from 'web3';
import {CreateRecord, Currency, DeployErc20, TransferCustomErc20, TransferEthErc20} from '../model';
import {CreateRecord, Currency, DeployErc20, TransferErc20} from '../model';
import {
polygonGetGasPriceInWei,
preparePolygonBurnErc721SignedTransaction,
Expand Down Expand Up @@ -28,7 +28,7 @@ describe('MATIC transactions', () => {

describe('MATIC common transactions', () => {
it('should test valid transaction MATIC data', async () => {
const body = new TransferEthErc20()
const body = new TransferErc20();
body.fromPrivateKey = '0x1a4344e55c562db08700dd32e52e62e7c40b1ef5e27c6ddd969de9891a899b29';
body.amount = '0.0001';
body.currency = Currency.MATIC;
Expand Down Expand Up @@ -122,7 +122,7 @@ describe('MATIC transactions', () => {

describe('MATIC ERC20 transactions', () => {
it('should test valid custom transaction ERC20 data', async () => {
const body = new TransferCustomErc20()
const body = new TransferErc20();
body.fromPrivateKey = '0x1a4344e55c562db08700dd32e52e62e7c40b1ef5e27c6ddd969de9891a899b29'
body.amount = '10'
body.contractAddress = '0x0b9808fce74030c87aae334a30f6c8f6c66b090d'
Expand Down

0 comments on commit 82b98a7

Please sign in to comment.