UNPKG

bifcore-sdk-nodejs-bop-test

Version:
246 lines (225 loc) 9.96 kB
'use strict' const BIFCoreSDK = require('../index') const sdk = new BIFCoreSDK({ host: 'https://domestic-testnet.bitfactory.cn', apiKey: 'EKV5DQ81VSNX9E9CJZNGIYR3B9XL71YL' }) it('test gasSend', async () => { let gasSendOperation = { sourceAddress: 'did:bid:efnVUgqQFfYeu97ABf6sGm3WFtVXHZB2', privateKey: 'priSPKkWVk418PKAS66q4bsiE2c4dKuSSafZvNWyGGp2sJVtXL', destAddress: 'did:bid:efYhALrHoHiaVnoZgRgJbCghZZdzkQUh', remarks: 'gasSend', amount: '100000000', ceilLedgerSeq: '10', feeLimit: '', gasPrice: '', domainId: '21', nonceType: '1' } let data = await sdk.transaction.gasSend(gasSendOperation) console.log('gasSend() : ', JSON.stringify(data)) }) it('test getTransactionInfo', async () => { let param = { hash: 'ca605ba336ab25daac38466762ad2ae5d6328c183caabf895cc75fcbe4e43e2f', domainId: '21' } let data = await sdk.transaction.getTransactionInfo(param) console.log('getTransactionInfo() : ', JSON.stringify(data)) }) it('test evaluateFee', async () => { // setMetadata let setMetadata = sdk.operaction.accountSetMetadataOperation setMetadata.setKey('mykey1') setMetadata.setValue('myvalue1') setMetadata.setVersion('1') setMetadata.setDeleteFlag(true) // accountCreate let accountCreateOperation = sdk.operaction.accountCreateOperation accountCreateOperation.setDestAddress(sdk.keypair.getBidAndKeyPair().encAddress) accountCreateOperation.setInitBalance(1) // accountCreate let accountSetPrivilegeOperation = sdk.operaction.accountSetPrivilegeOperation let signers = [ { 'address': 'did:bid:ef29FfnB21n7wS5U4VY1rCzbo8tjPsJv4', 'weight': 2 } ] let typeThresholds = [ { 'type': 1, 'threshold': 1 }, { 'type': 7, 'threshold': 2 } ] accountSetPrivilegeOperation.setMasterWeight(1) accountSetPrivilegeOperation.setSigners(signers) accountSetPrivilegeOperation.setTxThreshold(2) accountSetPrivilegeOperation.setTypeThresholds(typeThresholds) // gasSendOperation let gasSendOperation = sdk.operaction.gasSendOperation gasSendOperation.setAmount(1000) gasSendOperation.setDestAddress('did:bid:efYhALrHoHiaVnoZgRgJbCghZZdzkQUh') // contractCreateOperation let contractCreateOperation = sdk.operaction.contractCreateOperation contractCreateOperation.setPayload("\"use strict\";function init(bar){/*init whatever you want*/return;}function main(input){let para = JSON.parse(input);if (para.do_foo)\n {\n let x = {\n \'hello\' : \'world\'\n };\n }\n }\n \n function query(input)\n { \n return input;\n }\n ",) contractCreateOperation.setInitBalance(1) contractCreateOperation.setType(1) // contractInvokeOperation let contractInvokeOperation = sdk.operaction.contractInvokeOperation contractInvokeOperation.setContractAddress('did:bid:efL7d2Ak1gyUpU4eiM3C9oxvbkhXr4Mu') let request = { sourceAddress: 'did:bid:efnVUgqQFfYeu97ABf6sGm3WFtVXHZB2', privateKey: 'priSPKkWVk418PKAS66q4bsiE2c4dKuSSafZvNWyGGp2sJVtXL', operations: contractInvokeOperation, feeLimit: '20', gasPrice: '2', domainId: '20', nonceType: '1' } let data = await sdk.transaction.evaluateFee(request) console.log('evaluateFee() : ', JSON.stringify(data)) }) /** * 批量费用评估 */ it('test batchEvaluateFee', async () => { let amount = '0' const destAddress1 = sdk.keypair.getBidAndKeyPairBySM2().encAddress const destAddress2 = sdk.keypair.getBidAndKeyPairBySM2().encAddress let input1 = '{"method":"creation","params":{"document":{"@context": ["https://w3.org/ns/did/v1"],"context": "https://w3id.org/did/v1","id": "' + destAddress1 + '", "version": "1"}}}' let input2 = '{"method":"creation","params":{"document":{"@context": ["https://w3.org/ns/did/v1"],"context": "https://w3id.org/did/v1","id": "' + destAddress2 + '", "version": "1"}}}' let operations = [] let contractInvokeOperation1 = { contractAddress: destAddress1, amount: amount, input: input1 } let contractInvokeOperation2 = { contractAddress: destAddress2, amount: amount, input: input2 } operations.push(contractInvokeOperation1) operations.push(contractInvokeOperation2) let request = { sourceAddress: 'did:bid:efnVUgqQFfYeu97ABf6sGm3WFtVXHZB2', privateKey: 'priSPKkWVk418PKAS66q4bsiE2c4dKuSSafZvNWyGGp2sJVtXL', operations: operations, feeLimit: '20', gasPrice: '1', domainId: '0', nonceType: '1' } let data = await sdk.transaction.batchEvaluateFee(request) console.log('batchEvaluateFee() : ', JSON.stringify(data)) }) it('test BIFSubmit', async () => { // 初始化参数 let serialization = '0a286469643a6269643a65666e5655677151466659657539374142663673476d335746745658485a4232104f22330807522f0a286469643a6269643a65665968414c72486f486961566e6f5a6752674a624367685a5a647a6b5155681080c2d72f2a0767617353656e6430c0843d3864' let privateKey = 'priSPKkWVk418PKAS66q4bsiE2c4dKuSSafZvNWyGGp2sJVtXL' let privateKey1 = 'priSPKpeenYnvVLaGkCg6Lm5c8vsq85htyF62xyFz54eCkJ2rK' // sign serialization let signatures = sdk.transaction.signTransSerialization([privateKey, privateKey1] , serialization) // let request = { // serialization: serialization, // signatures: signatures // } // submit transaction let transactionInfo = await sdk.transaction.submitTrans( serialization, signatures ) console.log('BIFSubmit() : ', JSON.stringify(transactionInfo)) }) /** * 查询交易数据列表 * @param page * @param page_size */ it('test getTransactionListByQuery', async () => { let param = { // hash: 'ca605ba336ab25daac38466762ad2ae5d6328c183caabf895cc75fcbe4e43e2f', page: 1, // page_size: 10 // tx_type: 0 } let data = await sdk.transaction.getTransactionListByQuery(param) console.log('getTransactionListByQuery() : ', JSON.stringify(data)) }) /** * 获取交易池中交易条数 */ it('test getTxCacheSize', async () => { let domainId = '21' let data = await sdk.transaction.getTxCacheSize(domainId) console.log('getTxCacheSize() : ', JSON.stringify(data)) }) /** * 获取交易池中交易数据 */ it('test getTxCacheData', async () => { let request = { domainId: '0', hash: 'ca605ba336ab25daac38466762ad2ae5d6328c183caabf895cc75fcbe4e43e2f', poolType: 1 } let data = await sdk.transaction.getTxCacheData(request) // console.log('getTxCacheData() : ', JSON.stringify(data)) // let domainId = '21' // let data = await sdk.transaction.getTxCacheSize(domainId) console.log('getTxCacheSize() : ', JSON.stringify(data)) }) /** * 离线blob数据解析 */ it('test parseBlob', async () => { let transactionBlob = '0a296469643a6269643a656632416b5341753846684a53776351725864415546633279644b7a65333179431080a088ae9ca6d4940822f502080122f00212e10212de022275736520737472696374223b66756e6374696f6e20696e697428626172297b2f2a696e697420776861746576657220796f752077616e742a2f72657475726e3b7d66756e6374696f6e206d61696e28696e707574297b6c65742070617261203d204a534f4e2e706172736528696e707574293b69662028706172612e646f5f666f6f290a2020202020202020202020207b0a20202020202020202020202020206c65742078203d207b0a202020202020202020202020202020202768656c6c6f27203a2027776f726c64270a20202020202020202020202020207d3b0a2020202020202020202020207d0a202020202020202020207d0a202020202020202020200a2020202020202020202066756e6374696f6e20717565727928696e707574290a202020202020202020207b200a20202020202020202020202072657475726e20696e7075743b0a202020202020202020207d0a20202020202020201a041a02080132043134323330c0843d386440c1b3f101' let data = await sdk.transaction.parseBlob(transactionBlob) console.log('parseBlob() : ', JSON.stringify(data)) }) /** * 批量发起交易 */ it('test batchGasSend', async () => { let senderAddress = 'did:bid:efnVUgqQFfYeu97ABf6sGm3WFtVXHZB2' let senderPrivateKey = 'priSPKkWVk418PKAS66q4bsiE2c4dKuSSafZvNWyGGp2sJVtXL' // let amount = '18' const destAddress1 = sdk.keypair.getBidAndKeyPair().encAddress // const destAddress2 = sdk.keypair.getBidAndKeyPair().encAddress let operations = [] // let gasSendOperation1 = { // destAddress: destAddress1, // amount: amount // } // let gasSendOperation2 = { // destAddress: destAddress2, // amount: amount // } // operations.push(gasSendOperation1) // operations.push(gasSendOperation2) for (let i = 1;i <= 10; i++) { let operation = { destAddress: destAddress1, amount: '10' } operations.push(operation) } let gasSendRequestOperation = sdk.operaction.gasSendRequestOperation gasSendRequestOperation.setSenderAddress(senderAddress) gasSendRequestOperation.setPrivateKey(senderPrivateKey) gasSendRequestOperation.setRemarks('gas send') gasSendRequestOperation.setDomainId('0') gasSendRequestOperation.setNonceType('1') gasSendRequestOperation.setCeilLedgerSeq('') // gasSendRequestOperation.setOperations(null) gasSendRequestOperation.setOperations(operations) let data = await sdk.transaction.batchGasSend(gasSendRequestOperation) console.log('batchGasSend() : ', JSON.stringify(data)) })