@kynesyslabs/demosdk 2.3.27 → 2.4.0
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.
- package/build/contracts/ContractDeployer.d.ts +34 -0
- package/build/contracts/ContractDeployer.js +196 -0
- package/build/contracts/ContractDeployer.js.map +1 -0
- package/build/contracts/ContractFactory.d.ts +52 -0
- package/build/contracts/ContractFactory.js +98 -0
- package/build/contracts/ContractFactory.js.map +1 -0
- package/build/contracts/ContractInstance.d.ts +51 -0
- package/build/contracts/ContractInstance.js +138 -0
- package/build/contracts/ContractInstance.js.map +1 -0
- package/build/contracts/ContractInteractor.d.ts +55 -0
- package/build/contracts/ContractInteractor.js +242 -0
- package/build/contracts/ContractInteractor.js.map +1 -0
- package/build/contracts/index.d.ts +9 -0
- package/build/contracts/index.js +16 -0
- package/build/contracts/index.js.map +1 -0
- package/build/contracts/types/ContractABI.d.ts +111 -0
- package/build/contracts/types/ContractABI.js +6 -0
- package/build/contracts/types/ContractABI.js.map +1 -0
- package/build/contracts/types/TypedContract.d.ts +34 -0
- package/build/contracts/types/TypedContract.js +6 -0
- package/build/contracts/types/TypedContract.js.map +1 -0
- package/build/websdk/DemosContracts.d.ts +89 -0
- package/build/websdk/DemosContracts.js +170 -0
- package/build/websdk/DemosContracts.js.map +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Smart contract functionality for Demos SDK
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.DemosContracts = void 0;
|
|
7
|
+
const ContractFactory_1 = require("../contracts/ContractFactory");
|
|
8
|
+
class DemosContracts {
|
|
9
|
+
constructor(demos) {
|
|
10
|
+
this.demos = demos;
|
|
11
|
+
this.factory = new ContractFactory_1.ContractFactory(demos);
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Deploy a new smart contract
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* const contract = await demos.contracts.deploy(`
|
|
19
|
+
* class MyToken extends DemosContract {
|
|
20
|
+
* constructor() {
|
|
21
|
+
* this.state.set('totalSupply', 1000000)
|
|
22
|
+
* }
|
|
23
|
+
* }
|
|
24
|
+
* `)
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
async deploy(source, constructorArgs = [], options = {}) {
|
|
28
|
+
if (!this.demos.walletConnected) {
|
|
29
|
+
throw new Error('Wallet not connected');
|
|
30
|
+
}
|
|
31
|
+
return await this.factory.deploy(source, constructorArgs, options);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Get an instance of an existing contract
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```typescript
|
|
38
|
+
* const contract = await demos.contracts.at('contract_address')
|
|
39
|
+
* const result = await contract.call('balanceOf', ['my_address'])
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
async at(address, abi) {
|
|
43
|
+
return await this.factory.at(address, abi);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Call a contract method directly
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```typescript
|
|
50
|
+
* const result = await demos.contracts.call(
|
|
51
|
+
* 'contract_address',
|
|
52
|
+
* 'transfer',
|
|
53
|
+
* ['recipient', 100]
|
|
54
|
+
* )
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
async call(contractAddress, method, args = [], options = {}) {
|
|
58
|
+
const instance = await this.at(contractAddress);
|
|
59
|
+
return await instance.call(method, args, options);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Create a batch operation
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* ```typescript
|
|
66
|
+
* const batch = demos.contracts.batch()
|
|
67
|
+
* .deploy(tokenContract)
|
|
68
|
+
* .call(existingContract, 'initialize', [])
|
|
69
|
+
* .call(anotherContract, 'setOwner', [newOwner])
|
|
70
|
+
*
|
|
71
|
+
* const results = await batch.execute()
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
batch() {
|
|
75
|
+
return this.factory.batch();
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Estimate gas for a contract call
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```typescript
|
|
82
|
+
* const gasEstimate = await demos.contracts.estimateGas(
|
|
83
|
+
* 'contract_address',
|
|
84
|
+
* 'transfer',
|
|
85
|
+
* ['recipient', 100]
|
|
86
|
+
* )
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
89
|
+
async estimateGas(contractAddress, method, args = []) {
|
|
90
|
+
return await this.factory.estimateGas(contractAddress, method, args);
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Deploy a contract from a template
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* ```typescript
|
|
97
|
+
* const token = await demos.contracts.deployTemplate('ERC20', {
|
|
98
|
+
* name: 'MyToken',
|
|
99
|
+
* symbol: 'MTK',
|
|
100
|
+
* totalSupply: 1000000
|
|
101
|
+
* })
|
|
102
|
+
* ```
|
|
103
|
+
*/
|
|
104
|
+
async deployTemplate(templateName, params = {}) {
|
|
105
|
+
// This will be implemented with standard contract templates
|
|
106
|
+
const templates = {
|
|
107
|
+
'Storage': `
|
|
108
|
+
class Storage extends DemosContract {
|
|
109
|
+
constructor() {
|
|
110
|
+
super()
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
store(key: string, value: any) {
|
|
114
|
+
this.state.set(key, value)
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
retrieve(key: string) {
|
|
118
|
+
return this.state.get(key)
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
`,
|
|
122
|
+
'Token': `
|
|
123
|
+
class Token extends DemosContract {
|
|
124
|
+
constructor(totalSupply: number) {
|
|
125
|
+
super()
|
|
126
|
+
this.state.set('totalSupply', totalSupply)
|
|
127
|
+
this.state.set('balances', {})
|
|
128
|
+
const creator = this.sender
|
|
129
|
+
const balances = this.state.get('balances')
|
|
130
|
+
balances[creator] = totalSupply
|
|
131
|
+
this.state.set('balances', balances)
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
transfer(to: string, amount: number) {
|
|
135
|
+
const from = this.sender
|
|
136
|
+
const balances = this.state.get('balances')
|
|
137
|
+
|
|
138
|
+
if (!balances[from] || balances[from] < amount) {
|
|
139
|
+
this.revert('Insufficient balance')
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
balances[from] -= amount
|
|
143
|
+
balances[to] = (balances[to] || 0) + amount
|
|
144
|
+
this.state.set('balances', balances)
|
|
145
|
+
|
|
146
|
+
this.emit('Transfer', { from, to, amount })
|
|
147
|
+
return true
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
balanceOf(address: string) {
|
|
151
|
+
const balances = this.state.get('balances')
|
|
152
|
+
return balances[address] || 0
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
`
|
|
156
|
+
};
|
|
157
|
+
const template = templates[templateName];
|
|
158
|
+
if (!template) {
|
|
159
|
+
throw new Error(`Unknown template: ${templateName}`);
|
|
160
|
+
}
|
|
161
|
+
// Replace template parameters
|
|
162
|
+
let source = template;
|
|
163
|
+
for (const [key, value] of Object.entries(params)) {
|
|
164
|
+
source = source.replace(new RegExp(`{{${key}}}`, 'g'), value);
|
|
165
|
+
}
|
|
166
|
+
return await this.deploy(source, [params.totalSupply || 1000000]);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
exports.DemosContracts = DemosContracts;
|
|
170
|
+
//# sourceMappingURL=DemosContracts.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DemosContracts.js","sourceRoot":"","sources":["../../../src/websdk/DemosContracts.ts"],"names":[],"mappings":";AAAA;;GAEG;;;AAGH,kEAA8D;AAS9D,MA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