Skip to content

Commit

Permalink
Add balancesOf method to MRC20 contract and corresponding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben-Rey committed Nov 27, 2024
1 parent 669671c commit dd84f87
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
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 @@ export class MRC20 extends SmartContract {
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
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
)

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'
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) {
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()

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

0 comments on commit dd84f87

Please sign in to comment.