diff --git a/client/src/blockchain.js b/client/src/blockchain.js index b250377..53cc6e1 100644 --- a/client/src/blockchain.js +++ b/client/src/blockchain.js @@ -1,5 +1,6 @@ import {ethers} from 'ethers'; import {waitAsync} from './utils'; +import {buildTypeAbi} from './dtype_utils'; const defaults = { '[]': [], @@ -37,17 +38,49 @@ export const getContract = async function(address, abi, wallet) { return contract; }; +export const getDataItem = async function(contract, hash, index) { + let typeData = await contract.getByHash(hash); + typeData.index = index; + typeData.typeHash = hash; + return typeData; +}; + +export const getDataItems = async function(contract, callback) { + let hash; + let typeData; + const count = await contract.count(); + + for (let i = 0; i < count; i++) { + hash = await contract.typeIndex(i); + typeData = await getDataItem(contract, hash, i); + callback(typeData); + } +}; + +export const getDataItemsByTypeHash = async function(dtypeContract, wallet, typeStruct, callback) { + const dtypeAbi = await buildStructAbi(dtypeContract, typeStruct.typeHash); + const abi = buildTypeAbi(dtypeAbi); + const typeContract = await getContract( + typeStruct.contractAddress, + abi, + wallet, + ); + + getDataItems(typeContract, callback); +}; + export const buildStructAbi = async function(dtypeContract, typeHash, parentLabel) { const dtype = await dtypeContract.getByHash(typeHash); const {length} = dtype.data.name.length; let abi = {}; + abi.name = parentLabel; + if (dtype.data.types.length === 0) { - abi.name = parentLabel; abi.type = dtype.data.name; return abi; } - abi.name = dtype.data.name; + abi.type = dtype.data.name.substring(length - 2) === '[]' ? 'tuple[]' : 'tuple'; abi.components = []; for (let i = 0; i < dtype.data.types.length; i++) { diff --git a/client/src/components/FunctionRun.vue b/client/src/components/FunctionRun.vue index 4f6401b..477a0ca 100644 --- a/client/src/components/FunctionRun.vue +++ b/client/src/components/FunctionRun.vue @@ -1,13 +1,31 @@ @@ -44,17 +48,20 @@ diff --git a/client/src/views/dTypeLandingPage.vue b/client/src/views/dTypeLandingPage.vue index 648ed53..0c2b724 100644 --- a/client/src/views/dTypeLandingPage.vue +++ b/client/src/views/dTypeLandingPage.vue @@ -3,7 +3,6 @@ { + this.items.push(dataItem); + }); this.watchAll(); } } @@ -115,7 +127,7 @@ export default { const typeIndex = this.items.findIndex(dtype => dtype.typeHash === typeHash); if (typeIndex !== -1) return; - let typeData = await this.setDataItem(typeHash, index); + let typeData = await getDataItem(this.typeContract, typeHash, index); this.items.push(typeData); }); this.typeContract.on('LogUpdate', async (typeHash, index) => { @@ -123,7 +135,7 @@ export default { const typeIndex = this.items.findIndex(dtype => dtype.typeHash === typeHash); if (typeIndex === -1) return; - let typeData = await this.setDataItem(typeHash, index); + let typeData = await getDataItem(this.typeContract, typeHash, index); if (typeData && this.items[index]) { Object.assign(this.items[index], typeData); } @@ -187,22 +199,6 @@ export default { }; }); }, - async setDataItem(hash, i) { - let typeData = await this.typeContract.getByHash(hash); - typeData.index = i; - typeData.typeHash = hash; - return typeData; - }, - async setDataItems() { - let hash, typeData; - let count = await this.typeContract.count(); - - for (let i = 0; i < count; i++) { - hash = await this.typeContract.typeIndex(i); - let typeData = await this.setDataItem(hash, i); - this.items.push(typeData); - } - } }, } diff --git a/client/src/vuex.js b/client/src/vuex.js index ca1cb41..6c7fab1 100644 --- a/client/src/vuex.js +++ b/client/src/vuex.js @@ -62,12 +62,17 @@ const dTypeStore = new Vuex.Store({ commit('setContract', contract); }); }, + async getTypeStructByName({dispatch, state}, {lang, name}) { + const hash = await state.contract.getTypeHash(lang, name); + return dispatch('getTypeStruct', hash); + }, async getTypeStruct({state}, hash) { let struct = await state.contract.getByHash(hash); - struct.types = await state.contract.getTypes(hash); + // struct.data.types = await state.contract.getTypes(hash); + struct.data.outputs = await state.contract.getOutputs(hash); struct.data.typeHash = hash; struct.data.index = struct.index.toNumber(); - // console.log('getTypeStruct', struct); + // console.log('getTypeStruct', struct.data.name, struct.data); return struct.data; }, async setTypes({dispatch, commit, state}) { diff --git a/contracts/test/dtype_AB.js b/contracts/test/dtype_AB.js index c9a360f..b49a768 100644 --- a/contracts/test/dtype_AB.js +++ b/contracts/test/dtype_AB.js @@ -95,6 +95,22 @@ contract('dTypeAB', async (accounts) => { await dtype.setOutputs(hash, stakedFunction.outputs); }); + it('insert data', async() => { + let logs, hash, index, typeBStruct; + let tokenAddress = '0xEAd8C52989b587B0c6a8478f8B6dd447E2fc8B1f'; + + ({logs} = await typeB.insert({ + staked: 2, + typeA: {balance: 10, token: tokenAddress}, + })); + ({hash, index} = logs[0].args); + typeBStruct = await typeB.getByHash(hash); + + assert.equal(typeBStruct.staked, 2, 'run function incorrect output - staked'); + assert.equal(typeBStruct.typeA.balance, 10, 'run function incorrect output - typeA.balance'); + assert.equal(typeBStruct.typeA.token, tokenAddress, 'run function incorrect output - typeA.token'); + }); + it('run AB function', async () => { let logs, hash, index; let tokenAddress = '0xEAd8C52989b587B0c6a8478f8B6dd447E2fc8B1f';