Created
May 28, 2024 21:02
-
-
Save alejomendoza/40135749b863b72829c88a0ea5a9a9b0 to your computer and use it in GitHub Desktop.
Multiple auth Soroban contract call
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Keypair, Transaction, hash, scValToNative, xdr } from 'stellar-base'; | |
// generated from Typescript bindings | |
import { Client, networks } from 'contract-client'; | |
function getClientForKeypair(keys: Keypair) { | |
return new Client({ | |
contractId: networks.testnet.contractId, | |
networkPassphrase: networks.testnet.networkPassphrase, | |
rpcUrl: 'https://soroban-testnet.stellar.org', | |
publicKey: keys.publicKey(), | |
signTransaction: async (tx: string, opts) => { | |
const txFromXDR = new Transaction(tx, opts.networkPassphrase); | |
txFromXDR.sign(keys); | |
return txFromXDR.toXDR(); | |
}, | |
signAuthEntry: async (entryXdr, opts) => { | |
return keys | |
.sign(hash(Buffer.from(entryXdr, 'base64'))) | |
.toString('base64'); | |
}, | |
}); | |
} | |
const buy = async (mft: string, user?: User) => { | |
const contract = getClientForKeypair(sourceKeys); | |
// get mft issuer with our logic | |
let tx = await contract.buy( | |
{ | |
buyer: buyerKeys.publicKey(), | |
mft: mft, | |
amount: BigInt(10), | |
}, | |
{ fee: 100000000 } | |
); | |
console.log(await tx.needsNonInvokerSigningBy()); | |
const jsonFromRoot = tx.toJSON(); | |
console.log(tx.simulationData.transactionData.resources); | |
const buyerClient = getClientForKeypair(buyerKeys); | |
const txBuyer = buyerClient.fromJSON['buy'](jsonFromRoot); | |
await txBuyer.signAuthEntries(); | |
const jsonFromBuyer = txBuyer.toJSON(); | |
const txRoot = contract.fromJSON['buy'](jsonFromBuyer); | |
const result = await txRoot.signAndSend(); | |
// console.log('send res', result.sendTransactionResponseAll); | |
// console.log('get res', result.getTransactionResponseAll); | |
const getRes = result.getTransactionResponse as any; | |
console.log(getRes.resultMetaXdr.toXDR('base64')); | |
xdr.TransactionMeta.fromXDR(getRes.resultMetaXdr.toXDR('base64'), 'base64') | |
.v3() | |
.sorobanMeta() | |
?.diagnosticEvents() | |
.forEach((event) => { | |
// console.log(event); | |
// console.log('event', event.event().body().v0().data().toXDR('base64')); | |
console.log(scValToNative(event.event().body().v0().data())); | |
}); | |
return result.getTransactionResponse; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment