Skip to content

Commit

Permalink
add ETH custodial wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel Sramko committed Nov 4, 2021
1 parent f2ec524 commit c72ab7f
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 26 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.29.8",
"version": "1.29.9",
"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
9 changes: 5 additions & 4 deletions src/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import {
prepareOneSmartContractWriteMethodInvocation,
preparePolygonClient,
preparePolygonSmartContractWriteMethodInvocation,
prepareSmartContractWriteMethodInvocation
prepareSmartContractWriteMethodInvocation,
prepareTronSmartContractInvocation
} from '../transaction';

export const helperBroadcastTx = async (chain: Currency, txData: string, signatureId?: string) => {
Expand Down Expand Up @@ -82,9 +83,9 @@ export const helperPrepareSCCall = async (testnet: boolean, body: any, clazz: Cl
return await prepareBscSmartContractWriteMethodInvocation(r, provider);
case Currency.MATIC:
return await preparePolygonSmartContractWriteMethodInvocation(testnet, r, provider);
// case Currency.TRON:
// r.methodName = methodSig as string
// return await prepareTronSmartContractInvocation(testnet, r, body.feeLimit, body.from, provider)
case Currency.TRON:
r.methodName = methodSig as string;
return await prepareTronSmartContractInvocation(testnet, r, body.feeLimit, body.from, provider);
default:
throw new Error('Unsupported combination of inputs.');
}
Expand Down
14 changes: 12 additions & 2 deletions src/model/request/GenerateCustodialAddressBatch.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {Type} from 'class-transformer';
import {IsIn, IsNotEmpty, IsNumber, IsOptional, Length, Max, Min} from 'class-validator';
import {IsIn, IsNotEmpty, IsNumber, IsOptional, Length, Max, Min, ValidateIf} from 'class-validator';
import {Currency} from './Currency';
import {Fee} from './Fee';
import {PrivateKeyOrSignatureId} from './PrivateKeyOrSignatureId';

export class GenerateCustodialAddressBatch extends PrivateKeyOrSignatureId {

@IsNotEmpty()
@IsIn([Currency.MATIC, Currency.CELO, Currency.BSC, Currency.ETH])
@IsIn([Currency.MATIC, Currency.CELO, Currency.BSC, Currency.ETH, Currency.TRON])
public chain: Currency;

@IsNumber()
Expand All @@ -31,4 +31,14 @@ export class GenerateCustodialAddressBatch extends PrivateKeyOrSignatureId {
@IsOptional()
@IsIn([Currency.CELO, Currency.CUSD, Currency.CEUR])
public feeCurrency?: Currency;

@ValidateIf(o => o.signatureId && o.chain === Currency.TRON)
@IsNotEmpty()
@Length(34, 34)
public from?: string;

@ValidateIf(o => o.chain === Currency.TRON)
@IsNotEmpty()
@Min(0)
public feeLimit?: number;
}
48 changes: 32 additions & 16 deletions src/wallet/custodial.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,25 +258,41 @@ describe('Custodial wallet tests', () => {

it('should create on ETH no batch KMS', async () => {
const body = new GenerateCustodialAddress()
body.signatureId = '96e13f7f-393e-4f64-8fde-17bd90ce2c5b'
body.chain = Currency.ETH
body.enableFungibleTokens = true
body.enableNonFungibleTokens = true
body.enableSemiFungibleTokens = false
body.enableBatchTransactions = false
const txData = await prepareEthGenerateCustodialWalletSignedTransaction(body)
expect(txData).toContain('0x')
body.signatureId = '96e13f7f-393e-4f64-8fde-17bd90ce2c5b';
body.chain = Currency.ETH;
body.enableFungibleTokens = true;
body.enableNonFungibleTokens = true;
body.enableSemiFungibleTokens = false;
body.enableBatchTransactions = false;
const txData = await prepareEthGenerateCustodialWalletSignedTransaction(body);
expect(txData).toContain('0x');
})

it('should create on TRON', async () => {
const body = new GenerateCustodialAddressBatch();
body.fromPrivateKey = '842E09EB40D8175979EFB0071B28163E11AED0F14BDD84090A4CEFB936EF5701';
body.chain = Currency.TRON;
body.feeLimit = 500;
body.owner = 'TYMwiDu22V6XG3yk6W9cTVBz48okKLRczh';
body.batchCount = 10;
try {
const txData = await generateCustodialWalletBatch(true, body);
expect(txData.txId).toBeDefined();
console.log(txData.txId);
} catch (e) {
console.error(e);
}
});

it('should create on TRON no batch', async () => {
const body = new GenerateTronCustodialAddress()
body.fromPrivateKey = '842E09EB40D8175979EFB0071B28163E11AED0F14BDD84090A4CEFB936EF5701'
body.chain = Currency.TRON
body.enableFungibleTokens = true
body.enableNonFungibleTokens = true
body.feeLimit = 500
body.enableSemiFungibleTokens = false
body.enableBatchTransactions = true
const body = new GenerateTronCustodialAddress();
body.fromPrivateKey = '842E09EB40D8175979EFB0071B28163E11AED0F14BDD84090A4CEFB936EF5701';
body.chain = Currency.TRON;
body.enableFungibleTokens = true;
body.enableNonFungibleTokens = true;
body.feeLimit = 500;
body.enableSemiFungibleTokens = false;
body.enableBatchTransactions = true;
const txData = await sendTronGenerateCustodialWalletSignedTransaction(true, body)
expect(txData.txId).toBeDefined()
console.log(txData.txId)
Expand Down
16 changes: 13 additions & 3 deletions src/wallet/custodial.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import BigNumber from 'bignumber.js';
import {bscBroadcast, celoBroadcast, ethBroadcast, polygonBroadcast} from '../blockchain';
import {bscBroadcast, celoBroadcast, ethBroadcast, polygonBroadcast, tronBroadcast} from '../blockchain';
import {get, validateBody} from '../connector/tatum';
import {CUSTODIAL_PROXY_ABI} from '../constants';
import {
Expand Down Expand Up @@ -121,6 +121,8 @@ const getCustodialFactoryContractAddress = (chain: Currency, testnet: boolean) =
switch (chain) {
case Currency.CELO:
return testnet ? '0x7f6ECaef0d01De5D464B8c1Ca968b102ABd40Ca1' : '0xb1462fE8E9Cf82c0296022Cca7bEfA3Fd4c12B34';
case Currency.TRON:
return testnet ? 'TJMnJy7ZR5XtYzM3LSzAhAgXtrk5yjPcF4' : 'TWdxxUWtu9Q5GaX1qrAGsvURXu6ziLerkX';
case Currency.ETH:
return testnet ? (process.env.TESTNET_TYPE === 'ethereum-rinkeby' ? '0x664F97470654e8f00E42433CFFC0d08a5f4f7BC7' : '0x9120093df23a6b1486ded257b1cd0ce651fe1323') : '0x183363CE6418Fad855255B6681711eD56b0C442A';
case Currency.MATIC:
Expand Down Expand Up @@ -204,6 +206,8 @@ export const generateCustodialWalletBatch = async (testnet: boolean, body: Gener
switch (body.chain) {
case Currency.CELO:
return await celoBroadcast(txData, body.signatureId);
case Currency.TRON:
return await tronBroadcast(txData, body.signatureId);
case Currency.ETH:
return await ethBroadcast(txData, body.signatureId);
case Currency.MATIC:
Expand All @@ -225,11 +229,17 @@ export const generateCustodialWalletBatch = async (testnet: boolean, body: Gener
*/
export const prepareCustodialWalletBatch = async (testnet: boolean, body: GenerateCustodialAddressBatch, provider?: string) => {
await validateBody(body, GenerateCustodialAddressBatch);
const params = [body.owner.trim(), `0x${new BigNumber(body.batchCount).toString(16)}`];
const params =
body.chain === Currency.TRON
? [{type: 'address', value: convertAddressToHex(body.owner.trim())},
{type: 'uint256', value: `0x${new BigNumber(body.batchCount).toString(16)}`}]
: [body.owner.trim(), `0x${new BigNumber(body.batchCount).toString(16)}`];

const methodName = body.chain === Currency.TRON ? 'cloneNewWallet(address,uint256)' : 'cloneNewWallet';
return await helperPrepareSCCall(testnet, {
...body,
contractAddress: getCustodialFactoryContractAddress(body.chain, testnet)
}, GenerateCustodialAddressBatch, 'cloneNewWallet', params, undefined, provider, [CUSTODIAL_PROXY_ABI]);
}, GenerateCustodialAddressBatch, methodName, params, body.chain === Currency.TRON ? methodName : undefined, provider, [CUSTODIAL_PROXY_ABI]);
};

/**
Expand Down

0 comments on commit c72ab7f

Please sign in to comment.