as-soroban-sdk
Version:
AssemblyScript SDK for writing contracts for Soroban.
321 lines (275 loc) • 9.7 kB
JavaScript
const XDR = require('js-xdr');
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const contractId = `CC6L3TG6H7ZYO75VHYUKHB2T4KBKTFYAYVGNVZXQKCE3X6M3GORVCV62`;
const rpcAddress = 'https://soroban-testnet.stellar.org'
const rpcUrl = ' --rpc-url ' + rpcAddress;
const networkPassphrase = ' --network-passphrase "Test SDF Network ; September 2015"';
const cmdFetch = 'stellar contract fetch' + ' --id ' + contractId + rpcUrl + networkPassphrase ;
async function fetchContractSpec() {
const { error, stdout, stderr } = await exec(cmdFetch);
if (error) {
console.log(`error: ${error.message}`);
} if (stderr) {
console.log(`stderr: ${stderr}`);
}
let contractWasm = stdout;
let xdr = getXdrSpec();
let contractspecv0_key = "contractspecv0";
let contractmetav0_key = "contractmetav0";
var contractspecv0Bytes = contractWasm.substring(
contractWasm.indexOf(contractspecv0_key) + contractspecv0_key.length,
contractWasm.lastIndexOf(contractmetav0_key)
);
let entries = [];
try {
while (true) {
let entry = xdr.ScSpecEntry.fromXDR(contractspecv0Bytes)
entries.push(entry)
console.log(entry.value())
// next
let xdrEntry = entry.toXDR()
contractspecv0Bytes = contractWasm.substring(
contractWasm.indexOf(xdrEntry) + xdrEntry.length ,
contractWasm.lastIndexOf(contractmetav0_key)
);
}
} catch(error) {
console.log(`catched error: ${error.message}`);
}
console.log(`nr of entries : ${entries.length}`)
}
function getXdrSpec() {
return XDR.config((xdr) => {
xdr.typedef("Uint64", xdr.uhyper());
xdr.typedef("Uint32", xdr.uint());
xdr.typedef("ScSymbol", xdr.string(32));
xdr.struct("ScMetaV0", [
["key", xdr.string()],
["val", xdr.string()],
]);
xdr.enum("ScMetaKind", {
scMetaV0: 0,
});
xdr.union("ScMetaEntry", {
switchOn: xdr.lookup("ScMetaKind"),
switchName: "kind",
switches: [
["scMetaV0", "v0"],
],
arms: {
v0: xdr.lookup("ScMetaV0"),
},
});
xdr.enum("ScEnvMetaKind", {
scEnvMetaKindInterfaceVersion: 0,
});
xdr.union("ScEnvMetaEntry", {
switchOn: xdr.lookup("ScEnvMetaKind"),
switchName: "kind",
switches: [
["scEnvMetaKindInterfaceVersion", "interfaceVersion"],
],
arms: {
interfaceVersion: xdr.lookup("Uint64"),
},
});
xdr.enum("ScSpecType", {
scSpecTypeVal: 0,
scSpecTypeBool: 1,
scSpecTypeVoid: 2,
scSpecTypeStatus: 3,
scSpecTypeU32: 4,
scSpecTypeI32: 5,
scSpecTypeU64: 6,
scSpecTypeI64: 7,
scSpecTypeTimepoint: 8,
scSpecTypeDuration: 9,
scSpecTypeU128: 10,
scSpecTypeI128: 11,
scSpecTypeU256: 12,
scSpecTypeI256: 13,
scSpecTypeBytes: 14,
scSpecTypeString: 16,
scSpecTypeSymbol: 17,
scSpecTypeAddress: 19,
scSpecTypeOption: 1000,
scSpecTypeResult: 1001,
scSpecTypeVec: 1002,
scSpecTypeSet: 1003,
scSpecTypeMap: 1004,
scSpecTypeTuple: 1005,
scSpecTypeBytesN: 1006,
scSpecTypeUdt: 2000,
});
xdr.struct("ScSpecTypeOption", [
["valueType", xdr.lookup("ScSpecTypeDef")],
]);
xdr.struct("ScSpecTypeResult", [
["okType", xdr.lookup("ScSpecTypeDef")],
["errorType", xdr.lookup("ScSpecTypeDef")],
]);
xdr.struct("ScSpecTypeVec", [
["elementType", xdr.lookup("ScSpecTypeDef")],
]);
xdr.struct("ScSpecTypeMap", [
["keyType", xdr.lookup("ScSpecTypeDef")],
["valueType", xdr.lookup("ScSpecTypeDef")],
]);
xdr.struct("ScSpecTypeSet", [
["elementType", xdr.lookup("ScSpecTypeDef")],
]);
xdr.struct("ScSpecTypeTuple", [
["valueTypes", xdr.varArray(xdr.lookup("ScSpecTypeDef"), 12)],
]);
xdr.struct("ScSpecTypeBytesN", [
["n", xdr.lookup("Uint32")],
]);
xdr.struct("ScSpecTypeUdt", [
["name", xdr.string(60)],
]);
xdr.union("ScSpecTypeDef", {
switchOn: xdr.lookup("ScSpecType"),
switchName: "type",
switches: [
["scSpecTypeVal", xdr.void()],
["scSpecTypeBool", xdr.void()],
["scSpecTypeVoid", xdr.void()],
["scSpecTypeStatus", xdr.void()],
["scSpecTypeU32", xdr.void()],
["scSpecTypeI32", xdr.void()],
["scSpecTypeU64", xdr.void()],
["scSpecTypeI64", xdr.void()],
["scSpecTypeTimepoint", xdr.void()],
["scSpecTypeDuration", xdr.void()],
["scSpecTypeU128", xdr.void()],
["scSpecTypeI128", xdr.void()],
["scSpecTypeU256", xdr.void()],
["scSpecTypeI256", xdr.void()],
["scSpecTypeBytes", xdr.void()],
["scSpecTypeString", xdr.void()],
["scSpecTypeSymbol", xdr.void()],
["scSpecTypeAddress", xdr.void()],
["scSpecTypeOption", "option"],
["scSpecTypeResult", "result"],
["scSpecTypeVec", "vec"],
["scSpecTypeMap", "map"],
["scSpecTypeSet", "Set"],
["scSpecTypeTuple", "tuple"],
["scSpecTypeBytesN", "bytesN"],
["scSpecTypeUdt", "udt"],
],
arms: {
option: xdr.lookup("ScSpecTypeOption"),
result: xdr.lookup("ScSpecTypeResult"),
vec: xdr.lookup("ScSpecTypeVec"),
map: xdr.lookup("ScSpecTypeMap"),
Set: xdr.lookup("ScSpecTypeSet"),
tuple: xdr.lookup("ScSpecTypeTuple"),
bytesN: xdr.lookup("ScSpecTypeBytesN"),
udt: xdr.lookup("ScSpecTypeUdt"),
},
});
xdr.struct("ScSpecUdtStructFieldV0", [
["doc", xdr.string(1024)],
["name", xdr.string(30)],
["type", xdr.lookup("ScSpecTypeDef")],
]);
xdr.struct("ScSpecUdtStructV0", [
["doc", xdr.string(1024)],
["lib", xdr.string(80)],
["name", xdr.string(60)],
["fields", xdr.varArray(xdr.lookup("ScSpecUdtStructFieldV0"), 40)],
]);
xdr.struct("ScSpecUdtUnionCaseVoidV0", [
["doc", xdr.string(1024)],
["name", xdr.string(60)],
]);
xdr.struct("ScSpecUdtUnionCaseTupleV0", [
["doc", xdr.string(1024)],
["name", xdr.string(60)],
["type", xdr.varArray(xdr.lookup("ScSpecTypeDef"), 12)],
]);
xdr.enum("ScSpecUdtUnionCaseV0Kind", {
scSpecUdtUnionCaseVoidV0: 0,
scSpecUdtUnionCaseTupleV0: 1,
})
xdr.union("ScSpecUdtUnionCaseV0", {
switchOn: xdr.lookup("ScSpecUdtUnionCaseV0Kind"),
switchName: "kind",
switches: [
["scSpecUdtUnionCaseVoidV0", "voidCase"],
["scSpecUdtUnionCaseTupleV0", "tupleCase"],
],
arms: {
voidCase: xdr.lookup("ScSpecUdtUnionCaseVoidV0"),
tupleCase: xdr.lookup("ScSpecUdtUnionCaseTupleV0"),
},
});
xdr.struct("ScSpecUdtUnionV0", [
["doc", xdr.string(1024)],
["lib", xdr.string(80)],
["name", xdr.string(60)],
["cases", xdr.varArray(xdr.lookup("ScSpecUdtUnionCaseV0"), 50)],
]);
xdr.struct("ScSpecUdtEnumCaseV0", [
["doc", xdr.string(1024)],
["name", xdr.string(60)],
["value", xdr.lookup("Uint32")],
]);
xdr.struct("ScSpecUdtEnumV0", [
["doc", xdr.string(1024)],
["lib", xdr.string(80)],
["name", xdr.string(60)],
["cases", xdr.varArray(xdr.lookup("ScSpecUdtEnumCaseV0"), 50)],
]);
xdr.struct("ScSpecUdtErrorEnumCaseV0", [
["doc", xdr.string(1024)],
["name", xdr.string(60)],
["value", xdr.lookup("Uint32")],
]);
xdr.struct("ScSpecUdtErrorEnumV0", [
["doc", xdr.string(1024)],
["lib", xdr.string(80)],
["name", xdr.string(60)],
["cases", xdr.varArray(xdr.lookup("ScSpecUdtErrorEnumCaseV0"), 50)],
]);
xdr.struct("ScSpecFunctionInputV0", [
["doc", xdr.string(1024)],
["name", xdr.string(30)],
["type", xdr.lookup("ScSpecTypeDef")],
]);
xdr.struct("ScSpecFunctionV0", [
["doc", xdr.string(1024)],
["name", xdr.lookup("ScSymbol")],
["inputs", xdr.varArray(xdr.lookup("ScSpecFunctionInputV0"), 10)],
["outputs", xdr.varArray(xdr.lookup("ScSpecTypeDef"), 1)],
]);
xdr.union("ScSpecEntry", {
switchOn: xdr.lookup("ScSpecEntryKind"),
switchName: "kind",
switches: [
["scSpecEntryFunctionV0", "functionV0"],
["scSpecEntryUdtStructV0", "udtStructV0"],
["scSpecEntryUdtUnionV0", "udtUnionV0"],
["scSpecEntryUdtEnumV0", "udtEnumV0"],
["scSpecEntryUdtErrorEnumV0", "udtErrorEnumV0"],
],
arms: {
functionV0: xdr.lookup("ScSpecFunctionV0"),
udtStructV0: xdr.lookup("ScSpecUdtStructV0"),
udtUnionV0: xdr.lookup("ScSpecUdtUnionV0"),
udtEnumV0: xdr.lookup("ScSpecUdtEnumV0"),
udtErrorEnumV0: xdr.lookup("ScSpecUdtErrorEnumV0"),
},
});
xdr.enum("ScSpecEntryKind", {
scSpecEntryFunctionV0: 0,
scSpecEntryUdtStructV0: 1,
scSpecEntryUdtUnionV0: 2,
scSpecEntryUdtEnumV0: 3,
scSpecEntryUdtErrorEnumV0: 4,
});
});
}
fetchContractSpec()