Skip to content

Commit

Permalink
fix-bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
wsdt committed Aug 16, 2024
1 parent 1fef04a commit 5218ed8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
30 changes: 23 additions & 7 deletions backend/offchain/token-price.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {ethers, getBigInt, getBytes, getAddress} from "ethers";
import axios from "axios";
import * as dotenv from "dotenv";
import {getSelector, OffchainParameter, OffchainParameterParsed, parseRequest} from "./utils";
import {addHexPrefix, getSelector, OffchainParameter, OffchainParameterParsed, parseRequest} from "./utils";

dotenv.config();

Expand Down Expand Up @@ -49,10 +49,22 @@ export async function offchainTokenPrice(params: OffchainParameter) {
return generateResponse(request, 0, encodedTokenPrice);
} catch (error: any) {
console.log("received error: ", error);
return generateResponse(request, 1, ethers.toUtf8Bytes(error.message).toString());
return generateResponse(request, 1, hexEncode(error.message));
}
}

const hexEncode = (str: string) => {
let hex, i;

let result = "";
for (i=0; i<str.length; i++) {
hex = str.charCodeAt(i).toString(16);
result += ("000"+hex).slice(-4);
}

return `0x${result}`
}

export async function getTokenPrice(tokenSymbol: string): Promise<number> {
const headers = {
accept: "application/json",
Expand Down Expand Up @@ -83,27 +95,31 @@ export function generateResponse(
errorCode: number,
respPayload: string
) {
if (!respPayload.startsWith('0x')) {
respPayload = `0x${respPayload}`
}
respPayload = addHexPrefix(respPayload)

const encodedResponse = ethers.AbiCoder.defaultAbiCoder().encode(
["address", "uint256", "uint32", "bytes"],
[req.src_addr, req.src_nonce, errorCode, respPayload]
);
console.log("EncodedResponse: ", encodedResponse)
const putResponseCallData = ethers.AbiCoder.defaultAbiCoder().encode(
["bytes32", "bytes"],
[req.sk, encodedResponse]
);
console.log("putResponseCallData", putResponseCallData)
const putResponseEncoded = getSelector("PutResponse", ['bytes32', 'bytes']) + putResponseCallData.slice(2);

console.log("putResponseEncoded", putResponseEncoded)
const executeCallData = ethers.AbiCoder.defaultAbiCoder().encode(
["address", "uint256", "bytes"],
[HelperAddr, 0, putResponseEncoded]
);
const executeEncoded = getSelector("execute", ['address', 'uint256', 'bytes']) + executeCallData.slice(2);
console.log("executeCallData", executeCallData, executeEncoded)

console.log("RespPayload", respPayload)
const callGas = BigInt(705) * BigInt(getBytes(respPayload).length) + BigInt(170000);
console.log("callGas calculation", getBytes(respPayload).length, 4 + getBytes(executeCallData).length, callGas);
console.log("callGas calculation", getBytes(respPayload).length, 4 + getBytes(addHexPrefix(executeCallData)).length, callGas);

const finalEncodedParameters = ethers.AbiCoder.defaultAbiCoder().encode(
[
Expand Down Expand Up @@ -144,7 +160,7 @@ export function generateResponse(
);

const wallet = new ethers.Wallet(hc1_key);
const signature = wallet.signMessageSync(getBytes(finalHash));
const signature = wallet.signMessageSync(getBytes(addHexPrefix(finalHash)));

console.log(`Method returning success=${errorCode === 0} response=${respPayload} signature=${signature}`);

Expand Down
3 changes: 2 additions & 1 deletion backend/offchain/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ export type OffchainParameterParsed = {
payload: Uint8Array;
};

export const addHexPrefix = (value: string) => value.startsWith('0x') ? value : `0x${value}`;

export const parseRequest = (params: OffchainParameter): OffchainParameterParsed => {
const addHexPrefix = (value: string) => value.startsWith('0x') ? value : `0x${value}`;
return {
ver: params.ver,
sk: ethers.getBytes(addHexPrefix(params.sk)),
Expand Down

0 comments on commit 5218ed8

Please sign in to comment.