Skip to content

Commit

Permalink
L3-195: replaces duple declaration.{
Browse files Browse the repository at this point in the history
  • Loading branch information
n3op2 committed Oct 10, 2023
1 parent 331a7e9 commit debfe4e
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 21 deletions.
22 changes: 9 additions & 13 deletions src/controllers/v1/match2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,10 @@ export class Match2Controller extends Controller {
if (!match2.replacesId && state !== 'proposed' && state !== 'acceptedA' && state !== 'acceptedB')
throw new BadRequest(`state should not be ${state}`)


Check failure on line 246 in src/controllers/v1/match2/index.ts

View workflow job for this annotation

GitHub Actions / Run lint

Delete `⏎`
const [demandA]: DemandRow[] = await this.db.getDemand(match2.demandA)
validatePreOnChain(demandA, 'DemandA', {
subtype: 'demand_a',
state: match2.replacesId ? 'allocated' : 'created',
})

const [demandB]: DemandRow[] = await this.db.getDemand(match2.demandB)
Expand All @@ -256,12 +256,10 @@ export class Match2Controller extends Controller {
const ownsDemandA = demandA.owner === selfAddress
const ownsDemandB = demandB.owner === selfAddress

// helper
const acceptAB = async () => {
const newState = ownsDemandA ? 'acceptedA' : 'acceptedB'

const extrinsic = await this.node.prepareRunProcess(match2AcceptFirst(match2, newState, demandA, demandB))

console.log({ oldMatch2 })

Check failure on line 261 in src/controllers/v1/match2/index.ts

View workflow job for this annotation

GitHub Actions / Run lint

Unexpected console statement
const extrinsic = await this.node.prepareRunProcess(match2AcceptFirst(match2, newState, demandA, demandB, match2.replacesId ? oldMatch2?.originalTokenId : null ))

Check failure on line 262 in src/controllers/v1/match2/index.ts

View workflow job for this annotation

GitHub Actions / Run lint

Replace `match2AcceptFirst(match2,·newState,·demandA,·demandB,·match2.replacesId·?·oldMatch2?.originalTokenId·:·null·)` with `⏎········match2AcceptFirst(match2,·newState,·demandA,·demandB,·match2.replacesId·?·oldMatch2?.originalTokenId·:·null)⏎······`
const [transaction] = await this.db.insertTransaction({
transaction_type: 'accept',
api_type: 'match2',
Expand All @@ -274,7 +272,6 @@ export class Match2Controller extends Controller {
return transaction
}

// helper
const acceptFinal = async () => {
const extrinsic = await this.node.prepareRunProcess(match2AcceptFinal(match2, demandA, demandB))

Expand All @@ -291,19 +288,17 @@ export class Match2Controller extends Controller {
}

const acceptRematch = async () => {
if (!ownsDemandA && !ownsDemandB) throw new BadRequest(`You do not own an acceptable demand`)
const [oldMatch2]: Match2Row[] = await this.db.getMatch2(match2.replacesId || '')
const ownsNewDemandB = demandB.owner === selfAddress
if (!ownsDemandA && !ownsNewDemandB) throw new BadRequest(`You do not own an acceptable demand`)
if (match2.state === 'proposed') return acceptAB()

const [newDemandB]: DemandRow[] = await this.db.getDemand(match2.demandB)
validatePreOnChain(demandB, 'DemandB', { subtype: 'demand_b', state: 'created' })
const [oldDemandB]: DemandRow[] = await this.db.getDemand(oldMatch2.demandB)

const extrinsic = await this.node.prepareRunProcess(
rematch2AcceptFinal({
match2: oldMatch2,
demandA,
demandB,
newDemandB,
demandB: oldDemandB,
newDemandB: demandB,
newMatch2: match2,
})
)
Expand All @@ -320,6 +315,7 @@ export class Match2Controller extends Controller {
return transaction
}

const [oldMatch2]: Match2Row[] = match2.replacesId ? await this.db.getMatch2(match2.replacesId) : []
if (match2.replacesId) return acceptRematch()
if (state !== 'proposed' && state !== 'acceptedA' && state !== 'acceptedB')
throw new BadRequest(`state should not be ${state}`)
Expand Down
2 changes: 2 additions & 0 deletions src/lib/chainNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,13 @@ export default class ChainNode {
const { dispatchError, status } = result

if (dispatchError) {
this.logger.error(JSON.stringify(dispatchError), { result })
this.logger.warn('dispatch error %s', dispatchError)
transactionDbUpdate('failed')
unsub()
if (dispatchError.isModule) {
const decoded = this.api.registry.findMetaError(dispatchError.asModule)
this.logger.error(decoded)
throw new Error(`Node dispatch error: ${decoded.name}`)
}

Expand Down
1 change: 0 additions & 1 deletion src/lib/indexer/__tests__/fixtures/eventProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@ export const withMockEventProcessors: (result?: ChangeSet) => EventProcessors =
'match2-reject': sinon.stub().returns(result),
'rematch2-propose': sinon.stub().returns(result),
'match2-cancel': sinon.stub().returns(result),
'rematch2-acceptFinal': sinon.stub().returns(result),
})
4 changes: 3 additions & 1 deletion src/lib/payload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ export const match2AcceptFirst = (
match2: Match2Row,
newState: 'acceptedA' | 'acceptedB',
demandA: DemandRow,
demandB: DemandRow
demandB: DemandRow,
replaces?: number | null,

Check failure on line 162 in src/lib/payload.ts

View workflow job for this annotation

GitHub Actions / Run lint

Delete `,`
): Payload => ({
process: { id: 'match2-accept', version: 1 },
inputs: [match2.latestTokenId as number],
Expand All @@ -172,6 +173,7 @@ export const match2AcceptFirst = (
demandA: { type: 'TOKEN_ID', value: demandA.originalTokenId as number },
demandB: { type: 'TOKEN_ID', value: demandB.originalTokenId as number },
originalId: { type: 'TOKEN_ID', value: match2.originalTokenId as number },
...replaces ? { replaces: { type: 'TOKEN_ID', value: replaces }} : {},

Check failure on line 176 in src/lib/payload.ts

View workflow job for this annotation

GitHub Actions / Run lint

Replace `replaces·?·{·replaces:·{·type:·'TOKEN_ID',·value:·replaces·}}·:·{}` with `(replaces·?·{·replaces:·{·type:·'TOKEN_ID',·value:·replaces·}·}·:·{})`
},
},
],
Expand Down
13 changes: 8 additions & 5 deletions test/integration/onchain/match2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,25 +197,28 @@ describe('on-chain', function () {
expect(rematch2.latestTokenId).to.equal(lastTokenId + 4)
})

it.only('accepts a rematch2 proposal', async () => {
it('accepts a rematch2 proposal', async () => {
const proposal = await post(context.app, `/v1/match2/${ids.match2}/proposal`, {})
await pollTransactionState(db, proposal.body.id, 'finalised')
const resAcceptA = await post(context.app, `/v1/match2/${ids.match2}/accept`, {})
await pollTransactionState(db, resAcceptA.body.id, 'finalised')
const resAcceptFinal = await post(context.app, `/v1/match2/${ids.match2}/accept`, {})
await pollTransactionState(db, resAcceptFinal.body.id, 'finalised')

const lastTokenId = await node.getLastTokenId()
const reMatch = await post(context.app, '/v1/match2', {
demandA: ids.demandA,
demandB: ids.newDemandB,
replaces: ids.match2,
})
ids.rematch2 = reMatch.body['id'] as string
ids.rematch2 = reMatch.body['id'] as UUID

Check failure on line 213 in test/integration/onchain/match2.test.ts

View workflow job for this annotation

GitHub Actions / Run lint

Delete `·`

const resProposal = await post(context.app, `/v1/match2/${ids.rematch2}/proposal`, {})
console.log({ resProposal, reMatch })
await pollTransactionState(db, resProposal.body.id, 'finalised')
const lastTokenId = await node.getLastTokenId()
const resRematchAccept = await post(context.app, `/v1/match2/${ids.rematch2}/accept`, {})
await pollTransactionState(db, resRematchAccept.body.id, 'finalised')
const resFinal = await post(context.app, `/v1/match2/${ids.rematch2}/accept`, {})
await pollTransactionState(db, resFinal.body.id, 'finalised')

// output
const [match2]: Match2Row[] = await db.getMatch2(ids.match2)
Expand All @@ -228,7 +231,7 @@ describe('on-chain', function () {
expect(demandA.latestTokenId).to.equal(lastTokenId + 1)
expect(demandA.originalTokenId).to.equal(ids.originalDemandA)

expect(demandB.state).to.equal('allocated')
expect(demandB.state).to.equal('cancelled')
expect(demandB.latestTokenId).to.equal(lastTokenId + 2)

expect(match2.state).to.equal('cancelled')
Expand Down
2 changes: 1 addition & 1 deletion test/test.env
Original file line number Diff line number Diff line change
@@ -1 +1 @@
LOG_LEVEL=error
LOG_LEVEL=debug

0 comments on commit debfe4e

Please sign in to comment.