Skip to content

Instantly share code, notes, and snippets.

@snowkidind
Created September 24, 2020 02:27
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 snowkidind/1bc269566ec581e386d1f8a2d805e565 to your computer and use it in GitHub Desktop.
Save snowkidind/1bc269566ec581e386d1f8a2d805e565 to your computer and use it in GitHub Desktop.
/*
* initDeliverable.js
*
* Storefront example application where user purchases ERC20, 721 and
* pays fees in the network's native fee curency.
* This is not a complete application, hence a gist.
* Modified version of community-points::transaction.js
* See also
* keny@eastcoastbands.com
*/
async function sendUnsponsoredTx(
collectorVaultAddress, // erc20
collectorCustodialAddress, // erc20
producerVaultAddress, // erc20
erc20Address, // erc20 contract
transferAmount // 50 P token
){
try {
// outgoing fee utxo
const fee = await getLatestNetworkFee();
const markup = getMarkup(fee.fee, false);
const totalFee = fee.fee + markup; // 2 gwei
const feeUtxo = await utxoManager.getValidUtxos(childChain, collectorVaultAddress, n.networkFeeCurrency, totalFee);
// outgoing xFer utxo
const transferAmountWei = web3.utils.toWei(transferAmount.toString(), 'ether');
const bnTransferAmt = new BN(transferAmountWei);
const producerUtxo = await utxoManager.getValidUtxos(childChain, producerVaultAddress, erc20Address, bnTransferAmt)
if (producerUtxo.length > 0 && feeUtxo.length > 0) {
const feeInfo = await getFeeInfo(childChain, n.networkFeeCurrency);
// Create the transaction
const txMake = deliverable.create(
producerUtxo[0].owner,
collectorCustodialAddress,
[producerUtxo[0]], // only send the first qualifrying utxo, because im lazy.
bnTransferAmt,
'Unsponsored Plasma Drops',
erc20Address,
[feeUtxo[0]], // only send the first qualifrying utxo
feeInfo.amount,
collectorVaultAddress,
n.markupWallet,
markup
);
const typedData = deliverable.getTypedData(txMake, childChain.plasmaContractAddress);
const keyFeeVault = await dbUser.getKeyForVault(collectorVaultAddress);
const keyErc20vault = await dbUser.getKeyForVault(producerVaultAddress);
const privateKeys = [keyErc20vault, keyFeeVault];
const signatureForFees = childChain.signTransaction(typedData, privateKeys);
const signedTxnFees = childChain.buildSignedTransaction(typedData, signatureForFees);
const receipt = await childChain.submitTransaction(signedTxnFees);
console.log(receipt);
// wait for evidence to appear on the chainz
const recipientBalances = await getBalances(collectorCustodialAddress);
let lastBalance = 0;
for (let i = 0; i < recipientBalances.length; i++) {
if (recipientBalances[i].address === erc20Address) {
lastBalance = recipientBalances[i].balance.toString();
}
}
const expected = Number(lastBalance) + Number(bnTransferAmt.toString());
await wait.waitForBalance(
childChain,
collectorCustodialAddress,
expected,
erc20Address
);
return {status: 'ok', receipt: receipt};
} else {
// issues with utxo's
if (producerUtxo.length > 0) {
// producer needs to combine utxo's and retry
return {status: 'error', reason: 'producer funds need to combine utxos and retry'};
}
if (feeUtxo.length > 0) {
// collector needs to combine utxo's and retry
return {status: 'error', reason: 'collector payment funds needs to combine utxos and retry'};
}
}
} catch (error) {
return {status: 'error', reason: 'an error occurred', error: error};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment