-
Notifications
You must be signed in to change notification settings - Fork 8
/
create.ts
57 lines (50 loc) · 1.51 KB
/
create.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { TransactionInstruction } from '@solana/web3.js';
import { TransactionBuilder } from '~/transaction-builder';
import { Constants, Result, Try } from '~/suite-utils';
import { Secret } from '~/types/account';
import { MemoOptions } from '~/types/memo';
import bs from 'bs58';
import { CommonStructure } from '~/types/transaction-builder';
export namespace Memo {
export const decode = (encoded: string): string =>
bs.decode(encoded).toString();
export const encode = (data: string): Buffer => Buffer.from(data);
/**
* Create memo
*
* @param {string} data // memo data
* @param {Secret} owner // memo owner
* @param {Partial<DelegateOptions>} options
* @return Promise<Result<Transaction, Error>>
* @module Memo
*/
export const create = (
data: string,
owner: Secret,
options: Partial<MemoOptions> = {},
): Result<CommonStructure, Error> => {
return Try(() => {
const feePayer = options.feePayer;
const key = owner.toKeypair().publicKey
? [
{
pubkey: owner.toKeypair().publicKey,
isSigner: true,
isWritable: true,
},
]
: [];
const instruction = new TransactionInstruction({
programId: Constants.MEMO_PROGRAM_ID,
data: encode(data),
keys: key,
});
const payer = feePayer || owner;
return new TransactionBuilder.Common(
[instruction],
[owner.toKeypair()],
payer.toKeypair(),
);
});
};
}