Approving NFTs + NoRamp
Tags: nft, fiat, javascript, near
Published: 2022-11-08 22:05:19

NoRamp is a platform that allows developers, marketplaces and individual creators to perform buy and sell operations of NFTs through fiat. During the transaction, both parties will receive fiat instead of crypto which is why it's called "No"Ramp.

## 1: Creating a NoRamp Account

To create a NoRamp account you can look at [our tutorial](https://dev.to/noramp/seamlessly-integrating-a-payment-platform-in-10-lines-of-code-27i) or you can go ahead and go to [NoRamp](https://testnet.noramp.io) and follow the steps.

# 2: Asset Approval

In order for us to use NoRamp as the transmitter of the asset through fiat money, we need to go through the process of asset approval. 

**It is vital for you to grant approval:** Otherwise, NoRamp's objective might fail when transacting with the blockchain.

NFTs in NEAR and ETH have specific standards that allow granting permissions to wallet addresses such as [EIP-721](https://eips.ethereum.org/EIPS/eip-721) or [NEP-0178](https://github.com/near/NEPs/blob/master/neps/nep-0178.md).

In this tutorial, you will be granting approvals programmatically with Javascript.  

## 3: NoRamp's Wallet

In order to create a wallet in NoRamp so that we have the public key that we will granting approval for, you must go to [NoRamp's wallet page](https://testnet.noramp.io/apps/triggers/wallets) and click on "Add Wallet"

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/us53avs99s3iufdjqgwf.png)

In this step, you can import your own wallet or use NoRamp's managed wallet:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bl54u8kayoisxv6ein0y.png)

**It is recommended to use NoRamp's managed** since at the time of creating triggers, you won't have to worry about liquidity.

After creating the wallet, when you click on it, you will be able to see the public key. Something like this:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ax68pf4hopn9b6pflopa.png)

You will be using the public key (Account Id) in this tutorial referred as **"NORAMP_WALLET_ADDRESS"**

## 4: The Approvals + Javascript

### Near

In the case of NEAR, we can use NEAR's sdk ([Click Here](https://docs.near.org/tools/near-api-js/quick-reference) for more information)

```javascript
import * as nearAPI from "near-api-js";


const { keyStores, KeyPair } = nearAPI;
const myKeyStore = new keyStores.InMemoryKeyStore();
const PRIVATE_KEY = "YOUR_PRIVATE_KEY";
// creates a public / private key pair using the provided private key
const keyPair = KeyPair.fromString(PRIVATE_KEY);
// adds the keyPair you created to keyStore
await myKeyStore.setKey("testnet", "YOUR_ACCOUNT.testnet", keyPair);
```
In the code above, we are:
1. Importing a private key
2. Creating the relationship between the private key and **YOUR_ACCOUNT.testnet**
3. Saving the relation

It's important to note that if this is a production account, you must replace "testnet" with mainnet and the wallet address must contain "near". It would look like this:

```javascript
await myKeyStore.setKey("mainnet", "YOUR_ACCOUNT.near", keyPair);
```

Now, you can create an `account` variable containing the account:

```
const account = await nearConnection.account("YOUR_ACCOUNT.testnet");
```

After doing the step above, we can create the contract instance which will allow to communicate with the smart contract (NFT smart contract in this case)

```javascript
const contract = new Contract(
  account, // the account object that is connecting
  "YOUR_ACCOUNT.testnet",
  {
    // name of contract you're connecting to
    viewMethods: [""], // view methods do not change state but usually return a value
    changeMethods: ["nft_approve"], // change methods modify state (Change methods for NEP-0178)
  }
);
```

Now, you can call the contract with the NFT approval

```javascript
await contract. nft_approve(
  {
    token_id: "TOKEN_ID",
    account_id: "NORAMP_WALLET_ADDRESS"
  },
  "300000000000000", // attached GAS (optional)
  "1000000000000000000000000" // attached deposit in yoctoNEAR (optional)
);
```

The final code would look like this: [Click here to see Gist](https://gist.github.com/andreespirela/90411be052e1ebad2dcd17517295c1a5)

### ETH:

For this part, we will essentially execute the NFT approval in ethereum chain. Using `ethers` package (found [here](https://www.npmjs.com/package/ethers))

```javascript
import {Contract, providers, Wallet, utils} from "ethers";

const provider = new providers.JsonRpcProvider({
   url: "http://127.0.0.1:8545",
});

const wallet = new Wallet("ETH_PRIVATE_KEY", provider);
```

In the code above you are:
1) Creating a provider that communicates with the JSON RPC for the ETH chain. (Remember to put the URL of your preferred JSON RPC provider)
2) Creating a wallet object based on the ETH private key.


Now, you will create the contract object to execute the contract calls.

```javascript
const contract = new Contract("CONTRACT_ID", ["function approve(address _approved, uint256 _tokenId)"], wallet);
```

In the code above, we are creating the `Contract` instance. Any call to this instance will be paid under `wallet`. 

**Remember to replace CONTRACT_ID** with your NFT contract.

```javascript
const result = await (contract["approve"]("NORAMP_WALLET_ADDRESS", "TOKEN_ID", {
      gasPrice: "0",
      gasLimit: "1"
}));
```

In the code above, you are calling `approve` function based on **EIP-721** which has the following signature:

```solidity
function approve(address _approved, uint256 _tokenId) external payable;
```
which means we are passing two paremeters: **NORAMP_WALLET_ADDRESS** and **TOKEN_ID**.

In the case of `gasPrice` and `gasLimit`, you need to set the amounts to as required by the contract. This can vary.

## 6: Summary

1) We successfully created a NoRamp's managed wallet
2) We have successfully granted access NoRamp's to the NFT by calling the contract and granting the approval
3) We are ready to implement NoRamp's in our NFT marketplace

For NoRamp documentation: https://testnet.noramp.io/docs
For WebHook documentation: https://testnet.noramp.io/docs/api/webhooks
For further contact, you can reach out to: hello@noramp.io
We hope to see NoRamp powering your applications and developers.
Remember to follow us on Twitter [@No_Ramp](https://twitter.com/no_ramp) and stay tuned for upcoming and exciting developments.