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 e6fdaec
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> {

Check warning on line 6 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
const res = await this.read('version', undefined, options)

Check warning on line 7 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 bytesToStr(res.value)

Check warning on line 8 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 name(options?: ReadSCOptions): Promise<string> {

Check warning on line 11 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
const res = await this.read('name', undefined, options)

Check warning on line 12 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 bytesToStr(res.value)

Check warning on line 13 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 symbol(options?: ReadSCOptions): Promise<string> {

Check warning on line 16 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
const res = await this.read('symbol', undefined, options)

Check warning on line 17 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 bytesToStr(res.value)

Check warning on line 18 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 decimals(options?: ReadSCOptions): Promise<number> {

Check warning on line 21 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
const res = await this.read('decimals', undefined, options)

Check warning on line 22 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 Number(U8.fromBytes(res.value))

Check warning on line 23 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 totalSupply(options?: ReadSCOptions): Promise<bigint> {

Check warning on line 26 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
const res = await this.read('totalSupply', undefined, options)

Check warning on line 27 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 U256.fromBytes(res.value)

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

Check warning on line 52 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
ownerAddress: string,
spenderAddress: string,
options?: ReadSCOptions
): Promise<bigint> {
const res = await this.read(
'allowance',
new Args().addString(ownerAddress).addString(spenderAddress),
options

Check warning on line 60 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 U256.fromBytes(res.value)

Check warning on line 62 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 increaseAllowance(

Check warning on line 65 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
spenderAddress: string,
amount: bigint,
options?: CallSCOptions
): Promise<Operation> {
return this.call(
'increaseAllowance',
new Args().addString(spenderAddress).addU256(amount),
options
)

Check warning on line 74 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 decreaseAllowance(

Check warning on line 77 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
spenderAddress: string,
amount: bigint,
options?: CallSCOptions
): Promise<Operation> {
return this.call(
'decreaseAllowance',
new Args().addString(spenderAddress).addU256(amount),
options
)

Check warning on line 86 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 transferFrom(

Check warning on line 89 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
spenderAddress: string,
recipientAddress: string,
amount: bigint,
options?: CallSCOptions
): Promise<Operation> {
return this.call(
'transferFrom',
new Args()
.addString(spenderAddress)
.addString(recipientAddress)
.addU256(amount),
options
)

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)

🧾 Statement is not covered

Warning! Not covered statement
}
}
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)
})
})

0 comments on commit e6fdaec

Please sign in to comment.