mirror of
https://github.com/oceanprotocol/ocean.js.git
synced 2024-11-26 20:39:05 +01:00
fix datatoken unit testing
This commit is contained in:
parent
3307f7a177
commit
52260cdf7f
3870
package-lock.json
generated
3870
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
11
package.json
11
package.json
@ -16,8 +16,8 @@
|
|||||||
"release": "release-it --non-interactive",
|
"release": "release-it --non-interactive",
|
||||||
"changelog": "auto-changelog -p",
|
"changelog": "auto-changelog -p",
|
||||||
"prepublishOnly": "npm run build",
|
"prepublishOnly": "npm run build",
|
||||||
"test": "mocha --opts test/unit/mocha.opts",
|
"test": "mocha --config=test/unit/.mocharc.json --node-env=test --exit",
|
||||||
"test:integration": "mocha --opts test/integration/mocha.opts"
|
"test:integration": "mocha --opts test/integration/mocha.opts"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@ -46,8 +46,8 @@
|
|||||||
"whatwg-url": "^8.0.0"
|
"whatwg-url": "^8.0.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@oceanprotocol/contracts": "^0.2.2",
|
||||||
"@release-it/bumper": "^1.4.0",
|
"@release-it/bumper": "^1.4.0",
|
||||||
"@oceanprotocol/contracts": "^0.2.0",
|
|
||||||
"@truffle/hdwallet-provider": "^1.0.33",
|
"@truffle/hdwallet-provider": "^1.0.33",
|
||||||
"@types/chai": "^4.2.11",
|
"@types/chai": "^4.2.11",
|
||||||
"@types/chai-spies": "^1.0.1",
|
"@types/chai-spies": "^1.0.1",
|
||||||
@ -76,10 +76,7 @@
|
|||||||
"ts-node": "^8.10.2",
|
"ts-node": "^8.10.2",
|
||||||
"typedoc": "^0.17.1",
|
"typedoc": "^0.17.1",
|
||||||
"typescript": "^3.9.5",
|
"typescript": "^3.9.5",
|
||||||
"uglifyjs-webpack-plugin": "^2.2.0",
|
"yargs": "^15.3.1"
|
||||||
"webpack": "^4.42.0",
|
|
||||||
"webpack-cli": "^3.3.11",
|
|
||||||
"webpack-merge": "^4.2.2"
|
|
||||||
},
|
},
|
||||||
"nyc": {
|
"nyc": {
|
||||||
"include": [
|
"include": [
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import Account from '../ocean/Account'
|
import Account from '../ocean/Account'
|
||||||
|
|
||||||
import * as defaultFactoryABI from '@oceanprotocol/contracts/artifacts/development/Factory.json'
|
const defaultFactoryABI = require('@oceanprotocol/contracts/artifacts/development/Factory.json')
|
||||||
import * as defaultDatatokensABI from '@oceanprotocol/contracts/artifacts/development/DataTokenTemplate.json'
|
const defaultDatatokensABI = require('@oceanprotocol/contracts/artifacts/development/DataTokenTemplate.json')
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides a interface to DataTokens
|
* Provides a interface to DataTokens
|
||||||
@ -27,7 +27,6 @@ export class DataTokens {
|
|||||||
web3: any
|
web3: any
|
||||||
) {
|
) {
|
||||||
this.factoryAddress = factoryAddress
|
this.factoryAddress = factoryAddress
|
||||||
|
|
||||||
this.factoryABI = factoryABI || defaultFactoryABI
|
this.factoryABI = factoryABI || defaultFactoryABI
|
||||||
this.datatokensABI = datatokensABI || defaultDatatokensABI
|
this.datatokensABI = datatokensABI || defaultDatatokensABI
|
||||||
this.web3 = web3
|
this.web3 = web3
|
||||||
@ -41,11 +40,33 @@ export class DataTokens {
|
|||||||
*/
|
*/
|
||||||
public async create(metaDataStoreURI: string, account: Account): Promise<string> {
|
public async create(metaDataStoreURI: string, account: Account): Promise<string> {
|
||||||
// Create factory contract object
|
// Create factory contract object
|
||||||
const tokenAddress = null
|
const factory = new this.web3.eth.Contract(
|
||||||
const factory = new this.web3.eth.Contract(this.factoryABI, this.factoryAddress, {
|
this.factoryABI,
|
||||||
from: account
|
this.factoryAddress,
|
||||||
})
|
{
|
||||||
// TODO:
|
from: account
|
||||||
|
}
|
||||||
|
)
|
||||||
|
const estGas = await factory.methods
|
||||||
|
.createToken(metaDataStoreURI)
|
||||||
|
.estimateGas(function(err, estGas){
|
||||||
|
return estGas
|
||||||
|
})
|
||||||
|
// Invoke createToken function of the contract
|
||||||
|
const trxReceipt = await factory.methods
|
||||||
|
.createToken(metaDataStoreURI)
|
||||||
|
.send({
|
||||||
|
from: account,
|
||||||
|
gas: estGas+1,
|
||||||
|
gasPrice: '3000000000'
|
||||||
|
})
|
||||||
|
|
||||||
|
let tokenAddress = null
|
||||||
|
try {
|
||||||
|
tokenAddress = trxReceipt.events.TokenCreated.returnValues[0]
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e)
|
||||||
|
}
|
||||||
return tokenAddress
|
return tokenAddress
|
||||||
}
|
}
|
||||||
|
|
||||||
|
File diff suppressed because one or more lines are too long
12
test/unit/.mocharc.json
Normal file
12
test/unit/.mocharc.json
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
{
|
||||||
|
"require": [
|
||||||
|
"ts-node/register",
|
||||||
|
"source-map-support/register",
|
||||||
|
"mock-local-storage"
|
||||||
|
],
|
||||||
|
"full-trace": true,
|
||||||
|
"bail": true,
|
||||||
|
"exit": true,
|
||||||
|
"timeout": 20000,
|
||||||
|
"file": ["test/unit/tsconfig.json", "test/unit/DataTokens.test.ts"]
|
||||||
|
}
|
@ -1,12 +1,14 @@
|
|||||||
import { assert } from 'chai'
|
import { assert } from 'chai'
|
||||||
import { TestContractHandler } from '../TestContractHandler'
|
import { TestContractHandler } from '../TestContractHandler'
|
||||||
import { DataTokens } from '../../src/datatokens/Datatokens'
|
import { DataTokens } from '../../src/datatokens/Datatokens'
|
||||||
|
import Web3 from 'web3'
|
||||||
|
|
||||||
|
const factory = require('@oceanprotocol/contracts/artifacts/development/Factory.json')
|
||||||
|
const datatokensTemplate = require('@oceanprotocol/contracts/artifacts/development/DataTokenTemplate.json')
|
||||||
|
|
||||||
|
|
||||||
const Web3 = require('web3')
|
|
||||||
const web3 = new Web3("http://127.0.0.1:8545")
|
const web3 = new Web3("http://127.0.0.1:8545")
|
||||||
|
|
||||||
const factoryABI = require('../../src/datatokens/FactoryABI.json')
|
|
||||||
const datatokensABI = require('../../src/datatokens/DatatokensABI.json')
|
|
||||||
|
|
||||||
describe('DataTokens', () => {
|
describe('DataTokens', () => {
|
||||||
|
|
||||||
@ -16,46 +18,55 @@ describe('DataTokens', () => {
|
|||||||
let contracts
|
let contracts
|
||||||
let datatoken
|
let datatoken
|
||||||
let tokenAddress
|
let tokenAddress
|
||||||
|
|
||||||
let tokenAmount = 100
|
let tokenAmount = 100
|
||||||
let blob = 'https://example.com/dataset-1'
|
let blob = 'https://example.com/dataset-1'
|
||||||
|
|
||||||
describe('#test', () => {
|
describe('#test', () => {
|
||||||
it('should deploy contracts', async () => {
|
it('#deploy', async () => {
|
||||||
contracts = new TestContractHandler(factoryABI,datatokensABI)
|
contracts = new TestContractHandler(
|
||||||
|
factory.abi,
|
||||||
|
datatokensTemplate.abi,
|
||||||
|
datatokensTemplate.bytecode,
|
||||||
|
factory.bytecode
|
||||||
|
)
|
||||||
await contracts.getAccounts()
|
await contracts.getAccounts()
|
||||||
minter = contracts.accounts[0]
|
minter = contracts.accounts[0]
|
||||||
spender = contracts.accounts[1]
|
spender = contracts.accounts[1]
|
||||||
await contracts.deployContracts(minter)
|
await contracts.deployContracts(minter)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should create Datatoken object', async () => {
|
it('#init', async () => {
|
||||||
datatoken = new DataTokens(contracts.factoryAddress, factoryABI, datatokensABI, web3)
|
datatoken = new DataTokens(
|
||||||
|
contracts.factoryAddress,
|
||||||
|
factory.abi,
|
||||||
|
datatokensTemplate.abi,
|
||||||
|
web3
|
||||||
|
)
|
||||||
assert(datatoken !== null)
|
assert(datatoken !== null)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should create Datatoken contract', async () => {
|
it('#create', async () => {
|
||||||
tokenAddress = await datatoken.create(blob, minter)
|
tokenAddress = await datatoken.create(blob, minter)
|
||||||
assert(tokenAddress !== null)
|
assert(tokenAddress !== null)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should mint Datatokens', async () => {
|
it('#mint', async () => {
|
||||||
await datatoken.mint(tokenAddress, minter, tokenAmount)
|
await datatoken.mint(tokenAddress, minter, tokenAmount)
|
||||||
balance = await datatoken.balance(tokenAddress, minter)
|
balance = await datatoken.balance(tokenAddress, minter)
|
||||||
assert(balance.toString() === tokenAmount.toString())
|
assert(balance.toString() === tokenAmount.toString())
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should transfer Datatokens to spender', async () => {
|
it('#transfer', async () => {
|
||||||
await datatoken.transfer(tokenAddress, spender, tokenAmount, minter)
|
await datatoken.transfer(tokenAddress, spender, tokenAmount, minter)
|
||||||
balance = await datatoken.balance(tokenAddress, spender)
|
balance = await datatoken.balance(tokenAddress, spender)
|
||||||
assert(balance.toString() === tokenAmount.toString())
|
assert(balance.toString() === tokenAmount.toString())
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should approve Datatokens to spend', async () => {
|
it('#approve', async () => {
|
||||||
await datatoken.approve(tokenAddress, minter, tokenAmount, spender)
|
await datatoken.approve(tokenAddress, minter, tokenAmount, spender)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should transferFrom Datatokens back to the minter', async () => {
|
it('#transferFrom', async () => {
|
||||||
await datatoken.transferFrom(tokenAddress, spender, tokenAmount, minter)
|
await datatoken.transferFrom(tokenAddress, spender, tokenAmount, minter)
|
||||||
minter = await datatoken.balance(tokenAddress, spender)
|
minter = await datatoken.balance(tokenAddress, spender)
|
||||||
assert(balance.toString() === tokenAmount.toString())
|
assert(balance.toString() === tokenAmount.toString())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user