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

Reorg AccountService code #1

Open
wants to merge 1 commit into
base: splitup
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 34 additions & 34 deletions src/AccountService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BigNumber } from 'bignumber.js'
import { Buffer } from 'safe-buffer'
import * as util from "ethereumjs-util"
import * as util from 'ethereumjs-util'
import { Checkpoint } from './Checkpoint'
import { DepositTransaction } from './DepositTransaction'
import * as contracts from './index'
Expand All @@ -12,21 +12,21 @@ import { ERC20Transaction } from './ERC20Transaction'
import { WithdrawalTransaction } from './WithdrawalTransaction'

export class AccountService {
accountsState: Map<string, PlasmaState>
checkpoints: Map<string, Checkpoint>
deposits: Array<DepositTransaction>
participants: Array<Participant>
txs: Array<ERC20Transaction>
txTree: MerkleTree | undefined
changes: Map<string, BigNumber>
changesTree: MerkleTree | undefined
accounts: Map<string, Buffer>
accountsTree: MerkleTree | undefined
checkpointIdNext: BigNumber
plasmoidContract: contracts.Plasmoid.Contract
participantAddress: string
slotID: BigNumber
txID: BigNumber
accountsState: Map<string, PlasmaState>
checkpoints: Map<string, Checkpoint>
deposits: Array<DepositTransaction>
participants: Array<Participant>
txs: Array<ERC20Transaction>
txTree: MerkleTree | undefined
changes: Map<string, BigNumber>
changesTree: MerkleTree | undefined
accounts: Map<string, Buffer>
accountsTree: MerkleTree | undefined
checkpointIdNext: BigNumber
plasmoidContract: contracts.Plasmoid.Contract
participantAddress: string
slotID: BigNumber
txID: BigNumber

constructor (plasmoidContract: contracts.Plasmoid.Contract, participantAddress: string) {
this.accountsState = new Map()
Expand Down Expand Up @@ -63,16 +63,6 @@ export class AccountService {
return participant
}

private async addTransaction (tx: ERC20Transaction): Promise<ERC20Transaction> {
this.txs.push(tx)
await this.addChange(this.slotID, this.txID)
await this.addAccountChange(this.slotID, tx.lock, tx.amount)
this.slotID = this.slotID.plus(1)
this.txID = this.txID.plus(1)
await this.sync()
return tx
}

async addDepositTransaction (owner: string, amount: BigNumber): Promise<DepositTransaction> {
const depositTransaction = new DepositTransaction(owner, amount)
return await this.addTransaction(depositTransaction) as DepositTransaction
Expand All @@ -83,14 +73,6 @@ export class AccountService {
return await this.addTransaction(withdrawalTransaction) as WithdrawalTransaction
}

private async addChange (slotId: BigNumber, txId: BigNumber): Promise<void> {
this.changes!.set(slotId.toString(), txId)
}

private async addAccountChange (slotId: BigNumber, account: string, amount: BigNumber): Promise<void> {
this.accounts.set(slotId.toString(), Buffer.concat([solUtils.stringToBuffer(account), solUtils.bignumberToUint256(amount)]))
}

getParticipantByAddress (address: string): Participant | undefined {
const resArray = this.participants.filter(p => p.address === address)
let result
Expand Down Expand Up @@ -155,6 +137,24 @@ export class AccountService {
return util.addHexPrefix(this.accountsTree!.root.toString('hex'))
}

private async addTransaction (tx: ERC20Transaction): Promise<ERC20Transaction> {
this.txs.push(tx)
await this.addChange(this.slotID, this.txID)
await this.addAccountChange(this.slotID, tx.lock, tx.amount)
this.slotID = this.slotID.plus(1)
this.txID = this.txID.plus(1)
await this.sync()
return tx
}

private async addChange (slotId: BigNumber, txId: BigNumber): Promise<void> {
this.changes!.set(slotId.toString(), txId)
}

private async addAccountChange (slotId: BigNumber, account: string, amount: BigNumber): Promise<void> {
this.accounts.set(slotId.toString(), Buffer.concat([solUtils.stringToBuffer(account), solUtils.bignumberToUint256(amount)]))
}

// async lastSlotID (): Promise<BigNumber> {
// return this.plasmoidContract.
// }
Expand Down