Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MRC721 metadata #175

Merged
merged 8 commits into from
Dec 9, 2024
8 changes: 3 additions & 5 deletions smart-contracts/assembly/contracts/MRC1155/MRC1155.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,13 @@ import { _setOwner } from '../utils/ownership-internal';
/**
* Constructs a new Multi-NFT contract.
*
* @remarks You must call this function in your contract's constructor or re-write it to fit your needs !
*
* @param uri - the URI for the NFT contract
*/
export function constructor(binaryArgs: StaticArray<u8>): void {
export function mrc1155Constructor(uri: string): void {
assert(isDeployingContract());
const args = new Args(binaryArgs);
const uri = args.nextString().expect('uri argument is missing or invalid');

_setOwner(Context.caller().toString());

_constructor(uri);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import {
import {
balanceOf,
balanceOfBatch,
constructor,
isApprovedForAll,
mrc1155Constructor,
safeBatchTransferFrom,
safeTransferFrom,
setApprovalForAll,
Expand Down Expand Up @@ -48,7 +48,7 @@ beforeEach(() => {
switchUser(user1Address);
resetStorage();
setDeployContext(user1Address);
constructor(new Args().add(stringToBytes(TOKEN_URI)).serialize());
mrc1155Constructor(TOKEN_URI);
});

describe('Initialization', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
import {
balanceOf,
balanceOfBatch,
constructor,
mrc1155Constructor,
setApprovalForAll,
} from '../MRC1155';
import { u256 } from 'as-bignum/assembly';
Expand All @@ -37,7 +37,7 @@ beforeEach(() => {
switchUser(user1Address);
resetStorage();
setDeployContext(user1Address);
constructor(new Args().add(stringToBytes(TOKEN_URI)).serialize());
mrc1155Constructor(TOKEN_URI);
});

describe('burn', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { resetStorage, setDeployContext } from '@massalabs/massa-as-sdk';
import { Args, stringToBytes } from '@massalabs/as-types';
import { constructor } from '../MRC1155';

import { u256 } from 'as-bignum/assembly';
import {
_baseURI,
Expand All @@ -10,6 +10,7 @@ import {
_uri,
uri,
} from '../metadata';
import { mrc1155Constructor } from '../MRC1155';

const user1Address = 'AU12UBnqTHDQALpocVBnkPNy7y5CndUJQTLutaVDDFgMJcq5kQiKq';

Expand All @@ -18,7 +19,7 @@ const TOKEN_URI = 'ipfs://QmW77ZQQ7Jm9q8WuLbH8YZg2K7T9Qnjbzm7jYVQQrJY5Yd';
beforeEach(() => {
resetStorage();
setDeployContext(user1Address);
constructor(new Args().add(stringToBytes(TOKEN_URI)).serialize());
mrc1155Constructor(TOKEN_URI);
});

describe('_setBaseURI', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
setDeployContext,
} from '@massalabs/massa-as-sdk';
import { Args, stringToBytes, u256ToBytes } from '@massalabs/as-types';
import { balanceOf, constructor } from '../MRC1155';
import { balanceOf, mrc1155Constructor } from '../MRC1155';
import { u256 } from 'as-bignum/assembly';
import { _balanceOfBatch } from '../MRC1155-internal';
import { MINTER_ROLE, mint, mintBatch } from '..';
Expand All @@ -27,7 +27,7 @@ beforeEach(() => {
switchUser(user1Address);
resetStorage();
setDeployContext(user1Address);
constructor(new Args().add(stringToBytes(TOKEN_URI)).serialize());
mrc1155Constructor(TOKEN_URI);
});

describe('mint', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Args, stringToBytes } from '@massalabs/as-types';
import { u256 } from 'as-bignum/assembly';
import * as token from './MRC1155';
import * as mint from './mintable/mint';
import { mrc1155Constructor } from '../MRC1155';
import * as mint from '../mintable/mint';
import { Context } from '@massalabs/massa-as-sdk';
import { grantRole } from '../utils/accessControl';
import { grantRole } from '../../utils/accessControl';

export function constructor(binaryArgs: StaticArray<u8>): void {
const args = new Args(binaryArgs);
Expand All @@ -14,7 +14,9 @@ export function constructor(binaryArgs: StaticArray<u8>): void {
const amounts = args
.nextFixedSizeArray<u256>()
.expect('amounts argument is missing or invalid');
token.constructor(new Args().add(uri).serialize());

mrc1155Constructor(uri);

grantRole(
new Args()
.add(mint.MINTER_ROLE)
Expand All @@ -31,10 +33,10 @@ export function constructor(binaryArgs: StaticArray<u8>): void {
);
}

export * from './burnable/burn';
export * from './mintable/mint';
export * from '../utils/accessControl';
export * from '../utils/ownership';
export * from '../burnable/burn';
export * from '../mintable/mint';
export * from '../../utils/accessControl';
export * from '../../utils/ownership';
// export everything from the token module except the constructor
export {
uri,
Expand All @@ -44,4 +46,4 @@ export {
isApprovedForAll,
safeTransferFrom,
safeBatchTransferFrom,
} from './MRC1155';
} from '../MRC1155';
50 changes: 12 additions & 38 deletions smart-contracts/assembly/contracts/MRC20/MRC20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,55 +22,29 @@ export const DECIMALS_KEY = stringToBytes('DECIMALS');

/**
* Initialize the ERC20 contract
* Can be called only once
*
* @example
* ```typescript
* constructor(
* new Args()
* .add('TOKEN_NAME')
* .add('TOKEN_SYMBOL')
* .add(3) // decimals
* .add(1000) // total supply
* .serialize(),
* );
* ```
* @remarks You must call this function in your contract's constructor or re-write it to fit your needs !
*
* @param stringifyArgs - Args object serialized as a string containing:
* - the token name (string)
* - the token symbol (string).
* - the decimals (u8).
* - the totalSupply (u256)
* - first owner (address)e
* @param name - the name of the token
* @param symbol - the symbol of the token
* @param decimals - the number of decimals
* @param totalSupply - the total supply of the token
*/
export function constructor(stringifyArgs: StaticArray<u8>): void {
export function mrc20Constructor(
name: string,
symbol: string,
decimals: u8,
totalSupply: u256,
): void {
assert(isDeployingContract());

const args = new Args(stringifyArgs);

// initialize token name
const name = args.nextString().expect('Error while initializing tokenName');
Storage.set(NAME_KEY, stringToBytes(name));

// initialize token symbol
const symbol = args
.nextString()
.expect('Error while initializing tokenSymbol');
Storage.set(SYMBOL_KEY, stringToBytes(symbol));

// initialize token decimals
const decimals = args
.nextU8()
.expect('Error while initializing tokenDecimals');
Storage.set(DECIMALS_KEY, [decimals]);

// initialize totalSupply
const totalSupply = args
.nextU256()
.expect('Error while initializing totalSupply');
Storage.set(TOTAL_SUPPLY_KEY, u256ToBytes(totalSupply));

setOwner(new Args().add(Context.caller().toString()).serialize());

_setBalance(Context.caller(), totalSupply);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
symbol,
decimals,
version,
constructor,
mrc20Constructor,
increaseAllowance,
transfer,
allowance,
Expand Down Expand Up @@ -51,14 +51,7 @@ const DECIMALS: u8 = 2;
const TOTAL_SUPPLY = new u256(10000, 0, 0, 100);

describe('ERC20 BURN - Initialization', () => {
constructor(
new Args()
.add(TOKEN_NAME)
.add(TOKEN_SYMBOL)
.add(DECIMALS)
.add(TOTAL_SUPPLY)
.serialize(),
);
mrc20Constructor(TOKEN_NAME, TOKEN_SYMBOL, DECIMALS, TOTAL_SUPPLY);

test('total supply is properly initialized', () => {
expect(totalSupply([])).toStrictEqual(u256ToBytes(TOTAL_SUPPLY));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { mint } from '../mintable/mint';
import {
VERSION,
balanceOf,
constructor,
mrc20Constructor,
decimals,
name,
symbol,
Expand Down Expand Up @@ -43,14 +43,7 @@ const TOTAL_SUPPLY = new u256(10000, 1000, 100, 10);
beforeAll(() => {
resetStorage();
setDeployContext(user1Address);
constructor(
new Args()
.add(TOKEN_NAME)
.add(TOKEN_SYMBOL)
.add(DECIMALS)
.add(TOTAL_SUPPLY)
.serialize(),
);
mrc20Constructor(TOKEN_NAME, TOKEN_SYMBOL, DECIMALS, TOTAL_SUPPLY);
});

describe('ERC20 MINT - Initialization', () => {
Expand Down
11 changes: 2 additions & 9 deletions smart-contracts/assembly/contracts/MRC20/__tests__/MRC20.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
allowance,
increaseAllowance,
decreaseAllowance,
constructor,
mrc20Constructor,
VERSION,
} from '../MRC20';
import { u256 } from 'as-bignum/assembly';
Expand All @@ -49,14 +49,7 @@ function switchUser(user: string): void {
beforeAll(() => {
resetStorage();
setDeployContext(user1Address);
constructor(
new Args()
.add(TOKEN_NAME)
.add(TOKEN_SYMBOL)
.add(DECIMALS)
.add(TOTAL_SUPPLY)
.serialize(),
);
mrc20Constructor(TOKEN_NAME, TOKEN_SYMBOL, DECIMALS, TOTAL_SUPPLY);
});

describe('Initialization', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import { Args, u256ToBytes } from '@massalabs/as-types';
import {
balanceOf,
constructor,
mrc20Constructor,
deposit,
withdraw,
computeMintStorageCost,
Expand All @@ -33,9 +33,7 @@ function switchUser(user: string): void {
beforeEach(() => {
resetStorage();
setDeployContext(user1Address);
constructor(
new Args().add('Wrapped MAS').add('WMAS').add(9).add(u256.Zero).serialize(),
);
mrc20Constructor('Wrapped MAS', 'WMAS', 9, u256.Zero);
});

describe('deposit', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// These tests serve as an example of how to use the MRC20Wrapper class to interact with the MRC20 contract.
import { MRC20Wrapper } from '../wrapper';
import {
Address,
balance,
balanceOf,
changeCallStack,
mockBalance,
mockScCall,
Expand All @@ -14,13 +13,17 @@ import { u256 } from 'as-bignum/assembly';
const tokenName = 'myToken';

const userAddr = 'AU1mhPhXCfh8afoNnbW91bXUVAmu8wU7u8v54yNTMvY7E52KBbz3';
const tokenAddress = 'AU12BqZEQ6sByhRLyEuf0YbQmcF2PsDdkNNG1akBJu9XcjZA1eT';
const tokenAddress = 'AS12BqZEQ6sByhRLyEuf0YbQmcF2PsDdkNNG1akBJu9XcjZA1eT';

const tokenContract = new MRC20Wrapper(new Address(tokenAddress));

function switchUser(user: string): void {
changeCallStack(user + ' , ' + tokenAddress);
}

describe('Wrapper tests', () => {
beforeAll(() => {
changeCallStack(userAddr + ' , ' + tokenAddress);
switchUser(userAddr);
});

test('token name', () => {
Expand All @@ -45,9 +48,6 @@ describe('Wrapper tests', () => {
mockScCall([]);

tokenContract.transfer(recipient, amount);

expect(balance()).toBe(0);
expect(balanceOf(tokenAddress)).toBe(0);
});

test('transfer with coins', () => {
Expand All @@ -61,7 +61,5 @@ describe('Wrapper tests', () => {
mockScCall([]);

tokenContract.transfer(recipient, amount, coins);
expect(balance()).toBe(0);
expect(balanceOf(tokenAddress)).toBe(0);
});
});
19 changes: 19 additions & 0 deletions smart-contracts/assembly/contracts/MRC20/example/MRC20.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { u256 } from 'as-bignum/assembly';
import { mrc20Constructor } from '../MRC20';

export function constructor(): void {
mrc20Constructor('MassaToken', 'MT', 18, u256.fromU64(1010101010));
}

export {
name,
symbol,
totalSupply,
decimals,
balanceOf,
transfer,
allowance,
increaseAllowance,
decreaseAllowance,
transferFrom,
} from '../MRC20';
Loading
Loading