-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvm.js
69 lines (39 loc) · 3.01 KB
/
vm.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
██╗ ██╗██╗ ██╗ ██╗███╗ ██╗████████╗ █████╗ ██████╗ ██╗ ██╗██╗ ██╗███╗ ███╗
██║ ██╔╝██║ ╚██╗ ██╔╝████╗ ██║╚══██╔══╝██╔══██╗██╔══██╗ ██║ ██║██║ ██║████╗ ████║
█████╔╝ ██║ ╚████╔╝ ██╔██╗ ██║ ██║ ███████║██████╔╝ ██║ █╗ ██║██║ ██║██╔████╔██║
██╔═██╗ ██║ ╚██╔╝ ██║╚██╗██║ ██║ ██╔══██║██╔══██╗ ██║███╗██║╚██╗ ██╔╝██║╚██╔╝██║
██║ ██╗███████╗██║ ██║ ╚████║ ██║ ██║ ██║██║ ██║ ╚███╔███╔╝ ╚████╔╝ ██║ ╚═╝ ██║
╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝ ╚═══╝ ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═╝ ╚══╝╚══╝ ╚═══╝ ╚═╝ ╚═╝
Here will be the implementation of VM for KLYNTAR to allow workflows to import it and use in code
*/
import ContractInstance from './rustBase.js'
export let VM = {
//Function to create a contract instance from WASM bytecode with injected metering function
bytesToMeteredContract:async(contractBytecodeAsBuffer,energyLimit,extraModules)=>{
let contract = new ContractInstance(extraModules,contractBytecodeAsBuffer)
let contractHandler = await contract.setUpContract(energyLimit) //return instance and pointer to metadata to track energy changes => {contractInstance,contractMetadata}
return contractHandler
},
/**
*
*
* @param {*} contractInstance - WASM contract instance with injected modules e.g. "metering" and another extra functionality
* @param {*} contractMetadata - handler for energy used metering
* @param {Object} params - object that we should pass to contract
* @param {*} functionName - function name of contract that we should call
* @param {'RUST'|'ASC'} type
* @returns
*/
callContract:(contractInstance,contractMetadata,params,functionName,type)=>{
contractMetadata.energyUsed=0 //make null before call contract
let result
if(type==='RUST'){
result = contractInstance[functionName](params)
}else if(type==='ASC'){
let pointerToChunk = contractInstance.__newString(params);
result = contractInstance.__getString(contractInstance[functionName](pointerToChunk))
}
return {result,contractMetadata}
},
}