-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement SAC Transfer Functionality #27
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, there @ShantelPeters! This is looking pretty good so far. Couple comments and notes, but I think you're gettin' there.
Thanks again!
// Don't include this in a real-life production application. | ||
// It's just here to make the secret key accessible in case | ||
// we need to do some manual transactions or something. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's keep this in here please.
sign: async ({ transactionXDR, network, pincode }) => { | ||
signPayment: async ({ destination, amount, asset, network, pincode }) => { | ||
try { | ||
const keyManager = setupKeyManager() | ||
const { publicKey } = get(walletStore) | ||
const sourceAccount = new Account(publicKey, '0') // you have to Replace '0' with actual sequence number | ||
|
||
let transaction; | ||
|
||
if (contacts.isContractAddress(destination)) { | ||
// Handle SAC transfer | ||
const contract = new Contract(destination); | ||
const transferOp = contract.call( | ||
'transfer', | ||
xdr.ScVal.scvAddress(Address.fromString(publicKey).toScAddress()), | ||
xdr.ScVal.scvAddress(Address.fromString(destination).toScAddress()), | ||
xdr.ScVal.scvI128(new xdr.Int128Parts({ | ||
lo: xdr.Uint64.fromString(amount), | ||
hi: xdr.Int64.fromString('0') | ||
})), | ||
xdr.ScVal.scvSymbol(asset.getCode()) | ||
); | ||
|
||
transaction = new TransactionBuilder(sourceAccount, { fee: '100', networkPassphrase: network }) | ||
.addOperation(transferOp) | ||
.setTimeout(30) | ||
.build(); | ||
} else { | ||
// Handle regular payment | ||
transaction = new TransactionBuilder(sourceAccount, { fee: '100', networkPassphrase: network }) | ||
.addOperation(Operation.payment({ | ||
destination: destination, | ||
asset: asset, | ||
amount: amount | ||
})) | ||
.setTimeout(30) | ||
.build(); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these changes are leftover from the previous PR? If so, let's remove them here. Philosophically, we really want this function to take a transaction that the user has already approved, and only attach a signature. We don't want to build the transaction after they've approved "something." The tx building (and yes, simulating SAC transfers) need to be done prior to them pushing the sign
button, so they know exactly what they're signing.
fee: maxFeePerOperation, | ||
}); | ||
|
||
if (StrKey.isValidContract(destination)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you're doing the right check (that destination
is a valid contract), but I think we want to make that check before we call this function. Probably somewhere in the routes/dashboard/send/+page.svelte
page. Probably by adding some permutation in the previewPaymentTransaction
function.
@@ -331,3 +332,58 @@ export async function createPathPaymentStrictReceiveTransaction({ | |||
network_passphrase: networkPassphrase, | |||
} | |||
} | |||
|
|||
/** | |||
* Constructs and returns a Stellar transaction for transferring assets to a contract or account. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this application is intended to be a demonstrative resource, I think we should keep this function limited in scope to only performing SAC transfers. We have the other payment
operation transaction already being handled by another function. Let's make this one focused on SAC transfers, solely.
if (StrKey.isValidContract(destination)) { | ||
// Transfer to a contract | ||
const [assetCode, assetIssuer] = asset.split(':'); | ||
const contract = new Contract(destination); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like I mentioned before, checking if destination
is a contract, is the right check for this flow, but here the contract will need to be the contract for the asset. You'll need to use something like:
const contractId = new Asset(asset.split(':')[0], asset.split(':')[1]).contractId(Networks.TESTNET);
const contract = new Contract(contractId);
Then your use of contract.call(...)
should work. Or, you could use the invokeContractFunction
operation to add the operation to the transaction.
Address.fromString(source).toScVal(), | ||
Address.fromString(destination).toScVal(), | ||
xdr.ScVal.scvI128(new xdr.Int128Parts({ | ||
lo: xdr.Uint64.fromString(amountBigInt.toString(16).padStart(16, '0').slice(-16)), | ||
hi: xdr.Int64.fromString(amountBigInt.toString(16).slice(0, -16) || '0') | ||
})), | ||
xdr.ScVal.scvSymbol(assetCode) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could use the nativeToScVal()
function to make building these a little bit easier.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ElliotFriend Okay sir I’m making the required changes
); | ||
} | ||
|
||
const builtTransaction = transaction.setTimeout(standardTimebounds).build(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for contract invocations, you'll need to simulate the transaction. The rpc server has the prepareTransaction()
function that will make this process pretty easy on you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay sir I’m on it please
This PR implements the functionality to enable interactions with Stellar Asset Contracts (SACs) in the Basic Payment App.
createContractTransferTransaction
function insrc/lib/stellar/transactions.js
StrKey.isValidContract()
Contract.call()
for SAC transferswalletStore.sign()
to a separate function intransactions.js
TODO
previewPaymentTransaction
in+page.svelte
to use the newcreateContractTransferTransaction
functionTesting
Please review the changes and let me know if any further modifications are needed.