-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathaaa.js
179 lines (134 loc) · 5.32 KB
/
aaa.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
一、evm -> native, 转账成功的同时,
前置条件:
显示资产 20、721、1155
1、转账
export async function getPrismConfig() {
URL: https://prod-testnet.prod.findora.org:8668/display_checkpoint
Get Request /display_checkpoint, 去使用 prism_bridge_address: "0x275f13f0a99C4b9710A8Cde3927b611d0DaA2A08"
const web3 = getWeb3(Network.getRpcRoute());
const bridgeAddress = prism_bridge_address;
const prismContract = await getSimBridgeContract(web3, bridgeAddress);
const [ledgerAddress, assetAddress] = await Promise.all([
prismContract.methods.ledger_contract().call(),
prismContract.methods.asset_contract().call(),
]);
return { ledgerAddress, assetAddress, bridgeAddress };
}
export const fraAddressToHashAddress = (address: string) => {
const { data, prefix } = bech32ToBuffer.decode(address);
if (prefix == 'eth') {
return '0x01' + Buffer.from(data).toString('hex');
}
return '0x' + Buffer.from(data).toString('hex');
};
export async function evmToNativeOfFRC721(
nativeWallet: string, // fra
evmWallet: ethers.Wallet,
assetCode: string,
tokenId: string,
) {
const findoraTo = fraAddressToHashAddress(nativeWallet);
// 将 fra 格式的地址,转化为 0x格式的地址
const { bridgeAddress, ledgerAddress } = await getPrismConfig(evmWallet);
const erc721Contract = await getERC721Contract({
address: assetCode,
wallet: evmWallet,
});
const prismContract = await getSimBridgeContract({
address: bridgeAddress,
wallet: evmWallet,
});
const ownerOf = await erc721Contract.ownerOf(tokenId);
if (ownerOf !== evmWallet.address) throw 'not tokenId';
const tsxApprove = await erc721Contract.approve(ledgerAddress, tokenId);
const resultApprove = await tsxApprove.wait();
if (resultApprove.status == 1) {
const tsxDeposit = await prismContract.depositFRC721(
assetCode,
findoraTo,
tokenId,
);
const resultDeposit = await tsxDeposit.wait();
console.log(`Tsx Hash: ${resultDeposit.hash}`);
}
}
to地址,是在自己的钱包里面,同时,把转账的资产,添加到资产管理列表里
to地址,不再自己钱包里面,就不用管了 ( corssAsset )
2、往资产管理列表添加资产时,需要设置正确 tokenAssetType,
- 通过 UI 交互行为 ,决定转的什么类型 (客户端钱包使用此方案)
- 使用 第 二 步骤的方法,进行判断
二、将 evm 跨到 native 的资产,添加到 资产管理列表
1、判断资产是什么类型,需要使用 PrismXXAssetContract 合约的 getTokenType 方法,传递的参数 是这种编码格式 `0x${Buffer.from(tokenAddress, 'base64').toString('hex')}`。
const TOKEN_TYPE = {
FRC20: 0,
FRC721: 1,
FRC1155: 2,
FRA: -1,
CUSTOMNATIVE: -2,
};
tokenAddress = '输入框输入的资产地址, base64格式'
hashAddress = `0x${Buffer.from(tokenAddress, 'base64').toString('hex')}` // 把base64 转换为 hash
tokenAssetType = PrismXXAssetContract.getTokenType(hashAddress)
if (tokenAssetType == '0') {
if (tokenAddress === findoraWasm.fra_get_asset_code()) {
tokenAssetType = String(TOKEN_TYPE.FRA);
} else {
const tokenAssetInfo = await prismAssetContract.methods.getERC20Info(assetTypeContractParam1).call();
if (tokenAssetInfo == '0x0000000000000000000000000000000000000000') {
tokenAssetType = String(TOKEN_TYPE.CUSTOMNATIVE);
} else {
tokenAssetType = String(TOKEN_TYPE.FRC20);
}
}
}
const assetData: FindoraWallet.IAssetCustom = {
assetCode: tokenAddress,
nickname: values.nickname,
nicknames: [],
address: values.address,
type: Number(tokenAssetType),
options: {
builtIn: false,
owned: false,
},
};
将 assetData ,添加到资产管理列表
三、 native -> evm
console.log('1、[send FRA]');
await nativeToEvm(nativeWallet, evmWallet, await AssetApi.getFraAssetCode());
console.log('2、[send FRC20]');
await nativeToEvm(nativeWallet, evmWallet, frc20AssetCode);
console.log('3、[send FRC721]');
await nativeToEvm(nativeWallet, evmWallet, frc721AssetCode);
console.log('4、[send FRC1155]');
await nativeToEvm(nativeWallet, evmWallet, frc1155AssetCode);
-------
export async function nativeToEvm(
nativeWallet: WalletKeypar,
evmWallet: string,
assetCode: string,
amount: string,
) {
const findoraSdk = await import('@findora-network/findora-sdk.js');
const {
Evm: EvmApi,
Asset: AssetApi,
Transaction: TransactionApi,
} = findoraSdk.Api;
const transactionBuilder = await EvmApi.sendAccountToEvm(
nativeWallet,
'10',
evmWallet,
assetCode,
'',
);
const handle = await TransactionApi.submitTransaction(transactionBuilder);
await sleep(10 * 6000);
const hash = await getNativeHash(handle);
console.log(
`native to evm (FRA) : ${NETWORK_CONFIG.explorerUrl}/transactionshash?hash=${hash}`,
);
}
*
*/