openchain-nodejs-ts-yxl 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. package/LICENSE +661 -0
  2. package/README.md +66 -0
  3. package/_config.yml +1 -0
  4. package/doc/SDK_CN.md +2003 -0
  5. package/example/atp10TokenDemo.js +319 -0
  6. package/example/exchange.js +184 -0
  7. package/example/offlineSignatureDemo.js +83 -0
  8. package/example/submitTransactionDemo.js +92 -0
  9. package/index.d.ts +186 -0
  10. package/index.js +9 -0
  11. package/lib/account/index.js +234 -0
  12. package/lib/blockchain/block.js +380 -0
  13. package/lib/blockchain/transaction.js +264 -0
  14. package/lib/common/ctp.js +5 -0
  15. package/lib/common/operation/accountSetMetadata.js +55 -0
  16. package/lib/common/operation/accountSetPrivilege.js +90 -0
  17. package/lib/common/operation/activateAccount.js +58 -0
  18. package/lib/common/operation/contractCreate.js +72 -0
  19. package/lib/common/operation/contractInvokeByAsset.js +75 -0
  20. package/lib/common/operation/contractInvokeByBU.js +53 -0
  21. package/lib/common/operation/createLog.js +44 -0
  22. package/lib/common/operation/ctp10TokenApprove.js +59 -0
  23. package/lib/common/operation/ctp10TokenAssign.js +59 -0
  24. package/lib/common/operation/ctp10TokenChangeOwner.js +58 -0
  25. package/lib/common/operation/ctp10TokenIssue.js +78 -0
  26. package/lib/common/operation/ctp10TokenTransfer.js +59 -0
  27. package/lib/common/operation/ctp10TokenTransferFrom.js +60 -0
  28. package/lib/common/operation/issueAsset.js +35 -0
  29. package/lib/common/operation/payAsset.js +62 -0
  30. package/lib/common/operation/payCoin.js +44 -0
  31. package/lib/common/util.js +880 -0
  32. package/lib/contract/index.js +212 -0
  33. package/lib/crypto/protobuf/bundle.json +1643 -0
  34. package/lib/exception/customErrors.js +56 -0
  35. package/lib/exception/errors.js +240 -0
  36. package/lib/exception/index.js +8 -0
  37. package/lib/operation/account.js +193 -0
  38. package/lib/operation/asset.js +106 -0
  39. package/lib/operation/bu.js +52 -0
  40. package/lib/operation/contract.js +184 -0
  41. package/lib/operation/ctp10Token.js +394 -0
  42. package/lib/operation/index.js +30 -0
  43. package/lib/operation/log.js +47 -0
  44. package/lib/sdk.js +70 -0
  45. package/lib/token/asset.js +79 -0
  46. package/lib/token/ctp10Token.js +301 -0
  47. package/lib/token/index.js +17 -0
  48. package/lib/util/index.js +86 -0
  49. package/openchain-sdk-nodejs.iml +9 -0
  50. package/package.json +39 -0
  51. package/test/Ctp10Token.test.js +96 -0
  52. package/test/account.test.js +132 -0
  53. package/test/accountActivateOperation.test.js +99 -0
  54. package/test/accountSetMetadata.test.js +102 -0
  55. package/test/accountSetPrivilege.test.js +66 -0
  56. package/test/asset.test.js +63 -0
  57. package/test/assetIssueOperation.test.js +77 -0
  58. package/test/assetSendOperation.test.js +103 -0
  59. package/test/blob.test.js +128 -0
  60. package/test/block.test.js +165 -0
  61. package/test/buSendOperation.test.js +64 -0
  62. package/test/contract.test.js +64 -0
  63. package/test/contractCreateOperation.test.js +41 -0
  64. package/test/contractCreateTransaction.test.js +116 -0
  65. package/test/contractInvokeByAssetOperation.test.js +109 -0
  66. package/test/contractInvokeByBUOperation.test.js +107 -0
  67. package/test/ctp10TokenApproveOperation.test.js +98 -0
  68. package/test/ctp10TokenAssignOperation.test.js +99 -0
  69. package/test/ctp10TokenChangeOwnerOperation.test.js +98 -0
  70. package/test/ctp10TokenIssueOperation.test.js +40 -0
  71. package/test/ctp10TokenIssueOperationTransaction.test.js +106 -0
  72. package/test/ctp10TokenTransferFromOperation.test.js +101 -0
  73. package/test/ctp10TokenTransferOperation.test.js +98 -0
  74. package/test/log.transaction.test.js +103 -0
  75. package/test/transaction.test.js +166 -0
  76. package/test/util.test.js +93 -0
@@ -0,0 +1,90 @@
1
+ 'use strict';
2
+
3
+ const is = require('is-type-of');
4
+ const protobuf = require('protobufjs');
5
+ const long = require('long');
6
+ const errors = require('../../exception/errors');
7
+
8
+ /**
9
+ * Account set priviliege
10
+ * @param args
11
+ * @return {payload}
12
+ */
13
+ module.exports = function (args) {
14
+ try {
15
+
16
+ const { sourceAddress, masterWeight, txThreshold, metadata, signers, typeThresholds } = args;
17
+
18
+ const root = protobuf.Root.fromJSON(require('../../crypto/protobuf/bundle.json'));
19
+
20
+ let signersList = [];
21
+ let typeThresholdsList = [];
22
+
23
+ const signer = root.lookupType('protocol.Signer');
24
+
25
+ if (is.array(signers)) {
26
+ signers.forEach(item => {
27
+ let signerMsg = signer.create({
28
+ address: item.address,
29
+ weight: long.fromValue(item.weight),
30
+ });
31
+ signersList.push(signerMsg);
32
+ });
33
+ }
34
+
35
+
36
+ const typeThreshold = root.lookupType('protocol.OperationTypeThreshold');
37
+
38
+ if (is.array(typeThresholds)) {
39
+ typeThresholds.forEach(item => {
40
+ let typeThresholdMsg = typeThreshold.create({
41
+ type: parseInt(item.type),
42
+ threshold: long.fromValue(item.threshold),
43
+ });
44
+ typeThresholdsList.push(typeThresholdMsg);
45
+ });
46
+ }
47
+
48
+ const setPrivilege = root.lookupType('protocol.OperationSetPrivilege');
49
+
50
+ const opt = {};
51
+
52
+ if (masterWeight) {
53
+ opt.masterWeight = masterWeight;
54
+ }
55
+
56
+ if (txThreshold) {
57
+ opt.txThreshold = txThreshold;
58
+ }
59
+
60
+ if (signersList.length > 0) {
61
+ opt.signers = signersList;
62
+ }
63
+
64
+ if (typeThresholdsList.length > 0) {
65
+ opt.typeThresholds = typeThresholdsList;
66
+ }
67
+
68
+ const setPrivilegeMsg = setPrivilege.create(opt);
69
+ const operation = root.lookupType('protocol.Operation');
70
+
71
+ const payload = {
72
+ sourceAddress,
73
+ type: operation.Type.SET_PRIVILEGE,
74
+ setPrivilege: setPrivilegeMsg,
75
+ };
76
+
77
+ if (metadata) {
78
+ payload.metadata = metadata;
79
+ }
80
+ // const err = operation.verify(payload);
81
+ //
82
+ // if (err) {
83
+ // throw Error(err);
84
+ // }
85
+
86
+ return operation.create(payload);
87
+ } catch (err) {
88
+ throw err;
89
+ }
90
+ };
@@ -0,0 +1,58 @@
1
+ 'use strict';
2
+
3
+ const is = require('is-type-of');
4
+ const protobuf = require('protobufjs');
5
+ const long = require('long');
6
+ const tou8 = require('buffer-to-uint8array');
7
+
8
+ /**
9
+ * createAccount operation: Activate Account
10
+ * @param {string} args
11
+ * @returns {string}
12
+ */
13
+ module.exports = function (args) {
14
+ try {
15
+ const { sourceAddress, destAddress, initBalance, metadata } = args;
16
+ const root = protobuf.Root.fromJSON(require('../../crypto/protobuf/bundle.json'));
17
+
18
+ const accountThreshold = root.lookupType('protocol.AccountThreshold');
19
+ const accountThresholdMsg = accountThreshold.create({
20
+ txThreshold: 1,
21
+ });
22
+
23
+ const accountPrivilege = root.lookupType('protocol.AccountPrivilege');
24
+
25
+ const accountPrivilegeMsg = accountPrivilege.create({
26
+ masterWeight: 1,
27
+ thresholds: accountThresholdMsg,
28
+ });
29
+
30
+ const createAccount = root.lookupType('protocol.OperationCreateAccount');
31
+ const createAccountMsg = createAccount.create({
32
+ destAddress,
33
+ initBalance: long.fromValue(initBalance),
34
+ priv: accountPrivilegeMsg,
35
+ });
36
+
37
+ const operation = root.lookupType('protocol.Operation');
38
+ const payload = {
39
+ createAccount: createAccountMsg,
40
+ type: operation.Type.CREATE_ACCOUNT,
41
+ sourceAddress,
42
+ };
43
+
44
+ if (metadata) {
45
+ payload.metadata = tou8(Buffer.from(metadata, 'utf8'));
46
+ }
47
+
48
+ const err = operation.verify(payload);
49
+
50
+ if (err) {
51
+ throw Error(err);
52
+ }
53
+
54
+ return operation.create(payload);
55
+ } catch (err) {
56
+ throw err;
57
+ }
58
+ };
@@ -0,0 +1,72 @@
1
+ 'use strict';
2
+
3
+ const is = require('is-type-of');
4
+ const protobuf = require('protobufjs');
5
+ const long = require('long');
6
+ const tou8 = require('buffer-to-uint8array');
7
+
8
+ /**
9
+ * create contract account
10
+ * @param args
11
+ * @return {payload}
12
+ */
13
+ module.exports = function (args) {
14
+ try {
15
+ const { initBalance, payload, metadata, sourceAddress, initInput } = args;
16
+ const root = protobuf.Root.fromJSON(require('../../crypto/protobuf/bundle.json'));
17
+
18
+ const contract = root.lookupType('protocol.Contract');
19
+ const contractMsg = contract.create({
20
+ payload: payload,
21
+ // type: 0,
22
+ });
23
+
24
+ const accountThreshold = root.lookupType('protocol.AccountThreshold');
25
+ const accountThresholdMsg = accountThreshold.create({
26
+ txThreshold: 1,
27
+ });
28
+
29
+ const accountPrivilege = root.lookupType('protocol.AccountPrivilege');
30
+ const accountPrivilegeMsg = accountPrivilege.create({
31
+ // masterWeight: 0,
32
+ thresholds: accountThresholdMsg,
33
+ });
34
+
35
+ const createAccount = root.lookupType('protocol.OperationCreateAccount');
36
+ const opt = {
37
+ initBalance: long.fromValue(initBalance),
38
+ priv: accountPrivilegeMsg,
39
+ contract: contractMsg,
40
+ };
41
+
42
+ if (initInput) {
43
+ opt.initInput = initInput;
44
+ }
45
+
46
+ const createAccountMsg = createAccount.create(opt);
47
+
48
+ const operation = root.lookupType('protocol.Operation');
49
+ const operationPayload = {
50
+ type: operation.Type.CREATE_ACCOUNT,
51
+ createAccount: createAccountMsg,
52
+ };
53
+
54
+ if (sourceAddress) {
55
+ operationPayload.sourceAddress = sourceAddress;
56
+ }
57
+
58
+ if (metadata) {
59
+ operationPayload.metadata = metadata;
60
+ }
61
+
62
+ const err = operation.verify(operationPayload);
63
+
64
+ if (err) {
65
+ throw Error(err);
66
+ }
67
+
68
+ return operation.create(operationPayload);
69
+ } catch (err) {
70
+ throw err;
71
+ }
72
+ };
@@ -0,0 +1,75 @@
1
+ 'use strict';
2
+
3
+ const is = require('is-type-of');
4
+ const protobuf = require('protobufjs');
5
+ const long = require('long');
6
+ const tou8 = require('buffer-to-uint8array');
7
+
8
+ /**
9
+ * contractInvokeByAsset operation
10
+ * @param args
11
+ * @return {object}
12
+ */
13
+ module.exports = function (args) {
14
+ try {
15
+ const { sourceAddress, contractAddress, code, issuer, assetAmount, metadata, input } = args;
16
+
17
+ const root = protobuf.Root.fromJSON(require('../../crypto/protobuf/bundle.json'));
18
+
19
+ const assetKey = root.lookupType('protocol.AssetKey');
20
+ const assetKeyMsg = assetKey.create({
21
+ issuer,
22
+ code,
23
+ });
24
+
25
+ const asset = root.lookupType('protocol.Asset');
26
+
27
+ let arg = {
28
+ key: assetKeyMsg,
29
+ };
30
+
31
+ if (assetAmount) {
32
+ arg.amount = long.fromValue(assetAmount);
33
+ }
34
+
35
+ const assetMsg = asset.create(arg);
36
+
37
+ const operationPayAsset = root.lookupType('protocol.OperationPayAsset');
38
+
39
+ const opt = {
40
+ destAddress: contractAddress,
41
+ asset: assetMsg,
42
+ };
43
+
44
+ if (input) {
45
+ opt.input = input;
46
+ }
47
+
48
+ const operationPayAssetMsg = operationPayAsset.create(opt);
49
+
50
+ const operation = root.lookupType('protocol.Operation');
51
+
52
+ const payload = {
53
+ payAsset: operationPayAssetMsg,
54
+ type: operation.Type.PAY_ASSET,
55
+ };
56
+
57
+ if (sourceAddress) {
58
+ payload.sourceAddress = sourceAddress;
59
+ }
60
+
61
+ if (metadata) {
62
+ payload.metadata = metadata;
63
+ }
64
+
65
+ const err = operation.verify(payload);
66
+
67
+ if (err) {
68
+ throw Error(err);
69
+ }
70
+
71
+ return operation.create(payload);
72
+ } catch (err) {
73
+ throw err;
74
+ }
75
+ };
@@ -0,0 +1,53 @@
1
+ 'use strict';
2
+
3
+ const is = require('is-type-of');
4
+ const protobuf = require('protobufjs');
5
+ const long = require('long');
6
+ const tou8 = require('buffer-to-uint8array');
7
+
8
+ /**
9
+ * contractInvokeByBU Operation
10
+ * @param args
11
+ * @return {object}
12
+ */
13
+ module.exports = function (args) {
14
+ try {
15
+ const { sourceAddress, contractAddress, buAmount, metadata, input } = args;
16
+ const root = protobuf.Root.fromJSON(require('../../crypto/protobuf/bundle.json'));
17
+ const payCoin = root.lookupType('protocol.OperationPayCoin');
18
+ let opt = {
19
+ destAddress: contractAddress,
20
+ };
21
+
22
+ if (buAmount) {
23
+ opt.amount = long.fromValue(buAmount);
24
+ }
25
+
26
+ if (input) {
27
+ opt.input = input;
28
+ }
29
+
30
+ const payCoinMsg = payCoin.create(opt);
31
+
32
+ const operation = root.lookupType('protocol.Operation');
33
+ const payload = {
34
+ payCoin: payCoinMsg,
35
+ type: operation.Type.PAY_COIN,
36
+ sourceAddress,
37
+ };
38
+
39
+ if (metadata) {
40
+ payload.metadata = metadata;
41
+ }
42
+
43
+ const err = operation.verify(payload);
44
+
45
+ if (err) {
46
+ throw Error(err);
47
+ }
48
+
49
+ return operation.create(payload);
50
+ } catch (err) {
51
+ throw err;
52
+ }
53
+ };
@@ -0,0 +1,44 @@
1
+ 'use strict';
2
+
3
+ const is = require('is-type-of');
4
+ const protobuf = require('protobufjs');
5
+ const long = require('long');
6
+ const tou8 = require('buffer-to-uint8array');
7
+
8
+ /**
9
+ * log operation: Create Log
10
+ * @param {string} args
11
+ * @returns {string}
12
+ */
13
+ module.exports = function (args) {
14
+ try {
15
+ const { sourceAddress, topic, data, metadata } = args;
16
+ const root = protobuf.Root.fromJSON(require('../../crypto/protobuf/bundle.json'));
17
+ const log = root.lookupType('protocol.OperationLog');
18
+ const logMsg = log.create({
19
+ topic,
20
+ datas: [data],
21
+ });
22
+
23
+ const operation = root.lookupType('protocol.Operation');
24
+ const payload = {
25
+ log: logMsg,
26
+ type: operation.Type.LOG,
27
+ sourceAddress,
28
+ };
29
+
30
+ if (metadata) {
31
+ payload.metadata = tou8(Buffer.from(metadata, 'utf8'));
32
+ }
33
+
34
+ const err = operation.verify(payload);
35
+
36
+ if (err) {
37
+ throw Error(err);
38
+ }
39
+
40
+ return operation.create(payload);
41
+ } catch (err) {
42
+ throw err;
43
+ }
44
+ };
@@ -0,0 +1,59 @@
1
+ 'use strict';
2
+
3
+ const is = require('is-type-of');
4
+ const protobuf = require('protobufjs');
5
+ const long = require('long');
6
+ const ctp = require('../ctp');
7
+
8
+ /**
9
+ * ctp10TokenApproveOperation
10
+ * @param args
11
+ * @return {payload}
12
+ */
13
+ module.exports = function (args) {
14
+ try {
15
+ const { sourceAddress, contractAddress, spender, tokenAmount, metadata} = args;
16
+ const root = protobuf.Root.fromJSON(require('../../crypto/protobuf/bundle.json'));
17
+ const payCoin = root.lookupType('protocol.OperationPayCoin');
18
+ const opt = {
19
+ destAddress: contractAddress,
20
+ // amount: 0,
21
+ };
22
+
23
+ let inputObj = {
24
+ method: 'approve',
25
+ params: {
26
+ spender: spender,
27
+ value: tokenAmount,
28
+ },
29
+ };
30
+
31
+ opt.input = JSON.stringify(inputObj);
32
+
33
+ const payCoinMsg = payCoin.create(opt);
34
+
35
+ const operation = root.lookupType('protocol.Operation');
36
+ const payload = {
37
+ payCoin: payCoinMsg,
38
+ type: operation.Type.PAY_COIN,
39
+ };
40
+
41
+ if (sourceAddress) {
42
+ payload.sourceAddress = sourceAddress;
43
+ }
44
+
45
+ if (metadata) {
46
+ payload.metadata = metadata;
47
+ }
48
+
49
+ const err = operation.verify(payload);
50
+
51
+ if (err) {
52
+ throw Error(err);
53
+ }
54
+
55
+ return operation.create(payload);
56
+ } catch (err) {
57
+ throw err;
58
+ }
59
+ };
@@ -0,0 +1,59 @@
1
+ 'use strict';
2
+
3
+ const is = require('is-type-of');
4
+ const protobuf = require('protobufjs');
5
+ const long = require('long');
6
+ const ctp = require('../ctp');
7
+
8
+ /**
9
+ * ctp10TokenTransferOperation
10
+ * @param args
11
+ * @return {payload}
12
+ */
13
+ module.exports = function (args) {
14
+ try {
15
+ const { contractAddress, destAddress, tokenAmount, sourceAddress, metadata } = args;
16
+ const root = protobuf.Root.fromJSON(require('../../crypto/protobuf/bundle.json'));
17
+ const payCoin = root.lookupType('protocol.OperationPayCoin');
18
+ const opt = {
19
+ destAddress: contractAddress,
20
+ // amount: 0,
21
+ };
22
+
23
+ let inputObj = {
24
+ method: 'assign',
25
+ params: {
26
+ to: destAddress,
27
+ value: tokenAmount,
28
+ },
29
+ };
30
+
31
+ opt.input = JSON.stringify(inputObj);
32
+
33
+ const payCoinMsg = payCoin.create(opt);
34
+
35
+ const operation = root.lookupType('protocol.Operation');
36
+ const payload = {
37
+ type: operation.Type.PAY_COIN,
38
+ payCoin: payCoinMsg,
39
+ };
40
+
41
+ if (sourceAddress) {
42
+ payload.sourceAddress = sourceAddress;
43
+ }
44
+
45
+ if (metadata) {
46
+ payload.metadata = metadata;
47
+ }
48
+
49
+ const err = operation.verify(payload);
50
+
51
+ if (err) {
52
+ throw Error(err);
53
+ }
54
+
55
+ return operation.create(payload);
56
+ } catch (err) {
57
+ throw err;
58
+ }
59
+ };
@@ -0,0 +1,58 @@
1
+ 'use strict';
2
+
3
+ const is = require('is-type-of');
4
+ const protobuf = require('protobufjs');
5
+ const long = require('long');
6
+ const ctp = require('../ctp');
7
+
8
+ /**
9
+ * Ctp10TokenChangeOwnerOperation
10
+ * @param args
11
+ * @return {payload}
12
+ */
13
+ module.exports = function (args) {
14
+ try {
15
+ const { contractAddress, tokenOwner, sourceAddress, metadata } = args;
16
+ const root = protobuf.Root.fromJSON(require('../../crypto/protobuf/bundle.json'));
17
+ const payCoin = root.lookupType('protocol.OperationPayCoin');
18
+ const opt = {
19
+ destAddress: contractAddress,
20
+ // amount: 0,
21
+ };
22
+
23
+ let inputObj = {
24
+ method: 'changeOwner',
25
+ params: {
26
+ address: tokenOwner,
27
+ },
28
+ };
29
+
30
+ opt.input = JSON.stringify(inputObj);
31
+
32
+ const payCoinMsg = payCoin.create(opt);
33
+
34
+ const operation = root.lookupType('protocol.Operation');
35
+ const payload = {
36
+ payCoin: payCoinMsg,
37
+ type: operation.Type.PAY_COIN,
38
+ };
39
+
40
+ if (sourceAddress) {
41
+ payload.sourceAddress = sourceAddress;
42
+ }
43
+
44
+ if (metadata) {
45
+ payload.metadata = metadata;
46
+ }
47
+
48
+ const err = operation.verify(payload);
49
+
50
+ if (err) {
51
+ throw Error(err);
52
+ }
53
+
54
+ return operation.create(payload);
55
+ } catch (err) {
56
+ throw err;
57
+ }
58
+ };
@@ -0,0 +1,78 @@
1
+ 'use strict';
2
+
3
+ const is = require('is-type-of');
4
+ const protobuf = require('protobufjs');
5
+ const long = require('long');
6
+ const ctp = require('../ctp');
7
+
8
+ /**
9
+ * ctp10TokenIssueOperation
10
+ * @param args
11
+ * @return {payload}
12
+ */
13
+ module.exports = function (args) {
14
+ try {
15
+ const { initBalance, name, symbol, decimals, totalSupply, sourceAddress, metadata } = args;
16
+ const root = protobuf.Root.fromJSON(require('../../crypto/protobuf/bundle.json'));
17
+
18
+ const contract = root.lookupType('protocol.Contract');
19
+ const contractMsg = contract.create({
20
+ payload: ctp.v10,
21
+ });
22
+
23
+ const accountThreshold = root.lookupType('protocol.AccountThreshold');
24
+ const accountThresholdMsg = accountThreshold.create({
25
+ txThreshold: 1,
26
+ });
27
+
28
+ const accountPrivilege = root.lookupType('protocol.AccountPrivilege');
29
+ const accountPrivilegeMsg = accountPrivilege.create({
30
+ // masterWeight: 0,
31
+ thresholds: accountThresholdMsg,
32
+ });
33
+
34
+ const createAccount = root.lookupType('protocol.OperationCreateAccount');
35
+ const opt = {
36
+ initBalance: long.fromValue(initBalance),
37
+ priv: accountPrivilegeMsg,
38
+ contract: contractMsg,
39
+ };
40
+
41
+ let initInputObj = {
42
+ params: {
43
+ name,
44
+ symbol,
45
+ decimals,
46
+ supply: totalSupply,
47
+ },
48
+ };
49
+
50
+ opt.initInput = JSON.stringify(initInputObj);
51
+
52
+ const createAccountMsg = createAccount.create(opt);
53
+
54
+ const operation = root.lookupType('protocol.Operation');
55
+ const operationPayload = {
56
+ type: operation.Type.CREATE_ACCOUNT,
57
+ createAccount: createAccountMsg,
58
+ };
59
+
60
+ if (sourceAddress) {
61
+ operationPayload.sourceAddress = sourceAddress;
62
+ }
63
+
64
+ if (metadata) {
65
+ operationPayload.metadata = metadata;
66
+ }
67
+
68
+ const err = operation.verify(operationPayload);
69
+
70
+ if (err) {
71
+ throw Error(err);
72
+ }
73
+
74
+ return operation.create(operationPayload);
75
+ } catch (err) {
76
+ throw err;
77
+ }
78
+ };
@@ -0,0 +1,59 @@
1
+ 'use strict';
2
+
3
+ const is = require('is-type-of');
4
+ const protobuf = require('protobufjs');
5
+ const long = require('long');
6
+ const ctp = require('../ctp');
7
+
8
+ /**
9
+ * ctp10TokenTransferOperation
10
+ * @param args
11
+ * @return {payload}
12
+ */
13
+ module.exports = function (args) {
14
+ try {
15
+ const { contractAddress, destAddress, tokenAmount, sourceAddress, metadata } = args;
16
+ const root = protobuf.Root.fromJSON(require('../../crypto/protobuf/bundle.json'));
17
+ const payCoin = root.lookupType('protocol.OperationPayCoin');
18
+ const opt = {
19
+ destAddress: contractAddress,
20
+ // amount: 0,
21
+ };
22
+
23
+ let inputObj = {
24
+ method: 'transfer',
25
+ params: {
26
+ to: destAddress,
27
+ value: tokenAmount,
28
+ },
29
+ };
30
+
31
+ opt.input = JSON.stringify(inputObj);
32
+
33
+ const payCoinMsg = payCoin.create(opt);
34
+
35
+ const operation = root.lookupType('protocol.Operation');
36
+ const payload = {
37
+ payCoin: payCoinMsg,
38
+ type: operation.Type.PAY_COIN,
39
+ };
40
+
41
+ if (sourceAddress) {
42
+ payload.sourceAddress = sourceAddress;
43
+ }
44
+
45
+ if (metadata) {
46
+ payload.metadata = metadata;
47
+ }
48
+
49
+ const err = operation.verify(payload);
50
+
51
+ if (err) {
52
+ throw Error(err);
53
+ }
54
+
55
+ return operation.create(payload);
56
+ } catch (err) {
57
+ throw err;
58
+ }
59
+ };