Skip to content

Commit

Permalink
add mrc20 methods
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjah committed Aug 9, 2024
1 parent 27b9fd0 commit 7a17213
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 4 deletions.
85 changes: 81 additions & 4 deletions src/contracts-wrappers/token.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,33 @@
import { Args, U256 } from '../basicElements'
import { Args, bytesToStr, U256, U8 } from '../basicElements'
import { Operation } from '../operation'
import { ReadSCOptions, SmartContract } from '../smartContracts'
import { CallSCOptions, ReadSCOptions, SmartContract } from '../smartContracts'

export class MRC20 extends SmartContract {
async version(options?: ReadSCOptions): Promise<string> {
const res = await this.read('version', undefined, options)
return bytesToStr(res.value)
}

async name(options?: ReadSCOptions): Promise<string> {
const res = await this.read('name', undefined, options)
return bytesToStr(res.value)
}

async symbol(options?: ReadSCOptions): Promise<string> {
const res = await this.read('symbol', undefined, options)
return bytesToStr(res.value)
}

async decimals(options?: ReadSCOptions): Promise<number> {
const res = await this.read('decimals', undefined, options)
return Number(U8.fromBytes(res.value))
}

async totalSupply(options?: ReadSCOptions): Promise<bigint> {
const res = await this.read('totalSupply', undefined, options)
return U256.fromBytes(res.value)
}

async balanceOf(address: string, options?: ReadSCOptions): Promise<bigint> {
const res = await this.read(
'balanceOf',
Expand All @@ -15,13 +40,65 @@ export class MRC20 extends SmartContract {
async transfer(
to: string,
amount: bigint,
options?: ReadSCOptions
options?: CallSCOptions
): Promise<Operation> {
return this.call(
'transfer',
new Args().addString(to).addU256(amount),
options
)
}
// TODO: Implement the rest of the functions

async allowance(
ownerAddress: string,
spenderAddress: string,
options?: ReadSCOptions
): Promise<bigint> {
const res = await this.read(
'allowance',
new Args().addString(ownerAddress).addString(spenderAddress),
options
)
return U256.fromBytes(res.value)
}

async increaseAllowance(
spenderAddress: string,
amount: bigint,
options?: CallSCOptions
): Promise<Operation> {
return this.call(
'increaseAllowance',
new Args().addString(spenderAddress).addU256(amount),
options
)
}

async decreaseAllowance(
spenderAddress: string,
amount: bigint,
options?: CallSCOptions
): Promise<Operation> {
return this.call(
'decreaseAllowance',
new Args().addString(spenderAddress).addU256(amount),
options
)
}

async transferFrom(
spenderAddress: string,
recipientAddress: string,
amount: bigint,
options?: CallSCOptions
): Promise<Operation> {
return this.call(
'transferFrom',
new Args()
.addString(spenderAddress)
.addString(recipientAddress)
.addU256(amount),
options
)
}
}
58 changes: 58 additions & 0 deletions test/integration/MRC20.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,31 @@ describe('Token wrapper tests', () => {
usdcContract = new MRC20(provider, USDC)
})

test('version', async () => {
const version = await usdcContract.version()
expect(version).toBe('0.0.1')
})

test('name', async () => {
const name = await usdcContract.name()
expect(name).toBe('Sepolia USDC')
})

test('symbol', async () => {
const symbol = await usdcContract.symbol()
expect(symbol).toBe('USDC.s')
})

test('decimals', async () => {
const decimals = await usdcContract.decimals()
expect(decimals).toBe(6)
})

test('totalSupply', async () => {
const totalSupply = await usdcContract.totalSupply()
expect(totalSupply).toBeGreaterThan(0n)
})

test('transfer', async () => {
const amount = 1_000n
const balance = await usdcContract.balanceOf(provider.address)
Expand All @@ -23,4 +48,37 @@ describe('Token wrapper tests', () => {
const newBalance = await usdcContract.balanceOf(provider.address)
expect(newBalance).toBe(balance - amount)
})

test('allowance', async () => {
const previousAllowance = await usdcContract.allowance(
provider.address,
'AU1wN8rn4SkwYSTDF3dHFY4U28KtsqKL1NnEjDZhHnHEy6cEQm53'
)

const amount = 123_000_000n
let operation = await usdcContract.increaseAllowance(
'AU1wN8rn4SkwYSTDF3dHFY4U28KtsqKL1NnEjDZhHnHEy6cEQm53',
amount
)
await operation.waitSpeculativeExecution()

let newAllowance = await usdcContract.allowance(
provider.address,
'AU1wN8rn4SkwYSTDF3dHFY4U28KtsqKL1NnEjDZhHnHEy6cEQm53'
)

expect(newAllowance).toBe(previousAllowance + amount)

operation = await usdcContract.decreaseAllowance(
'AU1wN8rn4SkwYSTDF3dHFY4U28KtsqKL1NnEjDZhHnHEy6cEQm53',
amount
)
await operation.waitSpeculativeExecution()

newAllowance = await usdcContract.allowance(
provider.address,
'AU1wN8rn4SkwYSTDF3dHFY4U28KtsqKL1NnEjDZhHnHEy6cEQm53'
)
expect(newAllowance).toBe(previousAllowance)
})
})

1 comment on commit 7a17213

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage report for experimental massa-web3

St.
Category Percentage Covered / Total
🟡 Statements 66.12% 1136/1718
🔴 Branches 47.04% 191/406
🔴 Functions 48.39% 211/436
🟡 Lines 66.35% 1128/1700

Test suite run success

130 tests passing in 13 suites.

Report generated by 🧪jest coverage report action from 7a17213

Please sign in to comment.