-
-
Notifications
You must be signed in to change notification settings - Fork 252
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(unlockjs): support tx building to return only calldata (#15136)
* factor params * calldata only purchase * add support for purchaseKey and purchaseKeys * pass provider explicitely * bind self object to prepare purchase helper * allowance pass signer if exists * update README
- Loading branch information
Showing
13 changed files
with
389 additions
and
135 deletions.
There are no files selected for viewing
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
14 changes: 12 additions & 2 deletions
14
packages/unlock-js/src/PublicLock/utils/approveAllowance.js
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
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
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
41 changes: 41 additions & 0 deletions
41
packages/unlock-js/src/PublicLock/v10/preparePurchaseKeyTx.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import preparePurchaseKeysTx from './preparePurchaseKeysTx' | ||
|
||
/** | ||
* Build tx for purchasing a single key | ||
* @returns {object} | ||
* - {PropTypes.address} to | ||
* - {PropTypes.number} value | ||
* - {PropTypes.bytes} data | ||
*/ | ||
export default async function ( | ||
{ | ||
lockAddress, | ||
owner, | ||
keyManager, | ||
keyPrice, | ||
erc20Address, | ||
decimals, | ||
referrer, | ||
recurringPayments, // nb of reccuring payments to approve, | ||
totalApproval, // Explicit approval amount | ||
data, | ||
}, | ||
provider | ||
) { | ||
const txs = await preparePurchaseKeysTx.bind(this)( | ||
{ | ||
owners: owner ? [owner] : null, | ||
keyManagers: keyManager ? [keyManager] : null, | ||
keyPrices: keyPrice ? [keyPrice] : null, | ||
referrers: referrer ? [referrer] : null, | ||
data: data ? [data] : null, | ||
recurringPayments: recurringPayments ? [recurringPayments] : null, | ||
lockAddress, | ||
erc20Address, | ||
totalApproval, | ||
decimals, | ||
}, | ||
provider | ||
) | ||
return txs | ||
} |
69 changes: 69 additions & 0 deletions
69
packages/unlock-js/src/PublicLock/v10/preparePurchaseKeysTx.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { ZERO } from '../../constants' | ||
import getPurchaseKeysArguments from './getPurchaseKeysArguments' | ||
import approveAllowance from '../utils/approveAllowance' | ||
|
||
/** | ||
* This function will build a purchase tx based on the params | ||
* and return from, to, value, data so it can be sent directly | ||
* via a provider. | ||
* @param {object} params: | ||
* - {PropTypes.arrayOf(PropTypes.address)} lockAddress | ||
* - {PropTypes.arrayOf(PropTypes.address)} owners | ||
* - {PropTypes.arrayOf(string)} keyPrices | ||
* - {PropTypes.address} erc20Address | ||
* - {number} decimals | ||
* - {PropTypes.arrayOf(PropTypes.address)} referrers (address which will receive UDT - if applicable) | ||
* - {PropTypes.arrayOf(number)} recurringPayments the number of payments to allow for each keys. If the array is set, the keys are considered using recurring ERRC20 payments). | ||
* - {PropTypes.arrayOf(PropTypes.array[bytes])} _data (array of array of bytes, not used in transaction but can be used by hooks) | ||
* */ | ||
export default async function preparePurchase(options, provider) { | ||
const { lockAddress } = options | ||
const lockContract = await this.getLockContract(lockAddress, provider) | ||
options.lockContract = lockContract | ||
const { | ||
owners, | ||
keyPrices, | ||
keyManagers, | ||
referrers, | ||
data, | ||
totalPrice, | ||
erc20Address, | ||
totalAmountToApprove, | ||
} = await getPurchaseKeysArguments.bind(this)(options) | ||
|
||
const txs = [] | ||
|
||
// If the lock is priced in ERC20, we need to approve the transfer | ||
const approvalOptions = { | ||
erc20Address, | ||
totalAmountToApprove, | ||
address: lockAddress, | ||
onlyData: true, | ||
} | ||
|
||
// Only ask for approval if the lock is priced in ERC20 | ||
if ( | ||
approvalOptions.erc20Address && | ||
approvalOptions.erc20Address !== ZERO && | ||
totalAmountToApprove > 0 | ||
) { | ||
const approvalTxRequest = await approveAllowance.bind(this)(approvalOptions) | ||
txs.push(approvalTxRequest) | ||
} | ||
|
||
// parse | ||
const purchaseArgs = [keyPrices, owners, referrers, keyManagers, data] | ||
const callData = lockContract.interface.encodeFunctionData( | ||
'purchase', | ||
purchaseArgs | ||
) | ||
|
||
const value = !erc20Address || erc20Address === ZERO ? totalPrice : 0 | ||
const purchaseTxRequest = { | ||
data: callData, | ||
value, | ||
to: lockAddress, | ||
} | ||
txs.push(purchaseTxRequest) | ||
return txs | ||
} |
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
Oops, something went wrong.