-
Notifications
You must be signed in to change notification settings - Fork 92
Prototype Smart Contract reworking #159
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
Open
gregLibert
wants to merge
1
commit into
main
Choose a base branch
from
erc20
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+190
−3
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import { u256 } from "as-bignum/assembly"; | ||
| import { Exportable, ERC20 } from ".."; | ||
| import { Args } from "@massalabs/as-types"; | ||
| import { Address } from "@massalabs/massa-as-sdk"; | ||
|
|
||
| const totalSupply = u256.fromU32(1000); | ||
|
|
||
| // @eslint-ignore @typescript-eslint/no-unused-vars | ||
| // @ts-ignore | ||
| @lazy @global const FungibleToken = new ERC20(totalSupply); | ||
|
|
||
| describe('ERC20 - Documentation tests', () => { | ||
| it('should be simple to use - create smart contract', () => { | ||
|
|
||
| const totalSupplyBinary = new Args().add(totalSupply).serialize(); | ||
| const addr1 = new Address('AU12UBnqTHDQALpocVBnkPNy7y5CndUJQTLutaVDDFgMJcq5kQiKq'); | ||
| const addr1Binary = new Args().add(addr1).serialize(); | ||
| const addr2 = new Address('AU12UBnqTHDQALpocVBnkPNy7y5CndUJQTLutaVDDFgMJcq5kQiKr'); | ||
| const allowanceArgs = new Args().add(addr1).add(addr2).serialize(); | ||
| const u256ZeroBinary = new Args().add(u256.Zero).serialize(); | ||
|
|
||
| expect(Exportable.totalSupply()).toStrictEqual(totalSupplyBinary); | ||
| expect(Exportable.balanceOf(addr1Binary)).toStrictEqual(u256ZeroBinary); | ||
| expect(Exportable.allowance(allowanceArgs)).toStrictEqual(u256ZeroBinary); | ||
| }); | ||
| }); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import { Address, call } from "@massalabs/massa-as-sdk"; | ||
| import { ERC20Core } from "../interfaces/erc20Core"; | ||
| import { u256 } from "as-bignum"; | ||
| import { Args } from "@massalabs/as-types"; | ||
|
|
||
| export class ERC20CoreImpl { // implements ERC20Core { | ||
| constructor(public sc: Address) {} | ||
|
|
||
| allowance(owner: Address, spender: Address, coins: u64 = 0): u256 { | ||
| const args = new Args().add(owner).add(spender); | ||
| const response = call(this.sc, 'allowance', args, coins); | ||
| return new Args(response).mustNext<u256>("allowance"); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import { Args } from "@massalabs/as-types"; | ||
| import { Address } from "@massalabs/massa-as-sdk"; | ||
|
|
||
| export function allowance(_args: StaticArray<u8>): StaticArray<u8> { | ||
| const args = new Args(_args); | ||
| const owner = args.mustNext<Address>("owner"); | ||
Thykof marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const spender = args.mustNext<Address>("spender"); | ||
| FungibleToken.allowance(owner, spender).serialize(); | ||
gregLibert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| export function approve(_args: StaticArray<u8>): void{ | ||
| const args = new Args(_args); | ||
| const spender = args.mustNext<Address>("spender"); | ||
| const amount = args.mustNext<u64>("amount"); | ||
| FungibleToken.approve(spender, amount); | ||
| } | ||
|
|
||
| export function balanceOf(_args: StaticArray<u8>): StaticArray<u8> { | ||
| const of = new Args(_args).mustNext<Address>("of"); | ||
| return new Args().add(FungibleToken.balanceOf(of)).serialize(); | ||
| } | ||
|
|
||
| export function totalSupply(): StaticArray<u8> { | ||
| return new Args().add(FungibleToken.totalSupply()).serialize(); | ||
| } | ||
|
|
||
| export function transfer(_args: StaticArray<u8>): void{ | ||
| const args = new Args(_args); | ||
| const to = args.mustNext<Address>("to"); | ||
| const amount = args.mustNext<u64>("amount"); | ||
| FungibleToken.transfer(to, amount); | ||
| } | ||
|
|
||
| export function transferFrom(_args: StaticArray<u8>): void{ | ||
| const args = new Args(_args); | ||
| const from = args.mustNext<Address>("from"); | ||
| const to = args.mustNext<Address>("to"); | ||
| const amount = args.mustNext<u64>("amount"); | ||
| FungibleToken.transferFrom(from, to, amount); | ||
| } | ||
69 changes: 69 additions & 0 deletions
69
smart-contracts/erc20/assembly/implementation/erc20Full.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import { ERC20Core } from "../interfaces/erc20Core"; | ||
| import { Args } from "@massalabs/as-types"; | ||
| import { MapManager, | ||
| KeySequenceManager, | ||
| KeyIncrementer, ConstantManager, Address, Context, createEvent } from "@massalabs/massa-as-sdk"; | ||
| import { u256 } from "as-bignum/assembly"; | ||
|
|
||
| export const EVENT_PREFIX = "ERC20"; | ||
| export const EVENT_ERR_INSUFFICIENT_BALANCE = "transfer amount exceeds balance"; | ||
| export const EVENT_ERR_INSUFFICIENT_ALLOWANCE = "transfer amount exceeds allowance"; | ||
| export const EVENT_TRANSFER = "Transfer"; | ||
| export const EVENT_APPROVAL = "Approval"; | ||
|
|
||
| @inline | ||
| function triggerTransferEvent(from: Address, to: Address, value: u256): void { | ||
| createEvent(`${EVENT_PREFIX}: ${EVENT_TRANSFER}`, [from.toString(), to.toString(), value.toString()]); | ||
| } | ||
|
|
||
| export class ERC20 implements ERC20Core { | ||
| private _totalSupply: ConstantManager<u256>; | ||
| public balances: MapManager<Address, u256>; | ||
| public allowances: MapManager<StaticArray<u8>, u256>; | ||
|
|
||
| constructor(totalSupply: u256, keyManager: KeySequenceManager = new KeyIncrementer<u8>(0)) { | ||
| this._totalSupply = new ConstantManager<u256>(keyManager); | ||
| this.balances = new MapManager<Address, u256>(keyManager); | ||
| this.allowances = new MapManager<StaticArray<u8>, u256>(keyManager); | ||
|
|
||
| this._totalSupply.set(totalSupply); | ||
| } | ||
|
|
||
| totalSupply(): u256 { | ||
| return this._totalSupply.mustValue(); | ||
| } | ||
|
|
||
| balanceOf(address: Address): u256 { | ||
| return this.balances.value(address).unwrapOrDefault(); | ||
| } | ||
|
|
||
| transfer(to: Address, value: u256): void { | ||
| const from = Context.caller(); | ||
| const balance = this.balanceOf(from); | ||
| assert(balance >= value, `${EVENT_PREFIX}: ${EVENT_ERR_INSUFFICIENT_BALANCE}`); | ||
| this.balances.set(from, balance - value); | ||
| this.balances.set(to, this.balanceOf(to) + value); | ||
| triggerTransferEvent(from, to, value); | ||
| } | ||
|
|
||
| transferFrom(from: Address, to: Address, value: u256): void { | ||
| const caller = Context.caller(); | ||
| const allowance = this.allowance(from, caller); | ||
| assert(allowance >= value, `${EVENT_PREFIX}: ${EVENT_ERR_INSUFFICIENT_ALLOWANCE}`); | ||
| this.approve(caller, allowance - value); | ||
| this.transfer(to, value); | ||
| triggerTransferEvent(from, to, value); | ||
| } | ||
|
|
||
| approve(spender: Address, value: u256): void { | ||
| const from = Context.caller(); | ||
| const storageKey = new Args().add(from).add(spender).serialize(); | ||
| this.allowances.set(storageKey, value); | ||
| createEvent(`${EVENT_PREFIX}: ${EVENT_APPROVAL}`, [from.toString(), spender.toString(), value.toString()]); | ||
| } | ||
|
|
||
| allowance(owner: Address, spender: Address): u256 { | ||
| const storageKey = new Args().add(owner).add(spender).serialize(); | ||
| return this.allowances.value(storageKey).unwrapOrDefault(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| export * from './implementation/erc20Full'; | ||
| export * from './interfaces/erc20Core'; | ||
| import * as Exportable from './exportable/erc20Core'; | ||
| export { Exportable }; | ||
|
|
||
| export * from './connectable/erc20Core'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import { u256 } from "as-bignum/assembly"; | ||
| import { Address } from "@massalabs/massa-as-sdk"; | ||
|
|
||
| export interface ERC20Core { | ||
| allowance(owner: Address, spender: Address): u256; | ||
| approve(spender: Address, value: u256): void; | ||
| balanceOf(address: Address): u256; | ||
| // decimals(): u8; | ||
| // name(): string; | ||
| // symbol(): string; | ||
gregLibert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| totalSupply(): u256; | ||
| transfer(to: Address, value: u256): void; | ||
| transferFrom(from: Address, to: Address, value: u256): void; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| { | ||
| "extends": "assemblyscript/std/assembly.json", | ||
| "include": [ | ||
| "**/*.ts" | ||
| ], | ||
| "typedocOptions": { | ||
| "exclude": "assembly/**/__tests__", | ||
| "excludePrivate": true, | ||
| "excludeProtected": true, | ||
| "excludeExternals": true, | ||
| "includeVersion": true, | ||
| "skipErrorChecking": true | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| // todo TS code to connect a DAPP to an ERC20 smart contract | ||
| // Similar to assembly/connectable but for TS and from outside of the blockchain. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.