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

Commit

Permalink
campaign state decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
vdg committed Feb 16, 2020
1 parent d4bbd05 commit 9c2ffa0
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 19 deletions.
18 changes: 15 additions & 3 deletions src/campaign.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,27 @@ export default function Campaign (web3, address, { context, _decodeLog }) {
}
}

const state = async () => instance.methods.getState().call()
const stateRaw = async () => instance.methods.getState().call()
const isIssued = async () => instance.methods.campaignIssued().call()
const issuance = async () => instance.methods.issuance().call()
const available = async () => instance.methods.available().call()
const acquired = async () => instance.methods.acquired().call()
const redeemed = async () => instance.methods.redeemed().call()

const state = async () => {
const data = await stateRaw()
const result = {
issued: web3.utils.hexToNumber('0x' + data.slice(10, 12)) > 0
}
if (result.issued) {
result.issuance = web3.utils.hexToNumber(data.slice(0, 10))
result.free = web3.utils.hexToNumber('0x' + data.slice(12, 20))
result.acquired = web3.utils.hexToNumber('0x' + data.slice(20, 28))
result.redeemed = web3.utils.hexToNumber('0x' + data.slice(28, 36))
}
return result
}

const _isAuthorized = async (address, auth) => (await instance.methods.isAuthorized(
address, RougeAuthorization.All
).call()) || instance.methods.isAuthorized(
Expand Down Expand Up @@ -123,8 +137,6 @@ export default function Campaign (web3, address, { context, _decodeLog }) {

const receipt = await _transact(method, address)
// const Issuance = _decodeLog('Issuance', receipt.logs[0])
console.log('receipt', receipt)

return receipt
} catch (e) {
throw new Error(`[rouge.js] issueCampaign failed: ${e}`)
Expand Down
10 changes: 4 additions & 6 deletions src/rouge.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ function RougeProtocol (web3, context = {}) {
const _decodeLog = (name, log) => ({
_event: name,
_address: log.address,
_blockNumber: log.blockNumber,
_transactionHash: log.transactionHash,
_log: log,
...web3.eth.abi.decodeLog(_AbiEvents[name].inputs, log.data, log.topics.slice(1))
})
Expand Down Expand Up @@ -139,20 +141,16 @@ function RougeProtocol (web3, context = {}) {

// const getCampaignList = async ({issuer}) => {
// // NewCampaign TODO add in protocol issuer + version protocol

// event Issuance(bytes4 indexed scheme, string name, uint campaignExpiration);
// TODO add in protocol issuer + version protocol
const getIssuedCampaignList = async ({scheme, issuer}) => {
// TODO issuer // protocol version filter
try {
const abiSignEvent = web3.eth.abi.encodeEventSignature(_AbiEvents['Issuance'])
const encodedScheme = web3.utils.padRight(scheme, 64)
console.log('xxx', abiSignEvent, encodedScheme)
const logs = await web3.eth.getPastLogs({
fromBlock: 4056827, // should be factory/version create block by default
// fromBlock: 4056827, // should be factory/version create block by default
fromBlock: 1, // 4056827, should be factory/version create block by default per network ?
topics: [abiSignEvent, encodedScheme]
})

return Promise.resolve(logs.map(log => _decodeLog('Issuance', log)))
} catch (e) {
return Promise.reject(new Error(`[rouge.js] getIssuedCampaignList failed: ${e}`))
Expand Down
28 changes: 18 additions & 10 deletions test/campaign.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ beforeAll(async () => { mockup = await initializeWeb3() })
const defaultTare = '100000'
const campaignCreationTestDefaulParams = {
name: 'Jest __tests__ campaign',
issuance: '2'
issuance: 3
}

describe('RougeProtocol(web3).createCampaign()', () => {
Expand Down Expand Up @@ -45,7 +45,7 @@ describe('RougeProtocol(web3).createCampaign()', () => {
})

describe('issuance * tare RGE tokens have been from issuer to campaign contract', () => {
const expected = parseInt(defaultTare) * parseInt(campaignCreationTestDefaulParams.issuance)
const expected = parseInt(defaultTare) * campaignCreationTestDefaulParams.issuance
test(
`rge.balanceOf(campaign) should return ${expected}`,
() => campaignPromise.then(
Expand Down Expand Up @@ -84,12 +84,16 @@ describe('RougeProtocol(web3).createCampaign()', () => {
})

describe('campaign.info()', () => {
const expected = '0001ffff00000000000000000000000000000000000000000000000000000000'
const expected = a => ({
issuer: a,
name: 'Jest __tests__ campaign',
scheme: '0x0001ffff'
})
test(
`should return a string containing issuer+${expected}`,
`should return an object containing ${JSON.stringify(expected)}`,
() => campaignPromise.then(
async campaign => expect(await campaign.info).toEqual(
expect.stringContaining(mockup.accounts[0].toLowerCase() + expected)
expect.objectContaining(expected(mockup.accounts[0]))
)
)
)
Expand Down Expand Up @@ -135,9 +139,13 @@ describe('RougeProtocol(web3).createCampaign()', () => {
})

describe('campaign.state()', () => {
const expected =
'0x0000000' + campaignCreationTestDefaulParams.issuance +
'010000000' + campaignCreationTestDefaulParams.issuance + '0000000000000000'
const expected = {
acquired: 0,
free: campaignCreationTestDefaulParams.issuance,
issuance: campaignCreationTestDefaulParams.issuance,
issued: true,
redeemed: 0
}
test(
`should return ${JSON.stringify(expected)}`,
() => campaignPromise.then(
Expand All @@ -147,7 +155,7 @@ describe('RougeProtocol(web3).createCampaign()', () => {
})

describe('campaign.issuance()', () => {
const expected = campaignCreationTestDefaulParams.issuance
const expected = campaignCreationTestDefaulParams.issuance.toString()
test(
`should return ${JSON.stringify(expected)}`,
() => campaignPromise.then(
Expand All @@ -167,7 +175,7 @@ describe('RougeProtocol(web3).createCampaign()', () => {
})

describe('campaign.available()', () => {
const expected = campaignCreationTestDefaulParams.issuance
const expected = campaignCreationTestDefaulParams.issuance.toString()
test(
`should return ${JSON.stringify(expected)}`,
() => campaignPromise.then(
Expand Down

0 comments on commit 9c2ffa0

Please sign in to comment.