Skip to content

Instantly share code, notes, and snippets.

@FredericRezeau
Created December 29, 2023 13:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FredericRezeau/6f9704234417accf29afe523bb863270 to your computer and use it in GitHub Desktop.
Save FredericRezeau/6f9704234417accf29afe523bb863270 to your computer and use it in GitHub Desktop.
const { Keypair, Contract, SorobanRpc, TransactionBuilder, Address, xdr, ScInt, scValToNative} = require("stellar-sdk");
// Network config.
const networkPassphrase = "Test SDF Network ; September 2015";
const rpcurl = "https://soroban-testnet.stellar.org:443";
const server = new SorobanRpc.Server(rpcurl, { allowHttp: true });
const assets = {
market: "$ <soroban lab token wrap>",
property: "$ <soroban lab token wrap>",
lien: "$ <soroban lab token wrap>",
}
const licensor = Keypair.fromSecret("SEC---RET");
const contractId = "CON---TRACT";
async function main() {
try {
const response = await sendTransaction(
new Contract(contractId).call(
"add_property",
createScValMap({
licensor: { value: licensor.publicKey(), type: "address" },
property: { value: assets.property, type: "address" },
lien: { value: assets.lien, type: "address" },
compensation: { value: "Fixed", type: "enum" },
royalty_interest: { value: "100", type: "i128" },
transfer_fee: { value: "100", type: "i128" },
currency: { value: assets.market, type: "address" },
recur_period: { value: "0", type: "u64" },
grace_period: { value: "60", type: "u64" },
})), licensor
).then(_response => sendTransaction(
new Contract(contractId).call(
"execute",
new Address(assets.property).toScVal()
), licensor
));
console.log(scValToNative(response.returnValue));
} catch (e) {
console.error(e);
}
}
async function sendTransaction(operation, keys) {
let transaction = new TransactionBuilder(await server.getAccount(keys.publicKey()),
{ fee: 10000000, networkPassphrase: networkPassphrase })
.addOperation(operation)
.setTimeout(30)
.build();
transaction = await server.prepareTransaction(transaction);
transaction.sign(keys);
let response = await server.sendTransaction(transaction);
const hash = response.hash;
while (response.status === "PENDING" || response.status === "NOT_FOUND") {
await new Promise(resolve => setTimeout(resolve, 2000));
response = await server.getTransaction(hash);
}
console.log(`Tx ${hash}`);
return response;
}
function createScValMap(obj) {
const typeToScValMethod = {
i32: (value) => xdr.ScVal.scvI32(value),
u32: (value) => xdr.ScVal.scvU32(value),
i64: (value) => xdr.ScVal.scvI64(new xdr.Int64(value)),
u64: (value) => xdr.ScVal.scvU64(new xdr.Uint64(value)),
i128: (value) => new ScInt(value, { type: 'i128' }).toScVal(),
bool: (value) => xdr.ScVal.scvBool(value),
address: (value) => new Address(value).toScVal(),
bytes: (value) => xdr.ScVal.scvBytes(Uint8Array.from(value)),
enum: (value) => xdr.ScVal.scvVec([value].map(xdr.ScVal.scvSymbol)),
string: (value) => new xdr.ScVal.scvString(value),
};
const entries = Object.keys(obj)
.sort()
.map(key => {
const { value, type } = obj[key];
return new xdr.ScMapEntry({
key: xdr.ScVal.scvSymbol(Array.from(new TextEncoder().encode(key))),
val: typeToScValMethod[type](value)
});
});
return xdr.ScVal.scvMap(entries);
}
main();
@kmpartner
Copy link

I tried to use this function. But it did not work.
litemint_royalty_contract was deployed and initialized on testnet.
How can I find market, property, and lien addresses. What is market, property, and lien?

const assets = {
market: "$ ",
property: "$ ",
lien: "$ ",
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment