-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathtransfer-fee-burning.test.ts
232 lines (209 loc) · 6.37 KB
/
transfer-fee-burning.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import * as anchor from "@coral-xyz/anchor";
import * as spl from "@solana/spl-token";
import {
SystemProgram,
Transaction,
sendAndConfirmTransaction,
} from "@solana/web3.js";
import {
AccountAddress,
ChainAddress,
ChainContext,
Signer,
UniversalAddress,
Wormhole,
contracts,
encoding,
} from "@wormhole-foundation/sdk";
import * as testing from "@wormhole-foundation/sdk-definitions/testing";
import {
SolanaPlatform,
getSolanaSignAndSendSigner,
} from "@wormhole-foundation/sdk-solana";
import * as fs from "fs";
import { SolanaNtt } from "../ts/sdk/index.js";
import { handleTestSkip, signSendWait } from "./utils/index.js";
handleTestSkip(__filename);
const solanaRootDir = `${__dirname}/../`;
const CORE_BRIDGE_ADDRESS = contracts.coreBridge("Mainnet", "Solana");
const NTT_ADDRESS = anchor.workspace.ExampleNativeTokenTransfers.programId;
const w = new Wormhole("Devnet", [SolanaPlatform], {
chains: { Solana: { contracts: { coreBridge: CORE_BRIDGE_ADDRESS } } },
});
const remoteXcvr: ChainAddress = {
chain: "Ethereum",
address: new UniversalAddress(
encoding.bytes.encode("transceiver".padStart(32, "\0"))
),
};
const remoteMgr: ChainAddress = {
chain: "Ethereum",
address: new UniversalAddress(
encoding.bytes.encode("nttManager".padStart(32, "\0"))
),
};
const receiver = testing.utils.makeUniversalChainAddress("Ethereum");
const payerSecretKey = Uint8Array.from(
JSON.parse(
fs.readFileSync(`${solanaRootDir}/keys/test.json`, {
encoding: "utf-8",
})
)
);
const payer = anchor.web3.Keypair.fromSecretKey(payerSecretKey);
const connection = new anchor.web3.Connection(
"http://localhost:8899",
"confirmed"
);
// make sure we're using the exact same Connection obj for rpc
const ctx: ChainContext<"Devnet", "Solana"> = w
.getPlatform("Solana")
.getChain("Solana", connection);
const mintAuthority = anchor.web3.Keypair.generate();
const mintKeypair = anchor.web3.Keypair.generate();
const mint = mintKeypair.publicKey;
const transferFeeConfigAuthority = anchor.web3.Keypair.generate();
const withdrawWithheldAuthority = anchor.web3.Keypair.generate();
const decimals = 9;
const feeBasisPoints = 50;
const maxFee = BigInt(5_000);
const mintAmount = BigInt(1_000_000_000);
const transferAmount = 100_000n;
let signer: Signer;
let sender: AccountAddress<"Solana">;
let ntt: SolanaNtt<"Devnet", "Solana">;
let tokenAccount: anchor.web3.PublicKey;
let tokenAddress: string;
const TOKEN_PROGRAM = spl.TOKEN_2022_PROGRAM_ID;
describe("example-native-token-transfers", () => {
describe("Transfer Fee Burning", () => {
beforeAll(async () => {
try {
signer = await getSolanaSignAndSendSigner(connection, payer, {
//debug: true,
});
sender = Wormhole.parseAddress("Solana", signer.address());
// initialize mint
const extensions = [spl.ExtensionType.TransferFeeConfig];
const mintLen = spl.getMintLen(extensions);
const lamports = await connection.getMinimumBalanceForRentExemption(
mintLen
);
const transaction = new Transaction().add(
SystemProgram.createAccount({
fromPubkey: payer.publicKey,
newAccountPubkey: mint,
space: mintLen,
lamports,
programId: TOKEN_PROGRAM,
}),
spl.createInitializeTransferFeeConfigInstruction(
mint,
transferFeeConfigAuthority.publicKey,
withdrawWithheldAuthority.publicKey,
feeBasisPoints,
maxFee,
TOKEN_PROGRAM
),
spl.createInitializeMintInstruction(
mint,
decimals,
mintAuthority.publicKey,
null,
TOKEN_PROGRAM
)
);
await sendAndConfirmTransaction(
connection,
transaction,
[payer, mintKeypair],
undefined
);
// create and fund token account
tokenAccount = await spl.createAccount(
connection,
payer,
mint,
payer.publicKey,
undefined,
undefined,
TOKEN_PROGRAM
);
await spl.mintTo(
connection,
payer,
mint,
tokenAccount,
mintAuthority,
mintAmount,
[],
undefined,
TOKEN_PROGRAM
);
// create our contract client
tokenAddress = mint.toBase58();
ntt = new SolanaNtt("Devnet", "Solana", connection, {
...ctx.config.contracts,
ntt: {
token: tokenAddress,
manager: NTT_ADDRESS,
transceiver: { wormhole: NTT_ADDRESS },
},
});
// transfer mint authority to ntt
await spl.setAuthority(
connection,
payer,
mint,
mintAuthority,
spl.AuthorityType.MintTokens,
ntt.pdas.tokenAuthority(),
[],
undefined,
TOKEN_PROGRAM
);
// init
const initTxs = ntt.initialize(sender, {
mint,
outboundLimit: 100_000_000n,
mode: "burning",
});
await signSendWait(ctx, initTxs, signer);
// register
const registerTxs = ntt.registerTransceiver({
payer,
owner: payer,
transceiver: ntt.program.programId,
});
await signSendWait(ctx, registerTxs, signer);
// set Wormhole xcvr peer
const setXcvrPeerTxs = ntt.setWormholeTransceiverPeer(
remoteXcvr,
sender
);
await signSendWait(ctx, setXcvrPeerTxs, signer);
// set manager peer
const setPeerTxs = ntt.setPeer(remoteMgr, 18, 10_000_000n, sender);
await signSendWait(ctx, setPeerTxs, signer);
} catch (e) {
console.error("Failed to setup peer: ", e);
throw e;
}
});
it("Returns with error", async () => {
// TODO: keep or remove the `outboxItem` param?
// added as a way to keep tests the same but it technically breaks the Ntt interface
const outboxItem = anchor.web3.Keypair.generate();
const xferTxs = ntt.transfer(
sender,
transferAmount,
receiver,
{ queue: false, automatic: false, gasDropoff: 0n },
outboxItem
);
await expect(
signSendWait(ctx, xferTxs, signer, false, true)
).rejects.toThrow();
});
});
});