Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compat-helpers/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tensor-foundation/compat-helpers",
"version": "1.0.0-beta.1",
"version": "1.0.0-beta.2",
"description": "Helper functions to make old web3.js programs compatible with web3.js-next modules",
"sideEffects": false,
"module": "./dist/src/index.mjs",
Expand Down
38 changes: 38 additions & 0 deletions compat-helpers/src/accounts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { AccountInfo, PublicKey } from '@solana/web3.js';
import {
type EncodedAccount,
type Address,
lamports,
Lamports,
} from '@solana/web3.js-next';

export function fromAccountInfoToEncodedAccount<
TAddress extends string = string,
>(address: PublicKey, account: AccountInfo<Buffer>): EncodedAccount<TAddress> {
return {
address: address.toString() as Address<TAddress>,
data: new Uint8Array(account.data),
executable: account.executable,
lamports: lamports(BigInt(account.lamports)),
programAddress: account.owner.toString() as Address,
};
}

export function fromEncodedAccountToAccountInfo<
TAddress extends string = string,
>(account: EncodedAccount<TAddress>): AccountInfo<Buffer> {
return {
executable: account.executable,
owner: new PublicKey(account.programAddress),
lamports: lamportsToNumber(account.lamports),
data: Buffer.from(account.data),
};
}

function lamportsToNumber(lamportsValue: Lamports): number {
const num = Number(lamportsValue);
if (!Number.isSafeInteger(num)) {
throw new Error('Lamports value too large to convert to number safely');
}
return num;
}
Loading