Skip to content
This repository has been archived by the owner on Apr 25, 2023. It is now read-only.

Commit

Permalink
new API interface
Browse files Browse the repository at this point in the history
  • Loading branch information
vdg committed Aug 6, 2019
1 parent b0eec3f commit bd375ee
Show file tree
Hide file tree
Showing 11 changed files with 2,209 additions and 852 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
dist
coverage
src/cli.js
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ module.exports = {
"no-warning-comments": [0, { "location": "anywhere" }],
'arrow-parens': ["error", "as-needed"],
'no-empty': ["error", { "allowEmptyCatch": true }],
'no-async-promise-executor' : 'off'
}
}
92 changes: 92 additions & 0 deletions __tests__/campaign.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@

/* global jest:true describe:true test:true expect:true */

import Web3 from 'web3'

import { RougeProtocol } from '../src/index'

import * as protocolSolidity from '../node_modules/rouge-protocol-solidity/package.json'

const web3 = new Web3('https://sokol.poa.network')

const rouge = RougeProtocol(web3)

const issuerPkey = '0x1111111111111111111111111111111111111111111111111111111111111111'
const issuerAccount = web3.eth.accounts.privateKeyToAccount(issuerPkey)

const campaignAddress = '0x79fe544d081210b15bcfbb0cbd9ca12c5c11226c'
const campaign = rouge.as(issuerAccount).campaign$(campaignAddress)

jest.setTimeout(50000)

describe('campaign.version()', () => {
test(
`return ${protocolSolidity.version}`,
async () => expect(await campaign.version).toEqual(protocolSolidity.version)
)
})

describe('campaign.name()', () => {
test(
`return 'JEST TEST campaign'`,
async () => expect(await campaign.name).toEqual('JEST TEST campaign')
)
})

describe('campaign.info()', () => {
const expected = '0x19e7e376e7c213b7e7e7e46cc70a5dd086daff2a0001ffff000000000000000000000000000000000000000000000000000000005d2ec3854a45535420544553542063616d706169676e' // eslint-disable-line max-len
test(
`return ${expected}`,
async () => expect(await campaign.info).toEqual(expected)
)
})

describe('campaign.expiration()', () => {
const expected = '1563345797'
test(
`return ${JSON.stringify(expected)}`,
async () => expect(await campaign.expiration).toEqual(expected)
)
})

describe('campaign.issuance()', () => {
const expected = '1'
test(
`return ${expected}`,
async () => expect(await campaign.issuance).toEqual(expected)
)
})

describe('campaign.state()', () => {
const expected = '0x0000000101000000010000000000000000'
test(
`return ${expected}`,
async () => expect(await campaign.state).toEqual(expected)
)
})

// expected to build from state

describe('campaign.available()', () => {
const expected = '1'
test(
`return ${expected}`,
async () => expect(await campaign.available).toEqual(expected)
)
})

describe('campaign.acquired()', () => {
const expected = '0'
test(
`return ${expected}`,
async () => expect(await campaign.acquired).toEqual(expected)
)
})

describe('campaign.redeemed()', () => {
const expected = '0'
test(
`return ${expected}`,
async () => expect(await campaign.redeemed).toEqual(expected)
)
})
148 changes: 134 additions & 14 deletions __tests__/rouge.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,153 @@

/* global describe:true it:true expect: true */
/* global jest:true describe:true test:true expect:true */

import Web3 from 'web3'
// import moment from 'moment'

import { RougeProtocol } from '../src/index'

// const issuerPkey = '0x1111111111111111111111111111111111111111111111111111111111111111'
import * as solidity from '../node_modules/rouge-protocol-solidity/package.json'

const rgeAddressSokol = '0x5475300766433dd082a7340fc48a445c483df68f'
const currentFactory = '0x277FB7D416B6316E17954823aa621F3E321c8a72'
const currentDefaultTare = '100000'

// const provider = new Web3.providers.HttpProvider('https://sokol.poa.network')

const web3 = new Web3('https://sokol.poa.network')

const rouge = RougeProtocol({ web3 })
// TODO test on ganache
// const ganache = require("ganache-core")
// const web3 = new Web3(ganache.provider())

// issuer TEST addres is 0x19E7E376E7C213B7E7e7e46cc70A5dD086DAff2A
const issuerPkey = '0x1111111111111111111111111111111111111111111111111111111111111111'
const issuerAccount = web3.eth.accounts.privateKeyToAccount(issuerPkey)
const issuerMinFuel = 100 // finney
const issuerMinRGE = 10 * 10 ** 6

// user TEST addres is 0x1563915e194D8CfBA1943570603F7606A3115508
const userPkey = '0x2222222222222222222222222222222222222222222222222222222222222222'
const userAccount = web3.eth.accounts.privateKeyToAccount(userPkey)
const userMinFuel = 10 // finney

jest.setTimeout(50000)

describe('Precondition tests', () => {

// should have at least N ETH/POA to start the test

test('enough Fuel issuer', async () => {
const finney = parseInt(web3.utils.fromWei(await web3.eth.getBalance(issuerAccount.address), 'finney'))
return expect(finney).toBeGreaterThan(issuerMinFuel)
})

test('enough Fuel user', async () => {
const finney = parseInt(web3.utils.fromWei(await web3.eth.getBalance(userAccount.address), 'finney'))
return expect(finney).toBeGreaterThan(userMinFuel)
})

// and N RGE

test('enough RGE', async () => {
const tokens = web3.utils.toBN(await rouge.RGE$.balanceOf(issuerAccount.address)).toNumber()
return expect(tokens).toBeGreaterThan(issuerMinRGE)
})

})

const rouge = RougeProtocol(web3)

describe('RougeProtocol RGE object', () => {

test('RGE$address', async () => {
expect.assertions(1)
return expect(await rouge.RGE$.address).toBe(rgeAddressSokol)
})

test('RGE$balanceOf', async () => {
expect.assertions(1)
return expect(await rouge.RGE$.balanceOf('0x0101010101010101010101010101010101010101')).toEqual('0')
})

})

describe('RougeProtocol factory object', () => {

test('factory$address', async () => {
expect.assertions(1)
return expect(await rouge.factory$.address).toBe(currentFactory)
})

test('factory$version', async () => {
expect.assertions(1)
return expect(await rouge.factory$.version).toBe(solidity.version)
})

test('factory$tare', async () => {
expect.assertions(1)
return expect((await rouge.factory$.tare).toString()).toEqual(currentDefaultTare)
})

describe('RougeProtocol', () => {
})

// it('rgeAddress', async () => {
// // const w3Account = web3.eth.accounts.privateKeyToAccount(issuerPkey)
// const issuer = (await rouge)
// // const x2 = (await rouge).as(w3Account)
// })
describe('RougeProtocol account object', () => {

it('rgeAddress', async () => {
// const issuer = (await rouge)
test('as using address', async () => {
expect.assertions(1)
return expect((await rouge).rgeAddress).toBe('0x5475300766433dd082a7340fc48a445c483df68f')
return expect(await rouge.as(issuerAccount.address).account$.address).toEqual(issuerAccount.address)
})

it('tare', async () => {
test('as using private key', async () => {
expect.assertions(1)
return expect(await (await rouge).tare).toEqual({_hex: '0x0186a0'})
return expect(await rouge.as({ pkey: issuerPkey }).account$.address).toEqual(issuerAccount.address)
})

test('as using web3 account object', async () => {
expect.assertions(1)
return expect(await rouge.as(issuerAccount).account$.address).toEqual(issuerAccount.address)
})

})

describe('rouge.createCampaign()', () => {

const campaignName = 'Jest __tests__ campaign'
const campaignPromise = rouge.as(issuerAccount).createCampaign({ name: campaignName })

test('return campaign object', async () => {
expect.assertions(3)
await expect(campaignPromise).resolves.toHaveProperty('distributeNote')
await expect(campaignPromise).resolves.toHaveProperty('acquireNote')
await expect(campaignPromise).resolves.toHaveProperty('redeemNote')
})

test('campaign.address', async () => {
const campaign = await campaignPromise
await expect(campaign.address).resolves.toMatch(/^0x[0-9a-fA-F]{40}$/)
})

test('campaign.name', async () => {
const campaign = await campaignPromise
return expect(campaign.name).resolves.toEqual(campaignName)
})

test('campaign.canDistribute', async () => {
const campaign = await campaignPromise
return expect(campaign.canDistribute).resolves.toEqual(true)
})

test('campaign.canSignRedemption', async () => {
const campaign = await campaignPromise
return expect(campaign.canSignRedemption).resolves.toEqual(true)
})

test('campaign.distributeNote()', async () => {
const campaign = await campaignPromise
expect.assertions(3)
await expect(campaign.acquired).resolves.toEqual('0')
await expect(campaign.distributeNote(userAccount.address)).resolves.toHaveProperty('blockHash')
await expect(campaign.acquired).resolves.toEqual('1')
})

})
31 changes: 31 additions & 0 deletions examples/simple.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

import Web3 from 'web3'
import moment from 'moment'

import { RougeProtocol } from '../src/index'

const web3 = new Web3('https://sokol.poa.network')

const issuer = web3.eth.accounts.privateKeyToAccount('0xaaaa'.padEnd(66,'0'))

const user1 = web3.eth.accounts.privateKeyToAccount('0x1111'.padEnd(66,'0'))
const user2 = web3.eth.accounts.privateKeyToAccount('0x2222'.padEnd(66,'0'))
const user3 = web3.eth.accounts.privateKeyToAccount('0x3333'.padEnd(66,'0'))

const rouge = RougeProtocol(web3)

const run = async () => {

const campaign = await rouge.as(issuer).createCampaign({
name: 'Simple demo Rouge campaign',
issuance: 10,
expiration: moment().add(moment.duration(2, 'days')).format('X')
// precheck: true
})

console.log(campaign)


}

run()
49 changes: 25 additions & 24 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"src"
],
"engines": {
"node": ">=8.0.0"
"node": ">=10.0.0"
},
"keywords": [
"rouge.js",
Expand All @@ -37,9 +37,10 @@
"release": "release-it",
"coveralls": "cat ./coverage/lcov.info | ./node_modules/.bin/coveralls",
"cli": "NODE_ENV=test nodemon --exec babel-node src/cli.js",
"example": "NODE_ENV=test nodemon --exec babel-node examples/simple.js",
"lint": "yarn run lint:eslint",
"lint:eslint": "eslint src/*.js __tests__/*.js",
"test": "jest",
"test": "jest --verbose",
"test:coverage": "jest --coverage ",
"test:watch": "clear && jest --watch",
"lint:test": "yarn run lint && yarn run test:coverage",
Expand All @@ -63,44 +64,44 @@
}
},
"devDependencies": {
"@babel/cli": "^7.2.3",
"@babel/core": "^7.3.4",
"@babel/node": "^7.2.2",
"@babel/preset-env": "^7.3.1",
"@babel/cli": "^7.5.5",
"@babel/core": "^7.5.5",
"@babel/node": "^7.5.5",
"@babel/preset-env": "^7.5.5",
"babel-eslint": "^10.0.2",
"babel-7-jest": "^21.3.3",
"babel-core": "7.0.0-bridge.0",
"babel-eslint": "^10.0.1",
"babel-jest": "^24.1.0",
"babel-plugin-relative-path-import": "^2.0.1",
"coveralls": "^3.0.4",
"cross-env": "^5.0.5",
"eslint": "^5.10.0",
"eslint-config-standard": "^12.0.0",
"eslint-plugin-babel": "^4.1.2",
"eslint-plugin-import": "^2.14.0",
"eslint-plugin-node": "^8.0.0",
"eslint": "^6.1.0",
"eslint-config-standard": "^13.0.1",
"eslint-plugin-babel": "^5.3.0",
"eslint-plugin-import": "^2.18.2",
"eslint-plugin-node": "^9.1.0",
"eslint-plugin-promise": "^4.0.1",
"eslint-plugin-standard": "^4.0.0",
"husky": "^0.14.3",
"jest": "^24.1.0",
"minimist": "^1.2.0",
"ganache-cli": "^6.5.1",
"husky": "^3.0.2",
"jest": "^24.7.1",
"moment": "^2.24.0",
"nodemon": "^1.18.10",
"release-it": "^12.3.2",
"release-it": "^12.3.5",
"rimraf": "^2.6.1",
"rollup": "^0.39.2",
"rollup-plugin-babel": "^3.0.2",
"rollup": "^1.19.3",
"rollup-plugin-babel": "^4.3.3",
"rollup-plugin-bundle-size": "^1.0.2",
"rollup-plugin-commonjs": "^8.2.0",
"rollup-plugin-commonjs": "^10.0.2",
"rollup-plugin-ignore": "^1.0.5",
"rollup-plugin-json": "^3.1.0",
"rollup-plugin-node-resolve": "^3.0.0",
"rollup-plugin-uglify": "^2.0.1"
"rollup-plugin-json": "^4.0.0",
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-uglify": "^6.0.2"
},
"dependencies": {
"ethereumjs-abi": "^0.6.7",
"ethereumjs-util": "^6.1.0",
"hex64": "^0.4.0",
"rouge-protocol-solidity": "0.17.0",
"web3": "1.0.0-beta.55"
"web3": "^1.2.1"
}
}
Loading

0 comments on commit bd375ee

Please sign in to comment.