Skip to content

Commit

Permalink
feat(near): migrate to near-wallet-connect-core for signing
Browse files Browse the repository at this point in the history
  • Loading branch information
imsk17 committed Oct 22, 2024
1 parent 2e14076 commit bdc9b27
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 71 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"@multiversx/sdk-core": "^13.2.1",
"@multiversx/sdk-network-providers": "^2.2.1",
"@multiversx/sdk-wallet": "^4.3.0",
"@near-wallet-selector/core": "^8.9.13",
"@taquito/signer": "^20.0.1",
"@taquito/taquito": "^19.0.0",
"@taquito/tzip16": "^19.0.0",
Expand Down
150 changes: 91 additions & 59 deletions src/handlers/near/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Contract, connect } from "near-api-js";
import { Account, Contract, connect } from "near-api-js";
import { parseNearAmount } from "near-api-js/lib/utils/format";
import { unimplemented } from "../../utils";
import { TNearHandler, TNearParams } from "./types";
Expand Down Expand Up @@ -103,23 +103,30 @@ export async function nearHandler({
return storage;
},
async mintNft(connection, ma, gasArgs) {
const signer = connection.account();
const call = await signer.functionCall({
contractId: ma.contract,
methodName: "nft_mint",
args: {
token_id: ma.token_id,
receiver_id: ma.owner,
metadata: {
title: ma.title || "Xp Nft",
description: ma.description || "Xp Nft Description",
media: ma.uri,
const call = await connection.signAndSendTransaction({
actions: [
{
type: "FunctionCall",
params: {
args: {
token_id: ma.token_id,
receiver_id: ma.owner,
metadata: {
title: ma.title || "Xp Nft",
description: ma.description || "Xp Nft Description",
media: ma.uri,
},
},
// biome-ignore lint/style/noNonNullAssertion: <explanation>
deposit: parseNearAmount("0.007")!,
gas: gasArgs ? gasArgs.gasLimit.toString() : "",
methodName: "nft_mint",
},
},
},
attachedDeposit: BigInt(parseNearAmount("0.007") ?? 0),
gas: gasArgs ? BigInt(gasArgs.gasLimit) : undefined,
],
receiverId: ma.contract,
});
return call.transaction.hash;
return call?.transaction.hash;
},
transform(input) {
return {
Expand All @@ -141,18 +148,26 @@ export async function nearHandler({
};
},
async approveNft(connection, tokenId, contract) {
const signer = connection.account();
const approve = await signer.functionCall({
contractId: contract,
methodName: "nft_approve",
args: {
token_id: tokenId,
account_id: bridge,
msg: null,
},
attachedDeposit: BigInt(parseNearAmount("0.005") ?? 0),
const approve = await connection.signAndSendTransaction({
actions: [
{
type: "FunctionCall",
params: {
args: {
token_id: tokenId,
account_id: bridge,
msg: null,
},
// biome-ignore lint/style/noNonNullAssertion: <explanation>
deposit: parseNearAmount("0.005")!,
methodName: "nft_approve",
gas: "50000000",
},
},
],
receiverId: contract,
});
return approve.transaction.hash;
return approve?.transaction.hash;
},
async decodeLockedEvent(txHash) {
const receipts = await provider.connection.provider.txStatusReceipts(
Expand Down Expand Up @@ -193,49 +208,66 @@ export async function nearHandler({
tokenId,
metadata_uri,
) {
const signer = connection.account();
const locked = await signer.functionCall({
contractId: bridge,
methodName: "lock_nft",
args: {
source_nft_contract_address: sourceNft,
destination_chain: destinationChain,
destination_address: to,
token_id: tokenId.toString(),
metadata_uri,
},
gas: 100000000000000n,
attachedDeposit: BigInt(parseNearAmount("2") ?? 0),
const locked = await connection.signAndSendTransaction({
actions: [
{
type: "FunctionCall",
params: {
args: {
source_nft_contract_address: sourceNft,
destination_chain: destinationChain,
destination_address: to,
token_id: tokenId.toString(),
metadata_uri,
},
// biome-ignore lint/style/noNonNullAssertion: <explanation>
deposit: parseNearAmount("2")!,
gas: "100000000000000",
methodName: "lock_nft",
},
},
],
receiverId: bridge,
});
return {
ret: locked,
hash() {
return locked.transaction.hash;
return locked?.transaction.hash;
},
};
},
async claimNft(connection, claimData, sig) {
const signer = connection.account();
const claimed = await signer.functionCall({
contractId: bridge,
methodName: "claim_nft",
args: {
cd: claimData,
signatures: sig.map((e) => {
return {
signer: e.signerAddress.replace("0x", ""),
signature: [...Buffer.from(e.signature.replace("0x", ""), "hex")],
};
}),
},
attachedDeposit: BigInt(claimData.fee),
const claimed = await connection.signAndSendTransaction({
actions: [
{
type: "FunctionCall",
params: {
args: {
cd: claimData,
signatures: sig.map((e) => {
return {
signer: e.signerAddress.replace("0x", ""),
signature: [
...Buffer.from(e.signature.replace("0x", ""), "hex"),
],
};
}),
},
deposit: claimData.fee.toString(),
gas: "",
methodName: "claim_nft",
},
},
],
receiverId: bridge,
});
return { hash: () => claimed.transaction.hash, ret: claimed };
return { hash: () => claimed?.transaction.hash, ret: claimed };
},
async getBalance(connection, _) {
const signer = connection.account();
const ab = await signer.getAccountBalance();
return BigInt(ab.available);
const ab = (await connection.getAccounts())[0];
const acc = new Account(provider.connection, ab.accountId);
const bal = await acc.getAccountBalance();
return BigInt(bal.available);
},
};
}
17 changes: 7 additions & 10 deletions src/handlers/near/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Near, WalletConnection } from "near-api-js";
import { Wallet } from "@near-wallet-selector/core";
import { Near } from "near-api-js";
import { FinalExecutionOutcome } from "near-api-js/lib/providers";
import { BridgeStorage } from "../../contractsTypes/evm";
import {
Expand Down Expand Up @@ -40,20 +41,16 @@ export type NftIssueArgs = {
};

export type TNearHandler = TSingularNftChain<
WalletConnection,
Wallet,
TNearClaimArgs,
unknown,
FinalExecutionOutcome,
// biome-ignore lint/suspicious/noConfusingVoidType: <explanation>
void | FinalExecutionOutcome,
Near
> &
MintNft<
WalletConnection,
NftIssueArgs,
{ gasLimit: number; value: number },
string
> &
MintNft<Wallet, NftIssueArgs, { gasLimit: number; value: number }, string> &
DeployNFTCollection<
WalletConnection,
Wallet,
{ name: string; ticker: string },
{ gasLimit: number },
string
Expand Down
33 changes: 31 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,16 @@
"@near-js/utils" "1.0.0"
borsh "1.0.0"

"@near-wallet-selector/core@^8.9.13":
version "8.9.13"
resolved "https://registry.yarnpkg.com/@near-wallet-selector/core/-/core-8.9.13.tgz#d7190d7914e6c2cceac5af107d1f7366266f923a"
integrity sha512-ufI2yctF2N7+D6s5QaSs9Bqorh8xPH48wuBEvTbArJ6z9qvSCNsK9ZO5Mt5BFfO64ovmcflOOUBtZ4/BcNO6aQ==
dependencies:
borsh "0.7.0"
events "3.3.0"
js-sha256 "0.9.0"
rxjs "7.8.1"

"@noble/[email protected]":
version "1.2.0"
resolved "https://registry.yarnpkg.com/@noble/curves/-/curves-1.2.0.tgz#92d7e12e4e49b23105a2555c6984d41733d65c35"
Expand Down Expand Up @@ -1690,6 +1700,15 @@ borc@^2.1.1:
json-text-sequence "~0.1.0"
readable-stream "^3.6.0"

[email protected]:
version "0.7.0"
resolved "https://registry.yarnpkg.com/borsh/-/borsh-0.7.0.tgz#6e9560d719d86d90dc589bca60ffc8a6c51fec2a"
integrity sha512-CLCsZGIBCFnPtkNnieW/a8wmreDmfUtjU2m9yHrzPXIlNbqVs0AQrSatSG6vdNYUqdc83tkQi2eHfF98ubzQLA==
dependencies:
bn.js "^5.2.0"
bs58 "^4.0.0"
text-encoding-utf-8 "^1.0.2"

[email protected]:
version "1.0.0"
resolved "https://registry.yarnpkg.com/borsh/-/borsh-1.0.0.tgz#b564c8cc8f7a91e3772b9aef9e07f62b84213c1f"
Expand Down Expand Up @@ -2043,7 +2062,7 @@ eventemitter3@^5.0.1:
resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-5.0.1.tgz#53f5ffd0a492ac800721bb42c66b841de96423c4"
integrity sha512-GWkBvjiSZK87ELrYOSESUYeVIc9mvLLf/nXalMOS5dYrgZq9o5OVkbZAVM06CVxYsCwH9BDZFPlQTlPA1j4ahA==

events@^3.3.0:
events@3.3.0, events@^3.3.0:
version "3.3.0"
resolved "https://registry.yarnpkg.com/events/-/events-3.3.0.tgz#31a95ad0a924e2d2c419a813aeb2c4e878ea7400"
integrity sha512-mQw+2fkQbALzQ7V0MY0IqdnXNOeTtP4r0lN9z7AAawCXgqea7bDii20AYrIBrFd/Hx0M2Ocz6S111CaFkUcb0Q==
Expand Down Expand Up @@ -2360,6 +2379,11 @@ js-base64@^3.7.4, js-base64@^3.7.7:
resolved "https://registry.yarnpkg.com/js-base64/-/js-base64-3.7.7.tgz#e51b84bf78fbf5702b9541e2cb7bfcb893b43e79"
integrity sha512-7rCnleh0z2CkXhH67J8K1Ytz0b2Y+yxTPL+/KOJoa20hfnVQ/3/T6W/KflYI4bRHRagNeXeU2bkNGI3v1oS/lw==

[email protected]:
version "0.9.0"
resolved "https://registry.yarnpkg.com/js-sha256/-/js-sha256-0.9.0.tgz#0b89ac166583e91ef9123644bd3c5334ce9d0966"
integrity sha512-sga3MHh9sgQN2+pJ9VYZ+1LPwXOxuBJBA5nrR5/ofPfuiJBE2hnjsaN8se8JznOmGLN2p49Pe5U/ttafcs/apA==

[email protected]:
version "0.8.0"
resolved "https://registry.yarnpkg.com/js-sha3/-/js-sha3-0.8.0.tgz#b9b7a5da73afad7dedd0f8c463954cbde6818840"
Expand Down Expand Up @@ -3054,7 +3078,7 @@ ripemd160@^2.0.0, ripemd160@^2.0.1:
hash-base "^3.0.0"
inherits "^2.0.1"

rxjs@^7.8.1:
rxjs@7.8.1, rxjs@^7.8.1:
version "7.8.1"
resolved "https://registry.yarnpkg.com/rxjs/-/rxjs-7.8.1.tgz#6f6f3d99ea8044291efd92e7c7fcf562c4057543"
integrity sha512-AA3TVj+0A2iuIoQkWEK/tqFjBq2j+6PO6Y0zJcvzLAFhEFIO3HL0vls9hWLncZbAAbK0mar7oZ4V079I/qPMxg==
Expand Down Expand Up @@ -3290,6 +3314,11 @@ teslabot@^1.3.0:
resolved "https://registry.yarnpkg.com/teslabot/-/teslabot-1.5.0.tgz#70f544516699ca5f696d8ae94f3d12cd495d5cd6"
integrity sha512-e2MmELhCgrgZEGo7PQu/6bmYG36IDH+YrBI1iGm6jovXkeDIGa3pZ2WSqRjzkuw2vt1EqfkZoV5GpXgqL8QJVg==

text-encoding-utf-8@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/text-encoding-utf-8/-/text-encoding-utf-8-1.0.2.tgz#585b62197b0ae437e3c7b5d0af27ac1021e10d13"
integrity sha512-8bw4MY9WjdsD2aMtO0OzOCY3pXGYNx2d2FfHRVUKkiCPDWjKuOlhLVASS+pD7VkLTVjW268LYJHwsnPFlBpbAg==

thread-stream@^2.6.0:
version "2.7.0"
resolved "https://registry.yarnpkg.com/thread-stream/-/thread-stream-2.7.0.tgz#d8a8e1b3fd538a6cca8ce69dbe5d3d097b601e11"
Expand Down

0 comments on commit bdc9b27

Please sign in to comment.