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

example: fungible token #14

Merged
merged 53 commits into from
Oct 9, 2024
Merged

example: fungible token #14

merged 53 commits into from
Oct 9, 2024

Conversation

petersalomonsen
Copy link
Owner

@petersalomonsen petersalomonsen commented Apr 25, 2023

There are 3 highlights in this Pull Request:

A full example of building QuickJS for integrating with Rust in WebAssembly, and then also NEAR smart contracts

Now that this project downloads QuickJS, contains all the build scripts which is part of the Rust build pipeline, and compiles for WebAssembly targets including a NEAR protocol smart contract, it serves as a comprehensive and complete example of bundling QuickJS with Rust and WebAssembly. The combination of JS, Rust and NEAR protocol smart contracts, opens up for having a lightweight JavaScript customization layer on top of robust Rust implementation of standard contracts such as NFT and FT.

A customizable Fungible Token contract

The added JavaScript engine makes it possible to add custom transfer functions. Below is an example from the tests, that transfers a fixed amount of 2000 tokens to alice. When calling this function, a transfer_id is returned, which alice then can use to return excessive funds. In this example alice can call the refund function with the transfer_id as argument, and then the account that did the transfer to alice will get 1000 tokens in refund.

My intended use case of this is to be able to reserve the max needed tokens for an AI request, and then return back to the caller the unspent tokens.

            export function transfer_2_000_to_alice() {
                    const amount = 2_000n;
                    const transfer_id = env.signer_account_id() + '_' + new Date().getTime();
                    env.set_data(transfer_id, JSON.stringify({receiver_id: env.signer_account_id(), refund_amount: (amount / 2n).toString()}));
                    env.ft_transfer('alice.test.near', amount.toString());
                    env.value_return(transfer_id);
                }

                export function refund() {
                    const { transfer_id } = JSON.parse(env.input());
                    const {refund_amount, receiver_id} = JSON.parse(env.get_data(transfer_id));
                    env.clear_data(transfer_id);
                    env.ft_transfer(receiver_id, refund_amount);
                }

@petersalomonsen petersalomonsen marked this pull request as ready for review October 9, 2024 08:54
@petersalomonsen
Copy link
Owner Author

@race-of-sloths include

@race-of-sloths-bot
Copy link

race-of-sloths-bot commented Oct 9, 2024

@petersalomonsen Thank you for your contribution! Your pull request is now a part of the Race of Sloths!
You are true leader! Other sloths hot on your heels, keep moving!

Shows inviting banner with latest news.

Shows profile picture for the author of the PR

Current status: waiting for finalization

The pull request is merged, you have 24 hours to finalize your scoring. The scoring ends Thu Oct 10 19:22:25 2024

Reviewer Score
@Tguntenaar 13

Your contribution is much appreciated with a final score of 13!
You have received 130 Sloth points for this contribution

What is the Race of Sloths

Race of Sloths is a friendly competition where you can participate in challenges and compete with other open-source contributors within your normal workflow

For contributors:

  • Tag @race-of-sloths inside your pull requests
  • Wait for the maintainer to review and score your pull request
  • Check out your position in the Leaderboard
  • Keep weekly and monthly streaks to reach higher positions
  • Boast your contributions with a dynamic picture of your Profile

For maintainers:

  • Score pull requests that participate in the Race of Sloths
  • Engage contributors with fair scoring and fast responses so they keep their streaks
  • Promote the Race to the point where the Race starts promoting you
  • Grow the community of your contributors

Feel free to check our website for additional details!

Bot commands
  • For contributors
    • Include a PR: @race-of-sloths include to enter the Race with your PR
  • For maintainers:
    • Invite contributor @race-of-sloths invite to invite the contributor to participate in a race or include it, if it's already a runner.
    • Assign points: @race-of-sloths score [1/2/3/5/8/13] to award points based on your assessment.
    • Reject this PR: @race-of-sloths exclude to send this PR back to the drawing board.
    • Exclude repo: @race-of-sloths pause to stop bot activity in this repo until @race-of-sloths unpause command is called

@petersalomonsen
Copy link
Owner Author

Application for joining @race-of-sloths can be found here: NEAR-DevHub/race-of-sloths#187

@petersalomonsen
Copy link
Owner Author

@race-of-sloths include

@Tguntenaar
Copy link
Collaborator

@race-of-sloths score 13

@petersalomonsen petersalomonsen merged commit cb5575e into master Oct 9, 2024
11 checks passed
@petersalomonsen petersalomonsen deleted the example/fungibletoken branch October 9, 2024 19:22
Copy link
Collaborator

@Tguntenaar Tguntenaar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome magic!

@race-of-sloths
Copy link

@petersalomonsen Thank you for your contribution! Your pull request is now a part of the Race of Sloths!
King of Sloths shakes your hand! Yellow leader's jersey is yours deservedly!

Shows inviting banner with latest news.

Shows profile picture for the author of the PR

Current status: executed
Reviewer Score
@Tguntenaar 13

Your contribution is much appreciated with a final score of 13!
You have received 137 (130 base + 5% lifetime bonus) Sloth points for this contribution

We would appreciate your feedback or contribution idea that you can submit here

What is the Race of Sloths

Race of Sloths is a friendly competition where you can participate in challenges and compete with other open-source contributors within your normal workflow

For contributors:

  • Tag @race-of-sloths inside your pull requests
  • Wait for the maintainer to review and score your pull request
  • Check out your position in the Leaderboard
  • Keep weekly and monthly streaks to reach higher positions
  • Boast your contributions with a dynamic picture of your Profile

For maintainers:

  • Score pull requests that participate in the Race of Sloths
  • Engage contributors with fair scoring and fast responses so they keep their streaks
  • Promote the Race to the point where the Race starts promoting you
  • Grow the community of your contributors

Feel free to check our website for additional details!

Bot commands
  • For contributors
    • Include a PR: @race-of-sloths include to enter the Race with your PR
  • For maintainers:
    • Invite contributor @race-of-sloths invite to invite the contributor to participate in a race or include it, if it's already a runner.
    • Assign points: @race-of-sloths score [1/2/3/5/8/13] to award points based on your assessment.
    • Reject this PR: @race-of-sloths exclude to send this PR back to the drawing board.
    • Exclude repo: @race-of-sloths pause to stop bot activity in this repo until @race-of-sloths unpause command is called

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants