Skip to content

Commit

Permalink
Reverting back to old action syntax + new table syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
aaroncox committed Aug 10, 2023
1 parent 0b7a623 commit 1535d1f
Show file tree
Hide file tree
Showing 9 changed files with 233 additions and 124 deletions.
30 changes: 14 additions & 16 deletions src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,31 +94,29 @@ export class Contract {
return this.actionNames.includes(String(name))
}

public action(name): ActionConstructor {
public action(name, data: ActionDataType, options?: ActionOptions): Action {
if (!this.hasAction(name)) {
throw new Error(`Contract (${this.account}) does not have an action named (${name})`)
}

return (data: ActionDataType, options?: ActionOptions) => {
let authorization = [PlaceholderAuth]
if (options && options.authorization) {
authorization = options.authorization.map((auth) => PermissionLevel.from(auth))
}
return Action.from(
{
account: this.account,
name,
authorization,
data,
},
this.abi
)
let authorization = [PlaceholderAuth]
if (options && options.authorization) {
authorization = options.authorization.map((auth) => PermissionLevel.from(auth))
}
return Action.from(
{
account: this.account,
name,
authorization,
data,
},
this.abi
)
}

public actions(actions: ActionsArgs[], options?: ActionOptions): Action[] {
return actions.map((action) =>
this.action(action.name)(action.data, {
this.action(action.name, action.data, {
authorization: action.authorization || options?.authorization,
})
)
Expand Down
150 changes: 56 additions & 94 deletions test/data/contracts/mock-rewards.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,17 @@
import {
ActionOptions,
Contract as BaseContract,
ContractArgs,
PartialBy,
blobStringToAbi,
} from '@wharfkit/contract'
import {ActionOptions, Contract as BaseContract, ContractArgs, PartialBy} from '@wharfkit/contract'
import {
ABI,
Action,
APIClient,
Session,
Struct,
TransactResult,
Asset,
Checksum256,
AssetType,
Blob,
Float64,
Name,
NameType,
Struct,
TimePoint,
TimePointSec,
UInt128,
UInt16,
UInt32,
UInt64,
UInt8,
AssetType,
Blob,
Checksum256Type,
Float64Type,
NameType,
TimePointType,
UInt128Type,
UInt16Type,
UInt32Type,
UInt64Type,
UInt8Type,
} from '@wharfkit/session'
export namespace RewardsGm {
export const abiBlob = Blob.from(
Expand All @@ -48,19 +26,17 @@ export namespace RewardsGm {
account: Name.from('rewards.gm'),
})
}
action<T extends ActionNames>(
name: T
): (data: ActionNameParams[T], options?: ActionOptions) => Action {
return (data, options) => super.action(name)(data, options)
action<T extends 'adduser' | 'claim' | 'configure' | 'deluser' | 'receipt' | 'updateuser'>(
name: T,
data: ActionNameParams[T],
options?: ActionOptions
): Action {
return super.action(name, data, options)
}
table<T extends 'config' | 'users'>(name: T) {
return super.table(name, TableMap[name])
}
}
export type ActionNames =
| 'adduser'
| 'claim'
| 'configure'
| 'deluser'
| 'receipt'
| 'updateuser'
export interface ActionNameParams {
adduser: ActionParams.Adduser
claim: ActionParams.Claim
Expand All @@ -76,20 +52,24 @@ export namespace RewardsGm {
}
export interface Claim {
account: NameType
amount: AssetType
amount?: AssetType
}
export interface Configure {
token_symbol: Symbol
oracle_account: NameType
oracle_pairs: Types.Oracle_pair[]
oracle_pairs: Types.OraclePair[]
}
export interface Deluser {
account: NameType
}
export interface OraclePair {
name: NameType
precision: UInt16Type
}
export interface Receipt {
account: NameType
amount: AssetType
ticker: Types.Price_info[]
ticker: Types.PriceInfo[]
}
export interface Updateuser {
account: NameType
Expand All @@ -99,81 +79,63 @@ export namespace RewardsGm {
export namespace Types {
@Struct.type('adduser')
export class Adduser extends Struct {
@Struct.field('name')
declare account: Name
@Struct.field('uint16')
declare weight: UInt16
@Struct.field(Name) account!: Name
@Struct.field(UInt16) weight!: UInt16
}
@Struct.type('claim')
export class Claim extends Struct {
@Struct.field('name')
declare account: Name
@Struct.field('asset?')
declare amount: Asset
@Struct.field(Name) account!: Name
@Struct.field(Asset, {optional: true}) amount?: Asset
}
@Struct.type('oracle_pair')
export class OraclePair extends Struct {
@Struct.field(Name) name!: Name
@Struct.field(UInt16) precision!: UInt16
}
@Struct.type('config')
export class Config extends Struct {
@Struct.field('symbol')
declare token_symbol: Symbol
@Struct.field('name')
declare oracle_account: Name
@Struct.field('oracle_pair[]')
declare oracle_pairs: RewardsGm.Types.Oracle_pair
@Struct.field(Asset.Symbol) token_symbol!: Asset.Symbol
@Struct.field(Name) oracle_account!: Name
@Struct.field(OraclePair, {array: true}) oracle_pairs!: OraclePair[]
}
@Struct.type('configure')
export class Configure extends Struct {
@Struct.field('symbol')
declare token_symbol: Symbol
@Struct.field('name')
declare oracle_account: Name
@Struct.field('oracle_pair[]')
declare oracle_pairs: RewardsGm.Types.Oracle_pair
@Struct.field(Asset.Symbol) token_symbol!: Asset.Symbol
@Struct.field(Name) oracle_account!: Name
@Struct.field(OraclePair, {array: true}) oracle_pairs!: OraclePair[]
}
@Struct.type('deluser')
export class Deluser extends Struct {
@Struct.field('name')
declare account: Name
}
@Struct.type('oracle_pair')
export class Oracle_pair extends Struct {
@Struct.field('name')
declare name: Name
@Struct.field('uint16')
declare precision: UInt16
@Struct.field(Name) account!: Name
}
@Struct.type('price_info')
export class Price_info extends Struct {
@Struct.field('string')
declare pair: String
@Struct.field('float64')
declare price: Float64
@Struct.field('time_point')
declare timestamp: TimePoint
export class PriceInfo extends Struct {
@Struct.field('string') pair!: string
@Struct.field(Float64) price!: Float64
@Struct.field(TimePoint) timestamp!: TimePoint
}
@Struct.type('receipt')
export class Receipt extends Struct {
@Struct.field('name')
declare account: Name
@Struct.field('asset')
declare amount: Asset
@Struct.field('price_info[]')
declare ticker: RewardsGm.Types.Price_info
@Struct.field(Name) account!: Name
@Struct.field(Asset) amount!: Asset
@Struct.field(PriceInfo, {array: true}) ticker!: PriceInfo[]
}
@Struct.type('updateuser')
export class Updateuser extends Struct {
@Struct.field('name')
declare account: Name
@Struct.field('uint16')
declare weight: UInt16
@Struct.field(Name) account!: Name
@Struct.field(UInt16) weight!: UInt16
}
@Struct.type('user_row')
export class User_row extends Struct {
@Struct.field('name')
declare account: Name
@Struct.field('uint16')
declare weight: UInt16
@Struct.field('asset')
declare balance: Asset
export class UserRow extends Struct {
@Struct.field(Name) account!: Name
@Struct.field(UInt16) weight!: UInt16
@Struct.field(Asset) balance!: Asset
}
}
const TableMap = {
config: Types.Config,
users: Types.UserRow,
}
}

export default RewardsGm
18 changes: 18 additions & 0 deletions test/data/requests/00aac80526ae03d5cfe8c7e4f4ae1c0d2b916049.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"request": {
"path": "https://eos.greymass.com/v1/chain/get_table_rows",
"params": {
"method": "POST",
"body": "{\"table\":\"users\",\"code\":\"rewards.gm\",\"scope\":\"rewards.gm\",\"limit\":1,\"index_position\":\"primary\",\"key_type\":\"name\",\"json\":false}"
}
},
"status": 200,
"json": {
"rows": [
"0000900ca04b6b35f401000000000000000004454f5300000000"
],
"more": true,
"next_key": "4352878718026951040"
},
"text": "{\"rows\":[\"0000900ca04b6b35f401000000000000000004454f5300000000\"],\"more\":true,\"next_key\":\"4352878718026951040\"}"
}
18 changes: 18 additions & 0 deletions test/data/requests/0d9424766a10f0330866036588971c5779697dfa.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"request": {
"path": "https://eos.greymass.com/v1/chain/get_table_rows",
"params": {
"method": "POST",
"body": "{\"json\":false,\"limit\":1,\"table\":\"config\",\"code\":\"rewards.gm\",\"scope\":\"rewards.gm\",\"key_type\":\"name\"}"
}
},
"status": 200,
"json": {
"rows": [
"04454f5300000000a0223297ba56a34a010000000024ac31550400"
],
"more": false,
"next_key": ""
},
"text": "{\"rows\":[\"04454f5300000000a0223297ba56a34a010000000024ac31550400\"],\"more\":false,\"next_key\":\"\"}"
}
18 changes: 18 additions & 0 deletions test/data/requests/742c0f31283ca71045d23ddf3ce8418f87d4c474.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"request": {
"path": "https://eos.greymass.com/v1/chain/get_table_rows",
"params": {
"method": "POST",
"body": "{\"table\":\"config\",\"code\":\"rewards.gm\",\"scope\":\"rewards.gm\",\"limit\":1,\"index_position\":\"primary\",\"key_type\":\"name\",\"json\":false}"
}
},
"status": 200,
"json": {
"rows": [
"04454f5300000000a0223297ba56a34a010000000024ac31550400"
],
"more": false,
"next_key": ""
},
"text": "{\"rows\":[\"04454f5300000000a0223297ba56a34a010000000024ac31550400\"],\"more\":false,\"next_key\":\"\"}"
}
6 changes: 3 additions & 3 deletions test/tests/codegen.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {assert} from 'chai'
import fs from 'fs'
import {ABI, APIClient, Name} from '@wharfkit/antelope'
import {makeClient} from '@wharfkit/mock-data'
import {assert} from 'chai'
import fs from 'fs'
import {Contract} from 'src/contract'

import * as MockRewardsGm from '$test/data/contracts/mock-rewards'
Expand All @@ -11,7 +11,7 @@ import {runGenericContractTests} from './contract'
const GeneratedRewardsGm = await generateCodegenContract('rewards.gm')
const contracts = {
MockRewardsGm,
GeneratedRewardsGm,
GeneratedRewardsGm: GeneratedRewardsGm.import,
}

const files = {
Expand Down
Loading

0 comments on commit 1535d1f

Please sign in to comment.