UNPKG

godprotocol

Version:
90 lines (76 loc) 2.3 kB
import Storage from "./functions/storage"; class Twa extends Storage { constructor(config, manager) { super(manager); this.config = config this.type = 'twain' } call = async(name, args, options)=>{ let {config, chain} = options let me = await this.statics_() let oracle = this.account.manager.oracle; let spli = this.value.value.split('/'); let config_hash = spli.slice(-1)[0], store; let other = args[0], result; switch(name){ case 'set': me[await oracle.hash(await other.literal(), 'sha1')] = [await this.account.vm.instance_address((await other.statics_())), await this.account.vm.instance_address((await args[1].statics_()))] store = true; break; case 'get': other = other && await other.literal(); result = me[await oracle.hash(other, 'sha1')] result = result? {type:'address', value: result[1]}:null break; case 'entries': result = new Array() for (let prop in me){ let pair = me[prop] let res = await this.account.vm.execute({ type:'call', identifier: 'array', arguments: [pair], location: `${config.location}/${prop}` }, {chain}) result.push(res.value); } break; case "keys": result = new Array() for (let prop in me){ let pair = me[prop] result.push(pair[0]) } break; case "values": result = new Array() for (let prop in me){ let pair = me[prop] result.push(pair[1]) } break; case 'clear': me = {} store = true break; case 'remove': other = other && await other.literal() other = other && await oracle.hash(other, 'sha1') result = Object(me).hasOwnProperty(other) delete me[other] store = true break; case 'copy': result = me; break; case 'is_empty': result = Array.from(Object.keys(me)).length === 0 break; } if (store){ await oracle.write(spli.slice(0,-1).join('/'), {type: this.type, values: me}, {config_hash, config: true}) } return result; } } export default Twa;