Skip to content

Commit

Permalink
Make MFA active for all raffles
Browse files Browse the repository at this point in the history
  • Loading branch information
nebolax committed Mar 29, 2024
1 parent 8834797 commit 73270c1
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 74 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@squirrel-labs/peanut-sdk",
"version": "0.4.22",
"version": "0.4.23",
"description": "The Peanut Protocol SDK! Check out the documentation at https://docs.peanut.to",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
7 changes: 3 additions & 4 deletions src/data/contracts.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"Bv4.2": "0xE8d82ce1Af4f3497836449E0B2E8Dd545d2D129a",
"v4.3": "0x70b2132A0A39152722Dd3dA516658626A81Ab02B",
"Bv4.3": "0xf39BAd96f0586a5A4357FC7d4683ea101E7BD66a",
"Bv4.4": "0x5E8Be55D83BFF798B998fCC72b1091029b48D36B",
"Bv4.4": "0x5746f5F68705AE7a6f3Ce8D2F2A444FFa65411a4",
"v4.4": "0xA368cB964EaBC5CD37238A872bcaDee29f1Ae714"
},
"137": {
Expand All @@ -58,7 +58,7 @@
"v4.3": "0x5746f5F68705AE7a6f3Ce8D2F2A444FFa65411a4",
"Bv4.3": "0x6211ACCDa9cF4768684eb65B9941FCc4baf43753",
"v4.4": "0x3B92cB30bc3C9AD8Ae24F16E73dC59e08C520f92",
"Bv4.4": "0xAAD41bfa68B0fFe2b07143470D4d5705B3329586"
"Bv4.4": "0xC9bDba1764C4E870E839319A7d9522fFEa97B3d0"
},
"80001": {
"name": "polygon-mumbai",
Expand Down Expand Up @@ -107,8 +107,7 @@
"Bv4.2": "0x7560Ab3ffAD59D09aEA7b7Fc53E52664d5E71c74",
"Bv4.3": "0xF681c5B378882a3D8Df5170B579e1503EB97C275",
"v4.3": "0x43B90099a203957F1adf35Dde15ac88b3e323e75",
"v4.4": "0x7f573556E25Fc952838bE811612136DE1522992B",
"Bv4.4": "0x62aB649a32ED7494403F436608b09bF14dFB9e6D"
"v4.4": "0x7f573556E25Fc952838bE811612136DE1522992B"
},
"100": {
"name": "xdai-mainnet",
Expand Down
5 changes: 5 additions & 0 deletions src/data/peanutBatcherV4.4.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@
"name": "_pubKeys20",
"type": "address[]",
"internalType": "address[]"
},
{
"name": "_withMFAs",
"type": "bool[]",
"internalType": "bool[]"
}
],
"outputs": [
Expand Down
54 changes: 16 additions & 38 deletions src/raffle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,8 @@ export async function prepareRaffleDepositTxs({
)
}

if ([2, 3].includes(linkDetails.tokenType) && withMFA) {
throw new interfaces.SDKStatus(
interfaces.EPrepareCreateTxsStatusCodes.ERROR_VALIDATING_LINK_DETAILS,
'MFA raffles are only supported for ETH and ERC20 tokens'
)
}


// For simplicity doing raffles always on these contracts
const peanutContractVersion = 'v4.3'
const peanutContractVersion = 'v4.4'
const batcherContractVersion = 'Bv4.4'

if (!provider) {
Expand Down Expand Up @@ -209,35 +201,21 @@ export async function prepareRaffleDepositTxs({
}
}

let depositTxRequest: TransactionRequest
if (withMFA) {
assert([0, 1].includes(linkDetails.tokenType), 'MFA works only for ETH and ERC20')

// Call the specific old function that supports MFA
depositTxRequest = await batcherContract.populateTransaction.batchMakeDepositRaffleMFA(
peanutVaultAddress,
linkDetails.tokenAddress,
linkDetails.tokenType,
amounts,
pubKey20,
txOptions
)
} else {
// Call the general function that supports all tokens (but doesn't support MFA)
const args = [
peanutVaultAddress,
Array(numberOfLinks).fill(linkDetails.tokenAddress),
Array(numberOfLinks).fill(linkDetails.tokenType),
amounts,
tokenIds,
Array(numberOfLinks).fill(pubKey20)
]
config.verbose && console.log('Deposit arguments:', args)
depositTxRequest = await batcherContract.populateTransaction.batchMakeDepositArbitrary(
...args,
txOptions,
)
}
// Call the general function that supports raffles with all tokens
const batchingArgs = [
peanutVaultAddress,
Array(numberOfLinks).fill(linkDetails.tokenAddress),
Array(numberOfLinks).fill(linkDetails.tokenType),
amounts,
tokenIds,
Array(numberOfLinks).fill(pubKey20),
Array(numberOfLinks).fill(withMFA),
]
config.verbose && console.log('Deposit arguments:', batchingArgs)
const depositTxRequest = await batcherContract.populateTransaction.batchMakeDepositArbitrary(
...batchingArgs,
txOptions,
)
const depositTx = ethersV5ToPeanutTx(depositTxRequest)

const unsignedTxs: interfaces.IPeanutUnsignedTransaction[] = []
Expand Down
2 changes: 1 addition & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export async function signWithdrawalMessage(
onlyRecipientMode?: boolean // only for v4.2+
) {
let claimParams: any[]
if (vaultVersion == 'v4.2' || vaultVersion == 'v4.3') {
if (['v4.2', 'v4.3', 'v4.4'].includes(vaultVersion)) {
const extraData = onlyRecipientMode ? RECIPIENT_WITHDRAWAL_MODE : ANYONE_WITHDRAWAL_MODE
const stringHash = ethers.utils.solidityKeccak256(
['bytes32', 'uint256', 'address', 'uint256', 'address', 'bytes32'],
Expand Down
40 changes: 10 additions & 30 deletions test/live/raffle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,27 +79,28 @@ describe('raffle', () => {
}, 120000)

test('create a ERC1155 raffle', async () => {
const chainId = '11155111'
const chainId = '137'
const provider = await getDefaultProvider(chainId)
const wallet = new Wallet(TEST_WALLET_PRIVATE_KEY, provider)
config.verbose && console.log('Using wallet:', wallet.address)

const password = await getRandomString()
const numberOfLinks = 3
const numberOfLinks = 2
const withMFA = true
const linkDetails: interfaces.IPeanutLinkDetails = {
chainId,
tokenType: 3,
tokenAmount: 0,
tokenDecimals: 18,
tokenAddress: '0xcBA33566e07d7527ec14ff774b668eF8880e24C5',
tokenAddress: '0xb9223784be9f67b12ef6F13049ce7180695aAf73',
}
const { unsignedTxs } = await prepareRaffleDepositTxs({
linkDetails,
numberOfLinks,
tokenIds: [BigNumber.from(13), BigNumber.from(14), BigNumber.from(15)],
tokenIds: [BigNumber.from(6), BigNumber.from(7)],
password,
userAddress: wallet.address,
withMFA: false,
withMFA,
})

console.log({ unsignedTxs })
Expand All @@ -122,7 +123,7 @@ describe('raffle', () => {
numberOfLinks,
password,
name: 'baobob',
withMFA: false,
withMFA,
withCaptcha: false,
APIKey,
baseUrl: 'http://localhost:8000/submit-raffle-link',
Expand All @@ -139,49 +140,28 @@ describe('raffle', () => {
console.log('Raffle info!', info)
}, 120000)

test('claim raffle link no mfa', async () => {
test('claim raffle link', async () => {
// ETH raffle link
// const link =
// 'https://red.peanut.to/packet?c=137&v=v4.2&i=637,638,639,640,641,642,643,644,645,646&t=ui#p=rTe4ve5LkxcHbZVb'

// ERC-1155 raffle link
const link = 'https://peanut.to/claim?c=11155111&v=v4.3&i=(568,3)#p=ySkm12fpISGDqaP2'
const recipientAddress = '0xa3635c5A3BFb209b5caF76CD4A9CD33De65e2f72'
console.log({ recipient: recipientAddress })
const claimInfo = await claimRaffleLink({
link,
APIKey,
recipientAddress,
recipientName: 'amobest',
baseUrlAuth: 'http://localhost:8000/get-authorisation',
baseUrlClaim: 'http://localhost:8000/claim-v2',
})
console.log('Claimed a raffle slot!!', claimInfo)
const leaderboard = await getRaffleLeaderboard({
link,
APIKey,
baseUrl: 'http://localhost:8000/get-raffle-leaderboard'
})
console.log('Hooouray, leaderboard!', { leaderboard })
}, 120000)

test('claim raffle link with mfa', async () => {
const link = 'https://peanut.to/claim?c=137&v=v4.3&i=(57,3)#p=cWBDOZyqwwNaTyaW'
const link = 'https://peanut.to/claim?c=137&v=v4.4&i=(12,2)#p=MzsYuUlSwD1gU21M'
const recipientAddress = makeRandomAddress()
console.log({ recipient: recipientAddress })
const claimInfo = await claimRaffleLink({
link,
APIKey,
recipientAddress,
recipientName: 'amobest',
captchaResponse: 'using test recaptcha api key, so captcha is always valid',
baseUrlAuth: 'http://localhost:8000/get-authorisation',
baseUrlClaim: 'http://localhost:8000/claim-v2',
})
console.log('Claimed a raffle slot!!', claimInfo)
const leaderboard = await getRaffleLeaderboard({
link,
APIKey,
baseUrl: 'http://localhost:8000/get-raffle-leaderboard'
})
console.log('Hooouray, leaderboard!', { leaderboard })
}, 120000)
Expand Down

0 comments on commit 73270c1

Please sign in to comment.