waves-transactions
Using this library you can easily create and sign transactions for Waves blockchain.
It also allows you to multi-sign existing transactions or create them without signature at all.
Transactions
Creation
Transactions are created via transaction creating functions. There are 15 of them:
const {
alias, burn, cancelLease, data, exchange,
invokeScript, issue, lease, massTransfer, reissue,
setAssetScript, setScript, sponsorship, transfer, updateAssetInfo
} = require('@waves/waves-transactions')
Example:
const issueTx = issue({
name: 'foo',
description: 'bar',
quantity: 10000,
senderPublicKey: 'GucCLYU7aqzcVUwVXX4nosceDisky9UpbmpFK39tVYom',
chainId: 'T'
})
const burnTx = burn({
assetId: '6toKooURvF3CpRQV8hzhNbHjK3Rb3L9Krd7TFnzcoe8L',
senderPublicKey: 'GucCLYU7aqzcVUwVXX4nosceDisky9UpbmpFK39tVYom',
chainId: 'T'
})
Signing
You can provide seed or private key to transaction creating function to sign it. If you do, senderPublicKey can be omitted.
const signedTranfer = transfer({
recipient:'3N4mLCaHq2twRKnbUjdvAHyXjoccQE9KDRE',
amount: 100000}, 'secret seed phraze'
)
const signedTranferViaPrivateKey = transfer({
recipient:'3N4mLCaHq2twRKnbUjdvAHyXjoccQE9KDRE',
amount: 100000}, {privateKey: 'GucCLYU7aqzcVUwVXX4nosceDisky9UpbmpFK39tVYom'}
)
Params
Type LONG represents string or number. Strings are allowed since max js int is 2**53
Common params:
Present in all transactions
interface IBasicParams<LONG = string | number> {
fee?: LONG
additionalFee?: number
senderPublicKey?: string
timestamp?: number
chainId?: string | number
}
Issue transaction. Type 3
export interface IIssueParams<LONG = string | number> extends IBasicParams<LONG> {
name: string
description: string
quantity: LONG
decimals?: number
reissuable?: boolean
script?: string
}
Transfer transaction. Type 4
export interface ITransferParams<LONG = string | number> extends IBasicParams<LONG> {
recipient: string
amount: LONG
assetId?: string | null
feeAssetId?: string | null
attachment?: string | TTypedData
}
Reissue transaction. Type 5
export interface IReissueParams<LONG = string | number> extends IBasicParams<LONG> {
assetId: string
quantity: LONG
reissuable: boolean
}
Burn transaction. Type 6
export interface IBurnParams<LONG = string | number> extends IBasicParams<LONG> {
assetId: string
quantity: LONG
}
Exchange transaction. Type 7
Exchange transactions are used by DEX mather. If you want to create your own exchange transaction,
there is no params. You need to construct it by hand(see interface below, IOrder is described in Order section)
export interface IExchangeTransaction<LONG = string | number> extends ITransaction<LONG> {
type: 7
order1: IOrder
order2: IOrder
price: LONG
amount: LONG
buyMatcherFee: LONG
sellMatcherFee: LONG
}
Lease transaction. Type 8
export interface ILeaseParams<LONG = string | number> extends IBasicParams<LONG> {
recipient: string
amount: LONG
}
CancelLease transaction. Type 9
export interface ICancelLeaseParams<LONG = string | number> extends IBasicParams<LONG> {
leaseId: string
}
Alias transaction. Type 10
export interface IAliasParams<LONG = string | number> extends IBasicParams<LONG> {
alias: string
}
MassTransfer transaction. Type 11
export interface IMassTransferParams<LONG = string | number> extends IBasicParams<LONG> {
transfers: IMassTransferItem[]
attachment?: string | TTypedData
assetId?: string | null
}
export interface IMassTransferItem<LONG = string | number> {
recipient: string
amount: LONG
}
Data transaction. Type 12
export interface IDataParams<LONG = string | number> extends IBasicParams<LONG> {
data: Array<IBooleanData | IIntegerData | IStringData | IBinaryData| TDeleteRequest>
}
export type TDeleteRequest = {
type?: null
value?: null
key: string
}
export interface IBooleanData {
key: string
type: 'boolean'
value: boolean
}
export interface IIntegerData<LONG = string | number> {
key: string
type: 'integer'
value: LONG
}
export interface IStringData {
key: string
type: 'string'
value: string
}
export interface IBinaryData {
key: string
type: 'binary'
value: string
}
SetScript transaction. Type 13
export interface ISetScriptParams<LONG = string | number> extends IBasicParams<LONG> {
script: string | null
}
export interface ISponsorshipParams<LONG = string | number> extends IBasicParams<LONG> {
assetId: string
minSponsoredAssetFee: LONG
}
SetAssetScript transaction. Type 15
export interface ISetAssetScriptParams<LONG = string | number> extends IBasicParams<LONG> {
script: string
assetId: string
}
InvokeScript transaction. Type 16
export interface IInvokeScriptParams<LONG = string | number> extends IBasicParams<LONG> {
dApp: string
feeAssetId?: string | null
call?: {
function: string
args?: {
type: 'binary' | 'integer' | 'boolean' | 'string',
value: string | LONG | boolean
}[]
},
payment?: {
assetId?: string | null
amount: LONG
}[]
}
UpdateAssetInfo transaction. Type 17
export interface IUpdateAssetInfoParams<LONG = string | number> extends IBasicParams<LONG> {
assetId: string
name: string
description: string
}
If you want to create the minimum you need to provide is amount and recipient as defined in Transfer params:
const { transfer } = require('@waves/waves-transactions')
const seed = 'some example seed phrase'
const signedTranserTx = transfer({
amount: 1,
recipient: '3N4mLCaHq2twRKnbUjdvAHyXjoccQE9KDRE',
timestamp: 1536917842558
}, seed)
const signedTranserTx = transfer({
amount: 1,
recipient: 'alias:W:aliasForMyAddress'
}, seed)
Output will be a signed transfer transaction:
{
id: '8NrUwgKRCMFbUbqXKQAHkGnspmWHEjKUSi5opEC6Havq',
type: 4,
version: 2,
recipient: '3N4mLCaHq2twRKnbUjdvAHyXjoccQE9KDRE',
attachment: undefined,
feeAssetId: undefined,
assetId: undefined,
amount: 1,
fee: 100000,
senderPublicKey: '6nR7CXVV7Zmt9ew11BsNzSvVmuyM5PF6VPbWHW9BHgPq',
timestamp: 1536917842558,
proofs: [
'25kyX6HGjS3rkPTJRj5NVH6LLuZe6SzCzFtoJ8GDkojY9U5oPfVrnwBgrCHXZicfsmLthPUjTrfT9TQL2ciYrPGE'
]
}
You can also create transaction, but not sign it:
const unsignedTransferTx = transfer({
amount: 1,
recipient: '3N4mLCaHq2twRKnbUjdvAHyXjoccQE9KDRE',
senderPublicKey: '6nR7CXVV7Zmt9ew11BsNzSvVmuyM5PF6VPbWHW9BHgPq'
})
Now you are able to POST it to Waves API or store for future purpose or you can add another signature from other party:
const otherPartySeed = 'other party seed phrase'
const transferSignedWithTwoParties = transfer(signedTranserTx, seed)
So now there are two proofs:
{
id: '8NrUwgKRCMFbUbqXKQAHkGnspmWHEjKUSi5opEC6Havq',
type: 4,
version: 2,
recipient: '3N4mLCaHq2twRKnbUjdvAHyXjoccQE9KDRE',
attachment: undefined,
feeAssetId: undefined,
assetId: undefined,
amount: 1,
fee: 100000,
senderPublicKey: '6nR7CXVV7Zmt9ew11BsNzSvVmuyM5PF6VPbWHW9BHgPq',
timestamp: 1536917842558,
proofs: [
'25kyX6HGjS3rkPTJRj5NVH6LLuZe6SzCzFtoJ8GDkojY9U5oPfVrnwBgrCHXZicfsmLthPUjTrfT9TQL2ciYrPGE',
'CM9emPzpe6Ram7ZxcYax6s7Hkw6698wXCMPSckveFAS2Yh9vqJpy1X9nL7p4RKgU3UEa8c9RGXfUK6mFFq4dL9z'
]
}
Orders
Order is created the same way as transaction
const { order } = require('@waves/waves-transactions')
const params = {
amount: 100000000,
price: 10,
priceAsset: '8LQW8f7P5d5PZM7GtZEBgaqRPGSzS3DfPuiXrURJ4AJS',
matcherPublicKey: '7kPFrHDiGw1rCm7LPszuECwWYL3dMf6iMifLRDJQZMzy',
orderType: 'buy'
}
const signedOrder = order(params, 'Some seed ')
Broadcast
To send transaction you can use either node REST API or broadcast helper function:
const {broadcast} = require('@waves/waves-transactions');
const nodeUrl = 'https://nodes.wavesplatform.com';
broadcast(signedTx, nodeUrl).then(resp => console.log(resp))
You can send tx to any waves node you like:. E.g.:
Important!!!
Most transactions require chainId as parameter, e.g: IBurnParams. By default chainId is 'W', which means MAINNET. To make transaction in TESTNET be sure to pass chainId if it is present in params interface and then send it to TESTNET node
Dependencies
This library uses @waves/ts-lib-crypto
for cryptography and @waves/node-api-js
for interacting with node.
You can access them this way:
const libCrypto = require('@waves/waves-transactions').libs.crypto
const libApi = require('@waves/waves-transactions').libs.nodeApiJs