Skip to content

Commit

Permalink
add transactions to erc20
Browse files Browse the repository at this point in the history
  • Loading branch information
none00y committed Jun 17, 2024
1 parent f98fa7b commit cbebe6a
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 29 deletions.
61 changes: 34 additions & 27 deletions sdk/src/erc20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean> {
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()
}
}
93 changes: 91 additions & 2 deletions sdk/tests/fungible-token.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const account1 = await GearKeyring.fromSuri('//Bob')
let unsub: Promise<VoidFunction> | 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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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())
})
Expand Down

0 comments on commit cbebe6a

Please sign in to comment.