Skip to content

Commit

Permalink
test(sdk-coin-zketh): add test cases for zketh
Browse files Browse the repository at this point in the history
Ticket: WIN-1422
  • Loading branch information
ajays97 committed Dec 28, 2023
1 parent 897d969 commit 145ea98
Show file tree
Hide file tree
Showing 10 changed files with 851 additions and 11 deletions.
8 changes: 6 additions & 2 deletions modules/sdk-coin-zketh/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,14 @@
"@bitgo/abstract-eth": "^11.0.0",
"@bitgo/sdk-core": "^18.0.0",
"@bitgo/statics": "^39.0.0",
"@ethereumjs/common": "^2.6.5"
"@ethereumjs/common": "^2.6.5",
"superagent": "^3.8.3",
"@bitgo/sjcl": "^1.0.1",
"@bitgo/utxo-lib": "^9.16.0"
},
"devDependencies": {
"@bitgo/sdk-api": "^1.34.0",
"@bitgo/sdk-test": "^1.2.53"
"@bitgo/sdk-test": "^1.2.53",
"secp256k1": "5.0.0"
}
}
2 changes: 2 additions & 0 deletions modules/sdk-coin-zketh/src/lib/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const testnetCommon = EthereumCommon.custom(
},
{
baseChain: 'sepolia',
hardfork: 'london',
}
);

Expand All @@ -20,5 +21,6 @@ export const mainnetCommon = EthereumCommon.custom(
},
{
baseChain: 'mainnet',
hardfork: 'london',
}
);
4 changes: 4 additions & 0 deletions modules/sdk-coin-zketh/src/register.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { BitGoBase } from '@bitgo/sdk-core';
import { Zketh } from './zketh';
import { Tzketh } from './tzketh';
import { ZkethToken } from './zkethToken';

export const register = (sdk: BitGoBase): void => {
sdk.register('zketh', Zketh.createInstance);
sdk.register('tzketh', Tzketh.createInstance);
ZkethToken.createTokenConstructors().forEach(({ name, coinConstructor }) => {
sdk.register(name, coinConstructor);
});
};
27 changes: 26 additions & 1 deletion modules/sdk-coin-zketh/src/zketh.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/**
* @prettier
*/
import { BaseCoin, BitGoBase } from '@bitgo/sdk-core';
import request from 'superagent';
import { BaseCoin, BitGoBase, common } from '@bitgo/sdk-core';
import { BaseCoin as StaticsBaseCoin, coins } from '@bitgo/statics';
import { AbstractEthLikeNewCoins, TransactionBuilder as EthLikeTransactionBuilder } from '@bitgo/abstract-eth';
import { TransactionBuilder } from './lib';
Expand All @@ -18,4 +19,28 @@ export class Zketh extends AbstractEthLikeNewCoins {
protected getTransactionBuilder(): EthLikeTransactionBuilder {
return new TransactionBuilder(coins.get(this.getBaseChain()));
}

/**
* Make a query to zkSync explorer for information such as balance, token balance, solidity calls
* @param {Object} query key-value pairs of parameters to append after /api
* @returns {Promise<Object>} response from zkSync explorer
*/
async recoveryBlockchainExplorerQuery(query: Record<string, string>): Promise<Record<string, unknown>> {
const token = common.Environments[this.bitgo.getEnv()].arbiscanApiToken;
if (token) {
query.apikey = token;
}
const response = await request
.get(common.Environments[this.bitgo.getEnv()].zksyncExplorerBaseUrl + '/api')
.query(query);

if (!response.ok) {
throw new Error('could not reach zkSync explorer');
}

if (response.body.status === '0' && response.body.message === 'NOTOK') {
throw new Error('zkSync explorer rate limit reached');
}
return response.body;
}
}
51 changes: 51 additions & 0 deletions modules/sdk-coin-zketh/test/fixtures/zketh.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
export function getTxListRequest(address: string) {
return {
module: 'account',
action: 'txlist',
address: address,
};
}

export const getTxListResponse = {
status: '0',
message: 'No transactions found',
result: [],
};

export function getBalanceRequest(address: string) {
return {
module: 'account',
action: 'balance',
address: address,
};
}

export function getTokenBalanceRequest(tokenContractAddress: string, address: string) {
return {
module: 'account',
action: 'tokenbalance',
contractaddress: tokenContractAddress,
address: address,
tag: 'latest',
};
}

export const getBalanceResponse = {
status: '1',
message: 'OK',
result: '9999999999999999928',
};

export const getContractCallRequest = {
module: 'proxy',
action: 'eth_call',
to: '0xdf07117705a9f8dc4c2a78de66b7f1797dba9d4e',
data: 'a0b7967b',
tag: 'latest',
};

export const getContractCallResponse = {
jsonrpc: '2.0',
result: '0x0000000000000000000000000000000000000000000000000000000000002a7f',
id: 1,
};
7 changes: 7 additions & 0 deletions modules/sdk-coin-zketh/test/getBuilder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { BaseBuilder } from '@bitgo/sdk-core';
import { coins } from '@bitgo/statics';
import { TransactionBuilder } from '../src';

export function getBuilder(coinName: string): BaseBuilder {
return new TransactionBuilder(coins.get(coinName));
}
37 changes: 37 additions & 0 deletions modules/sdk-coin-zketh/test/resources.ts

Large diffs are not rendered by default.

60 changes: 60 additions & 0 deletions modules/sdk-coin-zketh/test/unit/transaction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import should from 'should';
import { coins } from '@bitgo/statics';
import { Transaction } from '../../src';
import { getCommon } from '../../src/lib/utils';
import * as testData from '../resources';

describe('Zketh Transaction', () => {
const coinConfig = coins.get('tzketh');
const common = getCommon(coinConfig.network.type);

/**
* return a new transaction object
*/
function getTransaction(): Transaction {
return new Transaction(coinConfig, common);
}

describe('should throw ', () => {
it('an empty transaction', () => {
const tx = getTransaction();
should.throws(() => {
tx.toJson();
});
should.throws(() => {
tx.toBroadcastFormat();
});
});
});

describe('should return', () => {
it('a valid transaction', () => {
const tx = getTransaction();
tx.setTransactionData(testData.TXDATA);
should.deepEqual(tx.toJson(), testData.TXDATA);
should.equal(tx.toBroadcastFormat(), testData.UNSIGNED_TX);
});
});

describe('should sign', () => {
it('invalid', () => {
const tx = getTransaction();
return tx.sign(testData.KEYPAIR_PRV).should.be.rejected();
});

it('valid', () => {
const tx = getTransaction();
tx.setTransactionData(testData.TXDATA);
return tx.sign(testData.KEYPAIR_PRV).should.be.fulfilled();
});
});

describe('should return encoded tx', () => {
it('valid sign', async function () {
const tx = getTransaction();
tx.setTransactionData(testData.TXDATA);
await tx.sign(testData.KEYPAIR_PRV);
should.equal(tx.toBroadcastFormat(), testData.ENCODED_TRANSACTION);
});
});
});
Loading

0 comments on commit 145ea98

Please sign in to comment.