Skip to content

Commit

Permalink
add typed smart message args
Browse files Browse the repository at this point in the history
  • Loading branch information
tsmbl committed Apr 11, 2024
1 parent 8d30435 commit f6388e0
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 21 deletions.
115 changes: 115 additions & 0 deletions packages/blockchain-sdk-solana/examples/exmple-actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// Considerations
// 1. Smart message should encapsulate all possible actions, including link and sign transaction - this is needed to be interoperable in different clients
// 2. Multiple actions should be later supported (e.g. both link and sign transaction), based on mockups e.g. https://www.figma.com/file/YMrtyevM6MlWYDZBO2fLb4/Use-Case-Examples?type=design&node-id=1-820&mode=dev
// 3. Single smart message should be produced if multiple actions exist, we should forbid using multiple tx-services in a context of single actionable notif
import { DappMessageActionType, type SmartMessageAction } from '@dialectlabs/sdk/src';
import type { DappMessages } from '../../sdk/src/';
// @ts-ignore

const dappMessages = (1 as DappMessages);
const buyNftSmartMessage: SmartMessageAction = {
type: DappMessageActionType.SMART_MESSAGE,
smartMessage: {
transactionServiceId: 'tensor-nft-buy',
transactionParams: {
collectionId: 'foo',
mintAddress: 'bar',
owner: 'foo',
price: 'foo',
priceWithFeeAndRoyalty: 'foo',
},
},

};
const tokenTransferSmartMessage: SmartMessageAction = {
type: DappMessageActionType.SMART_MESSAGE,
smartMessage: {
transactionServiceId: 'tensor-nft-buy',
transactionParams: {
collectionId: 'foo',
mintAddress: 'bar',
owner: 'foo',
price: 'foo',
priceWithFeeAndRoyalty: 'foo',
},
},
};
// Examples:
// 1. SENDING SIGN_TRANSACTION ACTIONS, including possible multiple actions
// a) W/O TX_SERVICE_ID NOT ALLOWED
dappMessages.send({
actionsV3: {
type: DappMessageActionType.SMART_MESSAGE,
smartMessage: {
transactionParams: {
payer: 'foo',
payee: 'bar',
amount: 'foo',
links: [{
label: 'foo',
url: 'foo',
}],
},
},
},
message: 'foo',
});
// b) multiple tx services not allowed
dappMessages.send({
actionsV3: {
serviceId: 'foo',
actions: [
buyNftSmartMessage,
],
},
message: 'foo',
});
// d) SINGLE TX_SERVICE ID ALLOWED
dappMessages.send({
actionsV3: buyNftSmartMessage,
message: 'foo',
});dappMessages.send({
actionsV3: tokenTransferSmartMessage,
message: 'foo',
});


// 2. SENDING LINK ACTIONS
// a) MULTIPLE W/O TX_SERVICE_ID ARE NOT ALLOWED
dappMessages.send({
actionsV3: {
actions: [{
type: DappMessageActionType.LINK,
label: '',
url: 'd',
}, {
type: DappMessageActionType.LINK,
label: '',
url: 'd',
}],
},
message: 'foo',
});// b) SINGLE link ALLOWED
dappMessages.send({
actionsV3: {
actions: [{
type: DappMessageActionType.LINK,
label: 'foo',
url: 'foo',
}],
},
message: 'foo',
});


// 3. SENDING MIXED ACTIONS
dappMessages.send({
actionsV3: {
actions: [{
type: DappMessageActionType.LINK,
label: 'foo',
url: 'foo',
}],
},
message: 'foo',
});
2 changes: 1 addition & 1 deletion packages/sdk-actions-spec/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './transactions/tensor/tensor-transactions-spec';
export * from './transactions/transaction-action-spec';
export * from './transactions/tensor/token-transfer';
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import type { TransactionParams } from '../transaction-action-spec';
import type { SmartMessage, SmartMessageAction, SmartMessageParams } from '@dialectlabs/sdk/src'; //TODO: upd import

export interface NftBuyTransaction extends TransactionParams {

export interface TensorNftBuySmartMessage extends SmartMessage {
transactionServiceId: 'tensor-nft-buy';
transactionParams: NftBuyTransactionParams;
}

export interface NftBuyTransactionParams extends SmartMessageParams{
mintAddress: string;
collectionId: string;
price: string;
Expand All @@ -9,4 +15,4 @@ export interface NftBuyTransaction extends TransactionParams {
imageUrl?: string;
collectionName?: string;
nftName?: string;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { SmartMessage, SmartMessageParams } from '@dialectlabs/sdk/src'; //TODO: upd import


export interface TokenTransferSmartMessage extends SmartMessage {
transactionServiceId: 'tensor-nft-buy';
transactionParams: TokenTransferSmartMessageParams;
}

export interface TokenTransferSmartMessageParams extends SmartMessageParams {
payer: string;
payee: string;
amount: string;
links: {
label: string,
url: string
}[]
}

This file was deleted.

44 changes: 28 additions & 16 deletions packages/sdk/src/dapp/dapp.interface.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import type { AddressType, DappAddress } from '../address/addresses.interface';
import type {
NotificationConfig,
NotificationSubscription,
NotificationType,
} from '../wallet/wallet.interface';
import type { NotificationConfig, NotificationSubscription, NotificationType } from '../wallet/wallet.interface';
import type { AccountAddress } from '../auth/auth.interface';

export interface Dapps {
Expand Down Expand Up @@ -72,9 +68,9 @@ export interface FindDappQuery {
blockchainType?: BlockchainType;
}

export interface DappMessageTransaction {
transactionServiceId: string;
transactionParams: Record<string, any>;

export interface SmartMessageParams { // TODO: Just a marker interface for now??

}

Check failure on line 75 in packages/sdk/src/dapp/dapp.interface.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

An empty interface is equivalent to `{}`
export interface DappMessageAction {
Expand All @@ -84,38 +80,53 @@ export interface DappMessageAction {

export enum DappMessageActionType {
LINK = 'Link',
SIGN_TRANSACTION = 'SignTransaction',
SMART_MESSAGE = 'SignTransaction',
}

export interface DappMessageActionV2 {
interface DappMessageActionV2Base {
type: DappMessageActionType;
}

export interface DappMessageLinkAction extends DappMessageActionV2 {

export interface DappMessageLinkAction extends DappMessageActionV2Base {
type: DappMessageActionType.LINK;
label: string;
url: string;
}

export interface DappMessageSignTransactionAction extends DappMessageActionV2 {
type: DappMessageActionType.SIGN_TRANSACTION;
transaction: DappMessageTransaction;
export interface SmartMessageAction extends DappMessageActionV2Base {
type: DappMessageActionType.SMART_MESSAGE;
smartMessage: SmartMessage;
}
export interface SmartMessage {
transactionServiceId: string;
transactionParams: SmartMessageParams;
}


export interface SendDappMessageCommandBase {
message: string;
title?: string;
notificationTypeId?: string;
addressTypes?: AddressType[];
// tags?: string[];
actions?: DappMessageAction[];
actions?: DappMessageAction[]; // TODO: deprecate it, I think it's ok to intro breaking change since only tensor and our dashboard uses it atm
}

export type BroadcastDappMessageCommand = SendDappMessageCommandBase;

export type ActionsV3 = LinkOnlyActions | SmartMessageAction;

export interface LinkOnlyActions {
actions: [DappMessageLinkAction];
// actions: [DappMessageLinkAction]; // TODO: we can allow single action in compile time using this notation
}


export interface UnicastDappMessageCommand extends SendDappMessageCommandBase {
recipient: AccountAddress;
actionsV2: DappMessageActionV2[];
// actionsV2: DappMessageActionV2[];
actionsV3: ActionsV3;
}

export interface MulticastDappMessageCommand
Expand Down Expand Up @@ -169,3 +180,4 @@ export class DappNotificationSubscription {
notificationType!: NotificationType;
subscriptions!: NotificationSubscription[];
}

0 comments on commit f6388e0

Please sign in to comment.