Skip to content

Instantly share code, notes, and snippets.

@EightRice
Created September 4, 2024 11:55
Show Gist options
  • Save EightRice/96508e8625b207a0fbe0be3367eabe0d to your computer and use it in GitHub Desktop.
Save EightRice/96508e8625b207a0fbe0be3367eabe0d to your computer and use it in GitHub Desktop.
import { Expr, Parser, packDataBytes, MichelsonType, MichelsonData } from "@taquito/michel-codec";
class MyEncoder {
static async encodeLambdaAddMetadata(
dataToEncode: any,
michelsonSchemaString: string,
) {
console.log("michelsonSchemaString: ", michelsonSchemaString);
console.log("dataToEncode: ", dataToEncode);
const parser = new Parser();
const dataJSON = parser.parseMichelineExpression(
dataToEncode
) as MichelsonData;
const typeJSON = parser.parseMichelineExpression(
michelsonSchemaString
) as MichelsonType;
const packed = packDataBytes(dataJSON, typeJSON);
console.log(packed);
return packed;
}
}
async function main() {
// Replace these with your actual data, schema, and TezosToolkit instance\
const michelsonCodeString = `
{ parameter unit ;
storage unit ;
code { DROP ;
PUSH (or (or (pair (pair (lambda
(pair (pair (map %handler_storage string bytes) (bytes %packed_argument))
(pair %proposal_info
(address %from)
(nat %frozen_token)
(bytes %proposal_metadata)))
(pair (pair (option %guardian address) (map %handler_storage string bytes))
(list %operations operation)))
(lambda (pair bytes (map string bytes)) unit))
string)
unit)
unit)
(Left (Left (Pair (Pair { DROP ;
NIL operation ;
EMPTY_MAP string bytes ;
NONE address ;
PAIR ;
PAIR }
{ DROP ; UNIT })
"sample"))) ;
DROP ;
UNIT ;
NIL operation ;
PAIR } }
`;
const myData = { };
const mySchema = "...";
// const myTezosInstance = new TezosToolkit(...);
try {
const encodedData = await MyEncoder.encodeLambdaAddMetadata(michelsonCodeString, mySchema);
console.log("Encoded Data:", encodedData);
} catch (error) {
console.error("Encoding Error:", error);
}
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment