Skip to content

Commit

Permalink
Add SMT support (no tests) #19
Browse files Browse the repository at this point in the history
  • Loading branch information
mvandeberg committed Dec 20, 2019
1 parent 3d672a2 commit b6c9863
Show file tree
Hide file tree
Showing 6 changed files with 2,623 additions and 2,109 deletions.
21 changes: 15 additions & 6 deletions src/steem/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,16 @@ export interface SMTAsset {
nai: string
}

export class AssetSymbol {
constructor(public readonly nai: string, public readonly precision: number) {}
}

/**
* Asset symbol string.
*/
export type AssetSymbol = 'STEEM' | 'VESTS' | 'SBD' | 'TESTS' | 'TBD'
export type CoreAssetSymbol = 'STEEM' | 'VESTS' | 'SBD' | 'TESTS' | 'TBD'

export type CompatibleAssetSymbol = CoreAssetSymbol | AssetSymbol

/**
* Class representing a steem asset, e.g. `1.000 STEEM` or `12.112233 VESTS`.
Expand All @@ -55,9 +61,10 @@ export class Asset {
/**
* Create a new Asset instance from a string, e.g. `42.000 STEEM`.
*/
public static fromString(string: string, expectedSymbol?: AssetSymbol) {
public static fromString(string: string, expectedSymbol?: CompatibleAssetSymbol) {
const [amountString, symbol] = string.split(' ')
if (['STEEM', 'VESTS', 'SBD', 'TESTS', 'TBD'].indexOf(symbol) === -1) {
if (!(expectedSymbol instanceof AssetSymbol)
&& ['STEEM', 'VESTS', 'SBD', 'TESTS', 'TBD'].indexOf(symbol) === -1) {
throw new Error(`Invalid asset symbol: ${ symbol }`)
}
if (expectedSymbol && symbol !== expectedSymbol) {
Expand All @@ -67,15 +74,15 @@ export class Asset {
if (!Number.isFinite(amount)) {
throw new Error(`Invalid asset amount: ${ amountString }`)
}
return new Asset(amount, symbol as AssetSymbol)
return new Asset(amount, symbol as CompatibleAssetSymbol)
}

/**
* Convenience to create new Asset.
* @param symbol Symbol to use when created from number. Will also be used to validate
* the asset, throws if the passed value has a different symbol than this.
*/
public static from(value: string | Asset | number, symbol?: AssetSymbol) {
public static from(value: string | Asset | number, symbol?: CompatibleAssetSymbol) {
if (value instanceof Asset) {
if (symbol && value.symbol !== symbol) {
throw new Error(`Invalid asset, expected symbol: ${ symbol } got: ${ value.symbol }`)
Expand Down Expand Up @@ -106,7 +113,7 @@ export class Asset {
return a.amount > b.amount ? a : b
}

constructor(public readonly amount: number, public readonly symbol: AssetSymbol) {}
constructor(public readonly amount: number, public readonly symbol: CompatibleAssetSymbol) {}

/**
* Return asset precision.
Expand All @@ -120,6 +127,8 @@ export class Asset {
return 3
case 'VESTS':
return 6
default:
return this.symbol.precision
}
}

Expand Down
31 changes: 30 additions & 1 deletion src/steem/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
* in the design, construction, operation or maintenance of any military facility.
*/

import {Asset} from './asset'
import {Asset, AssetSymbol} from './asset'

export interface Comment {
id: number // comment_id_type
Expand Down Expand Up @@ -94,3 +94,32 @@ export interface BeneficiaryRoute {
account: string // account_name_type
weight: number // uint16_t
}

export interface VotableAssetOptions {
max_accepted_payout: Number // share_type
allow_curation_rewards: boolean
beneficiaries: BeneficiaryRoute[]
}

export type CommentOptionsExtensionName =
| 'comment_payout_beneficiaries' // 0
| 'allowed_vote_assets' // 1

export interface CommentOptionsExtension {
0: CommentOptionsExtensionName
1: {[key: string]: any}
}

export interface CommentPayoutBeneficiaries extends CommentOptionsExtension {
0: 'comment_payout_beneficiaries'
1: {
beneficiaries: BeneficiaryRoute[]
}
}

export interface AllowedVoteAssets extends CommentOptionsExtension {
0: 'allowed_vote_assets'
1: {
votable_assets: Array<[AssetSymbol, VotableAssetOptions]>
}
}
131 changes: 128 additions & 3 deletions src/steem/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@

import {PublicKey} from './../crypto'
import {AuthorityType} from './account'
import {Asset, Price, PriceType} from './asset'
import {Asset, AssetSymbol, Price, PriceType} from './asset'
import {SignedBlockHeader} from './block'
import {BeneficiaryRoute} from './comment'
import {CommentOptionsExtension} from './comment'
import {ChainProperties, HexBuffer} from './misc'
import {SMTEmissionsUnit, SMTGenerationPolicy, SMTRuntimeParameter, SMTSetupParameter} from './smt'

/**
* Operation name.
Expand All @@ -54,6 +55,7 @@ export type OperationName = // <id>
| 'change_recovery_account' // 26
| 'claim_account' // 22
| 'claim_reward_balance' // 39
| 'claim_reward_balance2' // 47
| 'create_proposal' // 44
| 'comment' // 1
| 'comment_options' // 19
Expand Down Expand Up @@ -82,12 +84,20 @@ export type OperationName = // <id>
| 'reset_account' // 37
| 'set_reset_account' // 38
| 'set_withdraw_vesting_route' // 20
| 'smt_contribute' // 55
| 'smt_create' // 54
| 'smt_set_runtime_parameters' // 53
| 'smt_set_setup_parameters' // 52
| 'smt_setup' // 49
| 'smt_setup_emissions' // 50
| 'smt_setup_ico_tier' // 51
| 'transfer' // 2
| 'transfer_from_savings' // 33
| 'transfer_to_savings' // 32
| 'transfer_to_vesting' // 3
| 'update_proposal_votes' // 45
| 'vote' // 0
| 'vote2' // 48
| 'withdraw_vesting' // 4
| 'witness_set_properties' // 42
| 'witness_update' // 11
Expand Down Expand Up @@ -245,6 +255,15 @@ export interface ClaimRewardBalanceOperation extends Operation {
}
}

export interface ClaimRewardBalance2Operation extends Operation {
0: 'claim_reward_balance2' // 47
1: {
account: string // account_name_type
reward_tokens: AssetSymbol[] // flat_set< asset_symbol_type >
extensions: any[]
}
}

export interface ClaimAccountOperation extends Operation {
0: 'claim_account' // 22
1: {
Expand Down Expand Up @@ -283,7 +302,7 @@ export interface CommentOptionsOperation extends Operation {
allow_votes: boolean
/** Whether to allow post to recieve curation rewards. */
allow_curation_rewards: boolean
extensions: Array<[0, {beneficiaries: BeneficiaryRoute[]}]> // flat_set< comment_options_extension >
extensions: CommentOptionsExtension[]
}
}

Expand Down Expand Up @@ -747,6 +766,101 @@ export interface SetWithdrawVestingRouteOperation extends Operation {
}
}

/**
* Contribute to an ongoing SMT ICO.
*/
export interface SMTContributeOpertaion extends Operation {
0: 'smt_contribute' // 55
1: {
contributor: string // account_name_type
symbol: AssetSymbol
contribution_id: number // uint32_t
contribution: Asset
extensions: any[]
}
}

export interface SMTCreateOperation extends Operation {
0: 'smt_create' // 54
1: {
control_account: string // account_name_type
symbol: AssetSymbol
smt_creation_fee: Asset
precision: Number // uint8_t
extensions: any[]
}
}

export interface SMTSetRuntimeParametersOperation extends Operation {
0: 'smt_set_runtime_parameters' // 53
1: {
control_account: string // account_name_type
symbol: AssetSymbol
runtime_parameters: SMTRuntimeParameter[]
extensions: any[]
}
}

export interface SMTSetSetupParametersOperation extends Operation {
0: 'smt_set_setup_parameters' // 52
1: {
control_account: string // account_name_type
symbol: AssetSymbol
setup_parameters: SMTSetupParameter[]
extensions: any[]
}
}

export interface SMTSetupOperation extends Operation {
0: 'smt_setup' // 49
1: {
control_account: string // account_name_type
symbol: AssetSymbol
max_supply: Number // int64_t
contribution_begin_time: string // time_point_sec
contribution_end_time: string // time_point_sec
launch_time: string // time_point_sec
steem_units_min: Number // share_type
min_unit_ratio: Number // uint32_t
max_unit_ratio: Number // uint32_t
extensions: any[]
}
}

export interface SMTSetupEmissionsOperation extends Operation {
0: 'smt_setup_emissions' // 50
1: {
control_account: string // account_name_type
symbol: AssetSymbol
schedule_time: string // time_point_sec
emissions_unit: SMTEmissionsUnit
interval_seconds: Number // uint32_t
interval_count: Number // uint32_t
lep_time: string // time_point_sec
rep_time: string // time_point_sec
lep_abs_amount: Number // share_type
rep_abs_amount: Number // share_type
lep_rel_amount_numerator: Number // uint32_t
rep_rel_amount_numerator: Number // uint32_t
rel_amount_denom_bits: Number // uint8_t
remove: boolean
floor_emissions: boolean
extensions: any[]
}
}

export interface SMTSetupICOTierOperation extends Operation {
0: 'smt_setup_ico_tier' // 51
1: {
control_account: string // account_name_type
symbol: AssetSymbol
steem_units_cap: Number // share_type
generation_policy: SMTGenerationPolicy
remove: boolean
extensions: any[]
}
}

/**
* Transfers STEEM from one account to another.
*/
Expand Down Expand Up @@ -826,6 +940,17 @@ export interface VoteOperation extends Operation {
}
}

export interface Vote2Operation extends Operation {
0: 'vote2' // 48
1: {
voter: string // account_name_type
author: string // account_name_type
permlink: string
rshares: Array<[AssetSymbol, Number]> // flat_map< asset_symbol_type, int64_t >
extensions: any[]
}
}

/**
* At any given point in time an account can be withdrawing from their
* vesting shares. A user may change the number of shares they wish to
Expand Down
Loading

0 comments on commit b6c9863

Please sign in to comment.