Skip to content

Commit

Permalink
fix: get version function
Browse files Browse the repository at this point in the history
  • Loading branch information
stanleyyconsensys committed Nov 21, 2023
1 parent b87e23d commit ef4eb37
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
9 changes: 9 additions & 0 deletions packages/starknet-snap/src/utils/formatterUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const hexToString = (hex) => {
let str = '';
for (let i = 0; i < hex.length; i += 2) {
const hexValue = hex.substr(i, 2);
const decimalValue = parseInt(hexValue, 16);
str += String.fromCharCode(decimalValue);
}
return str;
};
6 changes: 5 additions & 1 deletion packages/starknet-snap/src/utils/starknetUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import { getAddressKey } from './keyPair';
import { getAccount, getAccounts, getTransactionFromVoyagerUrl, getTransactionsFromVoyagerUrl } from './snapUtils';
import { logger } from './logger';
import { RpcV4GetTransactionReceiptResponse } from '../types/snapApi';
import { hexToString } from './formatterUtils';

export const getCallDataArray = (callDataStr: string): string[] => {
return (callDataStr ?? '')
Expand Down Expand Up @@ -593,13 +594,16 @@ export const findAddressIndex = async (
export const isUpgradeRequired = async (network: Network, address: string) => {
try {
logger.log(`isUpgradeRequired: address = ${address}`);
const version = await getVersion(address, network);
const hexResp = await getVersion(address, network);
const version = hexToString(hexResp);
logger.log(`isUpgradeRequired: hexResp = ${hexResp}, version = ${version}`);
const versionArr = version.split('.');
return Number(versionArr[1]) < MIN_ACC_CONTRACT_VERSION[1];
} catch (err) {
if (!err.message.includes('Contract not found')) {
throw err;
}
logger.error(`isUpgradeRequired: error:`, err);
//[TODO] if address is cario0 but not deployed we should throw error
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/starknet-snap/test/utils/starknetUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,13 @@ describe('Test function: isUpgradeRequired', function () {
});

it('should return true when upgrade is required', async function () {
sandbox.stub(utils, 'getVersion').callsFake(async () => '0.2.3');
sandbox.stub(utils, 'getVersion').callsFake(async () => '0x302e322e33');
const result = await utils.isUpgradeRequired(STARKNET_TESTNET_NETWORK, userAddress);
expect(result).to.be.eq(true);
});

it('should return false when upgrade is not required', async function () {
sandbox.stub(utils, 'getVersion').callsFake(async () => '0.3.0');
sandbox.stub(utils, 'getVersion').callsFake(async () => '0x302e332e30');
const result = await utils.isUpgradeRequired(STARKNET_TESTNET_NETWORK, userAddress);
expect(result).to.be.eq(false);
});
Expand Down

0 comments on commit ef4eb37

Please sign in to comment.