Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use impl pattern for Account, Application, Asset, and log functions #23

Open
wants to merge 16 commits into
base: alpha
Choose a base branch
from
Open
Show file tree
Hide file tree
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
25 changes: 16 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@
"tslib": "^2.6.2"
},
"dependencies": {
"@algorandfoundation/algorand-typescript": "^0.0.1-alpha.24",
"@algorandfoundation/puya-ts": "^1.0.0-alpha.36",
"@algorandfoundation/algorand-typescript": "^1.0.0-beta.4",
"@algorandfoundation/puya-ts": "^1.0.0-beta.6",
"elliptic": "^6.5.7",
"js-sha256": "^0.11.0",
"js-sha3": "^0.9.3",
Expand Down
4 changes: 2 additions & 2 deletions src/abi-metadata.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BaseContract, Contract } from '@algorandfoundation/algorand-typescript'
import { AbiMethodConfig, BareMethodConfig, CreateOptions, OnCompleteActionStr } from '@algorandfoundation/algorand-typescript/arc4'
import { sha512_256 as js_sha512_256 } from 'js-sha512'
import js_sha512 from 'js-sha512'
import { TypeInfo } from './encoders'
import { getArc4TypeName as getArc4TypeNameForARC4Encoded } from './impl/encoded-types'
import { DeliberateAny } from './typescript-helpers'
Expand Down Expand Up @@ -79,7 +79,7 @@ export const getArc4Signature = (metadata: AbiMetadata): string => {
}

export const getArc4Selector = (metadata: AbiMetadata): Uint8Array => {
const hash = js_sha512_256.array(getArc4Signature(metadata))
const hash = js_sha512.sha512_256.array(getArc4Signature(metadata))
return new Uint8Array(hash.slice(0, 4))
}

Expand Down
13 changes: 10 additions & 3 deletions src/context-helpers/internal-context.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Account, BaseContract, internal } from '@algorandfoundation/algorand-typescript'
import { AccountData } from '../impl/account'
import { ApplicationData } from '../impl/application'
import { AssetData } from '../impl/asset'
import { AccountData, ApplicationData, AssetData } from '../impl/reference'
import { VoterData } from '../impl/voter-params'
import { TransactionGroup } from '../subcontexts/transaction-context'
import { TestExecutionContext } from '../test-execution-context'

Expand All @@ -12,7 +11,7 @@
*/
class InternalContext {
get value() {
return internal.ctxMgr.instance as TestExecutionContext

Check failure on line 14 in src/context-helpers/internal-context.ts

View workflow job for this annotation

GitHub Actions / Build @algorandfoundation/algorand-typescript-testing / node-ci

Conversion of type 'ExecutionContext' to type 'TestExecutionContext' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
}

get defaultSender() {
Expand Down Expand Up @@ -69,6 +68,14 @@
}
return data
}

getVoterData(account: Account): VoterData {
const data = this.ledger.voterDataMap.get(account)
if (!data) {
throw internal.errors.internalError('Unknown voter, check correct testing context is active')
}
return data
}
}

export const lazyContext = new InternalContext()
4 changes: 1 addition & 3 deletions src/encoders.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { biguint, BigUint, bytes, internal, TransactionType, uint64, Uint64 } from '@algorandfoundation/algorand-typescript'
import { ARC4Encoded, OnCompleteAction } from '@algorandfoundation/algorand-typescript/arc4'
import { AccountCls } from './impl/account'
import { ApplicationCls } from './impl/application'
import { AssetCls } from './impl/asset'
import { BytesBackedCls, Uint64BackedCls } from './impl/base'
import { arc4Encoders, encodeArc4Impl, getArc4Encoder } from './impl/encoded-types'
import { AccountCls, ApplicationCls, AssetCls } from './impl/reference'
import { DeliberateAny } from './typescript-helpers'
import { asBytes, asMaybeBigUintCls, asMaybeBytesCls, asMaybeUint64Cls, asUint64Cls, asUint8Array, nameOfType } from './util'

Expand Down
102 changes: 0 additions & 102 deletions src/impl/account.ts

This file was deleted.

20 changes: 13 additions & 7 deletions src/impl/acct-params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Account, Application, gtxn, internal, uint64 } from '@algorandfoundatio
import { lazyContext } from '../context-helpers/internal-context'
import { asMaybeUint64Cls } from '../util'
import { getApp } from './app-params'
import { Global } from './global'

export const getAccount = (acct: Account | internal.primitives.StubUint64Compat): Account => {
const acctId = asMaybeUint64Cls(acct)
Expand Down Expand Up @@ -84,14 +85,19 @@ export const AcctParams: internal.opTypes.AcctParamsType = {
const acct = getAccount(a)
return [acct.totalBoxBytes, acct.balance !== 0]
},
// TODO: implement v11 methods
acctIncentiveEligible: function (_a: Account | uint64): readonly [boolean, boolean] {
throw new Error('Function not implemented.')
acctIncentiveEligible: function (a: Account | internal.primitives.StubUint64Compat): readonly [boolean, boolean] {
const acct = getAccount(a)
const accountData = lazyContext.ledger.accountDataMap.get(acct)
return [accountData?.incentiveEligible ?? false, acct.balance !== 0]
},
acctLastProposed: function (_a: Account | uint64): readonly [uint64, boolean] {
throw new Error('Function not implemented.')
acctLastProposed: function (a: Account | internal.primitives.StubUint64Compat): readonly [uint64, boolean] {
const acct = getAccount(a)
const accountData = lazyContext.ledger.accountDataMap.get(acct)
return [accountData?.lastProposed ?? Global.round, acct.balance !== 0]
},
acctLastHeartbeat: function (_a: Account | uint64): readonly [uint64, boolean] {
throw new Error('Function not implemented.')
acctLastHeartbeat: function (a: Account | internal.primitives.StubUint64Compat): readonly [uint64, boolean] {
const acct = getAccount(a)
const accountData = lazyContext.ledger.accountDataMap.get(acct)
return [accountData?.lastHeartbeat ?? Global.round, acct.balance !== 0]
},
}
5 changes: 3 additions & 2 deletions src/impl/app-params.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Account, Application, Bytes, bytes, gtxn, internal, Uint64, uint64 } from '@algorandfoundation/algorand-typescript'
import { lazyContext } from '../context-helpers/internal-context'
import { asMaybeUint64Cls, asUint64 } from '../util'
import { AccountImpl } from './reference'

const resolveAppIndex = (appIdOrIndex: internal.primitives.StubUint64Compat): uint64 => {
const input = asUint64(appIdOrIndex)
Expand Down Expand Up @@ -54,10 +55,10 @@ export const AppParams: internal.opTypes.AppParamsType = {
},
appCreator(a: Application | internal.primitives.StubUint64Compat): readonly [Account, boolean] {
const app = getApp(a)
return app === undefined ? [Account(), false] : [app.creator, true]
return app === undefined ? [AccountImpl(), false] : [app.creator, true]
},
appAddress(a: Application | internal.primitives.StubUint64Compat): readonly [Account, boolean] {
const app = getApp(a)
return app === undefined ? [Account(), false] : [app.address, true]
return app === undefined ? [AccountImpl(), false] : [app.address, true]
},
}
80 changes: 0 additions & 80 deletions src/impl/application.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/impl/asset-holding.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Account, Asset, internal, Uint64, uint64 } from '@algorandfoundation/algorand-typescript'
import { lazyContext } from '../context-helpers/internal-context'
import { AssetHolding as AssetHoldingData } from './account'
import { getAccount } from './acct-params'
import { getAsset } from './asset-params'
import { AssetHolding as AssetHoldingData } from './reference'

const getAssetHolding = (
acctOrIndex: Account | internal.primitives.StubUint64Compat,
Expand Down
Loading
Loading