openchain-sdk-yxl-ts
Version:
OpenChain SDK for browser
70 lines (56 loc) • 1.75 kB
JavaScript
import protobuf from 'protobufjs';
import long from 'long';
import bundleJson from '../../crypto/protobuf/bundle.js';
/**
* Contract create operation
* @param args
* @return {payload}
*/
export default function (args) {
try {
const { initBalance, payload, metadata, sourceAddress, initInput } = args;
const root = protobuf.Root.fromJSON(bundleJson);
const contract = root.lookupType('protocol.Contract');
const contractMsg = contract.create({
payload: payload,
// type: 0,
});
const accountThreshold = root.lookupType('protocol.AccountThreshold');
const accountThresholdMsg = accountThreshold.create({
txThreshold: 1,
});
const accountPrivilege = root.lookupType('protocol.AccountPrivilege');
const accountPrivilegeMsg = accountPrivilege.create({
// masterWeight: 0,
thresholds: accountThresholdMsg,
});
const createAccount = root.lookupType('protocol.OperationCreateAccount');
const opt = {
initBalance: long.fromValue(initBalance),
priv: accountPrivilegeMsg,
contract: contractMsg,
};
if (initInput) {
opt.initInput = initInput;
}
const createAccountMsg = createAccount.create(opt);
const operation = root.lookupType('protocol.Operation');
const operationPayload = {
type: operation.Type.CREATE_ACCOUNT,
createAccount: createAccountMsg,
};
if (sourceAddress) {
operationPayload.sourceAddress = sourceAddress;
}
if (metadata) {
operationPayload.metadata = metadata;
}
const err = operation.verify(operationPayload);
if (err) {
throw Error(err);
}
return operation.create(operationPayload);
} catch (err) {
throw err;
}
};