Discussion on returnValue
in bcs
auto-format dryrun
transactions.
#18274
Replies: 2 comments
-
this is my auto format code. I wish I could get more professional help to perfect a more comprehensive auto format funcation. 🙏 async autoFormatDryrunValue(
getResult: DevInspectResults
): Promise<any[] | undefined> {
let returnValue = [];
// "success" | "failure";
if (getResult.effects.status.status === 'success') {
const resultList = getResult.results![0].returnValues!;
for (const res of resultList) {
const bcs = new BCS(getSuiMoveConfig());
const value = Uint8Array.from(res[0]);
const bcsType = res[1].replace(/0x1::ascii::String/g, 'string');
const data = bcs.de(bcsType, value);
returnValue.push(data);
}
return returnValue;
} else {
return undefined;
}
} |
Beta Was this translation helpful? Give feedback.
-
We did some preliminary contract parsing based on move's abi, and can now implement contract calls and bcs decoding of dryrun return values based on obelisk's sdk in a very friendly way. There are still some details that need to be worked out, but I believe that the current version is friendly to some developers who need to use the dryrun function for real-time queries. (Such as calculating blood based on a formula or customizing the query rule protocol) I believe this is friendly for some game related developers. try this in your "dependencies": {
"@0xobelisk/sui-client": "^0.5.21", and you can try this to test the query and decode func.
import {
Obelisk,
NetworkType,
TransactionArgument,
loadMetadata,
Transaction,
DevInspectResults,
} from "@0xobelisk/sui-client";
import * as process from 'process';
import dotenv from 'dotenv';
dotenv.config();
async function test() {
const network = 'testnet';
const packageId =
'0x3b09247b204f17ef5cd240e6c5c9b2c03bb4907c6d2cb6e27597cdf97a00f004';
const metadata = await loadMetadata(network as NetworkType, packageId);
const privateKey = process.env.PRIVATE_KEY;
const obelisk = new Obelisk({
networkType: network as NetworkType,
packageId: packageId,
metadata: metadata,
secretKey: privateKey,
});
console.log(obelisk.getAddress());
await obelisk.requestFaucet();
let balance = await obelisk.getBalance();
console.log('balance', balance);
let tx = new Transaction();
let params: TransactionArgument[] = [];
let query1 = (await obelisk.query.funt.get_u8(
tx,
params
)) as DevInspectResults;
console.log(JSON.stringify(query1.results![0]));
let formatData1 = obelisk.view(query1);
console.log(formatData1);
let tx2 = new Transaction();
let params2: TransactionArgument[] = [];
let query2 = (await obelisk.query.funt.get_u64(
tx2,
params2
)) as DevInspectResults;
console.log(JSON.stringify(query2.results![0]));
let formatData2 = obelisk.view(query2);
console.log(formatData2);
let tx3 = new Transaction();
let params3: TransactionArgument[] = [];
let query3 = (await obelisk.query.funt.get_u128(
tx3,
params3
)) as DevInspectResults;
console.log(JSON.stringify(query3.results![0]));
let formatData3 = obelisk.view(query3);
console.log(formatData3);
let tx4 = new Transaction();
let params4: TransactionArgument[] = [];
let query4 = (await obelisk.query.funt.get_u256(
tx4,
params4
)) as DevInspectResults;
console.log(JSON.stringify(query4.results![0]));
let formatData4 = obelisk.view(query4);
console.log(formatData4);
let tx5 = new Transaction();
let params5: TransactionArgument[] = [];
let query5 = (await obelisk.query.funt.get_bool(
tx5,
params5
)) as DevInspectResults;
console.log(JSON.stringify(query5.results![0]));
let formatData5 = obelisk.view(query5);
console.log(formatData5);
let tx6 = new Transaction();
let params6: TransactionArgument[] = [];
let query6 = (await obelisk.query.funt.get_address(
tx6,
params6
)) as DevInspectResults;
console.log(JSON.stringify(query6.results![0]));
let formatData6 = obelisk.view(query6);
console.log(formatData6);
let tx7 = new Transaction();
let params7: TransactionArgument[] = [];
let query7 = (await obelisk.query.funt.get_vector_u8(
tx7,
params7
)) as DevInspectResults;
console.log(JSON.stringify(query7.results![0]));
let formatData7 = obelisk.view(query7);
console.log(formatData7);
let tx8 = new Transaction();
let params8: TransactionArgument[] = [];
let query8 = (await obelisk.query.funt.get_tuple(
tx8,
params8
)) as DevInspectResults;
console.log(JSON.stringify(query8.results![0]));
let formatData8 = obelisk.view(query8);
console.log(formatData8);
let tx9 = new Transaction();
let params9: TransactionArgument[] = [];
let query9 = (await obelisk.query.funt.get_string(
tx9,
params9
)) as DevInspectResults;
console.log(JSON.stringify(query9.results![0]));
let formatData9 = obelisk.view(query9);
console.log(formatData9);
let tx10 = new Transaction();
let params10: TransactionArgument[] = [];
let query10 = (await obelisk.query.funt.get_option_type(
tx10,
params10
)) as DevInspectResults;
console.log(JSON.stringify(query10.results![0]));
let formatData10 = obelisk.view(query10);
console.log(formatData10);
let tx11 = new Transaction();
let params11: TransactionArgument[] = [];
let query11 = (await obelisk.query.funt.get_struct(
tx11,
params11
)) as DevInspectResults;
let datares = obelisk.view(query11);
console.log(datares);
let tx12 = new Transaction();
let params12: TransactionArgument[] = [];
let formatData12 = obelisk.view(
(await obelisk.query.funt.get_obj(tx12, params12)) as DevInspectResults
);
console.log(formatData12);
}
test(); |
Beta Was this translation helpful? Give feedback.
-
In a sui move environment, most developers would probably replace the #[view] query function with a dryrun transaction.
this is my code.
and this is my dryrun tx return data.
In this case function, the return type is u64 (and other functions could be string, or struct). In this case, we need to construct our own bcs type in sdk to format the data.
Since the return body in dryrun comes with type information, why can't bcs sdk provide a function that automatically determines the type and automatically formats it?
Beta Was this translation helpful? Give feedback.
All reactions