Skip to content

Commit

Permalink
Merge pull request #117 from peanutprotocol/prepareClaimLinkSenderTx
Browse files Browse the repository at this point in the history
feat: introduce function to prep tx to reclaim as sender
  • Loading branch information
borcherd authored Mar 1, 2024
2 parents eac9dd9 + aa2c543 commit 7617618
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 3 deletions.
49 changes: 47 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1401,10 +1401,13 @@ async function claimLinkSender({
}: interfaces.IClaimLinkSenderParams): Promise<interfaces.IClaimLinkSenderResponse> {
const signer = structSigner.signer
const chainId = String(await signer.getChainId())
const contract = await getContract(chainId, signer, contractVersion)

if (contractVersion == null) {
getLatestContractVersion({ chainId, type: 'normal' })
contractVersion = getLatestContractVersion({ chainId, type: 'normal' })
}

const contract = await getContract(chainId, signer, contractVersion)

// Prepare transaction options
let txOptions = {}
try {
Expand Down Expand Up @@ -1445,6 +1448,38 @@ async function claimLinkSender({
}
}

async function prepareClaimLinkSenderTx({
depositIndex,
provider = undefined,
chainId,
contractVersion = null,
}: {
depositIndex: number
provider?: ethers.providers.JsonRpcProvider // TODO: update to not use ethers.providers.JsonRpcProvider but just a url
chainId: string
contractVersion?: string
}) {
try {
if (!provider) {
provider = await getDefaultProvider(chainId)
}

if (contractVersion == null) {
contractVersion = getLatestContractVersion({ chainId, type: 'normal' })
}

const contract = await getContract(chainId, provider, contractVersion)

const tx = await contract.populateTransaction.withdrawDepositSender(depositIndex)

const convertedTx = ethersV5ToPeanutTx(tx)

return convertedTx
} catch (error) {
throw new interfaces.SDKStatus(interfaces.EClaimLinkStatusCodes.ERROR, error)
}
}

/**
* Gets all deposits for a given signer and chainId.
*
Expand Down Expand Up @@ -2280,11 +2315,15 @@ async function getAllUnclaimedDepositsWithIdxForAddress({
}
}) // get all address deposits

config.verbose && console.log(addressDeposits)

// filter out deposits not made by the address
addressDeposits = addressDeposits.filter((deposit: any) => {
return deposit.senderAddress.toString() == address.toString()
})

config.verbose && console.log(addressDeposits)

config.verbose && console.log('all deposits made by address: ', addressDeposits)

if (claimedOnly) {
Expand All @@ -2307,6 +2346,8 @@ async function getAllUnclaimedDepositsWithIdxForAddress({
}
}) // get all deposits to map idxs

config.verbose && console.log(mappedDeposits)

mappedDeposits.map((deposit: any, idx) => {
addressDeposits.map((addressDeposit: any) => {
if (compareDeposits(deposit, addressDeposit)) {
Expand Down Expand Up @@ -2777,6 +2818,7 @@ const peanut = {
prepareClaimTx,
claimLinkGasless,
claimLinkSender,
prepareClaimLinkSenderTx,
claimLinkXChainGasless,
config,
createClaimPayload,
Expand Down Expand Up @@ -2847,6 +2889,7 @@ const peanut = {
getTokenContractType,
getTokenContractDetails,
validateUserName,
getTxReceiptFromHash,
...raffle,
}

Expand All @@ -2873,6 +2916,7 @@ export {
prepareClaimTx,
claimLinkGasless,
claimLinkSender,
prepareClaimLinkSenderTx,
claimLinkXChainGasless,
config,
createClaimPayload,
Expand Down Expand Up @@ -2943,4 +2987,5 @@ export {
getTokenContractType,
getTokenContractDetails,
validateUserName,
getTxReceiptFromHash,
}
31 changes: 30 additions & 1 deletion test/live/claimLinkSender.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,36 @@ describe('claimLinkSender tests', function () {
new ethers.providers.JsonRpcBatchProvider(RPC_URL)
)

it('should create a link and claim it using claimLinkSender', async function () {
peanut.toggleVerbose(true)
it('should prepare the correct tx', async function () {
const chainId = '137'

const unclaimedTxs = await peanut.getAllUnclaimedDepositsWithIdxForAddress({
address: WALLET.address,
chainId,
peanutContractVersion: 'v4.3',
})

console.log(unclaimedTxs[unclaimedTxs.length - 1].idx)

const preparedClaimTx = await peanut.prepareClaimLinkSenderTx({
chainId,
depositIndex: unclaimedTxs[unclaimedTxs.length - 1].idx,
})

console.log(preparedClaimTx)

const feeOptions = peanut.setFeeOptions({
provider: WALLET.provider,
chainId: chainId,
})

const tx = { ...preparedClaimTx, ...feeOptions }

console.log({ tx })
}, 1000000000)

it.skip('should create a link and claim it using claimLinkSender', async function () {
return true
// can't be run if 24 hours hasn't passed since the link was created

Expand Down

0 comments on commit 7617618

Please sign in to comment.