-
Notifications
You must be signed in to change notification settings - Fork 11.1k
/
txn-builder.test.ts
218 lines (197 loc) · 6.5 KB
/
txn-builder.test.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0
import { describe, it, expect, beforeAll } from 'vitest';
import {
getExecutionStatusType,
getNewlyCreatedCoinRefsAfterSplit,
getObjectId,
LocalTxnDataSerializer,
RawSigner,
} from '../../src';
import {
DEFAULT_RECIPIENT,
DEFAULT_GAS_BUDGET,
SUI_SYSTEM_STATE_OBJECT_ID,
setup,
TestToolbox,
DEFAULT_RECIPIENT_2,
} from './utils/setup';
describe.each([{ useLocalTxnBuilder: true }, { useLocalTxnBuilder: false }])(
'Transaction Builders',
({ useLocalTxnBuilder }) => {
let toolbox: TestToolbox;
let signer: RawSigner;
beforeAll(async () => {
toolbox = await setup();
signer = new RawSigner(
toolbox.keypair,
toolbox.provider,
useLocalTxnBuilder
? new LocalTxnDataSerializer(toolbox.provider)
: undefined
);
});
it('Split coin', async () => {
const coins = await toolbox.provider.getGasObjectsOwnedByAddress(
toolbox.address()
);
const txn = await signer.splitCoin({
coinObjectId: coins[0].objectId,
splitAmounts: [DEFAULT_GAS_BUDGET * 2],
gasBudget: DEFAULT_GAS_BUDGET,
});
expect(getExecutionStatusType(txn)).toEqual('success');
});
it('Merge coin', async () => {
const coins = await toolbox.provider.getGasObjectsOwnedByAddress(
toolbox.address()
);
const txn = await signer.mergeCoin({
primaryCoin: coins[0].objectId,
coinToMerge: coins[1].objectId,
gasBudget: DEFAULT_GAS_BUDGET,
});
expect(getExecutionStatusType(txn)).toEqual('success');
});
it('Move Call', async () => {
const coins = await toolbox.provider.getGasObjectsOwnedByAddress(
toolbox.address()
);
const txn = await signer.executeMoveCall({
packageObjectId: '0x2',
module: 'devnet_nft',
function: 'mint',
typeArguments: [],
arguments: [
'Example NFT',
'An NFT created by the wallet Command Line Tool',
'ipfs://bafkreibngqhl3gaa7daob4i2vccziay2jjlp435cf66vhono7nrvww53ty',
],
gasBudget: DEFAULT_GAS_BUDGET,
gasPayment: coins[0].objectId,
});
expect(getExecutionStatusType(txn)).toEqual('success');
});
it('Move Shared Object Call', async () => {
const coins = await toolbox.provider.getGasObjectsOwnedByAddress(
toolbox.address()
);
const validators = await toolbox.getActiveValidators();
const validator_metadata = (validators[0] as SuiMoveObject).fields
.metadata;
const validator_address = (validator_metadata as SuiMoveObject).fields
.sui_address;
const txn = await signer.executeMoveCall({
packageObjectId: '0x2',
module: 'sui_system',
function: 'request_add_delegation',
typeArguments: [],
arguments: [
SUI_SYSTEM_STATE_OBJECT_ID,
coins[2].objectId,
validator_address,
],
gasBudget: DEFAULT_GAS_BUDGET,
gasPayment: coins[3].objectId,
});
expect(getExecutionStatusType(txn)).toEqual('success');
});
it('Transfer Sui', async () => {
const coins = await toolbox.provider.getGasObjectsOwnedByAddress(
toolbox.address()
);
const txn = await signer.transferSui({
suiObjectId: coins[0].objectId,
gasBudget: DEFAULT_GAS_BUDGET,
recipient: DEFAULT_RECIPIENT,
amount: 100,
});
expect(getExecutionStatusType(txn)).toEqual('success');
});
it('Transfer Object', async () => {
const coins = await toolbox.provider.getGasObjectsOwnedByAddress(
toolbox.address()
);
const txn = await signer.transferObject({
objectId: coins[0].objectId,
gasBudget: DEFAULT_GAS_BUDGET,
recipient: DEFAULT_RECIPIENT,
gasPayment: coins[1].objectId,
});
expect(getExecutionStatusType(txn)).toEqual('success');
});
it('Pay', async () => {
const coins =
await toolbox.provider.selectCoinsWithBalanceGreaterThanOrEqual(
toolbox.address(),
BigInt(DEFAULT_GAS_BUDGET)
);
// get some new coins with small amount
const splitTxn = await signer.splitCoin({
coinObjectId: getObjectId(coins[0]),
splitAmounts: [1, 2, 3],
gasBudget: DEFAULT_GAS_BUDGET,
gasPayment: getObjectId(coins[1]),
});
const splitCoins = getNewlyCreatedCoinRefsAfterSplit(splitTxn)!.map((c) =>
getObjectId(c)
);
// use the newly created coins as the input coins for the pay transaction
const txn = await signer.pay({
inputCoins: splitCoins,
gasBudget: DEFAULT_GAS_BUDGET,
recipients: [DEFAULT_RECIPIENT, DEFAULT_RECIPIENT_2],
amounts: [4, 2],
gasPayment: getObjectId(coins[2]),
});
expect(getExecutionStatusType(txn)).toEqual('success');
});
it('PaySui', async () => {
const gasBudget = 1000;
const coins =
await toolbox.provider.selectCoinsWithBalanceGreaterThanOrEqual(
toolbox.address(),
BigInt(DEFAULT_GAS_BUDGET)
);
const splitTxn = await signer.splitCoin({
coinObjectId: getObjectId(coins[0]),
splitAmounts: [2000, 2000, 2000],
gasBudget: gasBudget,
gasPayment: getObjectId(coins[1]),
});
const splitCoins = getNewlyCreatedCoinRefsAfterSplit(splitTxn)!.map((c) =>
getObjectId(c)
);
const txn = await signer.paySui({
inputCoins: splitCoins,
recipients: [DEFAULT_RECIPIENT, DEFAULT_RECIPIENT_2],
amounts: [1000, 1000],
gasBudget: gasBudget,
});
expect(getExecutionStatusType(txn)).toEqual('success');
});
it('PayAllSui', async () => {
const gasBudget = 1000;
const coins =
await toolbox.provider.selectCoinsWithBalanceGreaterThanOrEqual(
toolbox.address(),
BigInt(DEFAULT_GAS_BUDGET)
);
const splitTxn = await signer.splitCoin({
coinObjectId: getObjectId(coins[0]),
splitAmounts: [2000, 2000, 2000],
gasBudget: gasBudget,
gasPayment: getObjectId(coins[1]),
});
const splitCoins = getNewlyCreatedCoinRefsAfterSplit(splitTxn)!.map((c) =>
getObjectId(c)
);
const txn = await signer.payAllSui({
inputCoins: splitCoins,
recipient: DEFAULT_RECIPIENT,
gasBudget: gasBudget,
});
expect(getExecutionStatusType(txn)).toEqual('success');
});
}
);