From cbebe6ae66ef2bd31d802791d6257e532c44a810 Mon Sep 17 00:00:00 2001 From: none00y Date: Mon, 17 Jun 2024 10:09:42 +0200 Subject: [PATCH] add transactions to erc20 --- sdk/src/erc20.ts | 61 +++++++++++---------- sdk/tests/fungible-token.test.ts | 93 +++++++++++++++++++++++++++++++- 2 files changed, 125 insertions(+), 29 deletions(-) diff --git a/sdk/src/erc20.ts b/sdk/src/erc20.ts index 5d5755a..ab62e17 100644 --- a/sdk/src/erc20.ts +++ b/sdk/src/erc20.ts @@ -89,56 +89,63 @@ export class FungibleToken { return this.erc20.erc20.totalSupply(DEFAULT_ADDRESS) } + async approveTx(spender: ActorId, amount: bigint) { + return this.erc20.erc20.approve(spender as any, amount as any).withGas(this.gasLimit) + } + async approve(owner: Signer, spender: ActorId, amount: bigint): Promise { - const tx = await this.erc20.erc20 - .approve(spender as any, amount as any) - .withAccount(owner) - .withGas(this.gasLimit) - const { response } = await tx.signAndSend() + const tx = await this.approveTx(spender, amount) + const { response } = await tx.withAccount(owner).signAndSend() return response() } - async burn(account: ActorId, amount: bigint) { + async burnTx(account: ActorId, amount: bigint) { if (!this.admin) { throw new Error('Admin account is required to burn tokens') } - const tx = await this.erc20.admin - .burn(account as any, amount as any) - .withAccount(this.admin) - .withGas(this.gasLimit) - const { response } = await tx.signAndSend() + return this.erc20.admin.burn(account as any, amount as any).withGas(this.gasLimit) + } + + async burn(account: ActorId, amount: bigint) { + const tx = await this.burnTx(account, amount) + const { response } = await tx.withAccount(this.admin!).signAndSend() return response() } - async mint(account: ActorId, amount: bigint) { + async mintTx(account: ActorId, amount: bigint) { if (!this.admin) { - throw new Error('Admin account is required to burn tokens') + throw new Error('Admin account is required to mint tokens') } - const tx = await this.erc20.admin - .mint(account as any, amount as any) - .withAccount(this.admin) - .withGas(this.gasLimit) - const { response } = await tx.signAndSend() + return this.erc20.admin.mint(account as any, amount as any).withGas(this.gasLimit) + } + + async mint(account: ActorId, amount: bigint) { + const tx = await this.mintTx(account, amount) + const { response } = await tx.withAccount(this.admin!).signAndSend() return response() } + async transferTx(to: ActorId, amount: bigint) { + return this.erc20.erc20.transfer(to as any, amount as any).withGas(this.gasLimit) + } + async transfer(signer: Signer, to: ActorId, amount: bigint) { - const tx = await this.erc20.erc20 - .transfer(to as any, amount as any) - .withAccount(signer) - .withGas(this.gasLimit) - const { response } = await tx.signAndSend() + const tx = await this.transferTx(to, amount) + const { response } = await tx.withAccount(signer).signAndSend() return response() } - async transferFrom(signer: Signer, from: ActorId, to: ActorId, amount: bigint) { - const tx = await this.erc20.erc20 + async transferFromTx(from: ActorId, to: ActorId, amount: bigint) { + return this.erc20.erc20 .transferFrom(from as any, to as any, amount as any) - .withAccount(signer) .withGas(this.gasLimit) - const { response } = await tx.signAndSend() + } + + async transferFrom(signer: Signer, from: ActorId, to: ActorId, amount: bigint) { + const tx = await this.transferFromTx(from, to, amount) + const { response } = await tx.withAccount(signer).signAndSend() return response() } } diff --git a/sdk/tests/fungible-token.test.ts b/sdk/tests/fungible-token.test.ts index 243cbb6..6242101 100644 --- a/sdk/tests/fungible-token.test.ts +++ b/sdk/tests/fungible-token.test.ts @@ -12,7 +12,7 @@ const account1 = await GearKeyring.fromSuri('//Bob') let unsub: Promise | null = null const eventListener = new EventListener(api) eventListener.listen() -let token = await FungibleToken.deploy(api, account0, 'Coin', 'COIN', 12n) +let token: FungibleToken = await FungibleToken.deploy(api, account0, 'Coin', 'COIN', 12n) describe('FungibleToken', function () { before(function () { unsub = subscribeToNewHeads(api) @@ -34,6 +34,27 @@ describe('FungibleToken', function () { assert.deepStrictEqual(balance, 0n) }) }) + it('mint and burn tx', async function () { + { + const { response } = await (await token.mintTx(account0.addressRaw, 100n)) + .withAccount(account0) + .signAndSend() + assert.strictEqual(await response(), true) + } + + await token.balanceOf(account0.addressRaw).then(balance => { + assert.deepStrictEqual(balance, 100n) + }) + { + const { response } = await (await token.burnTx(account0.addressRaw, 100n)) + .withAccount(account0) + .signAndSend() + assert.strictEqual(await response(), true) + } + await token.balanceOf(account0.addressRaw).then(balance => { + assert.deepStrictEqual(balance, 0n) + }) + }) it('approve and transfer', async () => { { const res = await token.mint(account1.addressRaw, 100n) @@ -60,7 +81,8 @@ describe('FungibleToken', function () { assert.strictEqual(res, true) } await assertThrowsAsync( - token.transferFrom(account0, account1.addressRaw, account0.addressRaw, 300n) + token.transferFrom(account0, account1.addressRaw, account0.addressRaw, 300n), + 'Error: Panic occurred: InsufficientAllowance' ) { const res = await token.approve(account1, account0.addressRaw, 300n) @@ -81,6 +103,73 @@ describe('FungibleToken', function () { expect(await totalSupply).to.equal(400n) } }) + it('approve and transfer tx', async () => { + { + const { response } = await (await token.mintTx(account1.addressRaw, 100n)) + .withAccount(account0) + .signAndSend() + assert.strictEqual(await response(), true) + } + { + const { response } = await (await token.approveTx(account0.addressRaw, 100n)) + .withAccount(account1) + .signAndSend() + assert.strictEqual(await response(), true) + } + { + const allowance = token.allowance(account1.addressRaw, account0.addressRaw) + const balance0 = token.balanceOf(account0.addressRaw) + const balance1 = token.balanceOf(account1.addressRaw) + const totalSupply = token.totalSupply() + const decimals = token.decimals() + assert.deepStrictEqual(await allowance, 100n, 'allowance mismatch') + assert.deepStrictEqual(await balance0, 0n, 'balance0 mismatch') + assert.deepStrictEqual(await balance1, 100n, 'balance1 mismatch') + assert.deepStrictEqual(await totalSupply, 100n, 'totalSupply mismatch') + assert.deepStrictEqual(await decimals, 12n, 'decimals mismatch') + } + { + const { response } = await (await token.mintTx(account1.addressRaw, 300n)) + .withAccount(account0) + .signAndSend() + assert.strictEqual(await response(), true) + } + await assertThrowsAsync( + (async () => { + const { response } = await ( + await token.transferFromTx(account1.addressRaw, account0.addressRaw, 300n) + ) + .withAccount(account0) + .signAndSend() + assert.strictEqual(await response(), true) + })(), + 'Error: Panic occurred: InsufficientAllowance' + ) + { + const { response } = await (await token.approveTx(account0.addressRaw, 300n)) + .withAccount(account1) + .signAndSend() + assert.strictEqual(await response(), true) + } + { + const { response } = await ( + await token.transferFromTx(account1.addressRaw, account0.addressRaw, 300n) + ) + .withAccount(account0) + .signAndSend() + assert.strictEqual(await response(), true) + } + { + const allowance = token.allowance(account1.addressRaw, account0.addressRaw) + const balance0 = token.balanceOf(account0.addressRaw) + const balance1 = token.balanceOf(account1.addressRaw) + const totalSupply = token.totalSupply() + expect(await allowance).to.equal(0n) + expect(await balance0).to.equal(300n) + expect(await balance1).to.equal(100n) + expect(await totalSupply).to.equal(400n) + } + }) after(async function () { await unsub!.then(unsub => unsub()) })