forked from matter-labs/zksync
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
41ec905
commit e241d67
Showing
7 changed files
with
194 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import * as handlebars from 'handlebars'; | ||
import * as zksync from 'zksync'; | ||
import * as ethers from 'ethers'; | ||
|
||
export function getDirPath() { | ||
return path.join(process.env.ZKSYNC_HOME as string, 'infrastructure/api-docs'); | ||
} | ||
|
||
function pasteAllFilesInOne() { | ||
let template = handlebars.compile(fs.readFileSync(path.join(getDirPath(), 'blueprint/template.apib'), 'utf-8'), { | ||
noEscape: true | ||
}); | ||
|
||
let replaceObject: any = {}; | ||
|
||
const groupsFiles = fs.readdirSync(path.join(getDirPath(), 'blueprint/groups')); | ||
for (let file of groupsFiles) { | ||
const data = fs.readFileSync(path.join(getDirPath(), 'blueprint/groups', file), 'utf-8'); | ||
replaceObject[file.replace('.apib', '') + 'Endpoints'] = data; | ||
} | ||
|
||
const typesFiles = fs.readdirSync(path.join(getDirPath(), 'blueprint/types')); | ||
for (const file of typesFiles) { | ||
const data = fs.readFileSync(path.join(getDirPath(), 'blueprint/types', file), 'utf-8'); | ||
replaceObject[file.replace('.apib', '') + 'Types'] = data; | ||
} | ||
|
||
return template(replaceObject); | ||
} | ||
|
||
async function compileCommon() { | ||
const data = pasteAllFilesInOne(); | ||
let template = handlebars.compile(data, { noEscape: true }); | ||
|
||
let replaceObject: any = await getHashesAndSignatures(); | ||
replaceObject['isResultNullable'] = '{{isResultNullable}}'; | ||
|
||
return template(replaceObject); | ||
} | ||
|
||
async function setupWallet() { | ||
const pathToConfig = path.join(process.env.ZKSYNC_HOME as string, `etc/test_config/constant/eth.json`); | ||
const config = fs.readFileSync(pathToConfig, { | ||
encoding: 'utf-8' | ||
}); | ||
const ethTestConfig = JSON.parse(config); | ||
let web3Url = (process.env.ETH_CLIENT_WEB3_URL as string).split(',')[0]; | ||
const ethProvider = new ethers.providers.JsonRpcProvider(web3Url); | ||
ethProvider.pollingInterval = 100; | ||
const syncProvider = await zksync.getDefaultRestProvider('localhost'); | ||
const ethWallet = ethers.Wallet.fromMnemonic(ethTestConfig.test_mnemonic as string, "m/44'/60'/0'/0/0").connect( | ||
ethProvider | ||
); | ||
|
||
const syncWallet = await zksync.Wallet.fromEthSigner(ethWallet, syncProvider); | ||
|
||
const depositHandle = await syncWallet.depositToSyncFromEthereum({ | ||
depositTo: syncWallet.address(), | ||
token: 'ETH', | ||
amount: syncWallet.provider.tokenSet.parseToken('ETH', '1000') | ||
}); | ||
await depositHandle.awaitReceipt(); | ||
|
||
if (!(await syncWallet.isSigningKeySet())) { | ||
const changePubkeyHandle = await syncWallet.setSigningKey({ | ||
feeToken: 'ETH', | ||
ethAuthType: 'ECDSA' | ||
}); | ||
await changePubkeyHandle.awaitReceipt(); | ||
} | ||
|
||
return syncWallet; | ||
} | ||
|
||
interface Parameters { | ||
txHash: string; | ||
txBatchHash: string; | ||
address: string; | ||
accountId: number; | ||
pubKey: string; | ||
l2Signature: string; | ||
ethereumSignature: string; | ||
} | ||
|
||
async function getHashesAndSignatures() { | ||
let syncWallet = await setupWallet(); | ||
|
||
const handle = await syncWallet.syncTransfer({ to: syncWallet.address(), token: 'ETH', amount: 0 }); | ||
await handle.awaitReceipt(); | ||
const txHash = handle.txHash; | ||
|
||
const batch = await syncWallet | ||
.batchBuilder() | ||
.addTransfer({ to: syncWallet.address(), token: 'ETH', amount: 0 }) | ||
.build('ETH'); | ||
let txs = []; | ||
for (const signedTx of batch.txs) { | ||
txs.push(signedTx.tx); | ||
} | ||
|
||
const submitBatchResponse = await (syncWallet.provider as zksync.RestProvider).submitTxsBatchNew( | ||
txs, | ||
batch.signature | ||
); | ||
await syncWallet.provider.notifyTransaction(submitBatchResponse.transactionHashes[0], 'COMMIT'); | ||
const txBatchHash = submitBatchResponse.batchHash; | ||
|
||
const signedTransfer = await syncWallet.signSyncTransfer({ | ||
to: '0xD3c62D2F7b6d4A63577F2415E55A6Aa6E1DbB9CA', | ||
token: 'ETH', | ||
amount: '17500000000000000', | ||
fee: '12000000000000000000', | ||
nonce: 12123, | ||
validFrom: 0, | ||
validUntil: 1239213821 | ||
}); | ||
const address = syncWallet.address(); | ||
const accountId = (await syncWallet.getAccountId())!; | ||
const pubKey = signedTransfer.tx.signature!.pubKey; | ||
const l2Signature = signedTransfer.tx.signature!.signature; | ||
const ethereumSignature = signedTransfer.ethereumSignature!.signature; | ||
|
||
let result: Parameters = { | ||
txHash, | ||
txBatchHash, | ||
address, | ||
accountId, | ||
pubKey, | ||
l2Signature, | ||
ethereumSignature | ||
}; | ||
return result; | ||
} | ||
|
||
export async function compileApibForDocumentation() { | ||
const before = await compileCommon(); | ||
let template = handlebars.compile(before, { noEscape: true }); | ||
|
||
let replaceObject: any = {}; | ||
replaceObject['isResultNullable'] = ', nullable'; | ||
|
||
const after = template(replaceObject); | ||
|
||
fs.writeFileSync(path.join(getDirPath(), 'blueprint/documentation.apib'), after); | ||
} | ||
|
||
export async function compileApibForTest() { | ||
const before = await compileCommon(); | ||
let template = handlebars.compile(before, { noEscape: true }); | ||
|
||
let replaceObject: any = {}; | ||
replaceObject['isResultNullable'] = ''; | ||
|
||
const after = template(replaceObject); | ||
|
||
fs.writeFileSync(path.join(getDirPath(), 'blueprint/test.apib'), after); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { spawn as _spawn } from 'child_process'; | ||
|
||
// executes a command in a new shell | ||
// but pipes data to parent's stdout/stderr | ||
export function spawn(command: string) { | ||
command = command.replace(/\n/g, ' '); | ||
const child = _spawn(command, { stdio: 'inherit', shell: true }); | ||
return new Promise((resolve, reject) => { | ||
child.on('error', reject); | ||
child.on('close', (code) => { | ||
code == 0 ? resolve(code) : reject(`Child process exited with code ${code}`); | ||
}); | ||
}); | ||
} |
Oops, something went wrong.