bifcore-sdk-nodejs-bop
Version:
bifcore sdk nodejs
145 lines (135 loc) • 5.54 kB
JavaScript
'use strict'
const BIFCoreSDK = require('../index')
const sdk = new BIFCoreSDK({
// host: 'http://test.bifcore.bitfactory.cn'
// apiKey: 'WZ1RU5DL0UOM47HTNT2MZESZQY5K0IQ8'
// host: 'https://domestic-mainnet.bitfactory.cn',
// apiKey: 'WZ1RU5DL0UOM47HTNT2MZESZQY5K0IQ8'
host: 'https://domestic-testnet.bitfactory.cn',
apiKey: 'EKV5DQ81VSNX9E9CJZNGIYR3B9XL71YL' // dp-test-demo
})
/**
* 检测合约账户的有效性
*/
it('test checkContractAddress', async () => {
let param = {
contractAddress: 'did:bid:efnVUgqQFfYeu97ABf6sGm3WFtVXHZB2'
// domainId: '20'
}
let data = await sdk.contract.checkContractAddress(param)
console.log('checkContractAddress() : ', JSON.stringify(data))
})
/**
* 合约创建
*/
it('test createContract', async () => {
let createContractOperation = {
sourceAddress: 'did:bid:ef2AkSAu8FhJSwcQrXdAUFc2ydKze31yC',
privateKey: 'priSPKqZwAfiZ9ApFhSgejTHRUCGyGg5ZFiVsXZC6HXH6XN2Dp',
payload: "\"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 ",
initBalance: '0',
remarks: '',
type: 0,
feeLimit: '',
gasPrice: '',
ceilLedgerSeq: '2',
initInput: '1423',
domainId: '0',
nonceType: '0'
}
let data = await sdk.contract.createContract(createContractOperation)
console.log('createContract() : ', JSON.stringify(data))
})
/**
* 根据交易Hash查询合约地址
*/
it('test getContractAddress', async () => {
let param = {
hash: '57f50638038a8acf8e7e3715a2527fd1eb71980f0ec68880b65614f8e0711d0e',
domainId: '0'
}
let data = await sdk.contract.getContractAddress(param)
console.log('getContractAddress() : ', JSON.stringify(data))
})
/**
* 查询合约代码。
*/
it('test getContractInfo', async () => {
let param = {
contractAddress: 'did:bid:efRH1Lbsuqwc6jRw3hK4H5Hp2RhHnryS',
domainId: '0'
}
let data = await sdk.contract.getContractInfo(param)
console.log('getContractInfo() : ', JSON.stringify(data))
})
/**
* 调用合约查询接口。
*/
it('test contractQuery', async () => {
let contractQueryOperation = {
sourceAddress: 'did:bid:ef2AkSAu8FhJSwcQrXdAUFc2ydKze31yC',
contractAddress: 'did:bid:ef3HeAtEXiV3G1rAKgxT3DhZ6XA9NeSB',
input: '',
feeLimit: '',
gasPrice: '',
domainId: '0'
}
let data = await sdk.contract.contractQuery(contractQueryOperation)
console.log('contractQuery() : ', JSON.stringify(data))
})
/**
* 调用合约查询接口。
*/
it('test contractInvoke', async () => {
let contractInvokeOperation = {
sourceAddress: 'did:bid:efnVUgqQFfYeu97ABf6sGm3WFtVXHZB2',
privateKey: 'priSPKkWVk418PKAS66q4bsiE2c4dKuSSafZvNWyGGp2sJVtXL',
contractAddress: 'did:bid:efHtDebBjqEsgEVqyiwvHwdPWSnHmkzy',
ceilLedgerSeq: '1',
feeLimit: '',
gasPrice: '',
remarks: 'contractInvoke',
amount: '1',
input: '',
domainId: '0',
nonceType: '0'
}
let data = await sdk.contract.contractInvoke(contractInvokeOperation)
console.log('contractInvoke() : ', JSON.stringify(data))
})
/**
* 用于批量合约调用。
*/
it('test batchContractInvoke', async () => {
let senderAddress = 'did:bid:efnVUgqQFfYeu97ABf6sGm3WFtVXHZB2'
let senderPrivateKey = 'priSPKkWVk418PKAS66q4bsiE2c4dKuSSafZvNWyGGp2sJVtXL'
let contractAddress = 'did:bid:efHtDebBjqEsgEVqyiwvHwdPWSnHmkzy'
let amount = '0'
const destAddress1 = sdk.keypair.getBidAndKeyPair().encAddress
const destAddress2 = sdk.keypair.getBidAndKeyPair().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: contractAddress,
amount: amount,
input: input1
}
let contractInvokeOperation2 = {
contractAddress: contractAddress,
amount: amount,
input: input2
}
operations.push(contractInvokeOperation1)
operations.push(contractInvokeOperation2)
let contractInvokeRequestOperation = sdk.operaction.contractInvokeRequestOperation
contractInvokeRequestOperation.setSenderAddress(senderAddress)
contractInvokeRequestOperation.setPrivateKey(senderPrivateKey)
contractInvokeRequestOperation.setRemarks('contract invoke')
contractInvokeRequestOperation.setDomainId('0')
contractInvokeRequestOperation.setNonceType('0')
contractInvokeRequestOperation.setCeilLedgerSeq('')
contractInvokeRequestOperation.setOperations(operations)
let data = await sdk.contract.batchContractInvoke(contractInvokeRequestOperation)
console.log('batchContractInvoke() : ', JSON.stringify(data))
})