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 1 commit
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
20 changes: 19 additions & 1 deletion src/contracts-wrappers/token.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Args, bytesToStr, U256, U8 } from '../basicElements'
import { Args, bytesToStr, strToBytes, U256, U8 } from '../basicElements'
import { Operation } from '../operation'
import { CallSCOptions, ReadSCOptions, SmartContract } from '../smartContracts'

Expand Down Expand Up @@ -85,6 +85,24 @@
return U256.fromBytes(res.value)
}

async balancesOf(addresses: string[]): Promise<

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
{
address: string
balance: bigint
}[]
> {
const res = await this.provider.readStorage(
this.address,
addresses.map((a) => strToBytes(`BALANCE${a}`)),

Check warning on line 96 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 96 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
Ben-Rey marked this conversation as resolved.
Show resolved Hide resolved
true

Check warning on line 97 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
Ben-Rey marked this conversation as resolved.
Show resolved Hide resolved
)

return res.map((v, i) => ({

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)

🕹️ Function is not covered

Warning! Not covered function
address: addresses[i],
balance: v.length ? U256.fromBytes(v) : 0n,

Check warning on line 102 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

Check warning on line 102 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
}))

Check warning on line 103 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 103 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
}

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, Account } from '../../src'
Ben-Rey marked this conversation as resolved.
Show resolved Hide resolved
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)
}

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

const operation2 = await usdcContract.transfer(
recipientAddresses[1],
amounts[1]
)
await operation2.waitFinalExecution()
Ben-Rey marked this conversation as resolved.
Show resolved Hide resolved

const recipientBalance = await usdcContract.balancesOf(recipientAddresses)

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])
})
})