Skip to content

Instantly share code, notes, and snippets.

@bntzio
Created July 12, 2022 01:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bntzio/0a177e4d866a6349016014773a7654e8 to your computer and use it in GitHub Desktop.
Save bntzio/0a177e4d866a6349016014773a7654e8 to your computer and use it in GitHub Desktop.
shadow-drive - migrate.ts
import * as anchor from "@project-serum/anchor";
import { findAssociatedTokenAddress, sendAndConfirm } from "../utils/helpers";
import { isBrowser, tokenMint } from "../utils/common";
import { ShadowDriveResponse } from "../types";
/**
*
* @param {anchor.web3.PublicKey} key - PublicKey of a Storage Account
* @returns {ShadowDriveResponse} - Confirmed transaction ID
*/
export default async function migrate(
key: anchor.web3.PublicKey
): Promise<ShadowDriveResponse> {
const selectedAccount = await this.program.account.storageAccount.fetch(key);
let [migration, migrationBump] =
await anchor.web3.PublicKey.findProgramAddress(
[Buffer.from("migration-helper"), key.toBytes()],
this.program.programId
);
let migrateStep1Error: Error;
let migrateStep2Error: Error;
try {
let tx = await this.program.methods
.migrateStep1()
.accounts({
storageAccount: key,
migration: migration,
owner: selectedAccount.owner1.publicKey,
})
.transaction();
tx.recentBlockhash = (await this.connection.getLatestBlockhash()).blockhash;
tx.feePayer = this.wallet.publicKey;
if (!isBrowser) {
await tx.partialSign(this.wallet.payer);
} else {
await this.wallet.signTransaction(tx);
}
await sendAndConfirm(
this.provider.connection,
tx.serialize(),
{ skipPreflight: false },
"confirmed",
120000
);
} catch (err) {
migrateStep1Error = err;
}
let res;
try {
let tx2 = await this.program.methods
.migrateStep2()
.accounts({
storageAccount: key,
migration: migration,
owner: selectedAccount.owner1.publicKey,
})
.transaction();
tx2.recentBlockhash = (
await this.connection.getLatestBlockhash()
).blockhash;
tx2.feePayer = this.wallet.publicKey;
if (!isBrowser) {
await tx2.partialSign(this.wallet.payer);
} else {
await this.wallet.signTransaction(tx2);
}
res = await sendAndConfirm(
this.provider.connection,
tx2.serialize(),
{ skipPreflight: false },
"confirmed",
120000
);
} catch (err) {
migrateStep2Error = err;
}
if (migrateStep1Error || migrateStep2Error) {
return Promise.reject(new Error(migrateStep1Error?.message || migrateStep2Error?.message || 'Transaction Error'));
}
return Promise.resolve(res);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment