Skip to content

Commit

Permalink
Merge pull request #138 from mckrava/feature/add-bridge-chains-proper…
Browse files Browse the repository at this point in the history
…ty-support-social-remark

Feat: add definition of destination chain in SocialRemark
  • Loading branch information
olehmell authored Apr 12, 2023
2 parents a91bd67 + 9634890 commit a8a3198
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 51 deletions.
11 changes: 7 additions & 4 deletions packages/utils/src/socialRemark/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ The SocialRemark protocol is designed to process cross-chain actions.
## SocialRemark message format

- Domain Registration
`prot_name::version::action::op_id::target::domain_name::token`
`prot_name::version::destination::action::op_id::target::domain_name::token`
- Energy Generation
`prot_name::version::action::op_id::target::energy_amount::token`
`prot_name::version::destination::action::op_id::target::energy_amount::token`

A Remark message has 2 parts:
- the core part (which is the same for all messages): `prot_name::version::action`
Expand All @@ -18,6 +18,8 @@ A Remark message has 2 parts:
Core part options:
- `prot_name` - The protocol name (the default valid value is `social`, but it can be configured with the `.setConfig()` method)
- `version` - The protocol version (the current available version is `0.1`)
- `destination` - The chain which will be used for cross-chain actions. User can use predefined chain IDs which are
exposed in SocialRemark utility library.
- `action` - The message action (available actions: `DMN_REG`, `DMN_REG_OK`, `DMN_REG_REFUND`, `NRG_GEN`, `NRG_GEN_OK`, `NRG_GEN_REFUND`)

Content options:
Expand All @@ -30,7 +32,7 @@ Content options:
Here is a dummy example of a SocialRemark message:

```
social::0.1::DMN_REG::0x44d8d9f1bc70e45eb773731f9ffc5d3646df56497c40cdfff37c8ceb71fa2-2104480009442407::3t5NA8UKsGzrCDMfp8XMEBghiYthWGXGsHbjtJY45NUJDY5P::somenewdomain.sub::DOT
social::0.1::1::DMN_REG::0x44d8d9f1bc70e45eb773731f9ffc5d3646df56497c40cdfff37c8ceb71fa2-2104480009442407::3t5NA8UKsGzrCDMfp8XMEBghiYthWGXGsHbjtJY45NUJDY5P::somenewdomain.sub::DOT
```

## Usage examples:
Expand All @@ -48,8 +50,9 @@ SocialRemark.setConfig({ protNames: ['social_custom'] })

const remarkSource: SubSclSource<'DMN_REG'> = {
protName: 'social_custom',
action: 'DMN_REG',
version: '0.1',
action: 'DMN_REG',
destination: 1,
content: {
opId: `${randomAsNumber()}`,
domainName: `somenewdomain.sub`,
Expand Down
7 changes: 5 additions & 2 deletions packages/utils/src/socialRemark/config.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import {
SocialRemarkMessageAction,
SocialRemarkMessageProtocolName,
SocialRemarkMessageVersion
SocialRemarkMessageVersion,
SocialRemarkMessageDestination,
} from './types'

export type SocialRemarkConfigData = {
protNames?: Array<SocialRemarkMessageProtocolName>
actions?: SocialRemarkMessageAction[]
versions?: SocialRemarkMessageVersion[]
destinations?: SocialRemarkMessageDestination[]
}

/**
Expand All @@ -27,7 +29,8 @@ export class SocialRemarkConfig {
'NRG_GEN_OK',
'NRG_GEN_REFUND'
],
versions: ['0.1']
versions: ['0.1'],
destinations: ['1', '2', '3']
}

static getInstance(): SocialRemarkConfig {
Expand Down
46 changes: 33 additions & 13 deletions packages/utils/src/socialRemark/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
REMARK_CONTENT_VERSION_ACTION_MAP,
RemarkContentProps,
SocialRemarkMessage,
SocialRemarkMessageAction,
SocialRemarkMessageAction, SocialRemarkMessageDestination,
SocialRemarkMessageProtocolName,
SocialRemarkMessageVersion,
SubSclSource
Expand Down Expand Up @@ -34,24 +34,29 @@ export class SocialRemark {
SocialRemarkConfig.getInstance().config.versions
)

private destinations: Set<SocialRemarkMessageDestination> = new Set(
SocialRemarkConfig.getInstance().config.destinations
)

private actions: Set<SocialRemarkMessageAction> = new Set(
SocialRemarkConfig.getInstance().config.actions
)


private msgDelimiter: string = '::'

/**
* Get SocialRemark full data, if it's available. Otherwise, go throw error.
*/
public get message():
public get source():
| SocialRemarkMessage<SocialRemarkMessageAction, boolean>
| never {
if (!this.msgParsed) throw new Error('Message is not available.')
return this.msgParsed!
}

/**
* Get SocialRemark instance message content, if it's valid and available.
* Get SocialRemark instance source content, if it's valid and available.
*/
public get content() {
return this.msgParsed && this.msgParsed.valid
Expand Down Expand Up @@ -126,30 +131,31 @@ export class SocialRemark {
}

/**
* Encode SocialRemark message to remark string.
* Encode SocialRemark source to remark string.
*/
public toMessage(): string {
if (!this.isValidMessage)
throw new Error('Remark is not valid for build message.')

const msg: string[] = []
msg.push(this.message.protName)
msg.push(this.message.version)
msg.push(this.message.action)
msg.push(this.source.protName)
msg.push(this.source.version)
msg.push(this.source.destination)
msg.push(this.source.action)

try {
const contentPropsMap =
// @ts-ignore
REMARK_CONTENT_VERSION_ACTION_MAP[this.message.version][
this.message.action
REMARK_CONTENT_VERSION_ACTION_MAP[this.source.version][
this.source.action
]
for (const contentPropName in contentPropsMap) {
// @ts-ignore
msg[contentPropsMap[contentPropName]] = decorateRemarkContentValue(
this.message.action,
this.source.action,
contentPropName as RemarkContentProps,
// @ts-ignore
this.message.content[contentPropName]
this.source.content[contentPropName]
)
}
} catch (e) {
Expand Down Expand Up @@ -177,14 +183,16 @@ export class SocialRemark {
chunkedMsg.length === 0 ||
!this.isValidProtName(chunkedMsg[0]) ||
!this.isValidVersion(chunkedMsg[1]) ||
!this.isValidAction(chunkedMsg[2])
!this.isValidDestination(chunkedMsg[2]) ||
!this.isValidAction(chunkedMsg[3])
)
return

this.msgParsed = {
protName: chunkedMsg[0] as SocialRemarkMessageProtocolName,
version: chunkedMsg[1] as SocialRemarkMessageVersion,
action: chunkedMsg[2] as SocialRemarkMessageAction,
destination: chunkedMsg[2] as SocialRemarkMessageDestination,
action: chunkedMsg[3] as SocialRemarkMessageAction,
valid: false,
content: null
}
Expand Down Expand Up @@ -220,6 +228,9 @@ export class SocialRemark {
private isValidVersion(src: string): boolean {
return !!(src && this.versions.has(src as SocialRemarkMessageVersion))
}
private isValidDestination(src: string): boolean {
return !!(src && this.destinations.has(src as SocialRemarkMessageDestination))
}
private isValidAction(src: string): boolean {
return !!(src && this.actions.has(src as SocialRemarkMessageAction))
}
Expand Down Expand Up @@ -250,6 +261,15 @@ export class SocialRemark {
)}"`
)

if (!this.isValidDestination(rmrkSrc.destination))
throw new Error(
`Remark source is invalid - destination "${
rmrkSrc.destination
}" has been provided but expected - "${[...this.destinations.keys()].join(
' || '
)}"`
)

if (!this.isValidAction(rmrkSrc.action))
throw new Error(
`Remark source is invalid - action "${
Expand Down
1 change: 1 addition & 0 deletions packages/utils/src/socialRemark/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export {
SubSclSource,
SocialRemarkMessageContent,
SocialRemarkMessageProtocolName,
SocialRemarkDestChainsNameId,
SocialRemarkMessageAction,
SocialRemarkMessageVersion,
RemarkContentProps,
Expand Down
56 changes: 32 additions & 24 deletions packages/utils/src/socialRemark/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export type RemarkContentProps =
| keyof EnergyGenerateRefundContent;

export type SocialRemarkMessageVersion = '0.1';
export type SocialRemarkMessageDestination = '1' | '2' | '3';
export type SocialRemarkMessageAction =
| 'DMN_REG'
| 'DMN_REG_OK'
Expand All @@ -59,6 +60,12 @@ export type SocialRemarkMessageProtocolName =
| 'social'
| string;

export enum SocialRemarkDestChainsNameId {
subsocial = '1',
xsocial = '2',
soonsocial = '3',
}

export type SocialRemarkMessageContent<
A extends SocialRemarkMessageAction | string
> = A extends 'DMN_REG'
Expand All @@ -81,6 +88,7 @@ export type SocialRemarkMessage<
> = {
protName: SocialRemarkMessageProtocolName;
version: SocialRemarkMessageVersion;
destination: SocialRemarkMessageDestination;
action: A;
valid: V; // TODO make this prop optional
content: V extends true ? SocialRemarkMessageContent<A> : null;
Expand All @@ -102,40 +110,40 @@ type VersionActionPropsMap = Record<
export const REMARK_CONTENT_VERSION_ACTION_MAP: VersionActionPropsMap = {
'0.1': {
DMN_REG: {
opId: 3,
target: 4,
domainName: 5,
token: 6
opId: 4,
target: 5,
domainName: 6,
token: 7
},
DMN_REG_OK: {
opId: 3,
target: 4,
domainName: 5,
token: 6
opId: 4,
target: 5,
domainName: 6,
token: 7
},
DMN_REG_REFUND: {
opId: 3,
target: 4,
domainName: 5,
token: 6
opId: 4,
target: 5,
domainName: 6,
token: 7
},
NRG_GEN: {
opId: 3,
target: 4,
energyAmount: 5,
token: 6
opId: 4,
target: 5,
energyAmount: 6,
token: 7
},
NRG_GEN_OK: {
opId: 3,
target: 4,
energyAmount: 5,
token: 6
opId: 4,
target: 5,
energyAmount: 6,
token: 7
},
NRG_GEN_REFUND: {
opId: 3,
target: 4,
energyAmount: 5,
token: 6
opId: 4,
target: 5,
energyAmount: 6,
token: 7
}
}
};
Loading

0 comments on commit a8a3198

Please sign in to comment.