Skip to content

Commit

Permalink
sdk token returns contractId, gets metadata from node
Browse files Browse the repository at this point in the history
  • Loading branch information
DevRozaDev committed Aug 1, 2024
1 parent ef32027 commit f80c6a3
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 76 deletions.
74 changes: 37 additions & 37 deletions src/fungible-token.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
import { DUST_AMOUNT, hexToString, SignerProvider, TransactionBuilder } from '@alephium/web3'
import {
addressFromContractId,
DUST_AMOUNT,
hexToString,
NodeProvider,
SignerProvider,
TransactionBuilder
} from '@alephium/web3'
import { Network } from './network'
import { TokenFaucet, Withdraw } from '../artifacts/ts'
import { balanceOf, getNodeUrl, signAndSend, waitTxConfirmed } from './utils'
import { MAX_U256 } from './consts'

export type TokenMetadata = {
name: string
export type TokenMetaData = {
symbol: string
name: string
decimals: bigint
totalSupply: bigint
}

export class FungibleToken {
network: Network
nodeUrl: string
nodeProvider: NodeProvider
builder: TransactionBuilder

constructor(network: Network) {
private constructor(network: Network) {
this.network = network
this.nodeUrl = getNodeUrl(network)
this.builder = TransactionBuilder.from(this.nodeUrl)
const nodeUrl = getNodeUrl(network)
this.nodeProvider = new NodeProvider(nodeUrl)
this.builder = TransactionBuilder.from(nodeUrl)
}

static async deploy(
Expand Down Expand Up @@ -49,14 +58,15 @@ export class FungibleToken {
attoAlphAmount: DUST_AMOUNT
})

return deployResult.contractInstance.address
return deployResult.contractInstance.contractId
}

static async load(network: Network): Promise<FungibleToken> {
return new FungibleToken(network)
}

async mintTx(signer: SignerProvider, value: bigint, tokenAddress: string) {
async mintTx(signer: SignerProvider, value: bigint, tokenId: string) {
const tokenAddress = addressFromContractId(tokenId)
const tokenFaucet = TokenFaucet.at(tokenAddress)
const bytecode = Withdraw.script.buildByteCodeToDeploy({
token: tokenFaucet.contractId,
Expand All @@ -72,8 +82,8 @@ export class FungibleToken {
return unsignedTxBuild
}

async mint(signer: SignerProvider, value: bigint, tokenAddress: string) {
const tx = await this.mintTx(signer, value, tokenAddress)
async mint(signer: SignerProvider, value: bigint, tokenId: string) {
const tx = await this.mintTx(signer, value, tokenId)
return await signAndSend(signer, tx)
}

Expand All @@ -83,45 +93,35 @@ export class FungibleToken {
return new Map(tokens.map((token, i) => [token, balances[i]]))
}

async getBalanceOf(owner: string, tokenAddress: string): Promise<bigint> {
const tokenId = this.getContractId(tokenAddress)
async getBalanceOf(owner: string, tokenId: string): Promise<bigint> {
return balanceOf(tokenId, owner)
}

async getTokenMetadataMulti(tokenAddresses: Array<string>): Promise<Map<string, TokenMetadata>> {
const metadata = await Promise.all(
tokenAddresses.map((tokenAddress, _) => this.getTokenMetadata(tokenAddress))
)
async getTokenMetaDataMulti(tokenId: Array<string>): Promise<Map<string, TokenMetaData>> {
const metadata = await Promise.all(tokenId.map((tokenId, _) => this.getTokenMetadata(tokenId)))

return new Map(tokenAddresses.map((tokenAddress, i) => [tokenAddress, metadata[i]]))
return new Map(tokenId.map((tokenId, i) => [tokenId, metadata[i]]))
}

async getTokenMetadata(tokenAddress: string): Promise<TokenMetadata> {
const token = TokenFaucet.at(tokenAddress)
const result = await token.multicall({ getSymbol: {}, getName: {}, getDecimals: {} })
async getTokenMetadata(tokenId: string): Promise<TokenMetaData> {
const metadata = await this.nodeProvider.fetchFungibleTokenMetaData(tokenId)
return {
name: hexToString(result.getName.returns),
symbol: hexToString(result.getSymbol.returns),
decimals: result.getDecimals.returns
symbol: hexToString(metadata.symbol),
name: hexToString(metadata.name),
decimals: BigInt(metadata.decimals),
totalSupply: BigInt(metadata.totalSupply)
}
}

async getTokenName(tokenAddress: string): Promise<string> {
const token = TokenFaucet.at(tokenAddress)
return hexToString((await token.view.getName()).returns)
}

async getTokenSymbol(tokenAddress: string): Promise<string> {
const token = TokenFaucet.at(tokenAddress)
return hexToString((await token.view.getSymbol()).returns)
async getTokenName(tokenId: string): Promise<string> {
return hexToString((await this.nodeProvider.fetchFungibleTokenMetaData(tokenId)).name)
}

async getTokenDecimals(tokenAddress: string): Promise<bigint> {
const token = TokenFaucet.at(tokenAddress)
return (await token.view.getDecimals()).returns
async getTokenSymbol(tokenId: string): Promise<string> {
return hexToString((await this.nodeProvider.fetchFungibleTokenMetaData(tokenId)).symbol)
}

getContractId(tokenAddress: string): string {
return TokenFaucet.at(tokenAddress).contractId
async getTokenDecimals(tokenId: string): Promise<bigint> {
return BigInt((await this.nodeProvider.fetchFungibleTokenMetaData(tokenId)).decimals)
}
}
5 changes: 2 additions & 3 deletions src/invariant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ export class Invariant {

return pages
}
// async getPoolKeys() {}

async getAllPoolKeys() {
const [poolKeys, poolKeysCount] = await this.getPoolKeys(MAX_POOL_KEYS_QUERIED, 0n)

Expand All @@ -615,7 +615,6 @@ export class Invariant {
return [...poolKeys, ...poolKeysEntries.map(([poolKeys]) => poolKeys).flat()]
}

// async getPositionTicks() {}
async getRawTickmap(
poolKey: PoolKey,
lowerBatch: bigint,
Expand Down Expand Up @@ -672,7 +671,7 @@ export class Invariant {
const liquidityTicks = await Promise.all(promises)
return liquidityTicks.flat()
}
// async getUserPositionAmount() {}

async getLiquidityTicksAmount(poolKey: PoolKey, lowerTick: bigint, upperTick: bigint) {
const response = await this.instance.view.getLiquidityTicksAmount({
args: { poolKey, lowerTick, upperTick }
Expand Down
52 changes: 25 additions & 27 deletions test/sdk/e2e/fungible-token.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,54 +6,52 @@ import { PrivateKeyWallet } from '@alephium/web3-wallet'

web3.setCurrentNodeProvider('http://127.0.0.1:22973')

const token = new FungibleToken(Network.Local)
let token: FungibleToken
let admin: PrivateKeyWallet
let token0Address: string
let token0: string

describe('fungible token tests', () => {
beforeAll(async () => {
admin = await getSigner(ONE_ALPH * 1000n, 0)
token0Address = await FungibleToken.deploy(admin, 1000n, 'Coin', 'COIN', 12n)
token = await FungibleToken.load(Network.Local)
token0 = await FungibleToken.deploy(admin, 1000n, 'Coin', 'COIN', 12n)
})

test('set metadata', async () => {
expect(await token.getTokenName(token0Address)).toBe('Coin')
expect(await token.getTokenSymbol(token0Address)).toBe('COIN')
expect(await token.getTokenDecimals(token0Address)).toBe(12n)
expect(await token.getTokenName(token0)).toBe('Coin')
expect(await token.getTokenSymbol(token0)).toBe('COIN')
expect(await token.getTokenDecimals(token0)).toBe(12n)
})

test('mint tokens', async () => {
await token.mint(admin, 500n, token0Address)
expect(await token.getBalanceOf(admin.address, token0Address)).toBe(1500n)
await token.mint(admin, 500n, token0)
expect(await token.getBalanceOf(admin.address, token0)).toBe(1500n)
})

test('change instance', async () => {
const secondTokenAddress = await FungibleToken.deploy(admin, 1000n, 'SecondCoin', 'SCOIN', 12n)
const tokenName = await token.getTokenName(secondTokenAddress)
const secondToken = await FungibleToken.deploy(admin, 1000n, 'SecondCoin', 'SCOIN', 12n)
const tokenName = await token.getTokenName(secondToken)
expect(tokenName).toBe('SecondCoin')
})

test('get all balances', async () => {
const token0Address = await FungibleToken.deploy(admin, 0n, 'Coin', 'COIN', 12n)
const token1Address = await FungibleToken.deploy(admin, 0n, 'Coin', 'COIN', 12n)
const token2Address = await FungibleToken.deploy(admin, 0n, 'Coin', 'COIN', 12n)
const token3Address = await FungibleToken.deploy(admin, 0n, 'Coin', 'COIN', 12n)
const token0 = await FungibleToken.deploy(admin, 0n, 'Coin', 'COIN', 12n)
const token1 = await FungibleToken.deploy(admin, 0n, 'Coin', 'COIN', 12n)
const token2 = await FungibleToken.deploy(admin, 0n, 'Coin', 'COIN', 12n)
const token3 = await FungibleToken.deploy(admin, 0n, 'Coin', 'COIN', 12n)

await token.mint(admin, 100n, token0Address)
await token.mint(admin, 200n, token1Address)
await token.mint(admin, 300n, token2Address)
await token.mint(admin, 400n, token3Address)
await token.mint(admin, 100n, token0)
await token.mint(admin, 200n, token1)
await token.mint(admin, 300n, token2)
await token.mint(admin, 400n, token3)

const balances = await token.getAllBalances(
[token0Address, token1Address, token2Address, token3Address],
admin.address
)
const balances = await token.getAllBalances([token0, token1, token2, token3], admin.address)

expect(balances.size).toBe(4)
expect(balances.get(token0Address)).toBe(100n)
expect(balances.get(token1Address)).toBe(200n)
expect(balances.get(token2Address)).toBe(300n)
expect(balances.get(token3Address)).toBe(400n)
expect(balances.get(token0)).toBe(100n)
expect(balances.get(token1)).toBe(200n)
expect(balances.get(token2)).toBe(300n)
expect(balances.get(token3)).toBe(400n)
})

test('get metadata for all tokens', async () => {
Expand All @@ -63,7 +61,7 @@ describe('fungible token tests', () => {
await FungibleToken.deploy(admin, 0n, 'CoinTHREE', 'COIN3', 14n),
await FungibleToken.deploy(admin, 0n, 'CoinFOUR', 'COIN4', 15n)
]
const metadata = await token.getTokenMetadataMulti(tokenAddresses)
const metadata = await token.getTokenMetaDataMulti(tokenAddresses)

expect(metadata.size).toBe(4)
expect(metadata.get(tokenAddresses[0])).toMatchObject({
Expand Down
19 changes: 10 additions & 9 deletions test/sdk/e2e/get-position-with-associates.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,31 @@ import { toSqrtPrice } from '../../../src/math'

web3.setCurrentNodeProvider('http://127.0.0.1:22973')

const token = new FungibleToken(Network.Local)
let token: FungibleToken
describe('get position with associates tests', () => {
beforeAll(async () => {
token = await FungibleToken.load(Network.Local)
})
test('get position with associates', async () => {
const deployer = await getSigner(ONE_ALPH * 1000n, 0)
const initialFee = 0n
const invariant = await Invariant.deploy(deployer, Network.Local, initialFee)

const token0Address = await FungibleToken.deploy(deployer, 0n, 'Token0', 'TK0')
const token0ContractId = token.getContractId(token0Address)
const token1Address = await FungibleToken.deploy(deployer, 0n, 'Token1', 'TK1')
const token1ContractId = token.getContractId(token1Address)
const token0 = await FungibleToken.deploy(deployer, 0n, 'Token0', 'TK0')
const token1 = await FungibleToken.deploy(deployer, 0n, 'Token1', 'TK1')

const feeTier = await newFeeTier(...getBasicFeeTickSpacing())
const poolKey = await newPoolKey(token0ContractId, token1ContractId, feeTier)
const poolKey = await newPoolKey(token0, token1, feeTier)

await invariant.addFeeTier(deployer, feeTier)

const positionOwner = await getSigner(ONE_ALPH * 1000n, 0)
const supply = 10n ** 10n
await token.mint(positionOwner, supply, token0Address)
await token.mint(positionOwner, supply, token1Address)
await token.mint(positionOwner, supply, token0)
await token.mint(positionOwner, supply, token1)

const initSqrtPrice = toSqrtPrice(1n)
await invariant.createPool(deployer, token0ContractId, token1ContractId, feeTier, initSqrtPrice)
await invariant.createPool(deployer, token0, token1, feeTier, initSqrtPrice)

const [lowerTickIndex, upperTickIndex] = [-20n, 10n]

Expand Down

0 comments on commit f80c6a3

Please sign in to comment.