Skip to content

Commit

Permalink
Merge pull request #9 from wharfkit/no-anchor-link
Browse files Browse the repository at this point in the history
Dependencies cleanup + type changes
  • Loading branch information
dafuga authored Feb 2, 2023
2 parents da019d3 + 359f9df commit c27744e
Show file tree
Hide file tree
Showing 10 changed files with 189 additions and 189 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ check: node_modules

.PHONY: format
format: node_modules
@${BIN}/eslint src --ext .ts --fix
@${BIN}/eslint src test --ext .ts --fix

.PHONY: publish
publish: | distclean node_modules
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
},
"dependencies": {
"@greymass/eosio": "^0.6.4",
"anchor-link": "^3.5.1",
"eosio-signing-request": "^2.5.2",
"@wharfkit/session": "^0.2.0",
"git": "^0.1.5",
"tslib": "^2.1.0"
},
Expand Down Expand Up @@ -51,4 +50,4 @@
"typedoc": "^0.23.14",
"typescript": "^4.1.2"
}
}
}
70 changes: 27 additions & 43 deletions src/accounts.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import {API, APIClient, Asset, AssetType, Checksum256, Name, NameType} from '@greymass/eosio'
import {ChainId, ChainName} from 'anchor-link'
import type {ChainIdType} from 'anchor-link'
import {
API,
APIClient,
Asset,
AssetType,
Checksum256,
Checksum256Type,
Name,
NameType,
} from '@greymass/eosio'

import {PermissionActions} from './accounts/actions/permissions'
import {Permission} from './permissions'
import {ResourceActions} from './accounts/actions/resources'

// import type { Session } from '@wharfkit/session'

// Remove these when Contract and Session are used
export interface Session {
[key: string]: any
}

export interface SessionTransactResult {
id: Checksum256
}
import type {Session, TransactResult} from '@wharfkit/session'

export interface Resources {
cpu_available: number
Expand All @@ -28,29 +26,29 @@ export interface Resources {

export class Account {
account_name: Name
chain_id: ChainId
chain_id: Checksum256
api_client: APIClient
account_data: API.v1.AccountObject | undefined
account_data_timestamp: number | undefined
// contract: Contract | undefined
cache_duration: number = 1000 * 60 * 5 // 5 minutes

constructor(accountName: Name, chainId: ChainId, apiClient: APIClient) {
constructor(accountName: Name, chainId: Checksum256Type, apiClient: APIClient) {
this.account_name = accountName
this.chain_id = chainId
this.chain_id = Checksum256.from(chainId)
this.api_client = apiClient
// this.contract = new Contract(chainId, this, options?.session)
}

static from(accountName: NameType, chain: ChainIdType, apiClient: APIClient): Account {
return new Account(Name.from(accountName), ChainId.from(chain), apiClient)
static from(accountName: NameType, chain: Checksum256Type, apiClient: APIClient): Account {
return new Account(Name.from(accountName), Checksum256.from(chain), apiClient)
}

get accountName(): Name {
return this.account_name
}

get chainId(): ChainId {
get chainId(): Checksum256 {
return this.chain_id
}

Expand All @@ -63,7 +61,7 @@ export class Account {
updatePermission(
permission: Permission,
{session}: {session: Session}
): Promise<SessionTransactResult> {
): Promise<TransactResult> {
return PermissionActions.shared().updateAuth(permission.actionData, {
account: this,
session,
Expand All @@ -73,29 +71,29 @@ export class Account {
removePermission(
permissionName: NameType,
{session}: {session: Session}
): Promise<SessionTransactResult> {
): Promise<TransactResult> {
return PermissionActions.shared().deleteAuth(
Name.from(permissionName),
Name.from(this.account_name),
{account: this, session}
)
}

buyRam(amount: AssetType, {session}: {session: Session}): Promise<SessionTransactResult> {
buyRam(amount: AssetType, {session}: {session: Session}): Promise<TransactResult> {
return ResourceActions.shared().buyRam(this.accountName, this.accountName, amount, {
account: this,
session,
})
}

buyRamBytes(bytes: number, {session}: {session: Session}): Promise<SessionTransactResult> {
buyRamBytes(bytes: number, {session}: {session: Session}): Promise<TransactResult> {
return ResourceActions.shared().buyRamBytes(this.accountName, this.accountName, bytes, {
account: this,
session,
})
}

sellRam(bytes: number, {session}: {session: Session}): Promise<SessionTransactResult> {
sellRam(bytes: number, {session}: {session: Session}): Promise<TransactResult> {
return ResourceActions.shared().sellRam(this.accountName, bytes, {account: this, session})
}

Expand All @@ -104,7 +102,7 @@ export class Account {
net: AssetType,
transfer: boolean,
{session}: {session: Session}
): Promise<SessionTransactResult> {
): Promise<TransactResult> {
return ResourceActions.shared().delegateResources(
this.accountName,
this.accountName,
Expand All @@ -119,7 +117,7 @@ export class Account {
cpu: AssetType,
net: AssetType,
{session}: {session: Session}
): Promise<SessionTransactResult> {
): Promise<TransactResult> {
return ResourceActions.shared().undelegateResources(
this.accountName,
this.accountName,
Expand Down Expand Up @@ -158,9 +156,7 @@ export class Account {
if (!balance) {
reject(
new Error(
`No balance found for ${symbol} token of ${contract} contract on chain ${
ChainName[this.chain_id.chainName]
}.`
`No balance found for ${symbol} token of ${contract} contract.`
)
)
}
Expand All @@ -172,13 +168,7 @@ export class Account {
err.message.includes('No data') ||
err.message.includes('Account Query Exception')
) {
reject(
new Error(
`Token contract ${contract} does not exist on chain ${
ChainName[this.chain_id.chainName]
}.`
)
)
reject(new Error(`Token contract ${contract} does not exist.`))
}
reject(err)
})
Expand All @@ -204,13 +194,7 @@ export class Account {
})
.catch((error) => {
if (error.message.includes('Account not found')) {
return reject(
new Error(
`Account ${this.account_name} does not exist on chain ${
ChainName[this.chain_id.chainName]
}.`
)
)
return reject(new Error(`Account ${this.account_name} does not exist.`))
}
reject(error)
})
Expand Down
3 changes: 2 additions & 1 deletion src/permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {
} from '@greymass/eosio'
import {API} from '@greymass/eosio'

import type {Account, Session} from './accounts'
import type {Account} from './accounts'
import type {Session} from '@wharfkit/session'

export type PermissionParams =
| {permissionName: NameType; accountData: API.v1.AccountObject}
Expand Down
46 changes: 31 additions & 15 deletions src/tmp/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,38 @@ import {
Checksum256,
Name,
NameType,
PermissionLevel,
PrivateKey,
} from '@greymass/eosio'

// stubs for session kit
interface Session {
transact(SessionTransactArgs: any): Promise<SessionTransactResult>
}
interface SessionTransactArgs {
actions: AnyAction[]
}
export interface SessionTransactResult {
id: Checksum256
}
import {Session, TransactArgs, TransactResult, WalletPluginPrivateKey} from '@wharfkit/session'

// TODO: move this to core
export function isABISerializableObject(value: any): value is ABISerializableObject {
return value.constructor && typeof value.constructor.abiName === 'string'
}

function mockFetch(data) {
return Promise.resolve({
json: () => Promise.resolve(data),
text: () => Promise.resolve(JSON.stringify(data)),
})
}

// TODO: Remove this mock session once a real one exists
const mockSession = new Session({
broadcast: false, // Disable broadcasting by default for tests, enable when required.
chain: {
id: '73e4385a2708e6d7048834fbc1079f2fabb17b3c125b146af438971e90716c4d',
url: 'https://jungle4.greymass.com',
},
permissionLevel: PermissionLevel.from('foo@active'),
walletPlugin: new WalletPluginPrivateKey({
privateKey: PrivateKey.from('5Jtoxgny5tT7NiNFp1MLogviuPJ9NniWjnU4wKzaX4t7pL4kJ8s'),
}),
fetch: mockFetch,
})

export class Contract {
/** Account where contract is deployed. */
static account: Name
Expand All @@ -45,10 +59,7 @@ export class Contract {
}

/** Call a contract action. */
async call(
name: NameType,
data: ABISerializableObject | {[key: string]: any}
): Promise<SessionTransactResult> {
async call(name: NameType, data: ABISerializableObject | {[key: string]: any}) {
let action: Action
if (isABISerializableObject(data)) {
action = Action.from({
Expand All @@ -58,7 +69,12 @@ export class Contract {
data,
})

return {id: Checksum256.from('random_id')}
return mockSession.transact(
{
action,
},
{broadcast: false}
)
} else {
// TODO: here we need to fetch the ABI and construct the action
throw new Error('Not implemented')
Expand Down
Loading

0 comments on commit c27744e

Please sign in to comment.