Skip to content

Commit

Permalink
feat: Mint and Burn tokens (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
stanleyjones authored May 25, 2023
1 parent c81600d commit 237ecaa
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 15 deletions.
11 changes: 0 additions & 11 deletions src/network/modules/ledger/ledger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ interface Ledger extends NetworkModule {
_namespace_: string
info: () => Promise<LedgerInfo>
balance: (address?: string, symbols?: string[]) => Promise<Balances>
mint: () => Promise<unknown>
burn: () => Promise<unknown>
send: (
data: LedgerSendParam,
opts: { nonce?: ArrayBuffer },
Expand All @@ -30,15 +28,6 @@ export const Ledger: Ledger = {
const res = await this.call("ledger.balance", m)
return getBalance(res)
},

mint() {
throw new Error("Not implemented")
},

async burn() {
throw new Error("Not implemented")
},

async send(
param: LedgerSendParam,
{ nonce } = { nonce: makeRandomBytes(16) },
Expand Down
30 changes: 27 additions & 3 deletions src/network/modules/tokens/__tests__/tokens.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe("Tokens", () => {
})
describe("update", () => {
it("should return information for the updated token", async () => {
const mockCall = jest.fn(async () => {})
const mockCall = jest.fn(async () => { })
const tokens = setupModule(Tokens, mockCall)

tokens.update({
Expand All @@ -56,7 +56,7 @@ describe("Tokens", () => {
})
describe("addExtendedInfo", () => {
it("should return information for the updated token", async () => {
const mockCall = jest.fn(async () => {})
const mockCall = jest.fn(async () => { })
const tokens = setupModule(Tokens, mockCall)

tokens.addExtendedInfo({
Expand All @@ -69,14 +69,38 @@ describe("Tokens", () => {
})
describe("removeExtendedInfo", () => {
it("should return information for the updated token", async () => {
const mockCall = jest.fn(async () => {})
const mockCall = jest.fn(async () => { })
const tokens = setupModule(Tokens, mockCall)

tokens.removeExtendedInfo({
address: mockTokenAddress,
indices: [1],
})

expect(mockCall).toHaveBeenCalled()
})
})
describe("mint", () => {
it("should mint some tokens", async () => {
const mockCall = jest.fn(async () => { })
const tokens = setupModule(Tokens, mockCall)

const symbol = mockTokenString
const addresses = { maa: BigInt(1000) }
tokens.mint({ symbol, addresses })

expect(mockCall).toHaveBeenCalled()
})
})
describe("burn", () => {
it("should burn some tokens", async () => {
const mockCall = jest.fn(async () => { })
const tokens = setupModule(Tokens, mockCall)

const symbol = mockTokenString
const addresses = { maa: BigInt(1000) }
tokens.burn({ symbol, addresses })

expect(mockCall).toHaveBeenCalled()
})
})
Expand Down
32 changes: 31 additions & 1 deletion src/network/modules/tokens/tokens.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Address } from "../../../identity"
import { Message } from "../../../message"
import { tag } from "../../../message/cbor"
import { makeRandomBytes } from "../../../utils"
import {
Expand All @@ -10,6 +9,7 @@ import {
TokensAddExtendedParam,
TokensCreateParam,
TokensInfoParam,
TokensMintBurnParam,
TokensModule,
TokensRemoveExtendedParam,
TokensUpdateParam,
Expand Down Expand Up @@ -55,6 +55,22 @@ export const Tokens: TokensModule = {
const data = makeTokensRemoveExtendedData(param)
await this.call("tokens.removeExtendedInfo", data, { nonce })
},
async mint(
param: TokensMintBurnParam,
{ nonce } = { nonce: makeRandomBytes(16) },
) {
return await this.call("tokens.mint", makeTokensMintParam(param), {
nonce,
})
},
async burn(
param: TokensMintBurnParam,
{ nonce } = { nonce: makeRandomBytes(16) },
) {
return await this.call("tokens.burn", makeTokensBurnParam(param), {
nonce,
})
},
}

// Make maps from objects
Expand Down Expand Up @@ -117,6 +133,20 @@ function makeTokensRemoveExtendedData(
return data
}

function makeTokensMintParam(param: TokensMintBurnParam) {
const data = new Map()
data.set(0, param.symbol)
data.set(1, new Map(Object.entries(param.addresses)))
return data
}
function makeTokensBurnParam(param: TokensMintBurnParam) {
const data = new Map()
data.set(0, param.symbol)
data.set(1, new Map(Object.entries(param.addresses)))
data.set(3, true)
return data
}

// Get objects from maps

export function getTokenInfo(data: Map<number, any>): TokenInfo {
Expand Down
17 changes: 17 additions & 0 deletions src/network/modules/tokens/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ export interface TokensRemoveExtendedParam {
indices: AttrIndex[]
}

export interface TokensAddressMap {
[address: string]: bigint
}

export interface TokensMintBurnParam {
symbol: string
addresses: TokensAddressMap
}

export interface TokensModule extends NetworkModule {
info: (data: TokensInfoParam) => Promise<TokenInfo>
create: (
Expand All @@ -76,4 +85,12 @@ export interface TokensModule extends NetworkModule {
data: TokensRemoveExtendedParam,
opts?: { nonce?: ArrayBuffer },
) => void
mint: (
data: TokensMintBurnParam,
opts?: { nonce?: ArrayBuffer },
) => Promise<void>
burn: (
data: TokensMintBurnParam,
opts?: { nonce?: ArrayBuffer },
) => Promise<TokensAddressMap>
}

0 comments on commit 237ecaa

Please sign in to comment.