Skip to content
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

Add balancesOf method to MRC20 contract and corresponding tests #708

Merged
merged 2 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/contracts-wrappers/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,27 @@
return U256.fromBytes(res.value)
}

async balancesOf(

Check warning on line 88 in src/contracts-wrappers/token.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🕹️ Function is not covered

Warning! Not covered function
addresses: string[],
final = true

Check warning on line 90 in src/contracts-wrappers/token.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
): Promise<
{
address: string
balance: bigint
}[]
> {
const res = await this.provider.readStorage(
this.address,
addresses.map((a) => `BALANCE${a}`),

Check warning on line 99 in src/contracts-wrappers/token.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 99 in src/contracts-wrappers/token.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🕹️ Function is not covered

Warning! Not covered function
final

Check warning on line 100 in src/contracts-wrappers/token.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
)

return res.map((v, i) => ({
address: addresses[i],
balance: v.length ? U256.fromBytes(v) : 0n,
}))
}

async transfer(
to: string,
amount: bigint,
Expand Down
32 changes: 32 additions & 0 deletions test/integration/MRC20.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Account } from '../../src'
import { MRC20 } from '../../src/contracts-wrappers'
import { provider } from './setup'

Expand Down Expand Up @@ -80,4 +81,35 @@ describe('Generic token wrapper tests', () => {
)
expect(newAllowance).toBe(previousAllowance)
})

test('balancesOf', async () => {
const amounts = [1_000n, 2_000n]
const recipientAddresses = [
await (await Account.generate()).address.toString(),
await (await Account.generate()).address.toString(),
]

const b = await usdcContract.balancesOf(recipientAddresses)
for (const { address, balance } of b) {
Ben-Rey marked this conversation as resolved.
Show resolved Hide resolved
expect(balance).toBe(0n)
}

await usdcContract.transfer(recipientAddresses[0], amounts[0])

const operation2 = await usdcContract.transfer(
recipientAddresses[1],
amounts[1]
)
await operation2.waitSpeculativeExecution()

const recipientBalance = await usdcContract.balancesOf(
recipientAddresses,
false
)

expect(recipientBalance[0].balance).toBe(amounts[0])
expect(recipientBalance[0].address).toBe(recipientAddresses[0])
expect(recipientBalance[1].balance).toBe(amounts[1])
expect(recipientBalance[1].address).toBe(recipientAddresses[1])
})
})