Skip to content

Commit

Permalink
upgrade solana/web3.js from 1.41.0 to 1.47.4
Browse files Browse the repository at this point in the history
  • Loading branch information
atharmohammad committed Jul 22, 2022
1 parent 454c486 commit fa4551e
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 69 deletions.
3 changes: 2 additions & 1 deletion token/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"dependencies": {
"@solana/buffer-layout": "^4.0.0",
"@solana/buffer-layout-utils": "^0.2.0",
"@solana/web3.js": "^1.41.0"
"@solana/web3.js": "^1.47.4"
},
"devDependencies": {
"@solana/spl-memo": "^0.1.0",
Expand All @@ -52,6 +52,7 @@
"@types/eslint-plugin-prettier": "^3.1.0",
"@types/mocha": "^9.1.0",
"@types/node": "^16.11.21",
"@types/node-fetch": "^2.6.2",
"@types/prettier": "^2.4.3",
"@typescript-eslint/eslint-plugin": "^5.10.0",
"@typescript-eslint/parser": "^5.10.0",
Expand Down
30 changes: 9 additions & 21 deletions token/js/src/actions/amountToUiAmount.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import {
Connection,
PublicKey,
Signer,
Transaction,
} from '@solana/web3.js';
import { Connection, PublicKey, Signer, Transaction, TransactionError } from '@solana/web3.js';
import { TOKEN_PROGRAM_ID } from '../constants';
import { createAmountToUiAmountInstruction } from '../instructions/index';

Expand All @@ -16,26 +11,19 @@ import { createAmountToUiAmountInstruction } from '../instructions/index';
* @param amount Amount of tokens to be converted to Ui Amount
* @param programId SPL Token program account
*
* @return Ui Amount generated
* @return Ui Amount generated
*/
export async function amountToUiAmount(
connection: Connection,
payer: Signer,
mint: PublicKey,
amount: number | bigint,
programId = TOKEN_PROGRAM_ID
): Promise<string> {
const transaction = new Transaction().add(
createAmountToUiAmountInstruction(mint,amount,programId)
);
try{
const {returnData} = (await connection.simulateTransaction(transaction, [payer] , false)).value;
if(returnData?.data){
return Buffer.from(returnData.data[0],returnData.data[1]).toString();
}else{
throw new Error("Amount Cannot be converted to Ui Amount !");
}
}catch(error:unknown){
throw error;
): Promise<string | TransactionError | null> {
const transaction = new Transaction().add(createAmountToUiAmountInstruction(mint, amount, programId));
const { returnData, err } = (await connection.simulateTransaction(transaction, [payer], false)).value;
if (returnData?.data) {
return Buffer.from(returnData.data[0], returnData.data[1]).toString('utf-8');
}
}
return err;
}
36 changes: 11 additions & 25 deletions token/js/src/actions/uiAmountToAmount.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import {
Connection,
PublicKey,
Signer,
Transaction,
} from '@solana/web3.js';
import { Connection, PublicKey, Signer, Transaction, TransactionError } from '@solana/web3.js';
import { u64 } from '@solana/buffer-layout-utils';
import { TOKEN_PROGRAM_ID } from '../constants';
import { createUiAmountToAmountInstruction } from '../instructions/index';

/**
* Amount as a string using mint-prescribed decimals
*
Expand All @@ -16,29 +11,20 @@ import { createUiAmountToAmountInstruction } from '../instructions/index';
* @param amount Ui Amount of tokens to be converted to Amount
* @param programId SPL Token program account
*
* @return Ui Amount generated
* @return Amount generated
*/
export async function uiAmountToAmount(
connection: Connection,
payer: Signer,
mint: PublicKey,
amount: string,
programId = TOKEN_PROGRAM_ID
): Promise<number> {
const transaction = new Transaction().add(
createUiAmountToAmountInstruction(mint,amount,programId)
);
try{
const {returnData,err} = (await connection.simulateTransaction(transaction, [payer] , false)).value;
console.log(err)
if(returnData?.data){
const x = Buffer.from(returnData.data[0],returnData.data[1]);
console.log(x);
return 5245;
}else{
throw new Error("Amount Cannot be converted to Ui Amount !");
}
}catch(error:unknown){
throw error;
): Promise<bigint | TransactionError | null> {
const transaction = new Transaction().add(createUiAmountToAmountInstruction(mint, amount, programId));
const { returnData, err } = (await connection.simulateTransaction(transaction, [payer], false)).value;
if (returnData) {
const data = Buffer.from(returnData.data[0], returnData.data[1]);
return u64().decode(data);
}
}
return err;
}
9 changes: 6 additions & 3 deletions token/js/src/instructions/amountToUiAmount.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { struct, u8} from '@solana/buffer-layout';
import { struct, u8 } from '@solana/buffer-layout';
import { u64 } from '@solana/buffer-layout-utils';
import { AccountMeta, PublicKey, TransactionInstruction } from '@solana/web3.js';
import { TOKEN_PROGRAM_ID } from '../constants';
Expand All @@ -17,7 +17,10 @@ export interface AmountToUiAmountInstructionData {
}

/** TODO: docs */
export const amountToUiAmountInstructionData = struct<AmountToUiAmountInstructionData>([u8('instruction'), u64('amount')]);
export const amountToUiAmountInstructionData = struct<AmountToUiAmountInstructionData>([
u8('instruction'),
u64('amount'),
]);

/**
* Construct a AmountToUiAmount instruction
Expand All @@ -33,7 +36,7 @@ export function createAmountToUiAmountInstruction(
amount: number | bigint,
programId = TOKEN_PROGRAM_ID
): TransactionInstruction {
const keys = [{ pubkey: mint, isSigner: false, isWritable: true }];
const keys = [{ pubkey: mint, isSigner: false, isWritable: false }];

const data = Buffer.alloc(amountToUiAmountInstructionData.span);
amountToUiAmountInstructionData.encode(
Expand Down
10 changes: 10 additions & 0 deletions token/js/src/instructions/decode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { DecodedThawAccountInstruction, decodeThawAccountInstruction } from './t
import { DecodedTransferInstruction, decodeTransferInstruction } from './transfer';
import { DecodedTransferCheckedInstruction, decodeTransferCheckedInstruction } from './transferChecked';
import { DecodedAmountToUiAmountInstruction, decodeAmountToUiAmountInstruction } from './amountToUiAmount';
import { DecodedUiAmountToAmountInstruction, decodeUiAmountToAmountInstruction } from './uiAmountToAmount';
import { TokenInstruction } from './types';

/** TODO: docs */
Expand All @@ -46,6 +47,7 @@ export type DecodedInstruction =
| DecodedSyncNativeInstruction
| DecodedInitializeAccount3Instruction
| DecodedAmountToUiAmountInstruction
| DecodedUiAmountToAmountInstruction
// | DecodedInitializeMultisig2Instruction
// | DecodedInitializeMint2Instruction
// TODO: implement ^ and remove `never`
Expand Down Expand Up @@ -80,6 +82,7 @@ export function decodeInstruction(
return decodeInitializeAccount2Instruction(instruction, programId);
if (type === TokenInstruction.SyncNative) return decodeSyncNativeInstruction(instruction, programId);
if (type === TokenInstruction.AmountToUiAmount) return decodeAmountToUiAmountInstruction(instruction, programId);
if (type === TokenInstruction.UiAmountToAmount) return decodeUiAmountToAmountInstruction(instruction, programId);
// TODO: implement
if (type === TokenInstruction.InitializeAccount3)
return decodeInitializeAccount3Instruction(instruction, programId);
Expand Down Expand Up @@ -203,6 +206,13 @@ export function isAmountToUiAmountInstruction(
return decoded.data.instruction === TokenInstruction.AmountToUiAmount;
}

/** TODO: docs */
export function isUiamountToAmountInstruction(
decoded: DecodedInstruction
): decoded is DecodedUiAmountToAmountInstruction {
return decoded.data.instruction === TokenInstruction.UiAmountToAmount;
}

/** TODO: docs, implement */
// export function isInitializeMultisig2Instruction(
// decoded: DecodedInstruction
Expand Down
29 changes: 19 additions & 10 deletions token/js/src/instructions/uiAmountToAmount.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { u8, struct, cstr} from '@solana/buffer-layout';
import { u8, struct, blob } from '@solana/buffer-layout';
import { AccountMeta, PublicKey, TransactionInstruction } from '@solana/web3.js';
import { TOKEN_PROGRAM_ID } from '../constants';
import {
Expand All @@ -12,11 +12,10 @@ import { TokenInstruction } from './types';
/** TODO: docs */
export interface UiAmountToAmountInstructionData {
instruction: TokenInstruction.UiAmountToAmount;
amount: string;
amount: Uint8Array;
}

/** TODO: docs */
export const uiAmountToAmountInstructionData = struct<UiAmountToAmountInstructionData>([u8('instruction'),cstr('amount')]);

/**
* Construct a UiAmountToAmount instruction
Expand All @@ -32,17 +31,20 @@ export function createUiAmountToAmountInstruction(
amount: string,
programId = TOKEN_PROGRAM_ID
): TransactionInstruction {
const keys = [{ pubkey: mint, isSigner: false, isWritable: true }];

const keys = [{ pubkey: mint, isSigner: false, isWritable: false }];
const buf = Buffer.from(amount, 'utf8');
const uiAmountToAmountInstructionData = struct<UiAmountToAmountInstructionData>([
u8('instruction'),
blob(buf.length, 'amount'),
]);
const data = Buffer.alloc(uiAmountToAmountInstructionData.span);
uiAmountToAmountInstructionData.encode(
{
instruction: TokenInstruction.UiAmountToAmount,
amount: amount,
amount: buf,
},
data
);

return new TransactionInstruction({ keys, programId, data });
}

Expand All @@ -54,7 +56,7 @@ export interface DecodedUiAmountToAmountInstruction {
};
data: {
instruction: TokenInstruction.UiAmountToAmount;
amount: string;
amount: Uint8Array;
};
}

Expand All @@ -71,8 +73,11 @@ export function decodeUiAmountToAmountInstruction(
programId = TOKEN_PROGRAM_ID
): DecodedUiAmountToAmountInstruction {
if (!instruction.programId.equals(programId)) throw new TokenInvalidInstructionProgramError();
const uiAmountToAmountInstructionData = struct<UiAmountToAmountInstructionData>([
u8('instruction'),
blob(instruction.data.length - 1, 'amount'),
]);
if (instruction.data.length !== uiAmountToAmountInstructionData.span) throw new TokenInvalidInstructionDataError();

const {
keys: { mint },
data,
Expand All @@ -97,7 +102,7 @@ export interface DecodedUiAmountToAmountInstructionUnchecked {
};
data: {
instruction: number;
amount: string;
amount: Uint8Array;
};
}

Expand All @@ -113,6 +118,10 @@ export function decodeUiAmountToAmountInstructionUnchecked({
keys: [mint],
data,
}: TransactionInstruction): DecodedUiAmountToAmountInstructionUnchecked {
const uiAmountToAmountInstructionData = struct<UiAmountToAmountInstructionData>([
u8('instruction'),
blob(data.length - 1, 'amount'),
]);
return {
programId,
keys: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ describe('Amount', () => {
let payer: Signer;
let mint: PublicKey;
let mintAuthority: Keypair;
let account: PublicKey;
before(async () => {
connection = await getConnection();
payer = await newAccountWithLamports(connection, 1000000000);
Expand All @@ -34,12 +33,10 @@ describe('Amount', () => {
it('amountToUiAmount', async () => {
const amount = BigInt(5245);
const uiAmount = await amountToUiAmount(connection, payer, mint, amount, TEST_PROGRAM_ID);
expect(uiAmount).to.be.not.null;
expect(uiAmount).to.eql("52.45");
expect(uiAmount).to.eql('52.45');
});
it.only('uiAmountToAmount', async () => {
const uiAmount = await uiAmountToAmount(connection, payer, mint, "52.45", TEST_PROGRAM_ID);
expect(uiAmount).to.be.not.null;
expect(uiAmount).to.eql(5245);
it('uiAmountToAmount', async () => {
const uiAmount = await uiAmountToAmount(connection, payer, mint, '52.45', TEST_PROGRAM_ID);
expect(uiAmount).to.eql(BigInt(5245));
});
});
11 changes: 9 additions & 2 deletions token/js/test/unit/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import {
getAssociatedTokenAddressSync,
createInitializeAccount2Instruction,
createInitializeAccount3Instruction,
createAmountToUiAmountInstruction
createAmountToUiAmountInstruction,
createUiAmountToAmountInstruction,
} from '../../src';

chai.use(chaiAsPromised);
Expand Down Expand Up @@ -103,7 +104,13 @@ describe('spl-token-2022 instructions', () => {
});

it('AmountToUiAmount', () => {
const ix = createAmountToUiAmountInstruction(Keypair.generate().publicKey, 22 ,TOKEN_2022_PROGRAM_ID);
const ix = createAmountToUiAmountInstruction(Keypair.generate().publicKey, 22, TOKEN_2022_PROGRAM_ID);
expect(ix.programId).to.eql(TOKEN_2022_PROGRAM_ID);
expect(ix.keys).to.have.length(1);
});

it('UiAmountToAmount', () => {
const ix = createUiAmountToAmountInstruction(Keypair.generate().publicKey, '22', TOKEN_2022_PROGRAM_ID);
expect(ix.programId).to.eql(TOKEN_2022_PROGRAM_ID);
expect(ix.keys).to.have.length(1);
});
Expand Down

0 comments on commit fa4551e

Please sign in to comment.