Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Win 1421 zksync statics #4173

Merged
merged 2 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions modules/sdk-api/src/bitgoAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ export class BitGoAPI implements BitGoBase {
'polygonscanApiToken',
'arbiscanApiToken',
'optimisticEtherscanApiToken',
'zksyncExplorerApiToken',
];

Object.keys(params).forEach((key) => {
Expand Down
1 change: 1 addition & 0 deletions modules/sdk-api/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface BitGoAPIOptions {
polygonscanApiToken?: string;
arbiscanApiToken?: string;
optimisticEtherscanApiToken?: string;
zksyncExplorerApiToken?: string;
hmacVerification?: boolean;
proxy?: string;
refreshToken?: string;
Expand Down
7 changes: 5 additions & 2 deletions modules/sdk-coin-zketh/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,13 @@
"@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/utxo-lib": "^9.26.0"
},
"devDependencies": {
"@bitgo/sdk-api": "^1.34.0",
"@bitgo/sdk-test": "^1.2.53"
"@bitgo/sdk-test": "^1.2.53",
"secp256k1": "5.0.0"
}
}
4 changes: 4 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,8 @@ export const testnetCommon = EthereumCommon.custom(
},
{
baseChain: 'sepolia',
hardfork: 'london',
ajays97 marked this conversation as resolved.
Show resolved Hide resolved
eips: [1559],
}
);

Expand All @@ -20,5 +22,7 @@ export const mainnetCommon = EthereumCommon.custom(
},
{
baseChain: 'mainnet',
hardfork: 'london',
ajays97 marked this conversation as resolved.
Show resolved Hide resolved
eips: [1559],
}
);
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);
});
};
23 changes: 22 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,24 @@ 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 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
Loading