Skip to content

Commit

Permalink
sdk: update deprecated solana web3.js methods (#515)
Browse files Browse the repository at this point in the history
* sdk: update deprecated solana web3.js methods

* use sendAndConfirmTransaction
  • Loading branch information
kev1n-peters authored Sep 23, 2024
1 parent 00f83aa commit d7fc284
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 53 deletions.
21 changes: 14 additions & 7 deletions solana/tests/anchor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ import {
import { SolanaWormholeCore } from "@wormhole-foundation/sdk-solana-core";
import * as fs from "fs";

import { PublicKey, SystemProgram, Transaction } from "@solana/web3.js";
import {
PublicKey,
sendAndConfirmTransaction,
SystemProgram,
Transaction,
} from "@solana/web3.js";
import { DummyTransferHook } from "../ts/idl/1_0_0/ts/dummy_transfer_hook.js";
import { SolanaNtt } from "../ts/sdk/index.js";

Expand Down Expand Up @@ -154,13 +159,14 @@ describe("example-native-token-transfers", () => {
)
);

const { blockhash } = await connection.getRecentBlockhash();
const { blockhash } = await connection.getLatestBlockhash();

transaction.feePayer = payer.publicKey;
transaction.recentBlockhash = blockhash;

const txid = await connection.sendTransaction(transaction, [payer, mint]);
await connection.confirmTransaction(txid, "confirmed");
await sendAndConfirmTransaction(connection, transaction, [payer, mint], {
commitment: "confirmed",
});

tokenAccount = await spl.createAssociatedTokenAccount(
connection,
Expand Down Expand Up @@ -266,12 +272,13 @@ describe("example-native-token-transfers", () => {
initializeExtraAccountMetaListInstruction
);
transaction.feePayer = payer.publicKey;
const { blockhash } = await connection.getRecentBlockhash();
const { blockhash } = await connection.getLatestBlockhash();
transaction.recentBlockhash = blockhash;

transaction.sign(payer);
const txid = await connection.sendTransaction(transaction, [payer]);
await connection.confirmTransaction(txid, "confirmed");
await sendAndConfirmTransaction(connection, transaction, [payer], {
commitment: "confirmed",
});
});

test("Can send tokens", async () => {
Expand Down
107 changes: 61 additions & 46 deletions solana/ts/sdk/ntt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
WormholeNttTransceiver,
} from "@wormhole-foundation/sdk-definitions-ntt";
import {
AnySolanaAddress,
AnySolanaAddress,
SolanaAddress,
SolanaChains,
SolanaPlatform,
Expand All @@ -46,16 +46,15 @@ import { NTT, NttQuoter, WEI_PER_GWEI } from "../lib/index.js";

import { IdlVersion, NttBindings, getNttProgram } from "../lib/bindings.js";

export class SolanaNttWormholeTransceiver<N extends Network, C extends SolanaChains>
implements NttTransceiver<N, C, WormholeNttTransceiver.VAA> {

constructor(
readonly manager: SolanaNtt<N, C>,
readonly address: PublicKey
) {}
export class SolanaNttWormholeTransceiver<
N extends Network,
C extends SolanaChains
> implements NttTransceiver<N, C, WormholeNttTransceiver.VAA>
{
constructor(readonly manager: SolanaNtt<N, C>, readonly address: PublicKey) {}

async getPauser(): Promise<AccountAddress<C> | null> {
return null
return null;
}

async *setPauser(_newPauser: AccountAddress<C>, _payer: AccountAddress<C>) {
Expand All @@ -71,7 +70,10 @@ export class SolanaNttWormholeTransceiver<N extends Network, C extends SolanaCha
}

getAddress(): ChainAddress<C> {
return { chain: this.manager.chain, address: toUniversal(this.manager.chain, this.address.toBase58()) };
return {
chain: this.manager.chain,
address: toUniversal(this.manager.chain, this.address.toBase58()),
};
}

async *setPeer(peer: ChainAddress<C>, payer: AccountAddress<C>) {
Expand All @@ -94,7 +96,8 @@ export class SolanaNttWormholeTransceiver<N extends Network, C extends SolanaCha
}

export class SolanaNtt<N extends Network, C extends SolanaChains>
implements Ntt<N, C> {
implements Ntt<N, C>
{
core: SolanaWormholeCore<N, C>;
pdas: NTT.Pdas;

Expand Down Expand Up @@ -149,7 +152,10 @@ export class SolanaNtt<N extends Network, C extends SolanaChains>
if (ix !== 0) return null;
if (this.whTransceiverAddress === undefined) return null;

return new SolanaNttWormholeTransceiver(this, new PublicKey(this.whTransceiverAddress));
return new SolanaNttWormholeTransceiver(
this,
new PublicKey(this.whTransceiverAddress)
);
}

async getMode(): Promise<Ntt.Mode> {
Expand Down Expand Up @@ -190,7 +196,7 @@ export class SolanaNtt<N extends Network, C extends SolanaChains>

async getThreshold(): Promise<number> {
const config = await this.getConfig();
return config.threshold
return config.threshold;
}

async getOwner(): Promise<AccountAddress<C>> {
Expand All @@ -199,7 +205,7 @@ export class SolanaNtt<N extends Network, C extends SolanaChains>
}

async getPauser(): Promise<AccountAddress<C> | null> {
return null
return null;
}

async *setOwner(newOwner: AnySolanaAddress, payer: AccountAddress<C>) {
Expand Down Expand Up @@ -280,12 +286,17 @@ export class SolanaNtt<N extends Network, C extends SolanaChains>
}

async getPeer<C extends Chain>(chain: C): Promise<Ntt.Peer<C> | null> {
const peer = await this.program.account.nttManagerPeer.fetchNullable(this.pdas.peerAccount(chain));
const peer = await this.program.account.nttManagerPeer.fetchNullable(
this.pdas.peerAccount(chain)
);

if (!peer) return null;

return {
address: { chain: chain, address: toUniversal(chain, new Uint8Array(peer.address)) },
address: {
chain: chain,
address: toUniversal(chain, new Uint8Array(peer.address)),
},
tokenDecimals: peer.tokenDecimals,
inboundLimit: await this.getInboundLimit(chain),
};
Expand All @@ -308,7 +319,7 @@ export class SolanaNtt<N extends Network, C extends SolanaChains>
);
} catch (e) {
// This might happen if e.g. the program is not deployed yet.
const version = "2.0.0"
const version = "2.0.0";
return version;
}
}
Expand Down Expand Up @@ -504,17 +515,17 @@ export class SolanaNtt<N extends Network, C extends SolanaChains>
const transferIx =
config.mode.locking != null
? NTT.createTransferLockInstruction(
this.program,
config,
txArgs,
this.pdas
)
this.program,
config,
txArgs,
this.pdas
)
: NTT.createTransferBurnInstruction(
this.program,
config,
txArgs,
this.pdas
);
this.program,
config,
txArgs,
this.pdas
);

const releaseIx = NTT.createReleaseOutboundInstruction(
this.program,
Expand Down Expand Up @@ -555,10 +566,12 @@ export class SolanaNtt<N extends Network, C extends SolanaChains>
luts.push(await this.getAddressLookupTable());
} catch {}

const { blockhash } = await this.connection.getLatestBlockhash();

const messageV0 = new TransactionMessage({
payerKey: payerAddress,
instructions: tx.instructions,
recentBlockhash: (await this.connection.getRecentBlockhash()).blockhash,
recentBlockhash: blockhash,
}).compileToV0Message(luts);

const vtx = new VersionedTransaction(messageV0);
Expand Down Expand Up @@ -654,15 +667,15 @@ export class SolanaNtt<N extends Network, C extends SolanaChains>
const releaseIx =
config.mode.locking != null
? NTT.createReleaseInboundUnlockInstruction(
this.program,
config,
releaseArgs
)
this.program,
config,
releaseArgs
)
: NTT.createReleaseInboundMintInstruction(
this.program,
config,
releaseArgs
);
this.program,
config,
releaseArgs
);

const tx = new Transaction();
tx.feePayer = senderAddress;
Expand All @@ -673,10 +686,12 @@ export class SolanaNtt<N extends Network, C extends SolanaChains>
luts.push(await this.getAddressLookupTable());
} catch {}

const { blockhash } = await this.connection.getLatestBlockhash();

const messageV0 = new TransactionMessage({
payerKey: senderAddress,
instructions: tx.instructions,
recentBlockhash: (await this.connection.getRecentBlockhash()).blockhash,
recentBlockhash: blockhash,
}).compileToV0Message(luts);

const vtx = new VersionedTransaction(messageV0);
Expand Down Expand Up @@ -820,15 +835,15 @@ export class SolanaNtt<N extends Network, C extends SolanaChains>
tx.add(
await (config.mode.locking != null
? NTT.createReleaseInboundUnlockInstruction(
this.program,
config,
releaseArgs
)
this.program,
config,
releaseArgs
)
: NTT.createReleaseInboundMintInstruction(
this.program,
config,
releaseArgs
))
this.program,
config,
releaseArgs
))
);

yield this.createUnsignedTx(
Expand Down Expand Up @@ -890,7 +905,7 @@ export class SolanaNtt<N extends Network, C extends SolanaChains>
delete a[k];
}
}
}
};

deleteMatching(remote, local);

Expand Down

0 comments on commit d7fc284

Please sign in to comment.