Skip to content

Commit

Permalink
refactoring in JS is a nightmare
Browse files Browse the repository at this point in the history
  • Loading branch information
volovyks committed Jun 3, 2022
1 parent fecc5f1 commit e7d8724
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions examples/lockable-fungible-token/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,25 +64,25 @@ class LockableFungibleToken extends NearContract {
this.accounts = new LookupMap(prefix) // Account ID -> Account mapping
this.totalSupply = totalSupply // Total supply of the all tokens
let ownerId = near.signerAccountId()
let ownerAccount = this.getAccount({ ownerId })
let ownerAccount = this.getAccount(ownerId)
ownerAccount.balance = totalSupply
this.setAccount({ accountId: ownerId, account: ownerAccount })
this.setAccount(ownerId, ownerAccount)
}

deserialize() {
super.deserialize()
this.accounts = Object.assign(new LookupMap, this.accounts)
}

getAccount({ ownerId }) {
getAccount(ownerId) {
let account = this.accounts.get(ownerId)
if (account === null) {
return new Account(0, {}, {})
}
return Object.assign(new Account(), JSON.parse(account))
}

setAccount({ accountId, account }) {
setAccount(accountId, account) {
this.accounts.set(accountId, JSON.stringify(account))
}

Expand All @@ -92,14 +92,14 @@ class LockableFungibleToken extends NearContract {
if (escrowAccountId === ownerId) {
throw Error("Can't set allowance for yourself")
}
let account = this.getAccount({ ownerId })
let account = this.getAccount(ownerId)
let lockedBalance = account.getLockedBalance(escrowAccountId)
if (lockedBalance > allowance) {
throw Error("The new allowance can't be less than the amount of locked tokens")
}

account.setAllowance(escrowAccountId, allowance - lockedBalance)
this.setAccount({ accountId: ownerId, account })
this.setAccount(ownerId, account)
}

@call
Expand All @@ -108,7 +108,7 @@ class LockableFungibleToken extends NearContract {
throw Error("Can't lock 0 or less tokens")
}
let escrowAccountId = near.predecessorAccountId()
let account = this.getAccount({ ownerId })
let account = this.getAccount(ownerId)

// Checking and updating unlocked balance
if (account.balance < lockAmount) {
Expand All @@ -129,7 +129,7 @@ class LockableFungibleToken extends NearContract {
let lockedBalance = account.getLockedBalance(escrowAccountId)
account.setLockedBalance(escrowAccountId, lockedBalance + lockAmount)

this.setAccount({ accountId: ownerId, account })
this.setAccount(ownerId, account)
}

@call
Expand All @@ -138,7 +138,7 @@ class LockableFungibleToken extends NearContract {
throw Error("Can't unlock 0 or less tokens")
}
let escrowAccountId = near.predecessorAccountId()
let account = this.getAccount({ ownerId })
let account = this.getAccount(ownerId)

// Checking and updating locked balance
let lockedBalance = account.getLockedBalance(escrowAccountId)
Expand All @@ -156,7 +156,7 @@ class LockableFungibleToken extends NearContract {
// Updating unlocked balance
account.balance += unlockAmount

this.setAccount({ accountId: ownerId, account })
this.setAccount(ownerId, account)
}

@call
Expand All @@ -165,7 +165,7 @@ class LockableFungibleToken extends NearContract {
throw Error("Can't transfer 0 or less tokens")
}
let escrowAccountId = near.predecessorAccountId()
let account = this.getAccount({ ownerId })
let account = this.getAccount(ownerId)

// Checking and updating locked balance
let lockedBalance = account.getLockedBalance(escrowAccountId)
Expand Down Expand Up @@ -197,12 +197,12 @@ class LockableFungibleToken extends NearContract {
}
}

this.setAccount({ accountId: ownerId, account })
this.setAccount(ownerId, account)

// Deposit amount to the new owner
let newAccount = this.getAccount(newOwnerId)
newAccount.balance += amount
this.setAccount({ accountId: newOwnerId, newAccount })
this.setAccount(newOwnerId, newAccount)
}

@call
Expand All @@ -217,21 +217,21 @@ class LockableFungibleToken extends NearContract {

@view
getTotalBalance({ ownerId }) {
return this.getAccount({ ownerId }).totalBalance()
return this.getAccount(ownerId).totalBalance()
}

@view
getUnlockedBalance({ ownerId }) {
return this.getAccount({ ownerId }).balance
return this.getAccount(ownerId).balance
}

@view
getAllowance({ ownerId, escrowAccountId }) {
return this.getAccount({ ownerId }).getAllowance(escrowAccountId)
return this.getAccount(ownerId).getAllowance(escrowAccountId)
}

@view
getLockedBalance({ ownerId, escrowAccountId }) {
return this.getAccount({ ownerId }).getLockedBalance(escrowAccountId)
return this.getAccount(ownerId).getLockedBalance(escrowAccountId)
}
}

0 comments on commit e7d8724

Please sign in to comment.