ink icon indicating copy to clipboard operation
ink copied to clipboard

PSP22 chain extension example

Open stiiifff opened this issue 3 years ago β€’ 7 comments

Example implementation of the PSP22 Fungible Token Standard as a chain extension, supporting a multi-token system provided by the FRAME assets pallet. It demonstrates how ink! contracts (L2) can interact with native assets (L1) from the chain runtime in a standardised way.

stiiifff avatar May 03 '22 11:05 stiiifff

It is bad that we did double work=( We could cooperate together to deliver but it is too late. We did the same in the OpenBrush.

But it is still good to have several implementations=)

We plan to create a production-ready chains extension and define its interface in the PSP. So if you have some though regarding our implementation it will be cool to hear them here or in PSP=)

I want to highlight that maybe some contract wants to transfer his tokens, so it is why we added the Origin enum into the chain extension where the developer can specify it(Origin::Caller or Origin::Self).

But we also realized the problem with Origin::Caller. Allowing anyone to transfer/approve tokens on behalf of the caller without any restriction creates a vulnerability. Any contract can be malicious and can steal all tokens. I think you need to highlight that by commenting on the implementation of transfer in the chain extension.

xgreenx avatar May 03 '22 12:05 xgreenx

πŸ¦‘ πŸ“ˆ ink! Example Contracts β€’ Changes Report πŸ“‰ πŸ¦‘

These are the results when building the examples/* contracts from this branch with cargo-contract 1.5.0-ef06f4d and comparing them to ink! master:

Ξ” Optimized Size Ξ” Used Gas Total Optimized Size Total Used Gas
accumulator 1.00 K
adder 2.04 K
contract-terminate 0.92 K 275_000
contract-transfer 8.36 K 75_000
delegate-calls 2.89 K 76_242
delegator 6.34 K 232_138
dns 8.81 K 225_000
erc1155 17.15 K 450_000
erc20 8.42 K 225_000
erc721 11.62 K 600_000
flipper 1.24 K 75_000
forward-calls 2.87 K 151_412
incrementer 1.14 K
mother 12.24 K
multisig 25.79 K 470_483
payment-channel 7.95 K
psp22-extension +7.16 K 7.16 K
rand-extension 3.79 K 75_000
set-code-hash 1.49 K
subber 2.06 K
trait-erc20 8.69 K 225_000
trait-flipper 0.97 K 75_000
trait-incrementer 1.12 K 150_000
updated-incrementer 9.78 K
upgradeable-flipper 1.48 K

Link to the run | Last update: Tue Aug 23 14:08:39 CEST 2022

paritytech-cicd-pr avatar May 03 '22 12:05 paritytech-cicd-pr

Codecov Report

Merging #1244 (5dad725) into master (2b7f20e) will decrease coverage by 0.40%. The diff coverage is n/a.

@@            Coverage Diff             @@
##           master    #1244      +/-   ##
==========================================
- Coverage   70.52%   70.11%   -0.41%     
==========================================
  Files         189      190       +1     
  Lines        5910     5940      +30     
==========================================
- Hits         4168     4165       -3     
- Misses       1742     1775      +33     
Impacted Files Coverage Ξ”
crates/metadata/src/layout/mod.rs 73.10% <0.00%> (-1.69%) :arrow_down:
crates/lang/ir/src/ir/attrs.rs 81.99% <0.00%> (-0.28%) :arrow_down:
crates/allocator/src/bump.rs 0.00% <0.00%> (ΓΈ)

:mega: We’re building smart automated test selection to slash your CI/CD build times. Learn more

codecov-commenter avatar May 03 '22 12:05 codecov-commenter

@xgreenx Thanks for your feedback ! I was unaware of your implementation. This is just a simple, incomplete example. My motivation was to show a more advanced chain extension example than the existing FetchRandomExtension one, and I thought that PSP22 was a good use case. So I don't see it as double work. Good point re. the caller in the transfer function.

stiiifff avatar May 03 '22 12:05 stiiifff

hi @stiiifff Do you still plan to add this as example? I'd like to use it for one of my workshops

Maar-io avatar May 30 '22 19:05 Maar-io

@bidzyyys Hey, are you going to address the open comments?

HCastano avatar Aug 02 '22 18:08 HCastano

@HCastano I will resolve the comments.

Wiezzel avatar Aug 10 '22 08:08 Wiezzel

@HCastano I've made some changes according to your comments. Here's the substarte-contratcs-node code with the extension installed: https://github.com/Wiezzel/substrate-contracts-node/pull/1 CI tests are failing, but it seems to me that is not related to any changes from this PR. I've run cargo test -p ink_lang --test compile_tests on master and it fails as well.

Wiezzel avatar Sep 05 '22 14:09 Wiezzel

@HCastano All checks passed! πŸ˜„

Wiezzel avatar Sep 07 '22 14:09 Wiezzel