Skip to content

Commit

Permalink
Merge pull request #1494 from aeternity/feature/refactor-chain
Browse files Browse the repository at this point in the history
refactor(chain)!: drop stamps and use plain functions
  • Loading branch information
davidyuk authored Apr 30, 2022
2 parents 54eb1cc + 4197b3d commit 88aa387
Show file tree
Hide file tree
Showing 18 changed files with 467 additions and 614 deletions.
4 changes: 2 additions & 2 deletions docs/guides/connect-aepp-to-wallet.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ async created () {
},
onAddressChange: async () => {
this.address = await this.aeSdk.address()
this.balance = await this.aeSdk.balance(this.address)
this.balance = await this.aeSdk.getBalance(this.address)
},
onDisconnect: () => {
// you may want to reset state here
Expand Down Expand Up @@ -87,7 +87,7 @@ async connect(wallet) {
await this.aeSdk.connectToWallet(await wallet.getConnection())
this.connectedAccounts = await this.aeSdk.subscribeAddress('subscribe', 'connected')
this.address = await this.aeSdk.address()
this.balance = await this.aeSdk.balance(this.address).catch(() => '0')
this.balance = await this.aeSdk.getBalance(this.address).catch(() => '0')
this.nodeInfo = await this.aeSdk.getNodeInfo()
}
```
Expand Down
2 changes: 1 addition & 1 deletion examples/browser/aepp/src/Basic.vue
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export default {
([aeSdk, address]) => {
if (!aeSdk) return
this.compilerVersion = aeSdk.compilerVersion
this.balancePromise = aeSdk.balance(address)
this.balancePromise = aeSdk.getBalance(address)
this.heightPromise = aeSdk.height()
this.nodeInfoPromise = aeSdk.getNodeInfo()
},
Expand Down
2 changes: 1 addition & 1 deletion src/ae/aens.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ async function claim (name, salt, options) {

const result = await this.send(claimTx, opt)
if (!isAuctionName(name)) {
const nameInter = opt.waitMined ? await this.aensQuery(name, opt) : {}
const nameInter = result.blockHeight ? await this.aensQuery(name, opt) : {}
return Object.assign(result, nameInter)
}
return { ...result, nameFee: opt.nameFee }
Expand Down
3 changes: 1 addition & 2 deletions src/ae/aepp.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import Ae from './'
import Aens from './aens'
import Contract from './contract'
import AeppRpc from '../utils/aepp-wallet-communication/rpc/aepp-rpc'
import Chain from '../chain/node'
import Tx from '../tx/tx'
import Oracle from './oracle'

Expand All @@ -42,4 +41,4 @@ import Oracle from './oracle'
* @param {Object} [options={}] - Initializer object
* @return {Object} Aepp instance
*/
export default Ae.compose(Tx, Oracle, Contract, Aens, Chain, AeppRpc)
export default Ae.compose(Tx, Oracle, Contract, Aens, AeppRpc)
43 changes: 38 additions & 5 deletions src/ae/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@

import stampit from '@stamp/it'
import Tx from '../tx'
import Chain from '../chain'
import AccountBase from '../account/base'
import * as chainMethods from '../chain'
import NodePool from '../node-pool'
import AccountResolver from '../account/resolver'
import { buildTxHash, unpackTx } from '../tx/builder'
import BigNumber from 'bignumber.js'
import { AE_AMOUNT_FORMATS } from '../utils/amount-formatter'
import { ArgumentError } from '../utils/errors'
import { mapObject } from '../utils/other'

/**
* Sign and post a transaction to the chain
Expand Down Expand Up @@ -100,7 +102,7 @@ async function transferFunds (fraction, recipientIdOrName, options) {
const opt = { ...this.Ae.defaults, ...options }
const recipientId = await this.resolveName(recipientIdOrName, 'account_pubkey', opt)
const senderId = await this.address(opt)
const balance = new BigNumber(await this.balance(senderId))
const balance = new BigNumber(await this.getBalance(senderId))
const desiredAmount = balance.times(fraction).integerValue(BigNumber.ROUND_HALF_UP)
const { tx: { fee } } = unpackTx(
await this.spendTx({ ...opt, senderId, recipientId, amount: desiredAmount })
Expand Down Expand Up @@ -160,8 +162,39 @@ function destroyInstance () {
* @param {Object} [options={}] - Initializer object
* @return {Object} Ae instance
*/
const Ae = stampit(Tx, AccountBase, Chain, {
methods: { send, spend, transferFunds, payForTransaction, destroyInstance, signUsingGA },
const Ae = stampit(NodePool, Tx, AccountResolver, {
methods: {
send,
spend,
transferFunds,
payForTransaction,
destroyInstance,
signUsingGA,
...mapObject(
chainMethods,
([name, handler]) => [
name,
function (...args) {
const instanceOptions = {
...this.Ae.defaults,
onNode: this.selectedNode.instance,
onAccount: this
}
const lastArg = args[args.length - 1]
if (
lastArg && typeof lastArg === 'object' && lastArg.constructor === Object
) {
Object.assign(lastArg, {
...instanceOptions,
...lastArg,
...lastArg.onAccount && { onAccount: this._resolveAccount(lastArg.onAccount) }
})
} else args.push(instanceOptions)
return handler(...args)
}
]
)
},
deepProps: { Ae: { defaults: { denomination: AE_AMOUNT_FORMATS.AETTOS } } }
})

Expand Down
3 changes: 1 addition & 2 deletions src/ae/universal.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/

import Ae from './'
import Chain from '../chain/node'
import Aens from './aens'
import Transaction from '../tx/tx'
import Oracle from './oracle'
Expand All @@ -43,5 +42,5 @@ import Contract from './contract'
* @return {Object} Universal instance
*/
export default Ae.compose(
AccountMultiple, Chain, Transaction, Aens, Contract, Oracle, GeneralizedAccount
AccountMultiple, Transaction, Aens, Contract, Oracle, GeneralizedAccount
)
3 changes: 1 addition & 2 deletions src/ae/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/

import Ae from './'
import Chain from '../chain/node'
import Tx from '../tx/tx'
import Contract from './contract'
import GeneralizedAccount from '../contract/ga'
Expand All @@ -47,4 +46,4 @@ import Aens from './aens'
address: keypair.publicKey,
})
*/
export default Ae.compose(WalletRpc, Tx, Contract, Oracle, Aens, GeneralizedAccount, Chain)
export default Ae.compose(WalletRpc, Tx, Contract, Oracle, Aens, GeneralizedAccount)
Loading

0 comments on commit 88aa387

Please sign in to comment.